var freeze = true;
var bodyFirst = true;
var menuCleanMouse = true;
var navBackgroundOver = 'black';
var navBackgroundNormal = 'white';
var navForegroundOver = 'white';
var navForegroundNormal = 'black';
var menuLast = "";
var objLast = "";
var menuclose = true;
var activeMenu = "";
var menuOpacIn = 0;
var menuOpacOut = 0;
var menuOpacStep = 2;
var menuTimeout = 10;
var currMenuIn = new Object;
var currMenuOut = new Object;
var bodyOffsetX = 0;
var bodyOffsetY = 0;
var navStyle = "nav_tbl";
var mouseCursor = new Array;
	mouseCursor[0] = "default";
	mouseCursor[1] = "crosshair";
	mouseCursor[2] = "hand";
	mouseCursor[3] = "url('/images/cursor_a.gif')";
	mouseCursor[4] = "url('/images/cursor_b.gif')";
var styleReplace = new Array;
	styleReplace[0] = "backgroundColor";
	styleReplace[1] = "color";

var menuBoxX = 0;
var menuBoxY = 0;
var menuBoxA = 0;
var menuBoxB = 0;

var winSizeX = 0;
var winSizeY = 0;
var mainLeft = 256;
var mainHeight = 480;
var mainWidth = 640;

var menuList = new Array; //[name(0), x(1), y(2), width(3), height(4), opacity(5), visibility(6)]
	menuList[0] = ["biografija", 0, 0, 128, 0, 50, "hidden"];
	menuList[1] = ["galerija", 128, 0, 128, 160, 50, "hidden"];
	menuList[2] = ["razstave", 256, 0, 128, 60, 50, "hidden"];
	menuList[3] = ["clanki", 384, 0, 128, 40, 50, "hidden"];
	menuList[4] = ["naslov", 512, 0, 128, 0, 50, "hidden"];

/*	menuList[0] = ["galerija", 128, 0, 128, 160, 50, "hidden", 1];
	menuList[1] = ["razstave", 256, 0, 128, 60, 50, "hidden", 2];
	menuList[2] = ["clanki", 384, 0, 128, 40, 50, "hidden", 3];
*/


xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

IE5 = (document.all && document.getElementById);
GECKO = (!document.all && document.getElementById);

var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE);
//document.onmousemove = doSomething;
//window.onmousemove = doSomething;

function getWindowSize() {
	winSizeY = window.top.innerHeight;
	winSizeX = window.top.innerWidth;
}

function mouseMagic() {
	document.onmousemove = captureMousePosition;
	//body.onmousemove = captureMousePosition;
}

function captureMousePosition(e) {
    if (document.layers) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
    } else if (document.all) {
        xMousePos = window.event.clientX; // + document.body.scrollLeft;
        yMousePos = window.event.clientY; // + document.body.scrollTop;
    } else if (document.getElementById) {
        xMousePos = e.clientX;
        yMousePos = e.clientY;
    }
    if (menuCleanMouse) {
    	menuClean();
    } else if (!isMouseIn()) {
    	menuReset();
    }
    if (!freeze) window.status = xMousePos + "x" + yMousePos + " | " + menuBoxX + "," + menuBoxY + " | " + menuBoxA + "," + menuBoxB;
}

function setPointer(pstyle) {
	temp = document.styleSheets;
	for(ii = 0; ii < temp.length; ii++) {
		if (temp[ii].cssRules) {
			theRules = temp[ii].cssRules;
		} else {
			theRules = temp[ii].rules;
		}
		for(i = 0; i < theRules.length; i++) {
			ret = theRules[i].style.cursor = mouseCursor[pstyle];
		}
	}
}

function getBodyOffset() {
	obj = document.getElementById("bodyDIV");
	bodyOffsetX = obj.offsetLeft + 2;
	bodyOffsetY = obj.offsetTop + 2;
}

function getStyleSheet(sname) {
	ss = document.styleSheets;
	var ret = "";
	for(ii = 0; ii < ss.length; ii++) {
		if (ss[ii].cssRules) {
			theRules = ss[ii].cssRules;
		} else {
			theRules = ss[ii].rules;
		}
		for(i = 0; i < theRules.length; i++) {
			if (theRules[i].selectorText == sname) {
				ret = theRules[i].style;
				//alert("Found, no. " + i);
				break;
			}
		}
	}
	return ret;
}

function setHighlight(ref) {
	if (bodyFirst) {
		body.pauseTimer(ref);
	}
}

function unsetHighlight() {
	if (bodyFirst) {
		body.restoreTimer();
	}
}

function getFrames() {
	var temp = "";
	temp = window.parent.frames.length;
	for (i = 0; i < window.parent.frames.length; i++) {
		temp = temp + window.parent.frames[i].name + "\n";
	}
	//alert(temp);
}

function navChangeA(obj, sname, menuid, action) {
	if (action == "close") {
		unsetHighlight();
		hideMenu("menu" + menuid);
		nstyle = getStyleSheet("." + sname);
		//setPointerOvr(obj, 1);
	} else {
		setHighlight("bg_" + menuid);
		//showMenu("menu" + menuid);
		showMenuA(menuid);
		nstyle = getStyleSheet("." + sname + ":hover");
		//setPointerOvr(obj, 2);
	}
	obj.style.backgroundColor = nstyle.backgroundColor;
	obj.style.color = nstyle.color;
	objLast = obj;
	menuLast = menuid;
	return false;
}

function navChange(obj) {
	menuid = obj.id;
	menuid = menuid.substr(2);
	showMenuA(menuid);
	setHighlight("bg_" + menuid);
	nstyle = getStyleSheet("." + navStyle + ":hover");
	obj.style.backgroundColor = nstyle.backgroundColor;
	obj.style.color = nstyle.color;
}

function menuReset() {
	if (menuLast != "" && objLast != "") {
		navChange(objLast, "nav_tbl", menuLast, 'close');
		menuLast = "";
	}
}

function navRestore(id) {
	nstyle = getStyleSheet("." + navStyle);
	obj = document.getElementById("td" + id);
	unsetHighlight();
	obj.style.backgroundColor = nstyle.backgroundColor;
	obj.style.color = nstyle.color;
}

function menuClean() {
	for (i = 0; i < menuList.length; i++) {
		if (menuList[i][6] != "hidden") {
			if (!isMouseInNo(i)) {
				hideMenu(i);
				navRestore(menuList[i][0]);
			}
		}
	}
}

function stylesDisplay() {
	tmp = document.styleSheets;
	ret = "";
	for (i = 0; i < tmp.length; i++) {
		ret += "Length: " + tmp[i];
		for (ii = 0; ii < tmp[i].rules.length; ii++) {
			ret = ret + "Rule no. " + i + ": " + tmp[i].rules[ii].selectorText + "\n";
		}
	}
	alert(ret);
}

function isMouseIn() {
	maxX = menuBoxX + menuBoxA;
	maxY = menuBoxY + menuBoxB;
	if ((xMousePos >= menuBoxX - 5) && (yMousePos >= menuBoxY - 22) && (xMousePos <= maxX) && (yMousePos <= maxY)) {
		ret = true;
	} else {
		ret = false;
	}
	//window.status = xMousePos + "x" + yMousePos + " >> menuBoxX=" + menuBoxX + " menuiBoxA=" + menuBoxA + " ||| maxX=" + maxX + ", maxY=" + maxY;
	return ret;
}

function isMouseInNo(no) {
	maxX = menuList[no][1] + menuList[no][3];
	maxY = menuList[no][2] + menuList[no][4];
	if ((xMousePos >= menuList[no][1] - 5) && (yMousePos >= menuList[no][2] - 22) && (xMousePos <= maxX) && (yMousePos <= maxY)) {
		ret = true;
	} else {
		ret = false;
	}
	//window.status = "no=" + no + " --- " + xMousePos + "x" + yMousePos + " >> " + menuList[no][1] + "x" + menuList[no][2] + " ||| 3=" + menuList[no][3] + " 4 =" + menuList[no][4] + " maxX=" + maxX + ", maxY=" + maxY;
	return ret;
}

function showMenu(id) {
	if (currMenuIn = document.getElementById(id)) {
		menuBoxX = currMenuIn.offsetLeft;
		menuBoxY = currMenuIn.offsetTop;
		menuBoxA = currMenuIn.offsetWidth;
		menuBoxB = currMenuIn.offsetHeight;
		currMenuIn.style.visibility = 'visible';
		//menuOpacIn = 0;
		//fadeIn();
	}
}

function showMenuA(id) {
	if (currMenu = document.getElementById("menu" + id)) {
		for (i = 0; i < menuList.length; i++) {
			if (menuList[i][0] == id) {
				break;
			}
		}
		//menuList[i][5] = 100;
		menuList[i][6] = 'visible';
		currMenu.style.visibility = 'visible';
		//setOpacity(currMenu, menuList[i][5]);
	}
}

function hideMenu(id) {
	if (currMenuOut = document.getElementById("menu" + menuList[id][0])) {
		//menuOpacOut = 100;
		//fadeOut();
		currMenuOut.style.visibility = "hidden";
		menuList[id][6] = "hidden";		
	}
}

function noclose() {
	menuclose = false;
}

function menuClose() {
	//menuclose = true;
	menuReset();
}

function setMenus() {
	ret = "";
	for (i = 0; i < menuList.length; i++) {
		menuID = "menu" + menuList[i][0];
		temp = document.getElementById(menuID);
		temp.style.top = bodyOffsetY - 1;
		temp.style.left = bodyOffsetX + (128 * i) - 1;
		temp.style.visibility = menuList[i][6];
		menuList[i][1] = bodyOffsetX + (128 * i) - 1;
		menuList[i][2] = bodyOffsetY - 1;
		menuList[i][5] = 100;
		menuList[i][6] = 'hidden';
		ret += menuList[i][1] + " x " + menuList[i][2] + " | " + menuList[i][3] + " x " + menuList[i][4] + "\n";
	}
	//alert(ret);
}

function setMenuHighlight(obj, styleName) {
	sty = getStyleSheet("." + styleName + ":hover");
	//window.status = obj.style.border + ": " + sty + "\n" + sty.borderBottom + " ???";
	obj.style.borderBottom = sty.borderBottom;
	obj.style.borderBottomColor = sty.borderBottomColor;
	obj.style.borderTop = sty.borderTop;
	obj.style.borderTopColor = sty.borderTopColor;
	obj.style.backgroundColor = sty.backgroundColor;
}

function unsetMenuHighlight(obj, styleName) {
	sty = getStyleSheet("." + styleName);
	//obj = document.getElementById(id);
	obj.style.borderBottom = sty.borderBottom;
	obj.style.borderBottomColor = sty.borderBottomColor;
	obj.style.borderTop = sty.borderTop;
	obj.style.borderTopColor = sty.borderTopColor;
	obj.style.backgroundColor = sty.backgroundColor;
}

function openSection(bodyLink, navLink) {
	body.location = bodyLink;
	leftNav.location = navLink;
	bodyFirst = false;
}

// Fancy Stuff by Dr. K.

function fadeIn() {
	if (menuOpacIn < 100) {
		menuOpacIn += menuOpacStep;
		if(IE5) currMenuIn.filters.alpha.opacity = menuOpacIn;
		if(GECKO) currMenuIn.style.MozOpacity = menuOpacIn / 100;
		setTimeout("fadeIn()", menuTimeout);
	} else {
		currMenuIn = "";
	}
}

function fadeOut() {
	if (menuOpacOut > 0) {
		menuOpacOut -= menuOpacStep;
		if(IE5) currMenuOut.filters.alpha.opacity = menuOpacOut;
		if(GECKO) currMenuOut.style.MozOpacity = menuOpacOut / 100;
		setTimeout("fadeOut()", menuTimeout);
	} else {
		currMenuOut.style.visibility = "hidden";
		currMenuOut = "";
	}
}

function setOpacity(obj, opa) {
	if (IE5) obj.filters.alpha.opacity = opa;
	if (GECKO) obj.style.MozOpacity = opa / 100;
}

function writeStatus(msg) {
	window.status = msg;
}

function menuMagic() {
	window.status = "Resized!";
	setTimeout("writeStatus('')", 2000);
	getBodyOffset();
	setMenus();
}

function openSectionGal(navLink, scope) {
	body.location = 'galerija.php?lang=slo&scope=' + scope;
	leftNav.location = navLink;
	bodyFirst = false;
	menuReset();
}

function openSectionGalEng(navLink, scope) {
	body.location = 'galerija.php?lang=eng&scope=' + scope;
	leftNav.location = navLink;
	bodyFirst = false;
	menuReset();
}

// SOLATA!!!

function setPointerOvr(obj, id) {
	obj.style.cursor = mouseCursor[id];
}
