var formUrl = "/form/";
onInit(function(){
	jq(".sub-menu .item").each(function(i, item){
		item = jq(item);
		item.hover(function(){
			item.addClass('hover');
		}, function(){
			item.removeClass('hover');
		});
	});
});

function validatePassword(field, fields){
	if(fields.password.val == "")
		return;
	if(fields.password.val != fields.password2.val)
		field.addError("not.valid");
}

function changeYear(value)
{	
	var sex = jq("#selectsex option:selected").val();
	document.location.href="?year="+value+"&sex="+sex;
}

function changeSex(value)
{	
	var year = jq("#selectyears option:selected").val();
	document.location.href="?year="+year+"&sex="+value;
}

onInit(function(){
	jq(".menu .menu-item").each(function(i, item){
		item = jq(item);
		var id = item.attr("id").replace("item", "");
		var sub = jq("#sub" + id);
		if(sub.length == 0)
			return;
		var x = item.offsetLeft();
		var y = item.offsetTop() + 32;
		item.hover(function(){
			sub.css({left:x, top:y});
			sub.show();
		}, function(){
			sub.hide();
		});
		sub.hover(function(){
			sub.css({left:x, top:y});
			sub.show();
		}, function(){
			sub.hide();
		});
	});
});

onInit(function(){
	var s = jq(".search-inp input:text");
	if(s.length == 0)
		return;
	var b = jq(".search-inp .b");
	s.focus(function(){
		if(s.val().indexOf("...") > -1)
			s.val("");
	});
	s.keypress(function(e){
		if(e.which == 13)
			doSearch(s.val());
	});
	b.click(function(){
		return doSearch(s.val());
	});
	b.click(function(){
		
	});
});

function doSearch(v){
	if(v.indexOf("...") > -1 || v.length < 3)
	{
		alert("Введите минимум 3 символа");
		return;
	}
	document.location.href = "/catalog/search?word=" + v.toLowerCase();
}

function incCount(id){
	var inp = jq("#"+id);
	if(inp.val() == '')
		var n = 0;
	else
		var n = parseInt(inp.val());
	inp.val(n+1);
	return false;
}
function decCount(id){
	var inp = jq("#"+id);
	if(inp.val() == '')
		var n = 0;
	else
		var n = parseInt(inp.val());
	if(n > 0)
		inp.val(n-1);
	return false;
}

function addToCart(id){
	var allc = jq("#all_c");
	var allp = jq("#all_p");
	var sizes = {};
	var items = jq(".size-item");
	items.each(function(n, p){
		p = jq(p);
		var pid = p.attr("id").replace("product", "");
		sizes[pid] = {};
		jq("input", p).each(function(i, item){
			item = jq(item);
			var s = item.attr("name");
			var col = 0;
			if(item.val() != "")
				col = parseInt(item.val());
			if(!isNaN(col) && col > 0)
			{
				sizes[pid][s] = col;
			}
		});
	});
	var cols = [];
	var vals = [];
	for(i in sizes)
	{
		var col = 0;
		var v = i;
		for(c in sizes[i])
		{
			v += "," + c + "_" + sizes[i][c];
			col+= sizes[i][c];
		}
		vals.push(v);
		cols.push(col);
	}
	for(var i = 0; i < cols.length; i++)
	{
		if(i > 0 && cols[i] != cols[i - 1])
		{
			alert('Количество обоих вещей должно совпадать');
			return;
		}
	}
	jq.post("/cart/add-many", {vals:vals.join("|")}, function(data){
		var d = XMLParser.deserialize(data);
		allc.html(d.cart.count);
		allp.html(d.cart.total);
	});
	return false;
}

function addCartSize(id, name){
	var inp = jq("#"+id+name);
	var v =	parseInt(inp.val());
	var allc = jq("#all_c");
	var allp = jq("#all_p");
	jq.post("/cart/add", {id:id, size:name, count:v}, function(data){
		var d = XMLParser.deserialize(data);
		allc.html(d.cart.count);
		allp.html(d.cart.total);
	});
	return false;
}

function setCartCount(id, name, act){
	var bag = jq(".bag");
	if(bag.length == 0)
		return;
	var allc = jq("#all_c");
	var allp = jq("#all_p");
	var allc2 = jq("#bagall_c span");
	var allp2 = jq("#bagall_t span");
	var item = jq("#item"+id+name);
	var selfp = jq(".price span", item);	
	var inp = jq("#"+id+name);
	if(inp.val() == '')
		var v = 0;
	else
		if(act == "dec")
			var v = parseInt(inp.val())-1;
		else
			var v = parseInt(inp.val())+1;
	if(v >= 0)
	{
		inp.val(v);
		jq.post("/cart/add", {id:id, size:name, count:v}, function(data){
			var d = XMLParser.deserialize(data);
			allc.html(d.cart.count);
			allp.html(d.cart.total);
			allc2.html(d.cart.count);
			allp2.html(d.cart.total);
			selfp.html(d.cart.this_total);
		});	
	}
	return false;
}

onInit(function(){
	var bag = jq(".bag");
	if(bag.length == 0)
		return;
	var allc = jq("#all_c");
	var allp = jq("#all_p");
	var allc2 = jq("#bagall_c span");
	var allp2 = jq("#bagall_t span");
	jq("#clear_cart").click(function(){
		if(!confirm("Очистить карзину?"))
			return false;
		jq.get("/cart/clear", {rnd:Math.random()}, function(){
			document.location.href = document.location.href;
		});
		return false;
	});
	jq(".item", bag).each(function(i, item){
		item = jq(item);
		var inp = jq(".count input", item);
		var id = jq("[name=id]", item).val();
		var name = jq("[name=size]", item).val();
		var selfp = jq(".price span", item);
		inp.blur(function(){
			var v = 0;
			if(inp.val() != "")
				v = parseInt(inp.val());
			if(isNaN(v))
			{
				alert("Введите число");
				return;
			}
			jq.post("/cart/add", {id:id, size:name, count:v}, function(data){
				var d = XMLParser.deserialize(data);
				allc.html(d.cart.count);
				allp.html(d.cart.total);
				allc2.html(d.cart.count);
				allp2.html(d.cart.total);
				selfp.html(d.cart.this_total);
			});
		});
	});
});

onInit(function(){
	if(jq("#product").length == 0)
		return;
	var allc = jq("#all_c");
	var allp = jq("#all_p");
	jq(".size-item").each(function(i, c){
		c = jq(c);
		var id = c.attr("id").replace("product", "");
		jq("input", c).each(function(i, item){
			item = jq(item);
			var name = item.attr("name");
			return;
			item.blur(function(){
				var v = 0;
				if(item.val() != "")
					v = parseInt(item.val());
				if(isNaN(v))
				{
					alert("Введите число");
					return;
				}
				jq.post("/cart/add", {id:id, size:name, count:v}, function(data){
					var d = XMLParser.deserialize(data);
					allc.html(d.cart.count);
					allp.html(d.cart.total);
				});
			});
		});
	});
});

onInit(function(){
	var b = jq("#doauth");
	var panel =jq("#authpanel");
	var h = panel.height();
	var w = panel.width();
	jq(".auth-link").each(function(i, item){
		jq(item).click(function(){
			panel.css({top:getClientCenterY() - h / 2, left:getClientCenterX() - w/2});
			panel.show();
			return false;
		});
	});
	if(b.length == 0)
		return;
	b.click(function(){
		panel.css({top:getClientCenterY() - h / 2, left:getClientCenterX() - w/2});
		panel.show();
		return false;
	});
	panel.click(function(){
		return false;
	});
	jq("body").click(function(){
		panel.hide();
	});
});

onInit(function(){
	if(jq("#product").length == 0)
		return;
	if(jq("#subitem3").length == 0)
		var suf = ".item";	
	else
		var suf = ".item2";
	jq(suf).each(function(i, p){
		p = jq(p);
		var g = jq(".gallery", p);
		if(g.length == 0)
			return;
		var photo = jq(".big-pic img", p);
		var a = jq(".big-pic", p);
		jq("a", g).each(function(i, item){
			item = jq(item);
			var url = item.attr("href");
			var url2 = item.attr("id").replace("img", "");
			item.click(function(){
				photo.attr("src", url);
				a.attr("href", "/images/uploaded/original/" + url2 + ".jpg");
				return false;
			});
		});
	});
	
	
});
