/* menu code */
var timer = "";
var openMenu = null;
var canMenuBeClosed = true;

function clearTimer(){
	window.clearTimeout(timer);
}

function tryToClose(){
	var funcToCall = "canMenuBeClosed = true"
	timer = window.setTimeout(funcToCall, 2000);
	//alert('tryToClose')
}

function showMenu(id, objPos,homepage){
	//id - of the menu ; objPos - reference to button image
	if(document.getElementById){
		clearTimer();
		//setElementProperty('dealerpalette', 'display', 'none');//in our case, there is only one menu
		openMenu = id;
		canMenuBeClosed = false

		var x = 0;
		var y = 0;
		
		x = getElementLeft(objPos);
		y = getElementBottom(objPos);
		//alert('x='+x+'\n y='+y)
		
		//offsets w158 h89
		y=y-89-25;//palette h + button h (+1)
		if((navigator.appVersion.indexOf('Macintosh')>-1)&&document.all){
			//Mac IE is placing the menus 100px too low FIX
			y=y-100
		}
		
		x=x-19;
		
		if(homepage){
			x=x+50;
		}

		setElementProperty(id, 'display', 'block');
		setElementProperty(id, 'left', x + "px");
		setElementProperty(id, 'top', y + "px");
	}
}

function hideMenu(id){
	setElementProperty(id, 'display', 'none');
	openMenu = null;
	if(document['bwheretobuy'])inact('bwheretobuy');
}

function getMousePos(event){
	var x, y;
	if(window.event){
		x = window.event.clientX;
		y = window.event.clientY;
		if (document.documentElement && document.documentElement.scrollTop){
			y+=document.documentElement.scrollTop;
		} else {
			y+=document.body.scrollTop;
		}
	} else {
		x = event.pageX;
		y = event.pageY;
	}

	if(openMenu != null){
		var testInside = isInside(x, y, openMenu);
		if(!testInside && canMenuBeClosed == true){
			hideMenu(openMenu);
		}
		if(testInside){
			canMenuBeClosed = true;
			clearTimer();
		}
	}
}

function isInside(xMouse, yMouse, id){
	if( (id != null) && (xMouse >= getElementLeft(id)) && (xMouse <=getElementRight(id)) && (yMouse >= getElementTop(id)) && (yMouse <= getElementBottom(id)) ){
		return true;
	} else {
		return false;
	}
}

//moved to product template
/*
window.onload = function() {
	document.onmousemove = getMousePos;
}
*/


function switchDisplay(elm){
	var elm = elm.parentNode.nextSibling;
	while(elm.nodeType == 3){
		elm = elm.nextSibling;
	}
	
	var elmStyle = getElementProperty(elm, "display")

	if((elmStyle == "none") || (elmStyle == "") || (elmStyle == null)){
		setElementProperty(elm, "display", "block");
		return;
	}
	if(elmStyle == "block"){
		setElementProperty(elm, "display", "none");
		return;
	}
}


/* utilities */

/* Return the px distance from left border of the element to left border of the window */

function getElementLeft(p_elm) {
	var x = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		x+= elm.offsetLeft;
		elm = elm.offsetParent;
	}
	return parseInt(x);
}

/* Return the px width of the element */
function getElementWidth(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetWidth);
}

/* Return the px distances from right border of the element to left border of window */
function getElementRight(p_elm){
	return getElementLeft(p_elm) + getElementWidth(p_elm);
}

/* Return the px distances from top border of the element to top border of the window */
function getElementTop(p_elm) {
	var y = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		y+= elm.offsetTop;
		elm = elm.offsetParent;
	}
	return parseInt(y);
}

/* Return the px heght of the element */
function getElementHeight(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetHeight);
}

/* Return the px distances from bottom border of the element to border top of the window */
function getElementBottom(p_elm){
	return getElementTop(p_elm) + getElementHeight(p_elm);
}

/* Return a style property of the elemnt , it return null if does not exist */
function getElementProperty(p_elm, p_property){
	var elm = null;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	if (elm != null){
		if(elm.style){
			elm = elm.style;
			if(elm[p_property]){
				return elm[p_property];
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
}

/* Set a property of style type of the element */
function setElementProperty(p_elm, p_property, p_value){
	var elm = null;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	if((elm != null) && (elm.style != null)){
		elm = elm.style;
		elm[ p_property ] = p_value;
	}
}
