var mouseoverusermenu = false;

$(document).ready(function(){
	$('a.tech').click(function(){
		var popup = $('#popup-'+$(this).attr('id'));
		popup.toggle();
		if(popup.css('display')=='none') popup.css('height','0');
		else popup.css('height','100%');
	});
});

function hideMenu() {
	if(!mouseoverusermenu) $("#user_menu").hide();
}
									
function showMenu(is_show) {
	if(is_show) {
		mouseoverusermenu = true;
		$("#user_menu").show();
	} else {
		mouseoverusermenu = false;
		setTimeout('hideMenu()',10);
	}									
}

function block_show(idn)
{
	$('#'+idn).toggle();
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) end = cookie.length;
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
	
function cart_recount(id) {
	var price = parseFloat($('#price_'+id).html());
	var count = parseInt($('#colich_'+id).val());
	if(isNaN(count)) count = 0;
	var total = 0;
	
	$('#stoim_'+id).html((price * count).toFixed(2));
	$('div[id^="stoim_"]').each(function(index){ total = total + parseFloat($(this).html()); });
	$('#itogo').html(total.toFixed(2));
}

function img_show(id,ev) {
	if (ev==1) {
		var leadid = "000000"+id
		document.getElementById('img_big_'+id).src='/pictures/'+leadid.substr(-6,6)+'.jpg';
		if(document.getElementById('img_big_'+id).naturalWidth==88) return;
		document.getElementById('img_big_'+id).style.visibility='visible';
	} else document.getElementById('img_big_'+id).style.visibility='hidden';
}

function compareItems(select, active) {

	var id_items = '';
	var counter=0;

	if (active) {
		if ($('#'+select).length>0){
			$('#'+select).attr('checked','1');
			select=false;
		}
	}

	if (select) return;
	
	var elts = $("form[id=comp] input[name^=compare]:checked");
	
	if (elts.length<2) {
		alert('Для сравнения необходимо не менее 2 товаров');
		return;
	}
	
	if (elts.length>10) {
		alert('Для сравнения вы можете выбрать до 10 товаров');
		return;
	}
	
	elts.each(function() { id_items = id_items + 'd' + $(this).val(); });

	window.open('/shop/comp/?XID=2569&VRID=0&SECTION_ID=1121721&ARR_ID=' + id_items, 'comparetools'); 
}

function onchen2(id) {
	$('#href_'+id).attr('href','/shop/cart/?action=add&item_id='+id+'&count='+$('#count_'+id).val());
}

function setCookieRetrieved(element,href_location) {
	$.post('/shop/',
		{current_link: element.attr('href'),
		AjaxRequest: 1},
		function(Data){
			if (href_location == undefined) location=element.attr('href');
			else location = href_location;
		});
}

function defPosition(event) {
	
	var x = y = 0;
	if (document.attachEvent != null) { // Internet Explorer & Opera
		x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} else if (!document.attachEvent && document.addEventListener) { // Gecko
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	
	return { x:x, y:y };
}

function grouptempl(e) {

    /*var alldiv = document.getElementsByTagName('input');

    for (var i = 0; i < alldiv.length; i++)
        if ( alldiv[i].id && alldiv[i].id.indexOf('colich_') != -1 ) {

			var hidden = document.createElement("input");
			hidden.type = "hidden";
			hidden.name = alldiv[i].name;
			hidden.value = alldiv[i].value;
			var form = document.getElementById('formq');
			form.appendChild(hidden);

			var div = document.getElementById("templform");
			e = e || window.event;
			div.style.top = defPosition(e).y - 180 + "px";
			div.style.left = defPosition(e).x - 250 + "px";
			div.style.display = '';
        }*/
	
	var alldiv = $("input[id*=colich_]");
	
	for (var i = 0; i < alldiv.length; i++) {
		var hidden = document.createElement("input");
		hidden.type = "hidden";
		hidden.name = alldiv[i].name;
		hidden.value = alldiv[i].value;
		var form = document.getElementById('formq');
		form.appendChild(hidden);
		
		var div = document.getElementById("templform");
		e = e || window.event;
		div.style.top = defPosition(e).y - 180 + "px";
		div.style.left = defPosition(e).x - 250 + "px";
		div.style.display = '';
	}
}

function addToCart(_item_id, _count, _warehouse_id) {

	if(_warehouse_id == undefined) _warehouse_id = getCookie('warehouse_id');

	jQuery.get('/shop/cart/', {
		ajax_add_item_id: _item_id,
		count: _count,
		warehouse_id: _warehouse_id
		}, function(data) {
			// alerting
			alert(data);
			// redraw smallcart
			$("div#cartsmall").load('/shop/cartsmall/');
		}
	);
}

