var outInt;
var last;

theObjects = document.getElementsByTagName("object"); 
for (var i = 0; i < theObjects.length; i++) { 
  theObjects[i].outerHTML = theObjects[i].outerHTML; 
}

// contact page object id = 702;
// ez custom scripting places this id in the "active" variable, thinking the contact object is part of the menu (from the tests it performs)
// contact object is not part of the menu, resulting that there is no div with such an id, which causes this script to crash
// temporary solution: if active=="702", we manually "fix" this, making active=""
// all time solution: contact object must be marked as not being part of the top menu within eZ, my guess it that it is having a property that can set this option (as the rest of the menu has)

if (active=="702") active="";

function over(obj,e)
{
	if (obj!=null || obj)
	if(obj.id!="")
	{
		if (last!=obj && last!=undefined && last!=null && last) 
		{
			menuOut(last);
			hideSub(last.id);
			if (active!="") 
			{
				menuOut(document.getElementById(active));
				hideSub(active);
			}
		}
		
		clearInterval(outInt);
	
		obj.className="menuItemOver";
		obj.childNodes[0].className="menuExtOverL";
		obj.childNodes[1].className="menuContOver";
		obj.childNodes[2].className="menuExtOverR";
		if (obj.previousSibling!=null) obj.previousSibling.className="menuSepOver";
		if (obj.nextSibling!=null) obj.nextSibling.className="menuSepOver";
		
		showSub(obj.id);
		last=obj;
	}
}
function out(obj,e)
{
	/*if (obj.title!=active)
	{
		outInt=setTimeout(function(){
			menuOut(obj);
			hideSub(obj.id);
			over(document.getElementById(active));
		},3000);
	}*/
}

function menuOut(obj)
{
	obj.className="menuItem";
	obj.childNodes[0].className="menuExt";
	obj.childNodes[1].className="menuCont";
	obj.childNodes[2].className="menuExt";
	if (obj.previousSibling) obj.previousSibling.className="menuSep";
	if (obj.nextSibling) obj.nextSibling.className="menuSep";
}

function showSub(id)
{
	var subMenuObj=document.getElementById(id+"Sub");
	subMenuObj.style.visibility="visible";
	
	var bs=document.getElementById("subMenu");
	var subs=bs.getElementsByTagName("div");
	for (var i=0;i<subs.length;i++)
		if (subs[i]!=subMenuObj) subs[i].style.display="none";
			else subs[i].style.display="";
}
function hideSub(id)
{
	var subMenuObj=document.getElementById(id+"Sub");
	subMenuObj.style.visibility="hidden";
}


function overSub()
{
	clearInterval(outInt);
}
function outSub(it)
{
	id = it.id.substring(0,it.id.length-3);
	if (id!=active)
	{	
		var obj = document.getElementById(id);
		/*outInt=setTimeout(function(){
			menuOut(obj);
			hideSub(id);
			over(document.getElementById(active));
		},500);*/
	}
}

function init()
{
	if (active!="") over(document.getElementById(active));
}

// Cookie management functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {createCookie(name,"",-1);}
// Load random bg on various pages
function loadBg(target, loc, total, type)
{
	var targetDiv=document.getElementById(target);
	
	var cookieID=(target+'_rndBgID').toString();
	var lastRandomBg=readCookie(cookieID);
	
	var rnd = Math.floor(Math.random()*total);
	if (lastRandomBg!=null) while (lastRandomBg==rnd) rnd = Math.floor(Math.random()*total);
		else rnd=0;
	
	targetDiv.style.backgroundImage ='url('+loc+rnd+type+')';
	createCookie(cookieID,rnd,365);
}