var velocity=0.1;
var state='ready';
function slide(dir){
	if(state!='working'){
		if(document.body.scrollTop>130) {
			state='working';
			if(dir=="down"){
				document.getElementById('panel').style.top=document.body.scrollTop-89+'px';
				pos="hidden";
				velocity=0.1;
				slidebar();
			}else{
				document.getElementById('panel').style.top=document.body.scrollTop+90+'px';
				pos="shown";
				slidebar();
			}
		}
	}
}
function slidebar(){
	if(pos=='shown'){
		if(document.getElementById('panel').style.top.replace("px", "")>document.body.scrollTop-90) {
			document.getElementById('panel').style.top=(document.getElementById('panel').style.top.replace("px", "")*1)-velocity+'px';
			velocity=velocity*1.2;
			hideTimer=setTimeout("slidebar();", 10);
		}else{
			pos='hidden';
			velocity=0.1;
			document.getElementById('panel').style.top=-89+'px';
			state='ready';
		}
	}else if(pos=='hidden'){
		if(document.getElementById('panel').style.top.replace("px", "")<document.body.scrollTop) {

if((document.getElementById('panel').style.top.replace("px", "")*1+velocity)>document.body.scrollTop) document.getElementById('panel').style.top=document.getElementById('panel').style.top=document.body.scrollTop+'px'; else 		
document.getElementById('panel').style.top=(document.getElementById('panel').style.top.replace("px", "")*1)+velocity+'px';
			velocity=velocity*1.2;
			showTimer=setTimeout("slidebar();", 10);
		}else{
			pos='shown';
			velocity=0.1;
			hideTimer=setTimeout("slidebar();", 3000);
		}
	}
}
function hide(){
	clearTimeout(showTimer);
	clearTimeout(hideTimer);	
	document.getElementById('panel').style.top=-90;
	pos="hidden";
}

var pos="hidden";


var agt = navigator.userAgent.toLowerCase ();
var is_major = parseInt (navigator.appVersion);
var is_minor = parseFloat (navigator.appVersion);
var cmp_window=false;
var is_nav  = ((agt.indexOf ('mozilla') != -1) && (agt.indexOf ('spoofer') == -1) && (agt.indexOf ('compatible') == -1) && (agt.indexOf ('opera') == -1) && (agt.indexOf ('webtv') == -1) && (agt.indexOf ('hotjava') == -1) && (agt.indexOf ('safari') == -1));
var is_nav6 = (is_nav && (is_major == 5) && (agt.indexOf ("netscape") != -1) && (agt.indexOf ("netscape/7") == -1));
var is_nav6up = ((is_nav && (is_major >= 5)) || (is_nav && (agt.indexOf ('netscape/7') != -1)));
var is_ie      = ((agt.indexOf ("msie") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_firefox = ((agt.indexOf ("firefox") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_opera = (agt.indexOf ("opera") != -1);
var is_nn = ((is_nav6 || is_nav6up) ? true : false);

var mouse_x;
var mouse_y;
var Obj;
var ImgWidth;

function Checking(mouse_x,mouse_y,objTop,objRight,objBottom,objLeft){
	flag=false;
	if (objRight<mouse_x || objBottom<mouse_y || objLeft>mouse_x || (objTop+27)>mouse_y)  flag=true;
	if (objLeft<=mouse_x && objLeft+ImgWidth+2>mouse_x && objTop<mouse_y && (objTop+27)>mouse_y)  flag=false;

	return flag;
}
function imouse(e){ 
	if (Obj && Obj.style.display!="none") {
		objLeft=calcLeft(Obj);
		objTop=calcTop(Obj);
	}
	if (is_ie || is_opera) {
		mouse_y=event.y+document.body.scrollTop; 
		mouse_x=event.x+document.body.scrollLeft;
		if (Obj && Obj.style.display!="none") {
			objBottom=Obj.children[0].offsetHeight+objTop;
			objRight=Obj.children[0].offsetWidth+objLeft;		
		}
	} else {
		mouse_x=e.pageX;
		mouse_y=e.pageY;
		if (Obj && Obj.style.display!="none") {
			objBottom=Obj.offsetHeight+objTop;
			objRight=Obj.offsetWidth+objLeft;				
		}
	}
	if (Obj && Obj.style.display!="none") {
		if (Checking(mouse_x,mouse_y,objTop,objRight,objBottom,objLeft)) 
			Obj.style.display="none";
	}
} 
document.onmousemove=imouse;

	function calcTop(x_ele){
//		if (!document.all && !is_firefox) return (x_ele.offsetTop);
		var x_ret=0;
		var oParent = x_ele.offsetParent;
		if (oParent == null) return 0
			else x_ret=x_ele.offsetTop + calcTop(oParent);
		return x_ret;
	}
	
	function calcLeft(x_ele){
//		if (!document.all && !is_firefox) return (x_ele.offsetLeft);
		var x_ret=0;
		var oParent = x_ele.offsetParent;
		if (oParent == null) return 0
			else x_ret=x_ele.offsetLeft + calcLeft(oParent);
		return x_ret;
	}


	function show_menu(id){
		for (i=1; i<15; i++)
		if (document.getElementById('menu_a_'+i) && document.getElementById('menu_a_'+i).style.display!="none") document.getElementById('menu_a_'+i).style.display="none";

		Obj=document.getElementById('menu_a_'+id);
		Obj.style.top=calcTop(document.getElementById('menu_'+id));
		Obj.style.left=calcLeft(document.getElementById('menu_'+id));		
		Obj.style.display="block";
	}
	

	function hide_menu2(id){
		document.getElementById('menu_a_'+id).style.display="none";
	}
	



function SetCookie (name, value) {
	var largeExpDate = new Date ();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
	var expires = largeExpDate;
	document.cookie = name + "=" + escape (value) +"; expires=" + expires.toGMTString() +  "; path=/";
}


function getCookie(name) {
	if(document.cookie == "") return false;
	else {
		var cookieStart, cookieEnd;
		var cookieString = document.cookie;
		cookieStart = cookieString.indexOf(name+"=");
		if(cookieStart != -1) {
			cookieStart += name.length+1;
			cookieEnd = cookieString.indexOf(";", cookieStart);
			if(cookieEnd == -1) cookieEnd = cookieString.length;
			return cookieString.substring(cookieStart, cookieEnd);
		} else {
			return false;
		}
	}
}



function toggleItem(id, qty, updateqty, del){
	if(id!="clear"){
		data="";
		iqty=0;
		iterCost=0;
		idArray = new Array;
		idData = new Array;
		current_data=getCookie('ShoppingCart');
		flag=false;
		cost=0;
		discount=0;
		idiscount=0;
		if(!qty) qty=1;
		if(qty<0) qty=-qty;
		if(!updateqty) updateqty="";
		if(current_data != false){
			current_data=decodeURIComponent(current_data);
			idArray = current_data.split(" ");
			for(i=0; i<=idArray.length; i++){
				if(idArray[i]) {
					idData=idArray[i].split(":");
					if(!del && idData[0]==id){
						if(updateqty) idArray.splice(i, 1, idData[0]+":"+qty);
						else {
							idArray.splice(i, 1, idData[0]+":"+(parseInt(qty)+parseInt(idData[1])));
							var newqty=(parseInt(qty)+parseInt(idData[1]));
							if(window.opener) document.getElementById('add_'+id).innerHTML="<br /><a href='#' onclick='window.opener.location=\"/cart/\"; window.opener.focus(); window.close(); return false;';>В корзине</a>: "+newqty+"";
else document.getElementById('add_'+id).innerHTML="<br /><a href='/cart/'>В корзине</a>: "+newqty+"";
//							slide("down");
						}
slide("down");
						flag=true;
					}else{
						if(idData[0]==id && updateqty=="" && del!="") {
							idArray.splice(i, 1);
							flag=true;
						}
					}
					if(updateqty!=""){
						iqty=parseFloat(document.getElementById('qty_'+idData[0]).value);
						if(!iqty) {
							iterCost=0;
						} else iterCost=parseFloat(document.getElementById('price_'+idData[0]).innerHTML)*iqty;
						if(updateqty==1){
							flag=true;
							document.getElementById('cost_'+idData[0]).innerHTML=''+iterCost+'';
							cost=cost+iterCost;
							idiscount=parseFloat(document.getElementById('discount_'+idData[0]).value)*iqty;
							if(!idiscount){
								idiscount=0;
							}else discount=discount+idiscount;
						}
					}
				}
			}
		}
		if(flag==false) { idArray.push(id+":"+qty); slide("down"); }
		data=idArray.join(" ");
		SetCookie ("ShoppingCart", data);
		if(updateqty!=1){
			if(idArray.length>0) {
var goods_count=0;
for(i=0; i<idArray.length; i++){
idData=idArray[i].split(":");
goods_count=goods_count + parseInt(idData[1]);
}
if(document.getElementById('block_basket')){
document.getElementById('block_basket').style.background='url(/templates/html/pic/bg_block_basket_full.gif) no-repeat';				
document.getElementById('basket_num_items').innerHTML='Товаров: <b>' + goods_count + '</b><div><br /><a href="/cart/" style="font-size: 12px">перейти в корзину</a></div>';
document.getElementById('basket_num_items_card').innerHTML=document.getElementById('basket_num_items').innerHTML;
}else{

if(window.opener && window.opener.document.getElementById('block_basket')){
window.opener.document.getElementById('block_basket').style.background='url(/templates/html/pic/bg_block_basket_full.gif) no-repeat';				
window.opener.document.getElementById('basket_num_items').innerHTML='Товаров: <b>' + goods_count  + '</b><div><br /><a href="/cart/" style="font-size: 12px">перейти в корзину</a></div>';
window.opener.document.getElementById('basket_num_items_card').innerHTML=document.getElementById('basket_num_items').innerHTML;
if(window.opener.document.getElementById('add_'+id)) window.opener.document.getElementById('add_'+id).innerHTML="<br /><a href='/cart/'>В корзине:</a> "+(newqty?newqty:qty)+"";
if (window.opener.location.pathname=='/cart/') {
	window.opener.document.getElementById('qty_'+id).value=(newqty?newqty:qty);
	window.opener.toggleItem(id, (newqty?newqty:qty), 1);
}
}

if(window.opener)document.getElementById('basket_num_items_card').innerHTML='Товаров: <b>' + goods_count  + '</b><div><br /><a href="/cart/" '+(window.opener?" onClick=\"window.opener.location='/cart/'; window.opener.focus(); window.close(); return false;\" ":"")+'style="font-size: 12px">перейти в корзину</a></div>';
}
				} else {
					document.getElementById('block_basket').style.background='url(/templates/html/pic/bg_block_basket.gif) no-repeat';						document.getElementById('basket_num_items').innerHTML='Корзина пуста';
					document.getElementById('basket_num_items_card').innerHTML='Корзина пуста';
				}
			if(flag==false) {
				if(window.opener) document.getElementById('add_'+id).innerHTML="<br /><a href='#' onClick=\"window.opener.location='/cart/'; window.opener.focus(); window.close(); return false;\">В корзине:</a> "+qty+"";
				else
				document.getElementById('add_'+id).innerHTML="<br /><a href='/cart/'>В корзине:</a> "+qty+"";
			}else {
				//document.getElementById('order_'+id).style.display="inline";
			}
		}else{
			document.getElementById('order_cost').innerHTML=''+cost+'';
			document.getElementById('order_discount').innerHTML=''+discount+'';
		}
	}else{
		SetCookie ("ShoppingCart", "");
		document.getElementById('basket_num_items').innerHTML='Корзина пуста';
		document.getElementById('basket_num_items_card').innerHTML='Корзина пуста';
	}
}


function compare(id){
	if(getCookie('cmp_status')=="0" || !getCookie('cmp_status')) {
		SetCookie('CompareList', '');
		SetCookie('cmp_cat', '');
	}
	if(getCookie('cmp_cat') && getCookie('cmp_cat')!=cat) {
		if(confirm('Категории сравниваемых товаров не совпадают. Очистить текущую выборку?')){
			SetCookie('CompareList', '');
			SetCookie('cmp_cat', cat);
		}else {
			//if(!cmp_window) cmp_window=window.open('','compare_window', 'scrollbars=yes,status=yes,resizable=1');
			//cmp_window.focus();
			return false;
		}
	}
	data="";
	flag=false;
	idArray = new Array;
	idData = new Array;
	current_data=getCookie('CompareList');
	if(current_data != false){
			current_data=decodeURIComponent(current_data);
			idArray = current_data.split(" ");
			for(i=0; i<=idArray.length; i++){
				if(idArray[i]==id) {
					//if(confirm("Этот товар уже присутствует в сравнении. Удалить?")) idArray.splice(i, 1);
					if(!cmp_window) cmp_window=window.open('','compare_window', 'scrollbars=yes,status=yes,resizable=1');
					cmp_window.focus();
					return false;
					}
			}
	} else SetCookie('cmp_cat', cat);
	if(flag==false) {
		if (idArray.length==3) {
			if(confirm('Вы можете сравнивать только 3 товара одновременно. Заменить последний добавленый товар на новый?')){
				idArray.splice(idArray.length-1, 1);
				idArray.push(id);
			}
		} else
		idArray.push(id);
	}
	data=idArray.join(" ");
	
	SetCookie ("CompareList", data);
	if(cmp_window && !cmp_window.closed) {
		cmp_window.location.reload();
		cmp_window.focus();
	}
	else{
		cmp_window=window.open('/compare/','compare_window', 'scrollbars=yes,status=yes,resizable=1');
		cmp_window.focus();
	}
	return true;
}