var scrollApi;
var lastScrollHover=false;
var slidesArr=new Array();
var brandsShow=false;
var insideHelper=false;
$(function(){
	$(".menu .db").mousedown(function(){
		$(this).addClass("press");
		//return false;
	});
	$("body").mouseup(function(){
		$(".menu .db.press").removeClass("press");
		if(!insideHelper && !brandsShow)
		{
			insideHelper=false;
			//setTimeout(function(){
				$("#inputHelp").hide();
			//},100);
				//setTimeout(function(){ brandsShow=false; alert("root false"); }, 1000);
		}
		else
		{
			if(!insideHelper && brandsShow)
			{
				insideHelper=false;
				$("#inputHelp").hide();
				setTimeout(function(){ brandsShow=false; }, 100);
			}
		}
	});
    
	window.sc_shift       = 1;
	window.sc_shift_count = 5;
	
    $("#custome_slider .rightArrow").click(function (event) {
		var self            = this,
			wrapper         = $("#custome_slider"),
			items_container = $(".items", wrapper),
			item_width      = $(".item:first", items_container).width(),
			items_count     = $(".items .item", wrapper).length,
			items_width     = items_count * item_width;
			
		if (window.sc_shift > Math.ceil ((items_count - Math.floor (wrapper.width() / item_width)) / window.sc_shift_count)) return false;

		var offset     = item_width * window.sc_shift_count * (window.sc_shift - 1),
			new_offset = item_width * window.sc_shift_count * window.sc_shift;
			
		window.sc_shift++;
		
		if (new_offset > items_width - wrapper.width())
			new_offset = items_width - wrapper.width();
		
		items_container.stop(true).animate({
				left: -new_offset + "px" 
			},
			1200,
			"easeOutBack",
			onScrollSeek
		);

        return false;
    });
    
    $("#custome_slider .leftArrow").click(function (event) {
		var self            = this,
			wrapper         = $("#custome_slider"),
			items_container = $(".items", wrapper),
			item_width      = $(".item:first", items_container).width(),
			items_count     = $(".items .item", wrapper).length,
			items_width     = items_count * item_width;
			
		if (window.sc_shift <= 1) return false;
		window.sc_shift--;
		
		var offset     = item_width * window.sc_shift_count * window.sc_shift;	
		if (offset > items_width - wrapper.width()) offset = items_width - wrapper.width();
		new_offset = offset - item_width * window.sc_shift_count;
		
		items_container.stop(true).animate({
				left: -new_offset + "px" 
			},
			1200,
			"easeOutBack",
			onScrollSeek
		);

        return false;
    });

    /*scrollApi=$("#topScroller").scrollable({ 
        nextPage: ".rightArrow",
        prevPage: ".leftArrow",
        easing:"easeOutBack",
        speed: 1200,
        globalNav: true,
        clickable: false,
        api: true,
        onSeek: onScrollSeek
    });
	
	$(".slider .leftArrow").click(function(){
		return false;
	});
	$(".slider .rightArrow").click(function(){
		return false;
	});*/
	$(".inn .plashka, .inn .fPlashka").mouseover(function(){
		lastScrollHover=$(this);
		//var popravka=scrollApi.getRoot().find(".items").css("left");
		var popravka=$("#custome_slider").find(".items").css("left");
		popravka=parseInt(popravka);
		if(isNaN(popravka)) popravka=0;
		
		var off=$(this).offset();
		var scrOffset=$(this).parent().parent().parent().offset();
		
		var itog=off.left-scrOffset.left+popravka+($(this).width()/2);
		
		if(itog<0 || itog> 960)
		{
			$("#galka").hide();
		}
		else
		{
			$(".topPic .show").removeClass("show");
			var tmp=$(this).attr("id");
			tmp=tmp.replace("item","");
			$("#mainPic"+tmp).addClass("show");
			$("#galka").css({ left: itog, display: "block" });
			
		}
	});
	
	$("div.input input").focus(function(){
		if($(this).val()==$(this).parent().find("input[type=hidden]").val()) $(this).val("");
	});
	$("div.input input").blur(function(){
		if($(this).val()=="") $(this).val($(this).parent().find("input[type=hidden]").val());
	});
	
	
	$(".button.tab").click(function(){
		$(this).parent().find(".press").removeClass("press");
		$(this).addClass("press");
		var id=$(this).attr("id");
		$(this).parent().parent().find(".texts").children("div").hide();
		//alert($(this).parent().parent().find(".texts").html());
		$("#"+id.replace("_","")).show();
		return false;
	});
	
	$(".changePic").click(function(){
		var url=$(this).parent().attr("href");
		$(this).parent().parent().parent().find(".mainPic img").attr("src",url);
		return false;
	});
	$(".changePic").mouseover(function(){
		var url=$(this).parent().attr("href");
		$(this).parent().parent().parent().find(".mainPic img").attr("src",url);
		//alert($(this).parent().parent().parent().html());
	});
	
	
	
	/** Basket! **/
	
	$(".wantToBasket").click(function(){
		var itemId=$(this).attr("id");
		itemId=itemId.replace("itemId","");
		showMessage("Добавление товара в корзину..", "prepare", 3000);
		
		$.post("/ajax_handlers/basket.php",{ METHOD: "ADD", ID: itemId },function(e){
			if(e=="OK") { showMessage("Товар успешно добавлен", "success", 3000); refreshBasket(); }
			if(e=="ERROR") showMessage("Ошибка...", "error", 3000);
			//alert(e);
		});
		return false;
	});
	
	
	/** Brand list **/
	$("#inputHelp").mouseover(function(){
		//$("#debugger").html("inside!");
		insideHelper=true;
	});
	$("#inputHelp").mouseout(function(){
		//$("#debugger").html("outside!");
		insideHelper=false;
	});
	$("#showList").click(function(){
		if(!brandsShow)
		{
			brandsShow=true;
			var eto=$(this);
			var etoOff=eto.parent().offset();
			$("#inputHelp").css({ 
				display: "block",
				left: etoOff.left,
				top: etoOff.top+20,
				height: 50
			});
			$("#inputHelp").show();
			$.get("/ajax_handlers/manufacturers.php", { }, function(e){
				$("#inputHelp").css({ maxHeight:300, height: "auto" });
				$("#inputHelp").html(e);
			});
			$("#inputHelp").html("<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Загрузка...");
		}
		/*else
		{
			brandsShow=false;
			$("#inputHelp").html("");
			$("#inputHelp").hide();
		}*/
		return false;
	});
	
	$("#startSearch").click(function(){
		window.location.href="/search.php?val="+$(this).parent().find("input").val();
	});
	$("#search").keypress(function(e){
		if(e.which==13)
		{
			$("#startSearch").click();
			return false;
		}
	});
	
});
function refreshBasket()
{
	$.post("/ajax_handlers/basket.php",{ METHOD: "REFRESH" },function(e){
		$("#basketLink").html(e);
	})
}
function showMessage(str, type, hide)
{
	var message=$("#etalonMessage").clone();
	message.addClass(type);
	message.attr("id","");
	message.html(str);
	message.hide();
	$("#forMessages").append(message);
	message.fadeIn(300);
	setTimeout(function(){ 
			message.animate({ opacity: 0.01 },300, function(){ message.animate({ height: "0", marginBottom: "0" },200, function(){ message.remove() }) });
			/*message.fadeOut(300, function(){ 
				message.remove(); 
			});*/
		}, hide);
}

function onScrollSeek(e)
{
	try { lastScrollHover.mouseover(); } catch(e){  }
}
