// *****************************************************************************************************************
// Browser detector (extend for more browsers if necessary...)
// *****************************************************************************************************************
var isIE = ((navigator.userAgent.toUpperCase().indexOf("MSIE") > -1) && (navigator.userAgent.toUpperCase().indexOf("OPERA") == -1));
var isIE6orBelow = (isIE && parseBrowserVersion(navigator.appVersion) < 7);

function parseBrowserVersion(appVer) {
    if (isIE)
        return parseInt(appVer.substr(appVer.toUpperCase().indexOf('MSIE ')+5, 1));

    // return zero as default (unknown version)
    return 0;
}
// *****************************************************************************************************************
// Get submenu position and show it
// *****************************************************************************************************************
function showSubmenuAtMenuPosition (id, menuId) {
	var menuObj = getDiv (menuId);
	var leftPosition = findPosX(menuObj)+2;
	document.getElementById(id).style.left = leftPosition +'px';
	document.getElementById(id).style.position = 'absolute';
	document.getElementById(id).style.zIndex = '600';

    showSubmenu(id, menuId, true);
	hideSelectBoxesUnderDiv(id);
}

// Used in showSubmenuAtMenuPosition
// Shows submenu and keeps tab selected
function showSubmenu (id, menuId, visible) {
    var vista;
    if (document.layers) {
		vista = visible ? 'show' : 'hide';
		getDiv(id).visibility = vista;
	} else if (document.all) {
		vista = visible ? 'visible' : 'hidden';
		getDiv(id).style.visibility = vista;
	} else if (document.getElementById) {
		vista = visible ? 'visible' : 'hidden';
		getDiv(id).style.visibility = vista;
	}
	if (getDiv(menuId))
		getDiv(menuId).className = visible ? 'menu-links-current' : 'menu-links';
}

// Used in showSubmenuAtMenuPosition
// get div object
function getDiv(id){
	if (document.layers)
		return document.layers[id];
	else if (document.all)
		return document.all[id];
	else if (document.getElementById)
		return document.getElementById(id);
}

// Used in showSubmenuAtMenuPosition
// get div leftposition
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
 		while(1) {
    		curleft += obj.offsetLeft;
    		if(!obj.offsetParent)
     			break;
    		obj = obj.offsetParent;
 		}
	else if(obj.x)
 		curleft += obj.x;
	return curleft;
}


// *****************************************************************************************************************
// Hides all select boxes in a page
// NOTE! We only do this for old versions of IE so need to consider browser compability here
// *****************************************************************************************************************
function hideSelectBoxesUnderDiv(divId) {
	if (isIE6orBelow) {
		// Set coordinates in order left, top, width, height
		var coordinates = new Array(getDivLeftPos(divId), getDivTopPos(divId), getDivWidth(divId), getDivHeight(divId));
		var allSelects = document.all.tags("select");

           if (allSelects) {
			for (var i=0; i < allSelects.length; i++) {
				var selectLeft = getPageOffsetLeft(allSelects[i]);
				var selectTop = getPageOffsetTop(allSelects[i]);
				var selectWidth = allSelects[i].offsetWidth;
				var selectHeight = allSelects[i].offsetHeight;

				var horisontalMatch = false;
				var verticalMatch = false;
				if (selectLeft > coordinates[0] && selectLeft < (coordinates[0]+coordinates[2]))
					horisontalMatch = true;
				else if ((selectLeft+selectWidth) > coordinates[0] && (selectLeft+selectWidth) < (coordinates[0]+coordinates[2]))
					horisontalMatch = true;

				if (horisontalMatch) {
					if (selectTop > coordinates[1] && selectTop < (coordinates[1]+coordinates[3]))
						verticalMatch = true;
					else if ((selectTop+selectHeight) > coordinates[1] && (selectTop+selectHeight) < (coordinates[1]+coordinates[3]))
						verticalMatch = true;
				}

				if (horisontalMatch && verticalMatch)
					allSelects[i].style.visibility = 'hidden';
			}
		}
	}
}

// Used in hideSelectBoxesUnderDiv
// gets the absolute left position of an object
function getDivLeftPos(divId) {
    var obj = getDiv(divId)
    var objLeft = obj.offsetLeft;
    while(obj.offsetParent!=null) {
        var objParent = obj.offsetParent;
        objLeft += objParent.offsetLeft;
        obj = objParent;
    }
    return objLeft;
}

// Used in hideSelectBoxesUnderDiv
// gets the absolute left position of an object
function getDivTopPos(divId) {
    var obj = getDiv(divId);
    var objTop = obj.offsetTop;
    while(obj.offsetParent!=null) {
        var objParent = obj.offsetParent;
        objTop += objParent.offsetTop;
        obj = objParent;
    }
    return objTop;
}

// Used in hideSelectBoxesUnderDiv
function getDivWidth(divId) {
    var obj = getDiv(divId);
    if (obj.currentStyle && obj.currentStyle.width) {
        var objWidth = parseInt(obj.currentStyle.width);
        if (objWidth && !isNaN(objWidth))
            return objWidth;
        else {
            var objWidthOffsetWidth = obj.offsetWidth;
            if (objWidthOffsetWidth && !isNaN(objWidthOffsetWidth))
                return objWidthOffsetWidth;
        }
    } else if (window.getComputedStyle(obj, '')) {
        var computedStyle = window.getComputedStyle(obj, '');
        objWidth = parseInt(computedStyle.getPropertyValue('width'));
        if (objWidth && !isNaN(objWidth))
            return objWidth;
    }
    return -1;
}

// Used in hideSelectBoxesUnderDiv
function getDivHeight(divId) {
    var obj = getDiv(divId);
    var objHeight;
    if (isIE6orBelow) {
        objHeight = parseInt(obj.scrollHeight);
        if (!objHeight) objHeight = 100;
        if (objHeight && !isNaN(objHeight))
            return objHeight;
    } else if (obj.offsetHeight) {
        objHeight = parseInt(obj.offsetHeight);
        if (objHeight && !isNaN(objHeight))
            return objHeight;
    } else if (window.getComputedStyle(obj, '')) {
        var computedStyle = window.getComputedStyle(obj, '');
        objHeight = parseInt(computedStyle.getPropertyValue('height'));
        if (objHeight && !isNaN(objHeight))
            return objHeight;
    }
    return -1;
}

// Used in hideSelectBoxesUnderDiv
// calculates an objects left position in window (help function to hideSelectBoxesUnderDiv() initially so may not have full browser compability...)
function getPageOffsetLeft(obj){
    var x = obj.offsetLeft;
    if (obj.offsetParent != null)
            x += getPageOffsetLeft(obj.offsetParent);
    return x;
}

// Used in hideSelectBoxesUnderDiv
// calculates an objects top position in window (help function to hideSelectBoxesUnderDiv() initially so may not have full browser compability...)
function getPageOffsetTop(obj){
    var x = obj.offsetTop;
    if (obj.offsetParent != null)
            x += getPageOffsetTop(obj.offsetParent);
    return x;
}


// *****************************************************************************************************************
// Marks selected tab in top menu
// *****************************************************************************************************************
function setAboutMenu() {

		var hostname = document.location.href.substr(0, document.location.href.indexOf("/", 8));
        var path = document.location.pathname;
        // Path in URL for links in left menu
        if (document.location.href.indexOf("menuPath") != -1)
            path = document.location.href.substr(document.location.href.indexOf("menuPath")+9, document.location.href.length);
        var allATags = document.getElementsByTagName("a");
		try {
			for (var i = 0; i < allATags.length; i++) {
				if (allATags[i].href == (hostname + path) && allATags[i].parentNode.className == "menu-links") {
					allATags[i].parentNode.className = "menu-links-current";
                }
                if (allATags[i].href == (hostname + path) && allATags[i].parentNode.className == "menu-second") {
                    var topParent = getDiv(allATags[i].parentNode.id.replace('-second',''))
                    if (topParent)
                           topParent.className = "menu-links-current";
                    allATags[i].parentNode.style.display = "block";
                    allATags[i].style.fontWeight = "bold";
                }

            }
		} catch(ex) {
			/* ignore me later... */
			//alert("error it is: " + ex.message);
		}
}

// *****************************************************************************************************************
// Marks selected tab in left menu
// *****************************************************************************************************************
function setAboutLeftMenu() {
    var destMenuRoot = getDiv("destination-links");
	if (destMenuRoot) {
        
        var hostname = document.location.href.substr(0, document.location.href.indexOf("/", 8));
		var path = document.location.pathname;
        var currentSubdiv = "";
        if (document.location.href.indexOf("menuPath") != -1)
            path = document.location.href.substr(document.location.href.indexOf("menuPath")+9, document.location.href.length);
        if (document.location.href.indexOf("Press") != -1)
            currentSubdiv = "submenu-press-" + document.location.href.substr(document.location.href.indexOf("Press")+5, 4);

        var allDivTags = destMenuRoot.getElementsByTagName("div");
		try {
            for (var i = 0; i < allDivTags.length; i++) {
                if (allDivTags[i].className == "destination-links2" && allDivTags[i].id != currentSubdiv) {
					allDivTags[i].style.display = "none";
                } else if (allDivTags[i].className == "destination-links2" && allDivTags[i].id == currentSubdiv){
                    allDivTags[i].style.display = "block";
                    allDivTags[i].parentNode.className = "destination-current";
                }
			}

		} catch(ex) {
			/* ignore me later... */
			//alert("error it is: " + ex.message);
		}
	}
}

// *****************************************************************************************************************
// Hover function for left menu
// *****************************************************************************************************************
function hoverDiv (div, show){
    if (div.className != 'destination-current'){
	    if (show){
		    div.className = 'destination-links1-hover';
	    } else {
		    div.className = 'destination-links1';
	    }
    }
}

// *****************************************************************************************************************
// Open submenu function for left menu press
// *****************************************************************************************************************
function openPressSubmenu (div){
	var destMenuRoot = getDiv("destination-links");
	if (destMenuRoot) {
		var allDivTags = destMenuRoot.getElementsByTagName("div");
		try {
			for (var i = 0; i < allDivTags.length; i++) {
                if (allDivTags[i].className == "destination-links2") {
					allDivTags[i].style.display = "none";
                    allDivTags[i].parentNode.className = "destination-links1";
                }
			}
		} catch(ex) {
			/* ignore me later... */
			//alert("error it is: " + ex.message);
		}
	}
	getDiv(div).style.display = "block";
    getDiv(div).parentNode.className = "destination-current";
    //setAboutLeftMenu();
}

// *****************************************************************************************************************
// DIV functions
// *****************************************************************************************************************

// Shows a DIV value by given ID
// NOTE! The difference between this function and  the display function is that this one just hides or shows the
// 		 value in the DIV but the space it posseses on the page is never removed. The display function on the other
//		 hand collapse it's space (so basically the page can move when the DIV is displayed or collapsed)
function show(id, visible) {
    if (document.layers) {
        vista = visible ? 'show' : 'hide';
        getDiv(id).visibility = vista;
    } else if (document.all) {
        vista = visible ? 'visible' : 'hidden';
        getDiv(id).style.visibility = vista;
    } else if (document.getElementById) {
        vista = visible ? 'visible' : 'hidden';
        getDiv(id).style.visibility = vista;
    }
}