// Copyright (c) 2006 Klever
// http://klever.tomsk.ru/

function go(u) {
	document.location=u
}

function newWin(src, width, height) {
	w=window.open(src, "photo", "width="+width+",height="+height)
	w.focus()
	return false
}

function newWinEx(src, width, height) {
	return newWin(src, width, height + 40)
}

function closeWin(url) {
	if (wo = window.opener) {
		if (wo.closed)
			window.open(url,'_blank');
		else if (wo.location.href.search(url) == -1)
			wo.location.href = url;
		self.close()
		wo.focus()
		return false
	}
	window.open(url,'_blank')
	self.close()
	return false
}

function togglePanelChecked(checkbox, pname) {
	if (!checkbox) return
	p = document.getElementById(pname);
	if (checkbox.checked) {
		if (p.style.display!='block') p.style.display='block'
	}
	else {
		if (p.style.display!='none') p.style.display='none'
	}
}

function togglePanel(pname) {
	p = document.getElementById(pname)
	if (p) {
		if (p.style.display=='none')
			p.style.display='block'
		else
			p.style.display='none'
	}
}

function hidePanel(pname) {
	p = document.getElementById(pname)
	if (p) {
		if (p.style.display!='none') {
			p.style.display='none'
			return true
		}
	}
}

function showPanel(pname) {
	p = document.getElementById(pname)
	if (p) {
		if (p.style.display!='block') {
			p.style.display='block'
			return true
		}
	}
}

function getEl(elId) {
	return document.getElementById(elId)
}

function hideEl(elId) {
	return hidePanel(elId)
}

function hideElPos(elId) {
	el = getEl(elId)
	el.style.visibility = 'hidden'
}

function showEl(elId) {
	return showPanel(elId)
}

function showElPos(elId, sender, offsetX, offsetY) {
	if (!offsetX) offsetX = 8
	if (!offsetY) offsetY = 4
	
	el = getEl(elId)
	if (el) {
		var pos = getElPosition(sender)
		el.style.top  = (pos.y+offsetY) + 'px'
		el.style.left = (pos.x+offsetX) + 'px'
		el.style.visibility = 'visible'
	}
}

function switchElVis(el1, el2) {
	hideEl(el1)
	showEl(el2)
}

/*
Show element 
*/
function showElPosCenter(elId, sender) {
	el = getEl(elId)
	w = -el.scrollWidth / 2
	h = -el.scrollHeight / 2
	showElPos(elId, sender, w, h)
}

function getElPosition(el) {
	var pos = {x:0, y:0}
	if (el.offsetParent) {
		while (el.offsetParent) {
			//alert(el.offsetLeft)
			pos.x += el.offsetLeft
			pos.y += el.offsetTop
			el = el.offsetParent
		}
	}
	else if (el.x) {
		pos.x += el.x
		pos.y += el.y
	}
	return pos
}

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
document.cookie = escape(cookieName) + '=' + escape(cookieValue)
+ (expires ? '; EXPIRES=' + expires.toGMTString() : '')
+ (path ? '; PATH=' + path : '')
+ (domain ? '; DOMAIN=' + domain : '')
+ (secure ? '; SECURE' : '');
}


function attachHoverToEl(el) {
	// we have a line in CSS: .dataTable tr.over td
	var nodes = el.getElementsByTagName("tr")
	for (var i=0; i<nodes.length; i++) {
		nodes[i].onmouseover = function() {
			this.className += " over";
		}
		nodes[i].onmouseout = function() {
			this.className = this.className.replace(new RegExp(" over\\b"), "");
		}
	}
}

// mouse hover effect for dataTable
overForDataTable = function() {
	oTables = document.getElementsByTagName("TABLE")
	if (!oTables) return false
	
	for (var i=0; i<oTables.length; i++) {
		if (oTables[i].className.indexOf('dataTable')!=-1) {
			attachHoverToEl(oTables[i])
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", overForDataTable)

function attachHoverToId(elId) {
	node = getEl(elId)
	if (!node) return false
	
	node.onmouseover = function() {
		//this.className += " over";
		this.id += "Over";
	}
	
	node.onmouseout = function() {
		//this.className = this.className.replace(new RegExp(" over\\b"), "");
		this.id = this.id.replace(new RegExp("Over\\b"), "");
	}
}


function scrollCartPanel() 
{
	var browser = navigator.appName
	
	cpEl = getEl('shoppingCartPanel')
	cpTopEl = getEl('shoppingCartPanelTop')
	
	if (cpEl && cpTopEl) 
	{
		var posEl = getElPosition(cpEl)
		var posTopEl = getElPosition(cpTopEl)
		
		topOffset = (browser == "Netscape" ? window.pageYOffset : document.documentElement.scrollTop);
		
		if(!window.cart_panel_diff) window.cart_panel_diff = 0;
		
		var max = topOffset > posTopEl.y ? topOffset : posTopEl.y;
		
		if(posEl.y != max + window.cart_panel_diff)
		{
		 	window.cart_panel_diff = getWindowHeight() - cpEl.clientHeight;
		 	if(window.cart_panel_diff < 0) 
		 		max += window.cart_panel_diff;
		 	else
		 		window.cart_panel_diff = 0;
		 	
		 	cpEl.style.top = max + 'px';
		}
		
		if(posEl.x != posTopEl.x) cpEl.style.left = posTopEl.x + 'px'
	}
	
	setTimeout("scrollCartPanel()", 500)
}

function getWindowHeight()
{
	if(window.innerHeight) return window.innerHeight;
	if(document.documentElement.clientHeight > 0) return document.documentElement.clientHeight;
	return document.body.clientHeight;
}
