$(document).ready(function(){
	/* Sponsor animasyonu */
	$('#sponsorSub1,#sponsorSub2,#sponsorSub3,#sponsorSub4').cycle({fx: 'curtainX',delay:  -5000});

	/* Error Div Back Set */
	var doHeight=$(document).width();
	var scWidth=$(window).width();
	var scHeight=$(window).height();
	var eDivWidth= $("#errorDiv").width();
	var eDivHeight= $("#errorDiv").height();	
	var eDivLeft=(scWidth-eDivWidth)/2;
	var eDivTop=(scHeight-eDivHeight)/2;
	
	$("#errorBackDiv").css("height",doHeight).css("width",scWidth);
	$("#errorDiv").css("top",eDivTop+'px').css("left",eDivLeft+'px');

	/* Error Div Position Finder */
	$(window).scroll(function () {
		var sct=$(window).scrollTop();					
		if (jQuery.browser.msie && jQuery.browser.version<7) {
			$("#errorDiv").css("top",sct+eDivTop+'px').css("left",eDivLeft+'px');
			$("#compareAjaxDiv").css("top",sct+200);
			}
	});
	

	
	/* Star Rating START*/
	$('.auto-submit-star').rating({
		callback: function(value, link){
		$.ajax({
			   type: "GET",
			   url: siteName+"Ajax/starRatingSubmit/"+value,	
			   success: function(msg){
			    var newmsg=msg.replace(/[\n\r\t]/g,'');
					errorDivStart(newmsg);
			   }
			 });
		  }
		});
	/* Star Rating END */
	
	/* Light box Function START*/
	$('a[rel*=lightbox]').lightBox();
	/* Light box Function END*/
	
	$("#compareAjaxDivButton").toggle(
    	      function () {   
    	    		$.ajax({type: "POST",url: siteName+"Ajax/compareLoad/",
    	    			   success: function(msg){
    	    				if( !msg ){
    	    					msg = "<div style='color:#98ABC5;font-size:14px;font-weight:bold;text-align:center;padding:61px 0 12px 17px;'>Fiyat karşılaştırma sepetine henüz ürün eklemediniz.</div>";
    	    					$("#compareAjaxButtonContainer").hide();    	    					
    	    				}
	    					$("#compareAjaxRowContainer").html(msg);
    	    				$("#compareAjaxDiv").animate({"left": "-5px"}, "slow");}
    	    				
    	    			 });
    	      },
    	      function () {
    	    	  $("#compareAjaxDiv").animate({"left": "-596px"}, "slow"); 
    	    	  $("#compareAjaxDivButton").css("background-image","url("+siteName+"images/sepet_button_ileri.gif)");

    	      }
    	    );
	$("#compareAjaxDiv").animate({"left": "-596px"}, 1000);
	
	 $('a[href*=#]').click(function() {
	    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	    && location.hostname == this.hostname) {
	      var $target = $(this.hash);
	      $target = $target.length && $target
	      || $('[name=' + this.hash.slice(1) +']');
	      if ($target.length) {
	        var targetOffset = $target.offset().top;
	        $('html,body')
	        .animate({scrollTop: targetOffset-30}, 800);
	       return false;
	      }
	    }
	  });

	 /* Otomatik Ortalama */
	 $("(*)[rel='ortala']").each(function (i) {
	   	  var xLen = $(this).width();
	   	  var pLen = $(this).parent().width(); 
	   	  var son=(pLen-xLen)/2;
	   	  if (jQuery.browser.msie && jQuery.browser.version<7){son=son-106;}
	   	  $(this).css("margin-left",son+"px");
     });


});

function imageResizeFonk(whichPage){
	
    $('.story-small img').each(function() {
    	if(whichPage == 1){
    		var maxWidth = 225; // Max width for the image
            var maxHeight = 126;    // Max height for the image
    	}else{
    		var maxWidth = 150; // Max width for the image
    		var maxHeight = 145;    // Max height for the image
    	}
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
 
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }
 
        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }
    });
	
}

function compareRowLoadOrDelete(rowID){
	$("#compareRow1_"+rowID).css("background-color","gray").slideUp("slow");
	$("#compareRow2_"+rowID).slideUp("slow");
	$("#compareAjaxCompButton").fadeIn("slow");
	  $.ajax({
		   type: "POST",
		   url: siteName+"Ajax/compareLoad/"+rowID,
		   success: function(msg){
			var newmsg=msg.replace(/[\n\r\t]/g,'');
			
			if( !msg ){
				msg = "<div style='color:#98ABC5;font-size:14px;font-weight:bold;text-align:center;padding:61px 0 12px 17px;'>Fiyat karşılaştırma sepetine henüz ürün eklemediniz.</div>";
				$("#compareAjaxButtonContainer").hide();
			} else {
				$("#compareAjaxButtonContainer").show();
			}
			$("#compareAjaxRowContainer").html(msg);
		
	  		}
		 }); 	
	
}

function selectImageTab(sira,stopper){
	
	companyTabChange(sira);
    var yeniSira=parseInt(sira)+1;
    if (yeniSira>3){yeniSira=1;}
    if (stopper==1){
    	clearTimeout($slk);
    }else{    
    	$slk=setTimeout("selectImageTab("+yeniSira+")", 8000);    
    }
}


function compare(productID){
	 var cadrOffset=$("#compareAjaxDivResult").offset();
	 $.ajax({
		   type: "GET",
		   url: siteName+"Ajax/compare/"+productID,
		   success: function(msg){
		 	  var newmsg=msg.replace(/[\n\r\t]/g,'');
		 	  if (cadrOffset.left<-5){
		 		  errorDivStart(newmsg);		 	  
		 	  }
		   }
		 });
	  compareRowLoadOrDelete('');
}

function ajaxMenu(divWidth,divHeight,divLeft,divTop,divNo){	
	$("#ajaxMenuDiv_"+divNo).css({ width: divWidth+"px", height: divHeight+"px", marginLeft: divLeft+"px", marginTop: divTop+"px"});
	if ($("#ajaxMenuDiv_"+divNo).attr("rel")==1){
		$("div[id*='ajaxMenuDiv_']").slideUp("slow").attr("rel","1");
		$("#ajaxMenuDiv_"+divNo).slideDown("slow").attr("rel","2");
		}else{
		$("#ajaxMenuDiv_"+divNo).slideUp("slow").attr("rel","1");	
		}
}


function kSlider(direction,totalBoxNumber){
	var boxWidth=420;
	var defaultValue = new Number($("#slide").attr("rel"));	
	
	$("#slide").css("width",(boxWidth*totalBoxNumber));

	
	if (direction=="right"){
		$("#slideLeftButton").show();
		var zx=defaultValue+1; var sign="-";
		$(".slideContainer").css("margin-left","11px");
		if( totalBoxNumber > (zx - 1))
			$("#slide").attr("rel",zx).animate({"left": sign+"="+boxWidth+"px"}, "slow");
		if ( totalBoxNumber == zx )
			$("#slideRightButton").hide();
	}
	
	if (direction=="left"){
		$("#slideRightButton").show();
		var zx=defaultValue-1; var sign="+";
		if ( zx > 0 )
			$("#slide").attr("rel",zx).animate({"left": sign+"="+boxWidth+"px"}, "slow");	
		if( zx == 1){
			if (jQuery.browser.version == "6.0")
				$(".slideContainer").css("margin-left","33px");
			else
				$(".slideContainer").css("margin-left","73px");
			$("#slideLeftButton").hide();
		}
	}
	

}

function sendSearch(link){
	var searchName=$("#searchName").val();
	searchName = trim(searchName);
	if (searchName){
	    if (searchName.length<3){
				errorDivStart("Arama kelimesi en az üç karakter olmalıdır.");
		}else {
			$("body, input").css("cursor","wait");
			document.location.href=link+"POPULER-"+searchName+"-1-ASC.html";
	    }
	} else {
		errorDivStart("Lütfen aramak için bir kelime giriniz");
	}
}

function kTextCount(ID){
	var limit=2000;
	var textLength=$("#"+ID).attr("value").length;
	var text=$("#"+ID).val();
	var result=limit-textLength;
	var finalWord="<b>"+result+"</b> karakter daha yazabilirsiniz.";
	if (textLength>limit-1){
		var newText=text.substr(0,limit);
		$("#"+ID).val(newText);
		result=0;
		finalWord="<b>Yazma limitiniz doldu.</b>"
	}
	$("#remainingLinesText").html(finalWord);	
}

function kUpperCaseConvert(inputID){
	var text= $("#"+inputID).val();
	var newText= text.toUpperCase();
	$("#"+inputID).val(newText);
}

function userFormControl(formID){
	var control=0;
	if ($("#commentText").val()==""){control++;}
	if ($("#commentTitleX").val()==""){control++;}
	
	if (control>0){
		errorDivStart("Lütfen formu eksiksiz doldurunuz.");
		return;
	} else {
		$("#"+formID).submit();
	}
}

/* Error div functions */
function errorDivStart(message){
	setTimeout("errorDivShow('"+message+"')",500);
}

function errorDivShow(message){
	$("#errorMain").html(message);
	$("#errorBackDiv").show();
	$("#errorDiv").fadeIn("slow");	
	setTimeout("errorDivHide()",2000);
	
}

function errorDivHide(){
	$("#errorDiv").fadeOut("slow");
	$("#errorBackDiv").hide();
}

function removeSpaces(string) {
	//splitstring = string.split(' ');
	return string.split(' ').join('');
}

function LoginControl(formId){
	
	var error = "";
	if ($("#companyPassword").val()==""){error="Şifre alanı boş olmamalıdır";}
	if ($("#companyEmail").val()==""){error="E-Posta alanı boş olmamalıdır";}
	if(error!=""){
		errorDivStart(error);
		return false;
	}else{
		document.getElementById(formId).submit();
	}
}

function newCompanyControl(formID){
	var error="";
	 
	if ($("#userSName").val()==""){error="Firma Yetkilisinin Soyadı boş olmamalıdır";}
	if ($("#userName").val()==""){error="Firma Yetkilisinin Adı boş olmamalıdır";}
	if ($("#companyTel").val()==""){error="Firma Telefon alanı boş olmamalıdır";}
	if ($("#companyURL").val()=="http://" || $("#companyURL").val()==""){error="Firma Url alanı boş olmamalıdır";}
	if ($("#companyName").val()==""){error="Firma Adı alanı boş olmamalıdır";}
 	
/*	if (error==""){
		if (emailCheck($("#userEmail2").val())== false){error="Hatalı email adresi girdiniz";}
		if ($("#userPassword2").val()!=$("#reuserPassword").val()){error="Şifreler aynı olmalıdır";}
		if ($("#userEmail2").val()!=$("#reuserEmail").val()){error="Email adresleri aynı olmalıdır";}
	} */
	
	if(error!=""){
		errorDivStart(error);
		return false;
	}else{
		document.getElementById(formID).submit();
	}	
}

function newUserFormControl(formID){
	var error="";
	
	if ($("#reuserEmail2").val()==""){error="E-Posta tekrarı alanı boş olmamalıdır";}
	if ($("#userEmail2").val()==""){error="E-Posta alanı boş olmamalıdır";}
	if ($("#reuserPassword2").val()==""){error="Şifre tekrarı alanı boş olmamalıdır";}
	if ($("#userPassword2").val()==""){error="Şifre alanı boş olmamalıdır";}
	if ($("#userName2").val()==""){error="Ad alanı boş olmamalıdır";}
	if ($("#userSurname2").val()==""){error="Soyad alanı boş olmamalıdır";}
 	
	if (error==""){
		if (emailCheck($("#userEmail2").val())== false){error="Hatalı e-posta adresi girdiniz";}
		if ($("#userPassword2").val()!=$("#reuserPassword2").val()){error="Şifreler aynı olmalıdır";}
		if ($("#userEmail2").val()!=$("#reuserEmail2").val()){error="E-Posta adresleri aynı olmalıdır";}
	}
	
	if(error!=""){
		errorDivStart(error);
		return false;
	}else{
		document.getElementById(formID).submit();
	}	
		
}

function emailCheck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){return false;}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
	if (str.indexOf(at,(lat+1))!=-1){return false;}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if (str.indexOf(dot,(lat+2))==-1){return false;}
	if (str.indexOf(" ")!=-1){return false;}
	return true;					
}

function forgotPasswordFormControl(emailInputID,formID){
	var email=$("#"+emailInputID).val();
	if(email){
		if(emailCheck(email)){
		  $("#"+formID).submit();
		} else {
		   errorDivStart("Lütfen e-posta adresinizi kontrol ediniz.");
		}
	} else {
		errorDivStart("Lütfen e-posta adresinizi yazınız.");		
	}
}

function enterSubmit(evt,formID) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13 || charCode == 3) {
        return true;
    } else {
        return false;
    }
}

function trim(stringToTrim){
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isNumberKey(evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}

function ieDownload(){
	document.location.href="http://www.microsoft.com/downloads/info.aspx?na=90&p=&SrcDisplayLang=tr&SrcCategoryId=&SrcFamilyId=341c2ad5-8c3d-4347-8c03-08cdecd8852b&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2fC%2fB%2f7%2fCB7C2809-EB9C-4640-90CE-8E98E641BA64%2fIE8-WindowsXP-x86-TRK.exe";
	}

function companyTabChange(tabID){
	$("div[id*='companyTab_']").removeClass("logoHover").addClass("logoStandart");
	$("#companyTab_"+tabID).addClass("logoHover");
		
	$("div[id*='companyProductTabBox_']").hide();
	$("#companyProductTabBox_"+tabID).show();

}

function setBookmark() {
  url=document.location.href;
  if (window.sidebar) { // Mozilla Firefox Bookmark
    window.sidebar.addPanel(url, url,"");
  } else if( window.external ){ // IE Favorite
    window.external.AddFavorite( url, url);
  } else if(window.opera && window.print) { // Opera Hotlist
    return true;
  }
}

function cerezClose(){
	
	var sor = confirm('Kapatmak istediğinizden emin misiniz ?');
	if(sor){
		$.ajax({
			type: "GET",
			url: siteName+"Ajax/Cookies/",	
			success: function(msg){
				if(msg == "ok") {
					$("#cerezSabit").hide();
				}
		   	}
		 });
	}
}

function SliderRun(sira,direction,totalBoxNumber){
		
	var yeniSira;

    if (sira == 10){
    	direction = "left";
    } else if (sira == 1){
    	direction = "right";
    }
    
    if(direction == "left"){
    	yeniSira = parseInt(sira)-1;
    } else {
    	yeniSira = parseInt(sira)+1;
    }
    
    if (sira != 0)
    	kSlider(direction,totalBoxNumber);
    
    setTimeout("SliderRun('"+yeniSira+"','"+direction+"','"+totalBoxNumber+"')", 3000);
}
