$(function (){
	$(".light-box").lightBox();
	galleryFunc();
	$("a.preview").click(function(){
		var index	 = this.getAttribute("tabindex");
		var imgObject= window.gallery[index];
		$("#imgGall")[0].src = host+"/images/1x1.gif";
		var newImage = new Image();
		$(newImage).load(function(){
			$("#imgGall").attr("src",this.src).attr("alt",imgObject["title"]);
		});
		newImage.src = imgObject.src;
		$("#titleGall").html(imgObject["title"]);
		$("#contentGall").html(imgObject["content"]);
		$("#gallPreview").attr("href",imgObject["bigfoto"]).attr("title",imgObject["title"]);
		return false;
	});
	$(".basket_btn").click(function(){
		var id = this.id.split("_")[1];
		var kol_vo = $("#kol_"+id).val();
		$.ajax({
			type: "POST",
			url:  host+"/ajax/basket.php",
			data: "action=add&itemID="+id+"&itemCount="+kol_vo,
			cache: false,
			success: function(msg){
				try{
					var result = eval("("+msg+")");
					if (result.status == 'success'){
						$("#basket_kolvo").html(result.countItems);
						$("#basket_price").html(result.priceItems+" руб");
					}else if (result.status == 'error') showErrorBasket(result.error_code);
				}catch(e){}
			}
		});
	});
	$(".basket_dell").click(function(){
		var id = this.id.replace('dell_', '');
		$.ajax({
			type: "POST",
			url:  host+"/ajax/basket.php",
			data: "action=dell&itemID="+id,
			cache: false,
			success: function(msg){
				try{
					var result = eval("("+msg+")");
					if (result.status == 'success'){
						$("#basket_kolvo").html(result.countItems);
						$("#basket_price").html(result.priceItems+" руб");
						$(".ammount_price").html("<b>"+result.priceItems+"</b> рублей");
						$("#spacer_"+id).remove();
						$("#tr_"+id).remove();
						if (result.empty){
							$(".note").remove();
							$(".basket").html('<div class="text"><center><b>Ваша корзина пуста</b></center></div>');
						}
					}else if (result.status == 'error') showErrorBasket(result.error_code);
				}catch(e){}
			}
		});
		return false;
	});
	$(".basket_fail").click(function(){
		if (!confirm("Вы действительно хотите отменить заказ?")) return false;
		$.ajax({
			type: "POST",
			url:  host+"/ajax/basket.php",
			data: "action=remove",
			cache: false,
			success: function(msg){
				document.location.reload();
			}
		});
		return false;
	});
	$("a.print").click(function (){window.print();return false;})
	$("label").click(function(){
		var id  = this.getAttribute("from");
		var tmp = id.split("_")[0];
		$("#"+id)[0].checked = true;
		$("label[from^="+tmp+"_]").removeClass("checked");
		$(this).addClass("checked");
		if (id.indexOf('delivery_') != -1){
			if (id == 'delivery_4') $(".delivery_address").hide();
			else $(".delivery_address").show();
		}
	});
	$("a.order_success").click(function(){
		$(this).parents().each(function(){
			if (this.tagName.toLowerCase() == 'form') this.submit();
		});
	});
});
function showErrorBasket(error_code){
	var $error_str = "";
	switch(error_code){
		case '1': $error_str = "Количество должно быть целым числом";break;
		case '2': $error_str = "Количество должно быть больше нуля";break;
	}
	if ($error_str) alert($error_str);
}
function galleryFunc(){
	var arrowLeft  = $(".gallery_list a.arrow_left");
	var arrowRight = $(".gallery_list a.arrow_right");
	var container  = $(".hidden");
	var content	   = $(".hidden>div");
	var gallList   = $(".hidden .gallery_item");
	var ammountWidth = 0;
	var offset = 122;
	$.each(gallList,function(){
		ammountWidth += $(this).width()+5;
	});
	if (container.width() > ammountWidth){
		arrowLeft.css("visibility","hidden");
		arrowRight.css("visibility","hidden");
		return;
	}
	arrowLeft.click(function(){
		
		
		thisMargin = parseInt(content.css("marginLeft"));
		if (window.oldMargin){
			margin = window.oldMargin;
			window.oldMargin = 0;
		}else if (thisMargin+offset < 0){
			margin = thisMargin + offset;
		}else{
			margin = 0;
		}
		content.animate({"marginLeft":margin},1000);
		if (margin >= 0) arrowLeft.css("visibility","hidden");
		if (margin != (container.width()-ammountWidth)) arrowRight.css("visibility","visible");
	});
	arrowRight.click(function(){
		thisMargin = Math.abs(parseInt(content.css("marginLeft"))) || 0;
		if (Math.abs(thisMargin+offset) < (ammountWidth - container.width())){
			margin = -1*(thisMargin+offset);
		}else{
			margin = (container.width()-ammountWidth);
			window.oldMargin = parseInt(content.css("marginLeft"));
			arrowRight.css("visibility","hidden");
		}
		content.animate({"marginLeft":margin+"px"},1000);
		if (margin != 0) arrowLeft.css("visibility","visible");
	});
	arrowLeft.css("visibility","hidden");
}
function resize(){
	$(".cont_bg_r","#content").css("min-height","0");
	var contentHeight = $(".cont_bg_r","#content").height();
	var bodyHeight	  = $("#body").height();
	if (contentHeight < bodyHeight) $("#content").css("minHeight", bodyHeight);
	$(window).resize(resize);
}
