function PopupElement(cel, el, timeout)
{
	timeout = timeout || 500;
	if (typeof el != 'Object') el = document.getElementById(el);
	if (!el) return;
	el.style.display = 'block';
	
	if (!el.onmouseover) 
	{
		cel.onmouseover = el.onmouseover=function()
		{
			if (el.tmout) window.clearTimeout(el.tmout);
			el.style.display = 'block'
		};
	}
	
	if (!el.onmouseout) 
	{
		cel.onmouseout = el.onmouseout=function()
		{
			if (el.tmout) window.clearTimeout(el.tmout);
			el.tmout = window.setTimeout(function(){el.style.display = 'none'}, timeout);
		};
	}
}

function SwitchDivs(id)
{
	var sw0 = document.getElementById('switcher0');
	var sw1 = document.getElementById('switcher1');
	var div0 = document.getElementById('div0');
	var div1 = document.getElementById('div1');
	if (!(sw0 && sw1 && div0 && div1)) return;
	
	switch (id)
	{
		case 0:
			sw0.className = 'switchera';
			sw1.className = 'switcher';
			div0.className = 'catalogtree';
			div1.className = 'catalogtreehidden';
			break;
		case 1:
			sw0.className = 'switcher';
			sw1.className = 'switchera';
			div0.className = 'catalogtreehidden';
			div1.className = 'catalogtree';
			break;
	}
}

function ExpandBrand(id)
{
	var ex = document.getElementById("brandexpand"+id);
	var br = document.getElementById("brandbranch"+id);
	if (br.style.display == 'none')
	{
		br.style.display = 'block';
		ex.src = '/i/minus.gif';
	}
	else
	{
		br.style.display = 'none';
		ex.src = '/i/plus.gif';
	}
}

function ExpandCategory(id)
{
	var ex = document.getElementById("categoryexpand"+id);
	var br = document.getElementById("categorybranch"+id);
	if (br.style.display == 'none')
	{
		br.style.display = 'block';
		ex.src = '/i/minus.gif';
	}
	else
	{
		br.style.display = 'none';
		ex.src = '/i/plus.gif';
	}
}

function OnMouseMoveImage(div, event)
{
	window.mousedivpos = {x:0.0, y:0.0}
	
	if (window.event)
	{
		//if (window.event.srcElement != div) return;
		window.mousedivpos = {x:window.event.x, y: window.event.y};
		var s = "";
		for (k in window.event) s = s + k + " ";
		document.title = window.event.offsetX ;
	}
	else
	{
		var html = document.getElementsByTagName('html')[0];
		var body = document.getElementsByTagName('body')[0];
		var scrollLeft = 0; if (html.scrollLeft) scrollLeft +=html.scrollLeft; if (body.scrollLeft) scrollLeft +=body.scrollLeft; 
		var scrollTop = 0; if (html.scrollTop) scrollTop +=html.scrollTop; if (body.scrollTop) scrollTop +=body.scrollTop; 
		window.mousedivpos = {x:(event.clientX - div.offsetLeft + scrollLeft), y: (event.clientY - div.offsetTop+document.body.offsetLeft+scrollTop)};
	}

	window.mousedivposx = (window.mousedivpos.x*1.0/div.offsetWidth-0.5)*2+0.5;
	
	if (window.mousedivposx < 0.0) window.mousedivposx = 0.0;
	if (window.mousedivposx > 1.0) window.mousedivposx = 1.0;
	window.mousedivposy = (window.mousedivpos.y*1.0/div.offsetHeight-0.5)*2+0.5;
	if (window.mousedivposy < 0.0) window.mousedivposy = 0.0;
	if (window.mousedivposy > 1.0) window.mousedivposy = 1.0;
	
	SetImagePosition();
}

function ZoomImage()
{
	if (!productimages || !productzooms) return;
	document.productimagenum = (document.productimagenum+productimages[document.productcolor].length)%productimages[document.productcolor].length;
	var img = document.getElementById('productimage');
	if (!img) return;
	var zoomsrc = productzooms[document.productcolor][document.productimagenum];
	if (!zoomsrc) zoomsrc = defaultzoom;
	
	if (img.zoomed)
	{
		img.imgloaded = false;
		img.zoomed = false;
		img.src = productimages[document.productcolor][document.productimagenum];
		//img.style.width = img.oldwidth;
		var zoomsignobj = $('#zoomsign').get(0);
		if (zoomsignobj) zoomsignobj.innerHTML = '+';
	}
	else
	{
		img.imgloaded = false;
		img.zoomed = true;
		img.src = zoomsrc;
		//img.oldwidth = img.style.width;		
		//img.style.width = "auto";
		var zoomsignobj = $('#zoomsign').get(0);
		if (zoomsignobj) zoomsignobj.innerHTML = '+';
	}
	SetImagePosition();
}

function UnzoomImage()
{
	if (!productimages || !productzooms) return;
	document.productimagenum = (document.productimagenum+productimages[document.productcolor].length)%productimages[document.productcolor].length;
	var img = document.getElementById('productimage');
	var src = productimages[document.productcolor][document.productimagenum];
	if (img) 
	{
		img.imgloaded = false;
		img.src = src;
		SetImagePosition();
	}
}

function SetImagePosition()
{
	var img = document.getElementById('productimage');
	if (!img.imgloaded)
	{
		img.style.left = 400+"px";
		return;
	}
	
	var div = document.getElementById('productimagediv');
	var mousex = div.mousex || 0.0;
	var mousey = div.mousey || 0.0;
	
	if (!img || !div) return;
	
	var divwidth = div.offsetWidth;
	var divheight = div.offsetHeight;
	
	var imgwidth = img.offsetWidth;
	var imgheight = img.offsetHeight;
	
	if (imgwidth - divwidth < 100)
	{
		img.style.left = (divwidth - imgwidth)/2+'px';
	}
	else
	{
		img.style.left = imgwidth  > divwidth?  mousex * (divwidth-imgwidth)+'px': '0px';
	}
	img.style.top  = imgheight > divheight? mousey * (divheight-imgheight)+'px': '0px';
	
}

function SwitchImage(button)
{
	if (!productimages) return;
	if (!document.preloads)
	{
		document.preloads = [];
		for (var k in productimages) 
		{
			if (document.productcolor == undefined) document.productcolor = k; 
			for (var i=0; i<productimages[k].length; i++)
			{
				var img = new Image();
				img.imgloaded = false;
				img.src = productimages[k][i];
				document.preloads[document.preloads.length] = img;
			}
		}
	}

	if (document.productimagenum == undefined) document.productimagenum = 0;
	
	switch (button)
	{
		case "next":
			document.productimagenum++;
			break;
		case "prev": 
			document.productimagenum--;
			break;
		default:
			if (productimages[button])
			{
				document.productcolor = button;
				document.productimagenum = 0;
			}
	}
	

	if (document.productcolor != undefined)
	{
		document.productimagenum = (document.productimagenum+productimages[document.productcolor].length)%productimages[document.productcolor].length;
		var img = document.getElementById('productimage');
		if (img) 
		{
			img.imgloaded = false;
			img.zoomed = false;
			img.src = productimages[document.productcolor][document.productimagenum];
			var zoomsignobj = $('#zoomsign').get(0);
			if (zoomsignobj) zoomsignobj.innerHTML = '+';
		}
	}
	
	var prev = document.getElementById('productimageprev');
	var next = document.getElementById('productimagenext');
	if (document.productcolor==undefined || productimages[document.productcolor].length < 2)
	{
		if (prev) prev.style.display = 'none';
		if (next) next.style.display = 'none';
	}
	else
	{
		if (prev) prev.style.display = 'block';
		if (next) next.style.display = 'block';
	}
	/*
	if (prev) prev.style.display = document.productimagenum == 0? 'none': "block";
	if (next) next.style.display = document.productimagenum >= productimages[document.productcolor].length-1? 'none': "block";
	*/
	for (var k in productimages)
	{
		var switcher = document.getElementById("switch"+k);
		var div = document.getElementById("d"+k);
		if (document.productcolor == k) 
		{
			switcher.className = 'colorswitcha';
			if (div) div.style.display = 'block';
		}
		else
		{
			switcher.className = 'colorswitch';
			if (div) div.style.display = 'none';
		}
	}
	
	var sel = document.getElementById('colorselect');
	if (sel && document.productcolor!=undefined)
	{
		var id = document.productcolor.substr(1);
		if (sel.value != id) sel.value = id;
	}
}

function BindEvents()
{
	if (!$ == undefined) return;

	var div = $('#productimagediv').get(0);
	var img = $('#productimage').get(0);
	
	if (div)
	{
		$(div).bind('mousemove', function(e) 
		{
			var mousex = e.pageX - div.offsetLeft
			var mousey = e.pageY - div.offsetTop
			div.mousex = mousex*1.0/div.offsetWidth
			div.mousey = mousey*1.0/div.offsetHeight
			//document.title = mousex+" "+mousey
			SetImagePosition()
		})
	}
	
	/*
	if (img)
	{
		img.onload = function()
		{
			this.imgloaded = true;
			SetImagePosition();
		}
		SetImagePosition();
	}*/
	
	
	$(img).bind('load', function()
	{
		this.imgloaded = true;
		SetImagePosition()
	})
	
	img.onload = function()
	{
		this.imgloaded = true;
		SetImagePosition()
	}

	$(img).bind('mouseleave', function()
	{
		if (this.zoomed) ZoomImage()
		/*document.title = "!!!";*/
	})
	
	img.imgloaded = true;
}
function AddZero(i)
{
	if(i<10) return '0' + '' + i;
	return i;
}
function rusdate(d)
{
	dd = AddZero(d.getDate()) + '.' + AddZero((d.getMonth()+1)) + '.' + AddZero(d.getFullYear()) + ' ' + AddZero(d.getHours()) + ':' + AddZero(d.getMinutes());
	return dd;
}

function show(id)
{

   eval("document.getElementById('" + id + "').style.display = ( document.getElementById('" + id + "').style.display == 'block' ? 'none' : 'block');");

}

function check(id, id_order_idpay, transactionid)
{
	query = "";
	query += 'cardHolderName=' + document.getElementById('cardHolderName' + id).value;
	query += '&cardHolderPhone=' +  document.getElementById('cardHolderPhone' + id).value;
	query += '&cardHolderEmail=' +  document.getElementById('cardHolderEmail' + id).value;
	query += '&cardNumber=' +  document.getElementById('cardNumber' + id).value;
	query += '&cardSecurityCode=' +  document.getElementById('cardSecurityCode' + id).value;
	query += '&cardExpireMonth=' +  document.getElementById('cardExpireMonth' + id).value;
	query += '&cardExpireYear=' +  document.getElementById('cardExpireYear' + id).value;
	query += '&cardHolderCountryCode=' +  document.getElementById('cardHolderCountryCode' + id).value;
	query += '&id_order=' +  document.getElementById('id_order' + id).value;
	query += '&id_customer=' +  document.getElementById('id_customer' + id).value;
	//requestdata('/plugins/idpaypayment.php',query,'res' + id);

	document.getElementById('res' + id).innnerHTML = 'Подождите, проводится оплата...';
	
	$.get("/plugins/idpaypayment.php", { 
			cardHolderName: document.getElementById('cardHolderName' + id).value, 
			cardHolderPhone: document.getElementById('cardHolderPhone' + id).value, 
			cardHolderEmail: document.getElementById('cardHolderEmail' + id).value,
			cardNumber: document.getElementById('cardNumber' + id).value, 
			cardSecurityCode: document.getElementById('cardSecurityCode' + id).value, 
			cardExpireMonth: document.getElementById('cardExpireMonth' + id).value, 
			cardExpireYear: document.getElementById('cardExpireYear' + id).value, 
			cardHolderCountryCode: document.getElementById('cardHolderCountryCode' + id).value, 
			id_order: id_order_idpay,
			id_customer: document.getElementById('id_customer' + id).value,
                        transactionid: transactionid
		},
	  function(data){
			if(data == "1") 
			{
				document.getElementById('res' + id).innerHTML = document.getElementById('success').innerHTML;
				setInterval(document.location = document.getElementById('link').innerHTML, 2000);
			}	
			else 
			{
				document.getElementById('res' + id).innerHTML = document.getElementById('error').innerHTML;
			}
	});
}


function check2(id)
{
	query = "";
	query += 'cardHolderName=' + document.getElementById('cardHolderName' + id).value;
	query += '&cardHolderPhone=' +  document.getElementById('cardHolderPhone' + id).value;
	query += '&cardHolderEmail=' +  document.getElementById('cardHolderEmail' + id).value;
	query += '&cardNumber=' +  document.getElementById('cardNumber' + id).value;
	query += '&cardSecurityCode=' +  document.getElementById('cardSecurityCode' + id).value;
	query += '&cardExpireMonth=' +  document.getElementById('cardExpireMonth' + id).value;
	query += '&cardExpireYear=' +  document.getElementById('cardExpireYear' + id).value;
	query += '&cardHolderCountryCode=' +  document.getElementById('cardHolderCountryCode' + id).value;
	query += '&id_order=' +  document.getElementById('id_order' + id).value;
	query += '&id_customer=' +  document.getElementById('id_customer' + id).value;
	//requestdata('/plugins/idpaypayment.php',query,'res' + id);
        var id_order_idpay = document.getElementById('id_order' + id).value + '_' + Math.random();

        txt=document.getElementById('termurl'+id).value;
        neworder = 'orderid='+id_order_idpay+'&lang';
	reg=/orderid=.*lang/;
	result=txt.replace(reg, neworder);
	document.getElementById('termurl'+id).value=result;
        //alert(neworder + ", " + reg + ", " + result);
        


	//document.getElementById('res' + id).innerHTML = 'Подождите, проводится оплата...';

		$.getJSON("/plugins/idpaypayment2.php", {
                            cardHolderName: document.getElementById('cardHolderName' + id).value,
                            cardHolderPhone: document.getElementById('cardHolderPhone' + id).value,
                            cardHolderEmail: document.getElementById('cardHolderEmail' + id).value,
                            cardNumber: document.getElementById('cardNumber' + id).value,
                            cardSecurityCode: document.getElementById('cardSecurityCode' + id).value,
                            cardExpireMonth: document.getElementById('cardExpireMonth' + id).value,
                            cardExpireYear: document.getElementById('cardExpireYear' + id).value,
                            cardHolderCountryCode: document.getElementById('cardHolderCountryCode' + id).value,
                            id_order: id_order_idpay,
                            id_customer: document.getElementById('id_customer' + id).value
        		},
        		function(data)
                	{
				    if(data.err == "")
				    {
                                        $("#tobank"+id).attr("action", data.url);
                                        $("#pareq"+id).html(data.message);
                                        document.getElementById('tobank' + id).submit();
				    }
				    else
				    {
                                        if(data.err == "650" || data.err == "652")
                                        {
                                            check(id, id_order_idpay, data.transactionid);
                                        }
                                        else
                                        {
                                            document.getElementById('res' + id).innerHTML = document.getElementById('error').innerHTML;
                                        }
				    }
			});
}

