//*****************************************************************************
// Partial code is copyrighted by Mike Hall.
// See http://www.brainjar.com.
//*****************************************************************************
function getPageOffsetLeft(el) {
	var x;
	// Return the x coordinate of an element relative to the page.
	x = el.offsetLeft;
	if (el.offsetParent != null)
		x += getPageOffsetLeft(el.offsetParent);

	return x;
}

function getPageOffsetTop(el) {
	var y;
	// Return the y coordinate of an element relative to the page.
	y = el.offsetTop;
	if (el.offsetParent != null)
		y += getPageOffsetTop(el.offsetParent);

	return y;
}

var doMotion = false;
var el = null;
var nextSubMenu = null;
var expandedSubMenu = null;
var cnt = 0;
var curiter = 0;
function PullUp()
{
	if(el==null)
		return;
	el.style.top = getPageOffsetTop(el)-4+'px';
	curiter++;
	if(curiter<cnt)
		setTimeout("PullUp()", 20);
	else
	{
		el.style.zIndex = 150;
		doMotion = false;
	}
}
function PullDown()
{
	if(el==null)
		return;
	el.style.top = getPageOffsetTop(el)+4+'px';
	curiter++;
	if(curiter<cnt)
		setTimeout("PullDown()", 20);
	else
	{
		el.style.visibility = 'hidden';
		if(nextSubMenu == null)
			doMotion = false;
		else
		{
			menuPopUp(nextSubMenu);
			nextSubMenu = null;
		}
	}
}
function menuPopUp(mid)
{
	var menu = document.getElementById(mid);
	var smenu = document.getElementById(mid+'sub');
	if(smenu)
	{
		var mainmenu = document.getElementById('mainmenu');
		el = smenu;
		cnt = (smenu.offsetHeight+8)/4;
		curiter = 0;
		smenu.style.left = getPageOffsetLeft(mainmenu)+'px';
		smenu.style.top = getPageOffsetTop(mainmenu)+'px';
		smenu.style.visibility = 'visible';
		PullUp();
	}
	onMenu(menu, true);
	expandedSubMenu = mid;
	if(!smenu)
		doMotion = false;
}
function moveOffer()
{
	var mainmenu = document.getElementById('mainmenu');
	var smenu = document.getElementById('offermenu');
	if(smenu && mainmenu)
	{
		smenu.style.left = getPageOffsetLeft(mainmenu)+'px';
		smenu.style.top = getPageOffsetTop(mainmenu)+285+'px';
	}
}
function menuPopDown(mid)
{
	var menu = document.getElementById(mid);
	var smenu = document.getElementById(mid+'sub');
	if(smenu)
	{
		el = smenu;
		cnt = (smenu.offsetHeight+8)/4;
		curiter = 0;
		smenu.style.zIndex = 50;
		PullDown();
		expandedSubMenu = null;
	}
	else if(nextSubMenu != null)
	{
		expandedSubMenu = null;
		menuPopUp(nextSubMenu);
		nextSubMenu = null;
	}
	onMenu(menu, false);
	if(!smenu)
		doMotion = false;
}
function menuitemclick(mid, me)
{
	if(me)
		me.blur();
	var el = document.getElementById(mid);
	if(doMotion)
		return false;
	doMotion = true;
	if(expandedSubMenu == null)
		menuPopUp(mid);
	else
	{
		if(expandedSubMenu == mid)
			menuPopDown(mid);
		else
		{
			nextSubMenu = mid;
			menuPopDown(expandedSubMenu);
		}
	}
	doRotateImage();
	return false;
}
var time,thelayer,amount,maxlayeroffset;
var theTime = 40;
var mytop = 0;

function initscroll()
{
	thelayer = document.getElementById('maincontent');
	maxlayeroffset = 0;
	var cbox = document.getElementById('maincontentbox');
	var cheight = cbox ? cbox.clientHeight : 200;
	if(thelayer)
	{
		maxlayeroffset = -(thelayer.clientHeight-cheight);
		if(maxlayeroffset > 0)
			maxlayeroffset = 0;
	}
}
function scrollayer(amt)
{
	initscroll();
	if(thelayer)
	{
		amount = amt * 4;
		realscroll();
	}
}
function realscroll()
{
	mytop += amount;
	if(mytop>0)
		mytop = 0;
	if(mytop < maxlayeroffset)
		mytop = maxlayeroffset;
	thelayer.style.top = mytop + 'px';
	time = setTimeout('realscroll()',theTime);
}
function stopscroll()
{
	if (time) clearTimeout(time);
}
function getElOffset(link)
{
	var y;
	// Return the y coordinate of an element relative to the main container.
	y = link.offsetTop;
	if (link.offsetParent != null && link.getAttribute('id') != 'maincontentbox')
		y += getElOffset(link.offsetParent);

	return y;
}
function scrollto(element, me)
{
	initscroll();
	var link = document.getElementById(element);
	if(link)
	{
		mytop = getElOffset(thelayer)-getElOffset(link);
		if(mytop>0)
			mytop = 0;
		if(mytop < maxlayeroffset)
			mytop = maxlayeroffset;
		thelayer.style.top = mytop + 'px';
	}
	return dummyClick(me);
}

function steplayer(amt, mwidth, lw)
{
	thelayer = document.getElementById('galcont');
	if(thelayer)
	{
		amount = amt * 4;
		maxlayeroffset = -(mwidth-lw);
		if(maxlayeroffset > 0)
			maxlayeroffset = 0;
		realstep();
	}
}
function realstep()
{
	mytop += amount;
	if(mytop>0)
		mytop = 0;
	if(mytop < maxlayeroffset)
		mytop = maxlayeroffset;
	thelayer.style.left = mytop + 'px';
	time = setTimeout('realstep()',theTime);
}

function dummyClick(me)
{
	if(me && me.blur)
		me.blur();
	return false;
}

function showMap(url, me)
{
	dummyClick(me)
	var myi;
	var needBorder = false;
	var myp = document.getElementById('leftpanel');
	if(!myp)
	{
		myp = document.getElementById('leftpanel1');
		needBorder = true;
	}
	if(myp)
	{
		while(myp.hasChildNodes())
			myp.removeChild(myp.firstChild);
		myi = new Image();
		myi.src = url;
		if(needBorder)
			myi.style.border = '1px solid red';
		myp.appendChild(myi);
	}
	return false;
}

function showModel(pid, rid, hh, me)
{
	dummyClick(me);
	var s = screen;
	// 784x410
	var myw = 900;
	var myh = 570;
	var sheight;
	if(window.innerHeight)
		sheight = window.innerHeight;
	else
		sheight = document.body.clientHeight;

	if(s.width < myw)
		myw = s.width;
	if(myh < hh)
		myh = hh;
	if(sheight < myh)
		myh = sheight;

	if(pid>0)
	{
		var win = window.open('/catmodel.php?pid='+pid+'&rid='+rid, 'model'+pid+rid, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,personalbar=no,resizable=yes,width='+myw+',height='+myh);
		win.focus();
	}
	return false;
}

function addOffset(url)
{
	url.href += '&gl='+mytop;
	return true;
}

function openWin(url, winid, ww, hh, me)
{
	dummyClick(me);
	var win = window.open(url, winid, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,personalbar=no,resizable=yes,width='+ww+',height='+hh);
	win.focus();
	return false;
}

function onMenu(item, onoff)
{
	var ext = '_on.gif';
	var elen;
	if(!onoff)
		ext = '.gif';
	if(item)
	{
		try
		{
			if(item.id == expandedSubMenu)
				ext = '_on.gif';
		}
		catch (e)
		{ }
		if(item.src.indexOf('_on.gif') > 0)
			elen = 7;
		else
			elen = 4;
		item.src = item.src.substring(0,item.src.length-elen) + ext;
	}
}
function newImage(arg)
{
	var rslt = new Image();
	rslt.src = arg;
	return rslt;
}

function preloadMenuImages()
{
	if (document.images)
	{
		var menuItems = new Array('/pics/menu/about_on.gif', '/pics/menu/awards_on.gif', '/pics/menu/benefits_on.gif', '/pics/menu/buy_on.gif', '/pics/menu/cities_on.gif', '/pics/menu/coll_on.gif', '/pics/menu/color_on.gif', '/pics/menu/contacts_on.gif', '/pics/menu/coop_on.gif', '/pics/menu/history_on.gif', '/pics/menu/isi_on.gif', '/pics/menu/lets_talk_on.gif', '/pics/menu/lines_on.gif', '/pics/menu/links_on.gif', '/pics/menu/news_on.gif', '/pics/menu/novelty_on.gif', '/pics/menu/polez_on.gif', '/pics/menu/price_on.gif', '/pics/menu/remark_on.gif', '/pics/menu/shops_on.gif', '/pics/menu/size_on.gif', '/pics/menu/swimsuit_on.gif', '/pics/menu/terms_on.gif', '/pics/menu/today_on.gif', '/pics/menu/uxod_on.gif', '/pics/menu/client.gif', '/pics/menu/client_on.gif');
		if (document.images)
		{
			var tt;
			for(var i=0; i<menuItems.length; i++)
				tt = newImage(menuItems[i]);
		}
	}
}
var rotTime;
var rotTimeout = 20000;
var rImg;
function rotateImage()
{
	rImg = document.getElementById('leftimg');
	if(rImg)
	{
		rImg.onload = function () { clearTimeout(rotTime); rotTime = setTimeout('doRotateImage()',rotTimeout); };
		doRotateImage();
	}
}
function get_random(len)
{
    return Math.round(Math.random()*(len-1));
}
var rotImages = new Array('/pics/spl01.jpg','/pics/spl02.jpg','/pics/spl03.jpg','/pics/spl04.jpg','/pics/spl05.jpg','/pics/spl06.jpg','/pics/spl07.jpg','/pics/spl08.jpg','/pics/spl09.jpg','/pics/spl10.jpg','/pics/spl11.jpg');
function doRotateImage()
{
	clearTimeout(rotTime);
	if(rImg)
	{
		var idx;
		var cnt=0;
		do
		{
			idx = get_random(rotImages.length);
			cnt++;
		} while(rImg.src.indexOf(rotImages[idx]) >= 0 && cnt < 10);
		if(rImg.src.indexOf(rotImages[idx]) < 0)
		{
			rImg.src = rotImages[idx];
			rotTime = setTimeout('doRotateImage()',rotTimeout);
		}
	}
}

if(window.addEventListener)
{
	window.addEventListener("load",preloadMenuImages,false);
	window.addEventListener("load",rotateImage,false);
}
else if(window.attachEvent)
{
	window.attachEvent("onload",preloadMenuImages);
	window.attachEvent("onload",rotateImage);
}
else if(window.onload)
{
	var oldHandler = window.onload;
	window.onload = function preMenuBack()
	{
		oldHandler();
		preloadMenuImages();
		rotateImage();
	};
}
else
{
	window.onload = preloadMenuImages;
}
