$(document).ready(bindEvents);

	var aVote = new Array("Оценить товар","Ужасный товар","Плохой товар","Обычный товар","Хороший товар","Отличный товар");

	function bindEvents()
	{
		$(".advice:not(.voted) span i").mouseover(function(){
			$(this).nextAll().removeClass("starHover");
			$(this).addClass("starHover").prevAll().addClass("starHover");
			$("div.advice small").html(aVote[this.title]);
		})
		.mouseout(function(){var parent = $(this).parent();parent.children().removeClass("starHover");parent.children("i:nth-child("+parent.attr("rating")+")").addClass("starHover").prevAll().addClass("starHover");$("div.advice small").html(aVote[0]);
})
		.click(setVote);
	}

function setVote()
{
	var parent = $(this).parent();
	parent.children().unbind();
	parent.fadeTo(100, 0.3).fadeTo(100, 1);
	$.post("http://shop.med-post.ru/ajax/vote_product.php", {id_product: parent.attr("advice"), vote_val:this.title}, function(data)
	{
		if(data.indexOf(' ')==-1)
			parent.attr("rating",data);
		else
		{
			$.jGrowl(data);
			bindEvents();
		}
		parent.children().removeClass("starHover");
		parent.children("i[title="+parent.attr("rating")+"]").addClass("starHover").prevAll().addClass("starHover");
		$(".advice").addClass("voted");
	});
}

function animateCartPrice(iCurPrice)
{
	var cart=$("#cart em");
	var iCurTotalQty = parseInt($("#totalQty").html())+1;
	var end = getWordEnd(iCurTotalQty);
	$("#totalQty").html(iCurTotalQty);
	var iCurTotalPrice = parseInt($("#totalPrice").html());
	var iNewTotal = iCurTotalPrice+iCurPrice;
	$("#totalPrice").html(iNewTotal)
	var step = 1000, maxt=0, maxh=0;
	if(iNewTotal>10) maxd = Math.floor(iNewTotal/10)*10;
	if(iNewTotal>100) maxh = Math.floor(iNewTotal/100)*100;
	if(iNewTotal>1000) maxt = Math.floor(iNewTotal/1000)*1000;
	var intervalId = setInterval(function(){
		cart.html(iCurTotalQty+" товар"+end+" на "+makeRank(iCurTotalPrice)+" руб.");
		cnt_t=iCurTotalPrice+step;
		if(maxt!=0 && cnt_t>maxt)
		{
			step = 100;
			maxt=0;
		}
		else
			if(maxh!=0 && cnt_t>maxh)
			{
				step=10;
				maxh=0;
			}
			else
				if(cnt_t>maxd)
					step=1;
		iCurTotalPrice=iCurTotalPrice+step;
		if(iCurTotalPrice>iNewTotal)
		{
			cart.html(iCurTotalQty+" товар"+end+" на "+makeRank(iNewTotal)+" руб.");
			clearInterval(intervalId);
		}
		},20);
}

function addToCart(id, price)
{
	var size=$("#prodSizes").val();
	if(!size)
		size = "";
	var qty = getCookie ("prodMP["+id+"_"+size+"]");
	if(qty==null || isNaN(qty)){qty=1;}
	else qty=parseInt(qty)+1;
	setCookie("prodMP["+id+"_"+size+"]",qty);
	if($("#totalQty").html()=="0")
		$("#cart span").replaceWith('<p>В корзине</p><a href="http://shop.med-post.ru/cart/"><i></i><em></em></a><form action="http://shop.med-post.ru/cart/"><input type="submit" id="incart" value="Оформить"></form>');
//	$("#buyButton_"+id).hide();
	$("#prodForm").hide();	
	$("#cartLink").css("display","block");
	animateCartPrice(price);
	return false
}

function closeSubscribe()
{
	$("#subsResult p").remove();
	$("#subsResult img").hide();
	$("#subsForm").show();
	$("#sendSaleForm").hide();
	$(document).unbind("keydown",checkKey);
	$(document).unbind("click",checkClick);
	return false;
}

function showSubscribe()
{
	var layer = $("#sendSaleForm");
	if(layer.css("display")=="none")
	{
		layer.show();
		$(document).bind("keydown",checkKey);
		$(document).bind("click",checkClick);
	}
	else
	{
		closeSubscribe();
	}
	return false;
}

function checkClick(event)
{
	var target = null;
	if (!document.getElementById) return;
	if (window.event) 
		target = window.event.srcElement;
	else
		target = event.target;
	if(target.className!="subsLayer" && target.className!="dashed")
		closeSubscribe();
}

function checkKey(event)
{
	if(!document.getElementById) return;
	if(window.event) event = window.event;
	if(event.keyCode==27)
		closeSubscribe();
}

function sendSubscribe(product, price)
{
	var mail = document.getElementById("subscEmail");
	if(validator.formEmail(mail))
	{
		$("#subsForm").hide();
		$("#subsResult img").show();
		$.post("http://shop.med-post.ru/ajax/subscribe.ajax.php", {product:product, price:price, c_mail:mail.value},
				function(data)
				{
					$("#subsResult img").hide();
					$("#subsResult").append(data);
				}
			);
	}
	else
		mail.focus();
}

function processFavorite(obj,product)
{
	if(obj.className=="navAddFavorites dashed")
		$.post("http://shop.med-post.ru/ajax/favorite.ajax.php", {favorite:product, ftype:1, act:'add'},function(data){if(!data){obj.className="navDeleteFavorites dashed";obj.innerHTML="Удалить из избранного";}else $.jGrowl(data);});
	else
		$.post("http://shop.med-post.ru/ajax/favorite.ajax.php", {favorite:product, ftype:1, act:'erase'},function(data){if(!data){obj.className="navAddFavorites dashed";obj.innerHTML="Добавить в избранное";}else $.jGrowl(data);});
	return false;
}

function next_image(id)
{
	$("#productPhoto img:visible").hide();
	$("#currentPhoto").removeAttr("id");
	$("#morePhoto a:nth-child("+(id)+")").attr("id","currentPhoto");
	$("#pimage_"+id).show();
	return false;
}
