/*
 * TIMERS
 */
jQuery.fn.extend({everyTime:function(interval,label,fn,times,belay){return this.each(function(){jQuery.timer.add(this,interval,label,fn,times,belay)})},oneTime:function(interval,label,fn){return this.each(function(){jQuery.timer.add(this,interval,label,fn,1)})},stopTime:function(label,fn){return this.each(function(){jQuery.timer.remove(this,label,fn)})}});jQuery.extend({timer:{guid:1,global:{},regex:/^([0-9]+)\s*(.*s)?$/,powers:{ms:1,cs:10,ds:100,s:1000,das:10000,hs:100000,ks:1000000},timeParse:function(value){if(value==undefined||value==null){return null}var result=this.regex.exec(jQuery.trim(value.toString()));if(result[2]){var num=parseInt(result[1],10);var mult=this.powers[result[2]]||1;return num*mult}else{return value}},add:function(element,interval,label,fn,times,belay){var counter=0;if(jQuery.isFunction(label)){if(!times){times=fn}fn=label;label=interval}interval=jQuery.timer.timeParse(interval);if(typeof interval!="number"||isNaN(interval)||interval<=0){return}if(times&&times.constructor!=Number){belay=!!times;times=0}times=times||0;belay=belay||false;if(!element.$timers){element.$timers={}}if(!element.$timers[label]){element.$timers[label]={}}fn.$timerID=fn.$timerID||this.guid++;var handler=function(){if(belay&&this.inProgress){return}this.inProgress=true;if((++counter>times&&times!==0)||fn.call(element,counter)===false){jQuery.timer.remove(element,label,fn)}this.inProgress=false};handler.$timerID=fn.$timerID;if(!element.$timers[label][fn.$timerID]){element.$timers[label][fn.$timerID]=window.setInterval(handler,interval)}if(!this.global[label]){this.global[label]=[]}this.global[label].push(element)},remove:function(element,label,fn){var timers=element.$timers,ret;if(timers){if(!label){for(label in timers){this.remove(element,label,fn)}}else{if(timers[label]){if(fn){if(fn.$timerID){window.clearInterval(timers[label][fn.$timerID]);delete timers[label][fn.$timerID]}}else{for(var fn in timers[label]){window.clearInterval(timers[label][fn]);delete timers[label][fn]}}for(ret in timers[label]){break}if(!ret){ret=null;delete timers[label]}}}for(ret in timers){break}if(!ret){element.$timers=null}}}}});if(jQuery.browser.msie){jQuery(window).one("unload",function(){var global=jQuery.timer.global;for(var label in global){var els=global[label],i=els.length;while(--i){jQuery.timer.remove(els[i],label)}}})};


var galstate = 0;
var chatWindow = null;

(function($) {

	function InitSite()
	{
		if( $("#search-field"))
			InitSearchField();
		if( $("#cart"))
			InitInformerBasket();
		if( $("#gallery .thumb").length > 0)
			InitGallery();
		if( $("#nextstep").length > 0)
			InitOrderChange();	
		if ($("#gallery div").length > 1)
			$("#glr-toggle").fadeIn("slow");

			

	if(window.pageYOffset > 49)
		$("#informer").css({"top":"0px"});
	$(window).scroll(
	function ()
	{	
		if ((!$.browser.msie) && (!$.browser.opera)) {
		if(window.pageYOffset < 49)
			$("#informer").css({"top":(49-window.pageYOffset)+"px"});
		if(window.pageYOffset > ($(document).height()-1400))
			$("#nextstep").css({"top":($(document).height()-1060)+"px"});
		else
			$("#nextstep").css({"top":(340+window.pageYOffset)+"px"});
		}
	});
		/*$(document).pngFix();*/
	}

	function InitSearchField()
	{
		var sVal	= "";
		if($.browser.msie){
     	$(".search div input").css("padding-top","0");
     	}
		$("#search-field").val( sVal );
		$("#search-field").focus(function(){
			if( $(this).val() == sVal )
			{
				$(this).val("");
				$(this).addClass("active");
			}
		});
		$("#search-field").blur(function(){
			if( $(this).val() == "" )
			{
				$(this).val(sVal);
				$(this).removeClass("active");
			}
		});
	}

	function InitInformerBasket()
	{
		$(".add_basket").click(function(){
			var loadid = $(this).attr("rel");
			if(window.pageYOffset > 30)
				$("#informer").css({"top":"0px"});
			var id = $(this).attr("rel");
			$.ajax({
				url: '/f/ajax/cart.php?id='+id,
				beforeSend: function(){
					$('#loader_'+loadid).show();
				},
				success: function(data){
					$('#loader_'+loadid).hide();
					$("#cart-slider").empty().append(data);
					$("#informer").fadeIn("fast");
					$("#cart").html(Number($("#cart").html()) + 1);
					$("#informer").oneTime("1s", function(){
						$("#cart-slider").stop().animate({ marginLeft: -94 + "px" }, 600 );
					});
					$("#informer").oneTime("3s", function(){
						$(this).fadeOut(2000);
						$("#cart-slider").stop().animate({ marginLeft: "0px" }, 100 );
					});
				}
			});
		});

$(".btn-order").click(function(){
			if(window.pageYOffset > 50)
				$("#informer").css({"top":"0px"});
			var id = $(this).attr("rel");
			$.ajax({
				url: '/ajax/cart.php?id='+id,
				beforeSend: function(){},
				success: function(data){
					$("#cart-slider").empty().append(data);
					$("#informer").fadeIn("fast");
					$("#cart").html(Number($("#cart").html()) + 1);
					$("#cart-slider").stop().animate({ marginLeft: -94 + "px" }, 600 );
					$("#informer").oneTime("3s", function(){
						$(this).fadeOut(2000);
						$("#cart-slider").stop().animate({ marginLeft: "0px" }, 100 );
					});
				}
			});
		});

		$("#cart-container a").mouseover(function(){
		if ($("#cart").html() == "0") {
      	$("#informer").fadeIn("fast");
      	}
    	}).mouseout(function(){
      	$("#informer").fadeOut("slow");
    	});
	}
	function InitGallery()
	{
		$("#gallery .thumb").livequery("click", function(){
		//window.alert("clicked!");
			var BigImgSrc = $(this).attr('id');
			//alert(BigImgSrc);
			$.ajax({
				url: '/ajax/gallery.php?ID_IMG=' + BigImgSrc,
				type: "GET",
				dataType: "text",
				beforeSend: function(){
					$('#glr-image').fadeOut("fast").empty();
					$("#i-loader").show();
				},
				success: function(data){
					$("#i-loader").hide();
					$("#glr-image").html(data);
					$("#glr-image").fadeIn("fast");
				}
			});
		});
	}
	
	function InitOrderChange()
	{
		  var qtyval = 0;
		  $(".btn-add").livequery('click', function() {
			var id = $(this).parents(".product").attr("id");
			var geturl = ('/ajax/addtoorder.php?id=' + id);
			$.ajax({
				url: geturl,
				beforeSend: function(){
					$("#Spinner").show();
					$("#"+id).remove();
				},
				success: function(data){
					$("#Spinner").fadeOut("slow");
					$(".product-row:last").after(data, function() { $(this).fadeIn("slow");});
					UpdateOrderTotals();
					if ($(".product").length == 0) {
					$("#offer").fadeOut("slow");
					}
				}
			});
			return false;
		});
		  $(".row-del").livequery('click', function() {
			  // Ð? Ð²Ð¾Ñ? Ñ?Ñ?Ñ? Ð½Ñ?Ð¶ÐµÐ½ Ð²Ñ?Ð·Ð¾Ð² Ð°Ñ?ÐºÑ?Ð¾Ð²Ð¾Ð³Ð¾ Ñ?ÐºÑ?Ð¸Ð¿Ñ?Ð° Ð½Ð° Ñ?Ð´Ð°Ð»ÐµÐ½Ð¸Ðµ Ñ?Ð¾Ð²Ð°Ñ?Ð°
		  if ($(".row-del").length == 1) {
		  $("#content-order").fadeOut("fast", function() { $("#content-empty").fadeIn("slow");});
		  } 
      	  var id = $(this).parents(".product-row").attr("id");
		  // Ð?Ð¸Ñ?ÐµÐ¼ Ð°Ñ?ÐºÑ?Ð¾Ð²Ñ?Ð¹ Ð²Ñ?Ð·Ð¾Ð²
		  $.ajax({
				url: '/ajax/basket_del_row.php?id=' + id,
				beforeSend: function(){
				},
				success: function(data){
				  $("#"+id+" .btn-del").hide();
  				  $("#"+id+"_quant").val('0');
				  $("#"+id).fadeOut("slow", function() { $(this).empty();});
				  UpdateOrderTotals(); 
				}
			});      	  
    });
    	$(".addservice").livequery('change', function() { 
      	  var id = $(this).parents(".product-row").attr("id");
      	  var price = parseInt($("#"+id+"_price").html());
      	  var sprice = parseInt($("#"+id+"_service").val());
      	  var quantity = parseInt($("#"+id+"_quant").val());
      	  if ($("#"+id+"_addservice").is(":checked")){			  
      	  $("#"+id+"_price").html(price + sprice);
      	  $("#"+id+"_sum").html(price * quantity + sprice * quantity);
      	  UpdateOrderTotals();
      	  }
      	  else {			  
      	  $("#"+id+"_price").html(price - sprice);
      	  $("#"+id+"_sum").html(price * quantity - sprice * quantity);
      	  UpdateOrderTotals();
      	  } 
    	});
    	$(".paytype").change(function () { 
    	  var id = $(this).attr("id");
      	  if (id == "pay-card") {
      	  $("#card-bw").hide();
      	  $("#card-color").show();
      	  } else {
      	  $("#card-bw").show();
      	  $("#card-color").hide();
      	  } 
    	});
    	$(".quant").livequery('focus', function() {
         qtyval=$(this).val();
         id = $(this).parents(".product-row").attr("id");
    	});
    	$(".quant").livequery('blur', function() {
    	  var id = $(this).parents(".product-row").attr("id");
      	  var price = $("#"+id+"_price").html();
      	  if($("#"+id+"_quant").val()==0){
      	  $.ajax({
				url: '/ajax/basket_upd_row.php?id=' + id + '&cnt=0',
				beforeSend: function(){
				},
				success: function(data){
					$("#"+id+" .btn-del").hide();
				  $("#"+id).fadeOut("slow", function() { $(this).empty();});
				  if ($(".row-del").length == 1) {
				  $("#content-order").fadeOut("fast", function() { $("#content-empty").fadeIn("slow");});
				  }
				  UpdateOrderTotals(); 
				}
			});		  
    	}
    	else if($("#"+id+"_quant").val()>=1){
			$.ajax({
				url: '/ajax/basket_upd_row.php?id=' + id + '&cnt=' + $("#"+id+"_quant").val(),
				beforeSend: function(){
				},
				success: function(data){
					if(price == 'Ð?Ð° Ð·Ð°ÐºÐ°Ð·')
						$("#"+id+"_sum").html('Ð?Ð° Ð·Ð°ÐºÐ°Ð·');
					else
						$("#"+id+"_sum").html($("#"+id+"_quant").val()*price);
      				UpdateOrderTotals();  
				}
			});      	  
    	}
    	else {
    	  $("#"+id+"_quant").val(qtyval);
    	  UpdateOrderTotals();
    	}
    });
	}
	
	function UpdateOrderTotals()
	{
	var sum=0;
	$(".quant").each(function (i) {
    	  id = $(this).parents(".product-row").attr("id");
    	  price = $("#"+id+"_price").html();
		  if(price == 'Ð?Ð° Ð·Ð°ÐºÐ°Ð·') price = 0;
		  sum = sum + $(this).val() * price;
    	  });
    $("#total").html(sum);
    if (parseInt(sum) < 5000) {
    $("#shipping").html('200');
    } else {
    $("#shipping").html('0');
    }
    //window.alert(less);
    $("#gtotal").html(sum + $("#shipping").html() * 1);
    if ($(".oem-warning").length == 1) {
		$("#oem-warning").fadeOut("slow");
		$("#btn-pay-disabled").hide();
		$("#btn-pay-enabled").show();	
    }
    }
    

	
	function InitSorting()
	{
		$("#sorting UL").each(function(i){
			var iPos	= $(this).position();
			$(".sortlabel:eq("+ i +")").css({ top: iPos.top });
		});
		$("#sorting SPAN").click(function(){
			var pos	= $(this).position();
			var i	= $("#sorting UL").index($(this).parent().parent());
			$("#sorting UL:eq("+ i +") SPAN").removeClass("selected");
			$(this).addClass("selected");
			$(".sortlabel:eq("+ i +")").animate({ top: pos.top }, "fast");
		});
	}




////////////////////////////////////////////////////////////////////////////////
	$(document).ready( function(){ InitSite(); });
})(jQuery);
