﻿$(document).ready(function(){
	var placeholder = ".search input";
	var firstVal = "Поиск по сайту";
	var path = null;
	
	$(".products li:eq(0) a").addClass("active");
	path = $(".products li:eq(0) a").attr("href");
	replacer("first");
	
	$(".products li a").click(function(){
		if($(this).attr("class") != "active") {
			$(".products li a").removeClass("active");
			$(this).addClass("active");
			path = $(this).attr("href");
			replacer("next"); 
		}
        else {
            window.location.replace($(this).next('span').html());
        }
		return false;
	});
	
	function replacer(mode) {
		if(mode == "first") {
    		$(".products div img").remove();
			$(".products div").prepend("<img src="+path+" /> ");
		}
		else {
			$(".products div").prepend("<img src="+path+" /> ");
			$(".products div img:eq(1)").fadeOut(500, function(){ $(this).remove(); });
		}
	}

    $(".products div").click(function() {
	    var link = $(".products li a.active").next("span").html();
        window.location.replace(link);
    });
	
	/* view mode */
	if(window.location.hash) { var mode = window.location.hash.replace("#",''); setActive(mode); }	
	
	$(".list-view a").bind("click", function(){
		if($(".list-view a").attr("class") != "active")
		{
			$(".catalogue").css({"visibility":"hidden"});
			var mode= "list";
			setActive(mode);
			$(".catalogue").css({"visibility":"visible", "opacity":"0"}).animate({"opacity":"1"});
		}
	});

	$(".thumb-view a").bind("click", function(){
		if($(".thumb-view a").attr("class") != "active")
		{
			$(".catalogue").css({"visibility":"hidden"});
			var mode= "thumb";
			setActive(mode);
			$(".catalogue").css({"visibility":"visible", "opacity":"0"}).animate({"opacity":"1"});
		}
	});
	
	function setActive(mode){
		if (mode == "list")
		{
			$(".thumb-view a").removeClass();
			$(".list-view a").addClass("active");
			$(".catalogue").removeClass("thumb").addClass(mode);
		}
		if (mode == "thumb"){
			$(".list-view a").removeClass();
			$(".thumb-view a").addClass("active");
			$(".catalogue").removeClass("list").addClass(mode);
		}

	}
	/* view mode */
	
	$(".question").click(function(){
		$(".question-form").fadeIn(500);
		return false;
	});
	
	$(".close").click(function(){
		$(".question-form").fadeOut(500);
		return false;
	});
	

	// placeholder
	$(placeholder).focus(function(){ $(this).addClass("active"); if($(this).val() == firstVal) { setValu(""); } });
	$(placeholder).blur(function(){ $(this).removeClass("active"); if($(this).val() == "") { setValu(firstVal); } });
	function setValu(value){ $(placeholder).val(value); }
	// placeholder
	
    var curPartner = 0;
    var partners = $(".partners ul li");

    $(partners[0]).fadeIn("slow");

    window.setInterval(function(){
        $(partners[curPartner]).fadeOut("slow", function(){
            $(this).hide();
            if (++curPartner >= partners.length) {
                curPartner = 0;
                $(partners[0]).fadeIn("slow");
            }
            else {
                $(partners[curPartner]).fadeIn("slow");
            }
        });
    }, 4000);
    
    $(".product_descr").each(function(){
	$(this).css('width', $(this).prevAll("img").attr("width"));
    });
    
    $("a.inline").fancybox({
        /*
        'onStart': function(){
            var img_src = $(this.orig).parents("li.item").find(".product .img_src");
            $('<img src="'+img_src.html()+'"/>').insertAfter(img_src).bind('load', function(){
                $.fancybox.resize();
            });
        },
        ;*/
        'onComplete': function(){
    	    var height = $("#fancybox-wrap .product_wrap").height();
    	    //alert('height = ' + height);
    	    $('#fancybox-wrap').height (height+20);
    	    $('#fancybox-inner').height(height+20);
            $(this).resize();
        },
        'autoDimensions' : 'true'
    });
});

