/*
* POSITIONING UTILITIES (posUtil.js) ==========================================
*/

/**
 * Return the px distance from left border of the element to left border of the window 
 **/
function getElementLeft(p_elm) {
	var posutilx = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		posutilx+= elm.offsetLeft;
		elm = elm.offsetParent;
	}
	return parseInt(posutilx);
}

/**
 * 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 posutily = 0;
	var elm;
	if(typeof(p_elm) == "object") {
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		posutily+= elm.offsetTop;
		elm = elm.offsetParent;
	}
	return parseInt(posutily);
}

/**
 * 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;
	}
}

/**
 * MENU DROPDOWNS (menu.js) ======================================
**/
if (parseInt(navigator.appVersion) >= 4) {
    var IE4 = (navigator.appVersion.indexOf("MSIE") > 0);
    var NN4 = (navigator.appName == "Netscape");
    var OS;
    if (navigator.appVersion.indexOf("Win") > 0) {
		OS = "Windows";
	}
    if (navigator.appVersion.indexOf("Mac") > 0) {
		OS = "MacOS";
	}
}

var timer = "";
var openMenu = null;
var canMenuBeClosed = true;

function clearTimer(){
	window.clearTimeout(timer);
}

function tryToClose(){
	var funcToCall = "canMenuBeClosed = true";
	timer = window.setTimeout(funcToCall, 400);
}

function showMenu(id, objPos){
	document.onmousemove = getMousePos;
	if(document.getElementById){
		clearTimer();
		setElementProperty('proandserv', 'display', 'none');
		setElementProperty('trainingandservices', 'display', 'none');
		setElementProperty('community', 'display', 'none');
		setElementProperty('partners', 'display', 'none');
		setElementProperty('company', 'display', 'none');
		setElementProperty('support', 'display', 'none');
		openMenu = id;
		canMenuBeClosed = false;

		var menux = 0;
		var menuy = 0;
		
		/* The positioning of the submenus is adjusted to
		compensate for a problem in Mac IE */
		
		if ((IE4 > 0) && (OS == "MacOS")) {
			menux = getElementLeft(objPos);
			menuy = getElementBottom(objPos) - 73;
		} else {
			menux = getElementLeft(objPos);
			menuy = getElementBottom(objPos);
		}		
		
		/*if(!window.event){
			y-=1;
		}*/
		
		setElementProperty(id, 'display', 'block');
		setElementProperty(id, 'left', menux + "px");
		setElementProperty(id, 'top', menuy + "px");
	}
}

function hideMenu(id){
	setElementProperty(id, 'display', 'none');
	openMenu = null;
}

function getMousePos(event){
	var menux, menuy;
	if(window.event){
		menux = window.event.clientX;
		menuy = window.event.clientY;
		if (document.documentElement && document.documentElement.scrollTop){
			menuy+=document.documentElement.scrollTop;
		} else {
			menuy+=document.body.scrollTop;
		}
	} else {
		menux = event.pageX;
		menuy = event.pageY;
	}

	if(openMenu != null){
		var testInside = isInside(menux, menuy, 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;
	}
}
/* 
* This function change the property display of an element
*/
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;
	}
}

/**
 * Image functions for swapping, preloading, etc (rollover.js) ================
 **/
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


	
function GoUrl(s)
{	var d = s.options[s.selectedIndex].value;
	window.top.location.href = d;
	s.selectedIndex = 0;
}

if(document.images) {
	contactOn = new Image;
	contactOn.src = "/images/nav_head/sparky/contact_text.gif";

	emailOn = new Image;
	emailOn.src = "/images/nav_head/sparky/email_text.gif";

	printOn = new Image;
	printOn.src = "/images/nav_head/sparky/print_text.gif";

	ptcexpressOn = new Image;
	ptcexpressOn.src = "/images/nav_head/sparky/ptcexpress_text.gif";

	toolsOn = new Image;
	toolsOn.src = "/images/nav_head/sparky/tools_text.gif";
}

function img_on(imgName) {
        if (document.images) {
                imgOn = eval(imgName + "On.src");
                document[imgName].src = imgOn;
                return true;
        } else {
                return true;
        }
}

function img_off(imgName) {
        if (document.images) {
                imgOff = eval(imgName + "Off.src");
                document[imgName].src = imgOff;
                return true;
        } else {
                return true;
        }
}

function img_on_tools(docName,imgName) {
	if (document.images) {
                imgOn = eval(imgName + "On.src");
                document[docName].src = imgOn;
                return true;
        } else {
                return true;
        }
}
