$(function(){
    $('.lupa').click(function(){
        $(this).parents('form').submit();
    });


	$(window).resize(correct_image_size);
	correct_image_size();
	
	$(".shape-bg").fadeTo(0, .7);
	$(".gifts-catalogue a, .materials-catalogue a").click(function(e){
		e.preventDefault();
		$href=$(this).closest(".catalogue").attr("rel");

		if(!$href)
			$href=window.location.href;

		$.ajax({
			type: 'POST',
			url: $(this).attr("href"),
			data: null,
			success: function(data){
				if(data.ok==1)
				{
					window.location.href=$href;
				}
			},
			dataType: "json"
		});		
	})

	$basket_q=0;
	$basket_sum=0;
	
	$(".basketAction, .basketActionSimple")/*
.mouseenter(function(){
		if($(this).is(".basketAction"))
			$(this).prev().stop(true, true).fadeIn("fast");
	}).mouseleave(function(){
		if($(this).is(".basketAction"))
			$(this).prev().stop(true, true).fadeOut("fast");
	})
*/.click(function(e){
		e.preventDefault();
		//var price=parseInt($(this).closest(".threecol").find(".price span").html().replace(" ", ""));
		if($(this).is(".button3"))
			$self=$(this);
		else
			$self=$(this).closest(".buy");	
		
		$next=$self.next();

		$self.hide();
		$self.after("<div class='loader'></div>");

		$.ajax({
			type: 'POST',
			url: $(this).attr("href"),
			data: null,
			success: function(data){
				$self.next().remove();

				if(data.ok)
				{
					$next.fadeIn("fast");

					setTimeout(function(){
						$next.fadeOut("fast", function(){
							$self.fadeIn("fast");
						});
					}, 1000);
					
					$(".basket-blank").hide();
					$(".basket-full").show();
					
					$(".basket-full .basket-q").html(data.ok.total);
					$(".basket-full .basket-sum").html(data.ok.sum);
                   
                    //window.location.href = "/basket.html";
				}
			},
			dataType: "json"
		});
	})

	$(".gallery .visible div:not(.active), gallery-full div:not(.active)").fadeTo(0, .5);
	$(".gallery .visible div").mouseenter(function(){
		if($(this).is(".active")==false)
		{
			$(".gallery .visible div").removeClass("active");
			$(".gallery .visible div").stop(true, true).fadeTo("normal", .5);
			$(this).addClass("active").stop(true, true).fadeTo("normal", 1);

			$(".gallery-full div").stop(true, true).fadeTo("normal", 0);
			$(".gallery-full div:eq("+$(this).index()+")").stop(true, true).fadeTo("normal", 1);
		}
	})
	
	$(".fadeInOut50").fadeTo(0, .3);
	$(".fadeInOut50").mouseenter(function(){
		$(this).stop(true, true).fadeTo(200, 1);
	}).mouseleave(function(){
		$(this).stop(true, true).fadeTo(200, .3);
	});

	
	//fixed-left
	$menuheighttop=$("#fixed-left").offset().top-70;
	$wh=$(window).height();
	
	$(window).resize(function(){
		if($("#fixed-left").is(".fixed"))
			$("#fixed-left").css({width:$("#fixed-left-container").width()});

		$wh=$(window).height();
	})
	
    var currentScrollPosittion;
    
	$(window).scroll(function(){
		scrollFunc();

		$menuheight=$("#fixed-left").height();
        $contentheight=$('#fixed-left-container').next().height();

		$st=$(window).scrollTop();
        
		if($st>$menuheighttop)
		{
                //console.log($menuheight, $menuheighttop, $wh, $st + "("+($menuheight+$menuheighttop-$wh+80)+")");
			
				if((($menuheight+$menuheighttop+20)>$wh) && (($menuheight+$menuheighttop-$wh+150))<$st)
				{
                    var tmp=parseInt($("#fixed-left").css("top"));
                        
					console.log($scrollDirection);
					if($scrollDirection=="up" && $("#fixed-left").is(".fixed"))
					{
                        //tmp=tmp+2;
						//console.log(tmp, $menuheighttop);
						//$("#fixed-left").css({position:"relative", width:"auto"});

						//if(tmp<=$menuheighttop)
						//	$("#fixed-left").css({top:tmp});
                        
                        var delta = $st - currentScrollPosittion;   
                       // alert(tmp+"\n"+delta);
                       console.log(delta);
                        if(delta && tmp < 0){
                              $("#fixed-left").css("top", (tmp-delta)+"px");
                        }
                        else if(tmp > 0){
                              $("#fixed-left").css("top", "100px");
                        }
                        else{                            
                        }
					}
                    if($scrollDirection=="down" && $("#fixed-left").is(".fixed"))
					{
                        //alert($st+"\n"+currentScrollPosittion);
                        var delta = $st - currentScrollPosittion;   
                        //alert( tmp+"\n"+ (-($menuheight-$wh)-80))
                        if(delta && tmp > (-($menuheight-$wh)-80) ){
                             $("#fixed-left").css("top", (tmp-delta)+"px");
                        }
                        else if( tmp < (-($menuheight-$wh)-80) ){
                             $("#fixed-left").css("top", (-($menuheight-$wh)-80)+"px");
                        }
                        else{                            
                        }
                    }

                   
					if(!$("#fixed-left").is(".fixed") && $contentheight>$menuheight)
					{
						$("#fixed-left").addClass("fixed").css({top:-($menuheight-$wh)-80, width:$("#fixed-left-container").width()});
					}
                    
                    
				}
				else if(($menuheight+$menuheighttop+20)<=$wh)
				{
                   
					if(!$("#fixed-left").is(".fixed") && $contentheight>$menuheight)
					{
						$("#fixed-left").addClass("fixed").css({top:70, width:$("#fixed-left-container").width()});
					}
				}
                currentScrollPosittion = $st;
		}
		else
		{
			if($("#fixed-left").is(".fixed"))
				$("#fixed-left").removeClass("fixed").css({width:"auto"});
		}

		//console.log("window: "+$(window).height())
		//console.log("top: "+)
		//	$(".onecol").addClass("fixed");
	})


	//index-promo
	$currentslide=0;
	$containerWidth=954;
	$totalslides=$(".main-photo .picture").size();

    $('.arrow .l, .arrow .r').click(function(event){
        event.preventDefault();
    });

    $('.arrow .r').click(function(){ 
	    //~ $('.main-photo .picture:eq('+$currentslide+')').css({"background-position":"0px 0px"}).stop(true, true).animate({"background-position": (0-$containerWidth)+"px 0px"}, "slow", "easeOutBack");
	    //~ $('.main-photo .shape').stop(true, true).animate({"bottom":"-55px"}, "slow");
	    //~ 
	    //~ $currentslide++;
//~ 
	    //~ if($currentslide==$totalslides)
		    //~ $currentslide=0;
//~ 
	    //~ $('.main-photo .picture:eq('+($currentslide)+')').show().css({"background-position":$containerWidth+"px 0px"}).stop(true, true).animate({"background-position": "0px 0px"}, "slow", "easeOutBack");
//~ 
	    //~ if($('.main-photo .shape .name:eq('+$currentslide+')').children().text()!=""){
		    //~ $('.main-photo .shape').stop(true, true).animate({"bottom":"0px"}, "slow");
            //~ $('.main-photo .shape .name').hide();
            //~ $('.main-photo .shape .name:eq('+$currentslide+')').show();
        //~ }
        
        $('.main-photo .picture:eq('+$currentslide+')').stop(true, true).fadeOut(2000);
        $currentslide++;
        if($currentslide==$totalslides)
		    $currentslide=0;
        $('.main-photo .picture:eq('+$currentslide+')').stop(true, true).fadeIn(2000);
        
        
        if($('.main-photo .shape .name:eq('+$currentslide+')').children().text()!=""){
		    $('.main-photo .shape').stop(true, true).animate({"bottom":"0px"}, 2000);
            $('.main-photo .shape .name').hide();
            $('.main-photo .shape .name:eq('+$currentslide+')').show();
        }
        

            //clearInterval(autoChangePromoblock);
    });
    
    $('.arrow .l').click(function(){ 

	    //~ $('.main-photo .picture:eq('+$currentslide+')').css({"background-position":"0px 0px"}).stop(true, true).animate({"background-position": $containerWidth+"px 0px"}, "slow", "easeOutBack");
	    //~ $('.main-photo .shape').stop(true, true).animate({"bottom":"-55px"}, "slow");
	    //~ 
	    //~ $currentslide--;
//~ 
	    //~ if($currentslide<0)
		    //~ $currentslide=$totalslides-1;
//~ 
//~ 
	    //~ $('.main-photo .picture:eq('+($currentslide)+')').show().css({"background-position":(0-$containerWidth)+"px 0px"}).stop(true, true).animate({"background-position": "0px 0px"}, "slow", "easeOutBack");
//~ 
	    //~ if($('.main-photo .shape .name:eq('+$currentslide+')').children().text()!=""){
            //~ 
		    //~ $('.main-photo .shape').stop(true, true).animate({"bottom":"0px"}, "slow");
            //~ $('.main-photo .shape .name').hide();
            //~ $('.main-photo .shape .name:eq('+$currentslide+')').show();
        //~ }
        $('.main-photo .picture:eq('+$currentslide+')').stop(true, true).fadeOut(2000);
        $currentslide--;
        if($currentslide<0)
		    $currentslide=$totalslides-1;
        //alert($currentslide);
        $('.main-photo .picture:eq('+$currentslide+')').stop(true, true).fadeIn(2000);
        
         if($('.main-photo .shape .name:eq('+$currentslide+')').children().text()!=""){
            
		    $('.main-photo .shape').stop(true, true).animate({"bottom":"0px"}, 2000);
            $('.main-photo .shape .name').hide();
            $('.main-photo .shape .name:eq('+$currentslide+')').show();
        }

            //clearInterval(autoChangePromoblock);

    });
    
    
    var autoChangePromoblock = setInterval(function(){
            $(".arrow .r").trigger('click');
        }, 8000);

})

function correct_image_size()
{
	$ww=$(window).width();

	if($ww<1286)
	{
		$("div.photo").css({"height":"156px"});
		
		$("div.photo").each(function(){
			$bi=$(this).css("background-image").replace("/296x198/", "/233x156/");
			$(this).css({"background-image":$bi});
		})
	}
	else
	{
		$("div.photo").css({"height":"198px"});
		
		$("div.photo").each(function(){
			$bi=$(this).css("background-image").replace("/233x156/", "/296x198/");
			$(this).css({"background-image":$bi});
		})
	}
}

function correct_basket_sum(href)
{
	$.ajax({
		type: 'POST',
		url: href,
		data: null,
		success: function(data){
			if(data.ok)
			{
				if(parseInt(data.ok.sum)==0)
				{
					$(".basket-blank").show();
					$(".basket-full").hide();					

					$(".basket-blank1").slideDown();
					$(".basket-full1").slideUp();					
				}
				else
				{
					$(".basket-blank").hide();
					$(".basket-full").show();

					$(".basket-blank1").slideUp();
					$(".basket-full1").slideDown();					

					$(".basket-full .basket-q").html(data.ok.total);
					$(".basket-full .basket-sum").html(data.ok.sum);
					$(".total-sum").html(data.ok.sum);
				}
			}
		},
		dataType: "json"
	});
}


function scrollFunc(e) {
	$scrollDirection="";

    if ( typeof scrollFunc.x == 'undefined' ) {
        scrollFunc.x=window.pageXOffset;
        scrollFunc.y=window.pageYOffset;
    }
    var diffX=scrollFunc.x-window.pageXOffset;
    var diffY=scrollFunc.y-window.pageYOffset;

    if( diffX<0 ) {
        // Scroll right
    } else if( diffX>0 ) {
        // Scroll left
    } else if( diffY<0 ) {
        $scrollDirection="down";
    } else if( diffY>0 ) {
        $scrollDirection="up";
    } else {
        // First scroll event
    }
    scrollFunc.x=window.pageXOffset;
    scrollFunc.y=window.pageYOffset;
}

