//<!--

// Functionality applicable to all skins


var browser = "";
var ie = "Internet Explorer";
var mozilla = "Mozilla Compatible";
var safari = "Safari";
var mediaPath = "";
var warningText = "";
var debug = false;
var closeTime = 0;
var debug = true;
var debugWindow;

var trackingCursor = false;
var cursorX = -1;
var cursorY = -1;

var TOP_LEFT = "top left";
var TOP_RIGHT = "top right";
var BOTTOM_RIGHT = "bottom right";
var BOTTOM_LEFT = "bottom left";


function switchClass (elementID, newCssClass) {
    var element = getElement(elementID);
    if (element != undefined) {
	    element.className = newCssClass;
	}
	else {
		//reportEvent("ERROR: Document element " + elementID + " not found!");
	}
}


function getBodyElement () {
	return (document.compatMode && document.compatMode.toLowerCase() != "backcompat") ? document.documentElement : (document.body || null);	
}


function getElement (elementID) {
  var element;
  if (document.getElementById) element = document.getElementById(elementID);
  else if(document.all) element = document.all[elementID];
  if (element == null)
  	element = document.getElementsByTagName(elementID)[0];
  return element;
}


function hide (element, disableBlocking) {
	//reportEvent("Hiding element " + ((typeof(element) == "string") ? element : element.id));
	if (typeof(element) == "string") 
		element = getElement(element);
	if (element) {
		element.style.visibility = "hidden";
		if (disableBlocking)
			element.style.display = "none";
	}
}


function show (element, enableBlocking) {
	//reportEvent("Showing element " + ((typeof(element) == "string") ? element : element.id));
	if (typeof(element) == "string") 
		element = getElement(element);
	if (enableBlocking == null) 
		enableBlocking = true;
	if (element) {
		element.style.visibility = "visible";
		if (enableBlocking) 
			element.style.display = "block";
	}
}


function toggleVisibility (element, blocking) {
	if (typeof(element) == "string") 
		element = getElement(element);

	if (element.style.visibility == "hidden") {
		show(element, blocking);
	}
	else {
		hide(element, blocking);
	}
}


function getCSSValue (element, property) {
	if (document.defaultView && document.defaultView.getComputedStyle) {
		var computedStyle = document.defaultView.getComputedStyle(element,"");
		if (computedStyle != null) {
			return parseInt(computedStyle.getPropertyValue(property));
		}
	}
}


function getWidth (element) {
	
	var width = 0;
	
	//reportEvent("Detecting width for " + ((typeof(element) == "string") ? element : element.id), true);
	
	if (typeof(element) == "string") 
		element = getElement(element);

	if (element) {
		if (browser == ie) {
			var rects = element.getClientRects();
			if (rects[0]) rects = rects[0];
			width = rects.right - rects.left;
		}
		else {
			width = element.offsetWidth;
		}
		//reportEvent(" (" + width + ")");
		return width;
	} 
	else {
		//reportEvent("ERROR: Element " + ((typeof(element) == "string") ? element : element.id) + " not found in document (getWidth()).");
	}
}


function setWidth (element, newWidth) {
	
	var padding = 0;
	var border = 0;
	
	//reportEvent("Setting width for " + ((typeof(element) == "string") ? element : element.id) + " (" + newWidth + ")");
	
	if (typeof(element) == "string") 
		element = getElement(element);

	if (element) {
		if (browser == mozilla) {
			padding = getCSSValue(element, "padding-left") + getCSSValue(element, "padding-right");
			border = getCSSValue(element, "border-left-width") + getCSSValue(element, "border-right-width");
		}
		element.style.width = newWidth - border - padding;
	} 
	else {
		//reportEvent("ERROR: Element " + ((typeof(element) == "string") ? element : element.id) + " not found in document (setWidth()).");
	}
}


function getHeight (element) {

	var height = 0;

	//reportEvent("Detecting height for " + ((typeof(element) == "string") ? element : element.id), true);

	if (typeof(element) == "string") 
		element = getElement(element);

	if (element) {
		if (browser == ie) {
			var rects = element.getClientRects();
			if (rects[0]) rects = rects[0];
			if (rects.bottom && rects.top) {
				height = (rects.bottom - rects.top);
			}
			else if (element.getBoundingClientRect().bottom) {
				height = element.getBoundingClientRect().bottom - element.getBoundingClientRect().top;
			}
			//reportEvent(" (" + height + ")");
			return height;
		}
		else if (element.offsetHeight) {
			//reportEvent(" (" + element.offsetHeight + ")");
			return element.offsetHeight;
		}
		else {
			//reportEvent("ERROR: Unable to detect height for " + ((typeof(element) == "string") ? element : element.id) + " (getHeight()).");
			return 0;
		}
	} 
	else {
		//reportEvent("ERROR: Element " + ((typeof(element) == "string") ? element : element.id) + " not found in document (getHeight()).");
	}
}


function setHeight (element, newHeight) {
	
	var padding = 0;
	var border = 0;
	
	//reportEvent("Setting height for " + ((typeof(element) == "string") ? element : element.id) + " (" + newHeight + ")");

	if (typeof(element) == "string") 
		element = getElement(element);
	
	if (element) {
		if (browser == mozilla) {
			padding = getCSSValue(element, "padding-top") + getCSSValue(element, "padding-bottom");
			border = getCSSValue(element, "border-top-width") + getCSSValue(element, "border-bottom-width");
		}
		if (newHeight != "auto")
			newHeight = newHeight - border - padding;
		element.style.height = newHeight;
	} 
	else {
		//reportEvent("Element " + element + " not found in document (setHeight()).");
	}
}


function getLeft (element) {

	var left = 0;
	
	//reportEvent("Detecting Left position for " + ((typeof(element) == "string") ? element : element.id), true);
	
	if (typeof(element) == "string") 
		element = getElement(element);

	if (element) {
		if (browser == ie) {
			var rects = element.getClientRects();
			if (rects[0]) rects = rects[0];
			// for some reason IE always adds 2 to this value
			left = rects.left - 2;
		}
		else {
			left = element.offsetLeft;
		}
		//reportEvent(" (" + left + ")");
		return left;
	} 
	else {
		//reportEvent("ERROR: Element " + ((typeof(element) == "string") ? element : element.id) + " not found in document (getLeft()).");
	}
}


function getTop (element) {

	var top;

	//reportEvent("Detecting Top position for " + ((typeof(element) == "string") ? element : element.id), true);

	if (typeof(element) == "string") 
		element = getElement(element);

	if (element) {
		if (browser == ie) {
			var rects = element.getClientRects();
			if (rects[0]) rects = rects[0];
			// for some reason IE always adds 2 to this value
			top = rects.top - 2;
		}
		else {
			top = element.offsetTop;
		}
		//reportEvent(" (" + top + ")");
		return top;
	} 
	else {
		//reportEvent("ERROR: Element " + ((typeof(element) == "string") ? element : element.id) + " not found in document (getTop()).");
	}

}


function moveTo (element, x, y, relativeTo) {

	if (! relativeTo) relativeTo = TOP_LEFT;
	
	//reportEvent("Moving element " + ((typeof(element) == "string") ? element : element.id) + " to (" + x + ", " + y + "), relative to " + relativeTo + " corner");
	
	if (typeof(element) == "string") 
		element = getElement(element);

	if (element) {
		if (x) {
			if (typeof(x) == "string") x = parseInt(x);
			if (relativeTo == TOP_LEFT || relativeTo == BOTTOM_LEFT) element.style.left = x + "px";
			if (relativeTo == TOP_RIGHT || relativeTo == BOTTOM_RIGHT) element.style.right = x + "px";
		}
		if (y) {
			if (typeof(y) == "string") y = parseInt(y);
			if (relativeTo == TOP_LEFT || relativeTo == TOP_RIGHT) element.style.top = y + "px";
			if (relativeTo == BOTTOM_RIGHT || relativeTo == BOTTOM_LEFT) element.style.bottom = y + "px";
		}
	}
	else {
		//reportEvent("ERROR: Element " + ((typeof(element) == "string") ? element : element.id) + " not found while executing moveTo()");
	}
}


function center (element) {
	
	//reportEvent("Centering element " + ((typeof(element) == "string") ? element : element.id), true);
	
	if (typeof(element) == "string") 
		element = getElement(element);

	if (element) {
		var left = getInnerWidth()/2 - getWidth(element)/2;
		var top = getInnerHeight()/2 - getHeight(element)/2;
		//reportEvent(" (" + left + ", " + top + ")");
		moveTo(element, left, top);
	}
	else {
		//reportEvent("ERROR: Element " + ((typeof(element) == "string") ? element : element.id) + " not found while executing center()");
	}
}


function overlay (overlayElementID, elementID, resize) {

	//default resize to True
	if (! resize) resize = true;
	
	//reportEvent("Overlaying element " + overlayElementID + " over element " + elementID + " with resize = " + resize);

	moveTo(overlayElementID, getLeft(elementID), getTop(elementID));
	var overlay = getElement(overlayElementID);
	var element = getElement(elementID);
	if (resize) {
		setWidth(overlay, getWidth(elementID));
		setHeight(overlay, getHeight(elementID));
	}
}


function getInnerHeight () {
    var height = 0;
    if (document.body && document.body.clientHeight)
        height = document.body.clientHeight;
    else if (window.innerHeight)
        height = window.innerHeight;
    else if(document.documentElement && document.documentElement.clientHeight)
        height = document.documentElement.clientHeight;
    else {
        height = 0;
    }
    //reportEvent("Detecting inner browser height (" + height + ")");
    return height;
}


function getInnerWidth () {
    var width = 0;
    if (window.innerWidth)
        width = window.innerWidth;
    else if (document.body && document.body.clientWidth)
        width = document.body.clientWidth;
    else if(document.documentElement && document.documentElement.clientWidth)
        width = document.documentElement.clientWidth;
    else {
        width = 0;
    }
    //reportEvent("Detecting inner browser width (" + width + ")");
    return width;
}


function openWindow (url, name, width, height, resizable) {
	var leftPos = (screen.availWidth/2) - (width/2);
	var topPos = (screen.availHeight/2) - (height/2);
	var properties = "";

	if (resizable == null)
		resizable = true;

	if (screen.availHeight < height) {
		height = screen.availHeight - 50;
		topPos = 0;
	}
	if (screen.availWidth < width) {
		width = screen.availWidth;
		leftPos = 0;
	}
	properties+= "width=" + width + ",";
	properties+= "height=" + height + ",";
	properties+= "screenX=" + leftPos + ",";
	properties+= "screenY=" + topPos + ",";
	properties+= "directories=no,";
	properties+= "location=no,";
	properties+= "menubar=no,";
	properties+= "personalbar=no,";
	if (resizable) {
		properties+= "resizable=yes,";
	}
	else {
		properties+= "resizable=no,";
	}
	properties+= "scrollbars=yes,";
	properties+= "status=no,";
	properties+= "toolbar=no";
	
	return window.open(url, name, properties);
	
}


function fixPngImages () {
	if (browser == ie) {
		var allImages = document.getElementsByTagName('img'); 
		
		for (var i=0; i < allImages.length; i++) {
			if (allImages[i].src.indexOf(".png") > -1 && allImages[i].src.indexOf("transparency=no") < 0) {
				var parent = allImages[i].parentNode;
				var image = allImages[i];
				var rects = parent.getClientRects();
				var sizingMethod = image.getAttribute("sizing");
				
				if (sizingMethod == null) {
					if (parent.style.position == "absolute" || image.style.width.indexOf("%") > -1 || parent.style.width.indexOf("%") > -1 || image.style.height.indexOf("%") > -1 || parent.style.height.indexOf("%") > -1) {
						sizingMethod = "scale";
					}
					else {
						sizingMethod = "image";
					}
				}
if (rects[0]) rects = rects[0];
				parent.removeChild(image);
				parent.style.backgroundImage = 'none';
				parent.style.width = rects.right - rects.left;
				parent.style.height = rects.bottom - rects.top;
				parent.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image.src + "',sizingMethod='" + sizingMethod + "')";
				i-= 1;
			}
		}
	}
}


function addOption (selectObject, text, value, addToTop) {
	if (selectObject != null) {
		if (addToTop) {
			for (var i=selectObject.options.length-1; i >= 0 ; i--) {
				selectObject.options[i+1] = new Option(selectObject.options[i].text, selectObject.options[i].value);
			}
			selectObject.options[0] = new Option(text, value);
		}
		else {
			selectObject.options[selectObject.options.length] = new Option(text, value);
		}

	}
}


function launchHelp ()
{
	openWindow(helpUrl, "helpWindow", 640, 480, true);
}

function createLangSelectionWindow()
{
    openWindow("/uPortal/Print?F=/layout/getlanguages&xslFileName=com/dynix/hip/uportal/channels/standard/selectlang.xsl", "selectLanguages", 480, 250, true);
}

function jumpToDivAnchor (anchorName) {
	if (browser == ie) {
		getElement(anchorName).scrollIntoView(true);
		getElement("topAnchor").scrollIntoView(true);
	}
	else {
		location.replace("#" + anchorName);
		window.scroll(0,0);
	}
}


//fixme: is this used?
function addFormSubmitAction (functionObj) {
	var elements = document.getElementsByTagName("form");
	
	for (var i=0; i < elements.length; i++) {
		elements[i].onsubmit = functionObj;
	}
}


function readCookie (key) {
	var cookie_string = "" + document.cookie;
	var cookie_array = cookie_string.split ("; ");

	for (var i = 0; i < cookie_array.length; i++) {
		var single_cookie = cookie_array [i].split ("=");
		if (single_cookie.length != 2)	{ continue; }
		var name  = unescape(single_cookie [0]);
		var value = unescape(single_cookie [1]);
		if (key == name) { 
			return value; 
		}
	}
	return "";
}


function trackCursor (e) {
	if (trackingCursor == false) {
		document.onmousemove = trackCursor;
		trackingCursor = true;
	}
	else {
		if (e != null) {
			cursorX = e.pageX;
			cursorY = e.pageY;
		}
		else {
			cursorX = event.clientX;
			cursorY = event.clientY;
		}
		if (mouseMoveAction) {
			mouseMoveAction(cursorX, cursorY);
		}
	}
}


if (navigator.appVersion.indexOf("MSIE") > -1) {
	browser = ie;
}
else if (navigator.appVersion.indexOf("Safari") > -1) {
	browser = safari;
}
else {
	browser = mozilla;
}


/* DEBUGGING FUNCTIONS */


if (debug && location.href.indexOf("debugWindow") > -1) {
	/*
	debugWindow = openWindow("media/com/dynix/hip/uportal/layout/tab-column/jsdebug.html", "jsDebug", 800, 600, false);
	window.onerror = sendErrorToDebugWindow;
	*/
}

var newDebugLine = true;

function reportEvent (string, suppressLineBreak) {
	/*
	if (debug && debugWindow && debugWindow.addText) {
		if (newDebugLine) {
			debugWindow.addText("-> " + string);
		}
		else {
			debugWindow.addText(string);
		}
		if (suppressLineBreak) {
			newDebugLine = false;
		}
		else {
			debugWindow.addText("\n\n");
			newDebugLine = true;
		}
	}
	*/
}


function showString (label, string) {
	/*
	if (debug && debugWindow && debugWindow.addText) {
		debugWindow.addText("================================================\n");
		debugWindow.addText(label + ":\n");
		debugWindow.addText("------------------------------------------------\n");
		debugWindow.addText(string + "\n");
		debugWindow.addText("================================================\n");
	}
	*/
}


function sendErrorToDebugWindow (msg, url, lno) {
	//reportEvent("ERROR: " + msg + " in file " + url + " (line " + lno + ")");
}



// -->
