$("document").ready(function(){
	

	UploadComponent();
	
	
 var tabContainers = $('#tabsB > div');
tabContainers.hide().filter('li.current').show();
                        
                        $('#tabsB ul.navigation a').click(function () {
                        	
                       var href = this.href;
              
                       var hash = href.split("=")[1];
                     hash = hash.split("&")[0];
          
                                tabContainers.hide();
                                
                                
                                if (hash =="photos")
                                {
                                	showTab($(this),hash);
                                
                                	var data = $("#photo_thumb_wrap").html();
                                	if(data == 0)
                                	{
                                		$("#photo_thumb_wrap").append('<div id="loader" style="margin:10px" align="center"><img src="images/photo_loader_gif.gif" /></div>');
                                		
                                		$("#photo_thumb_wrap").load(href,data, function() {
                                			
                                			
                                			enable_foto_delete();
                                			
                                		});
                                
                                	}
                                	
                                }
                                else if (hash =="webpage")
                                {
                                	
                                
                                	var data = $("#webpage").html();
                                	if(data == 0)
                                	{
                                		
                                		$("#"+escape(hash)).load(href, data, function() {
                                		
                                		enableWebRequestLinkForAjax(hash);
                                		
                                		 
                                		
                                	});
                                
                                	}
                                	showTab($(this),escape(hash));
                              
                                }
                                else if (hash =="logo")
                                {
                                	
                                
                                	var data = $("#logo").html();
                                	if(data == 0)
                                	{
                                		
                                		$("#"+escape(hash)).load(href, data, function() {
                                		
                                		enableWebRequestLinkForAjax(hash);
                                		 
                                		
                                	});
                                
                                	}
                                	showTab($(this),escape(hash));
                                }                                       
                                else
                                {
                                	
                                var data =$("#"+escape(hash)).html();
                              
                                if (data.length == 0)
                                {
                                	
                                	$("#"+escape(hash)).load(href, data, function() {
                                		
                                		 showTab($(this),escape(hash));
                                		
                                	});
                                }
                                else
                                {
                                showTab($(this),hash);
                              
                                }
                                }
                                return false;
                        }).filter('.curr').click();
                        


                    enableFormAjax();
             
	
});

var valid = false;

function enableFormAjax()
{

	$("form a").each(function() {
		
		$(this).click(function(){
	
			var $this = $(this);
			var href= $this.href;			
			var formid = $this.parents('form').attr("id");
			
			var img = $this.next("img");
			
				
			if (formid =="formlogin")
			{
				
		
				$("#"+formid).validate();				
				if ($("#"+formid).valid())
				{
					
					img.show();
				$this.hide();
					$.ajax({'method':'GET', 'url':this.href, 'data':'password='+$("#password").val()+"&username="+$("#username").val(),
						'complete':function() {	
							$this.show();
							img.hide();
							valid = false;
							
							
						},
						
						'success': function(data, textStatus){ if (!eval(data)) { $this.show();img.hide();
					var cur = $("#curusername").val();
						
					
						$("#"+formid).parent().append("Kaydınız yapılmıştır.");
						$("#"+formid).remove();
						
						
						}}
						});
				}
				
				
				$this.show();
				img.hide()
			}
			else
			{
			
				$("#"+formid).validate().form();
		
				if ($("#"+formid).valid())
				{
			
					img.show();
					$this.hide();
					
					$.ajax({'method':'GET', 'url':this.href, 'data':encodeURI(generateFormQuery(formid)),
							'complete':function() {
								
									$this.show();img.hide();
								
							
					},
					'success': function(data, textStatus){ if (!eval(data)) { 
						$this.show();img.hide();
						$("#"+formid +" :input").val("");
						$("#"+formid).parent().append("Kaydınız yapılmıştır.")
						$("#"+formid).remove();
						
						
						}}
					
					});
				}				
			}
		
		return false;
});
});

}
function generateFormQuery(formid)
{
	var query='';
	$("#" + formid + " input:text").each(function(){
		var $this = $(this);
		var ret = $this.attr("name")+"="+($this.val() == "#" ? "voidlink" : $this.val());
		
			
		query += ret +"&";
	});
	$("#" + formid + " input:checkbox").each(function(){
		var $this = $(this);
		var ret = $this.attr("name")+"="+$this.attr("checked");
		query += ret +"&";
	});
	$("#" + formid + " textarea").each(function(){
		var $this = $(this);
		var ret = $this.attr("name")+"="+$this.val();
		query += ret +"&";
	});
	
	return query;
}
function enableWebRequestLinkForAjax(hash)
{
	
	$("#webrequest_link").click(function(){
		$(this).hide();
		$("#webrequest_loader").show();
		$("#"+hash).load(this.href, generateFormQuery("webrequestform"));
		return false;
	});
	
	$("#webupdate a").click(function(){
		$(this).hide();
	
		$("#"+hash).load(this.href, encodeURI(generateFormQuery("webupdate")));
		return false;
	});
	
}
function enable_foto_delete()
{
	
                                			$("#photo_thumb_wrap p a").click(function(){
													var href = this.href;
													
													
													$.ajax({'method':'GET', 'url':this.href});
													$(this).parent().parent().remove();
													return false;
												});  
}
function thumbGuncelle(thumbID, thumbURL, type)

{

	if(type=="yeni"){


		tempFoto.push(thumbURL);
$("#photo_thumb_wrap .container").append('<div class="float">' +
		'<img src="fotosube/'+thumbURL+'" width="90" height="75"/><p>' +
		'<a href="?action=resimsil&param='+thumbURL.split("/")[2]+'"><img src="images/fotocancel.gif" /></a>'+
		'</p></div>');
		enable_foto_delete();

	}else if(type=="sil"){

		$('#fileUploadThumb'+(thumbID)).html('<img src="images/fotoekle_buton.gif" width="100" height="75" />');

	}else{

		$('#fileUploadThumb'+(thumbID+1)).html('<img src="fotosube/'+ thumbURL + '" width="100" height="75" />');

		$('#uyeIlanEkleResimSil'+(thumbID+1)).show();

	}

	return false;

}
var tempFoto= [];
function UploadComponent()
{
	$("#fileUploadAll").fileUpload({

        'uploader': 'images/jQuploadify.swf',

        'cancelImg': 'images/cancelUpload.png',

        'folder': 'fotosube',

        'script': 'jQuploadSubeFoto.php',

        'fileDesc': 'Resim Dosyalari',

        'fileExt': '*.jpg;*.jpeg;',

        'multi': true,

        'auto': true,

		'simUploadLimit': 1,
		
		'scriptData': {'subepk':$("#subepk").val()}
	});
	
}

function showTab($this,hash)
{
	 var tabContainers = $('#tabsB > div');
	tabContainers.filter("#"+hash).show();
                                $('#tabsB ul.navigation li').removeClass('current');
                                $this.parent().addClass('current');
}
