// Tuksi Frontend - JavaScript
// Version 2005-09-09-AMA

// activate using <body onload="init()">
function init(){
	setTimeout("setElementsToFade()", 350);
}

function imageListExpand(thisobj){
	var imgdiv = thisobj.getElementsByTagName("span")[0].style;
	var img = thisobj.getElementsByTagName("img")[0].style;
	if (imgdiv.visibility == "hidden" || imgdiv.visibility == ""){
		imgdiv.visibility = "visible";
		img.cursor = "pointer";
	}
	else
		imgdiv.visibility = "hidden";
}

function bookListExpand(thisobj,iIterator){
	var bookdiv = thisobj.getElementsByTagName("span")[0];
	var bookdescr = thisobj.getElementsByTagName("span")[1];

	if (iIterator == 1){
		bookdiv.style.display = "block";
		bookdescr.style.display = "none";
	}else if (iIterator == 0){
		bookdescr.style.display = "block";
		bookdiv.style.display = "none";
	}
}


/*
Tuksi Frontend basic JavaScript functions

getElementHeight(sElementId)
getElementWidth(sElementId)
setElementHeight(sElementId, iHeight)
setElementWidth(sElementId, iWidth)
getWindowWidth()
getWindowHeight()
getScrollWindowHeight()
setPopup(oThis, sHeight, sWidth)
getMouseX(oEvent)
getMouseY(oEvent)
getFlashVersion()
getBrowserData()
setDisplayElement(sElementId)
setPreloadImages(sImageUrls (comma-separated))
setSwapImage(oThis, iSwapToImage)
setPng()
setAlert(aAlerts)
*/


// Returns the height of an element with sElementId as id
function getElementHeight(sElementId){
	var oElement;
	if (typeof(sElementId) != "object"){
		oElement = document.getElementById(sElementId);
	} else {
		oElement = sElementId;
	}
	if (oElement){ return oElement.offsetHeight }
}

// Returns the width of an element with sElementId as id
function getElementWidth(sElementId){
	var oElement;
	if (typeof(sElementId) != "object"){
		oElement = document.getElementById(sElementId);
	} else {
		oElement = sElementId;
	}
	if (oElement){ return oElement.offsetWidth}
}

// Sets a specific height (iHeight) on an element with sElementId as id
function setElementHeight(sElementId, iHeight){
	if (typeof(sElementId) != "object"){
		oElement = document.getElementById(sElementId);
	} else {
		oElement = sElementId;
	}
	if (oElement){
		oElement.style.height = iHeight + "px";
	}
}

// Sets a specific width (iHeight) on an element with sElementId as id
function setElementWidth(sElementId, iWidth){
	if (typeof(sElementId) != "object"){
		oElement = document.getElementById(sElementId);
	} else {
		oElement = sElementId;
	}
	if (oElement){
		oElement.style.width = iWidth + "px";
	}
}

// Returns width of browser window
function getWindowWidth(){
	var oHtml = document.getElementsByTagName("HTML")[0];
	if (oHtml) {
		return oHtml.offsetWidth
	}
}

// Returns height of browser window
function getWindowHeight(){
	var oHtml = document.getElementsByTagName("HTML")[0];
	if (oHtml) {
		switch(getBrowserData()){
			case "Safari":
			case "MSIE_50":
			case "MSIE_55":
			case "MSIE_60":
				return oHtml.offsetHeight
			break;
			
			case "Firefox":
				return oHtml.clientHeight
			break;
			
			case "MSIE_MAC":
				return document.body.clientHeight;
			break;
		}
	}
}

// Returns amount of pixels scrolled from the top
function getScrollWindowHeight(){
	var oBody = document.getElementsByTagName("BODY")[0];
	var oHtml = document.getElementsByTagName("HTML")[0];
	if (oBody && oHtml) {
		switch(getBrowserData()){
			case "Firefox":
			case "MSIE_60":
				return oHtml.scrollTop;
			break;
			
			default:
				return oBody.scrollTop;
		}
	}
}

// Opens a popup - <a href="http://dwarf.dk/ onclick="return setPopup(this, 300, 300)">
function setPopup(oThis, sHeight, sWidth, bResize, bScroll, bMenu, bLocation, bToolbar){
	var sUrl;
	if(typeof(oThis) == "string"){sUrl = oThis}else{sUrl=oThis.href}
	if(bResize){bResize="resizable"}else{bResize="resizable=0"}
	if(bScroll){bScroll="scrollbars"}else{bScroll="scrollbars=0"}
	if(bMenu){bMenu="menubar"}else{bMenu="menubar=0"}
	if(bLocation){bLocation="location"}else{bLocation="location=0"}
	if(bToolbar){bToolbar="toolbar"}else{bToolbar="toolbar=0"}
	var dwarfPopup = window.open(sUrl, "dwarfPopup", "width=" + sWidth + ", height=" + sHeight + ", " + bResize + ", " + bScroll + ", " + bMenu + ", " + bLocation + ", " + bToolbar + "");

	return false;
}

// Returns x coordinate of mousepointer
function getMouseX(oEvent){
	var x = oEvent.clientX;
	if (!x){x = 0}
	return x;
}

// Returns y coordinate of mousepointer
function getMouseY(oEvent){
	var y = oEvent.clientY;
	if (!y){y = 0}
	return y;
}

// Returns major flash version
function getFlashVersion(){
	var iMaxFlashVersion = 20;
	var iFlashVersion = 0;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i = 0; i < navigator.plugins.length; i++) {
			if (navigator.plugins[i].name.indexOf('Shockwave Flash') != -1) {
				iFlashVersion = navigator.plugins[i].description.split('Shockwave Flash ')[1].charAt(0);
				break;
			}
		}
	} else if (window.ActiveXObject) {
		for (var i = 2; i <= iMaxFlashVersion; i++) {
			try {
				oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + i + "');");
				if( oFlash ) {
					iFlashVersion = i;
				}
			}
			catch(e) {}
		}
	}
	return iFlashVersion
}

// Show or hides an element with sElementId as id
function setDisplayElement(sElementid){
	var oElement = document.getElementById(sElementid);
	if (oElement){
		if(oElement.style.display == "block" || oElement.style.display == ""){
			oElement.style.display = "none";
		} else {
			oElement.style.display = "block";
		}
	}
}

// Returns browser name
function getBrowserData(){
	var sBrowserData = navigator.userAgent + " " + navigator.appVersion
	if (sBrowserData.indexOf("MSIE") != -1 && sBrowserData.indexOf("Macintosh") != -1) { return "MSIE_MAC" }
	else if (sBrowserData.indexOf("MSIE 5.0") != -1 && sBrowserData.indexOf("Windows") != -1) {	return "MSIE_50" }
	else if (sBrowserData.indexOf("MSIE 5.5") != -1 && sBrowserData.indexOf("Windows") != -1) { return "MSIE_55" } 
	else if (sBrowserData.indexOf("MSIE 6.0") != -1 && sBrowserData.indexOf("Windows") != -1) { return "MSIE_60" } 
	else if (sBrowserData.indexOf("Firefox") != -1) { return "Firefox" } 
	else if (sBrowserData.indexOf("Safari") != -1) { return "Safari" } 
	else { return "Unknown Browser"	}
}

// Preload all images in sImageUrls - url absolute to root e.g. '/images/menu/bn_buttonActive_1.gif'
var aImageObjects = [];
function setPreloadImages(sImageUrls){
	if(sImageUrls){
		var aImageUrls = sImageUrls.split(",")
		for (var i=0; i<aImageUrls.length; i++){
			var oTemp = new Image();
			oTemp.src = aImageUrls[i].replace(" ", "");
			aImageObjects[aImageObjects.length] = oTemp;
		}
	}
}

// Swaps source of oThis to a preloaded image in aImageObjects[] - either by directly referring or by image name
function setSwapImage(oThis, iSwapToImage){
	if(oThis){
		if(typeof(iSwapToImage) == "number"){
			oThis.src = aImageObjects[iSwapToImage].src;
		} else {
			for(var i = 0; i<aImageObjects.length; i++){
				if(aImageObjects[i].src.indexOf(iSwapToImage) != -1 ){
					oThis.src = aImageObjects[i].src;
				}
			}
		}
	}
}

// Replace all inline PNGs with filter property and set clear gif as src (only works in ie6)
function setPng(){
	if(getBrowserData().indexOf("MSIE") != -1 && getBrowserData() != "MSIE_MAC"){
		var aImgs = document.getElementsByTagName("IMG");
		var sImgSrc;
		for(var i = 0; i<aImgs.length; i++){
			sImgSrc = aImgs[i].src.toLowerCase()
			if(sImgSrc.lastIndexOf(".png") != -1){
				aImgs[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + aImgs[i].src + ', sizingMethod="image")';
				aImgs[i].src = "/images/graphics/gx_blank.gif";
			}
		}
	}
}

// Display inline error popup
function setAlert(aAlerts){
	var oBody = document.getElementsByTagName("BODY")[0];
	var oAlert = document.getElementById("alert");
	
	if(oBody){
		// remove existing alerts
		if(oAlert){
			oBody.removeChild(oAlert);
		}
	
		// is alerts array or string?
		// generate html for displaying the errors
		var sAlert = "";
		if(typeof(aAlerts) == "object"){
			for(var i = 0; i<aAlerts.length; i++){
				sAlert += "<li>" + aAlerts[i] + "</li>";
			}
		} else {
			sAlert += "<li>" + aAlerts + "</li>";
		}
		
		// create elements for alert
		var oAlert = document.createElement("DIV");
		oAlert.id = "alert";
		
		var oAlertContentList = document.createElement("UL");
		oAlertContentList.innerHTML = sAlert;
		
		var oAlertContentClose = document.createElement("A");
		oAlertContentClose.innerHTML = "luk";
		oAlertContentClose.onclick = function(){
			document.getElementById("alert").style.display = "none";
			return false;
		}
		
		var oAlertContent = document.createElement("DIV");
		oAlertContent.className = "alertcontent";
		oAlertContent.appendChild(oAlertContentList);
		oAlertContent.appendChild(oAlertContentClose);
		
		var oAlertShadow = document.createElement("DIV");
		oAlertShadow.className = "alertshadow";
		
		// append elements for alert
		oAlert.appendChild(oAlertContent);
		oAlert.appendChild(oAlertShadow);
				
		// append alert to body	
		oBody.appendChild(oAlert);
		
		// placement of error
		oAlert.style.visibility = "hidden";	
		oAlert.style.display = "block";
		oAlert.style.top = getScrollWindowHeight() + 150 + "px";
		oAlert.style.left = (getWindowWidth() / 2) - (getElementWidth(oAlert.getElementsByTagName("DIV")[0]) / 2) + "px";
		
		// update the height of the shadow
		oAlertShadow.style.height = getElementHeight(oAlertContent) + "px";
		
		// make alert visible
		oAlert.style.visibility = "visible";
	}
}

function GP_AdvOpenWindow(theURL,winName,features,popWidth,popHeight,winAlign,ignorelink,alwaysOnTop,autoCloseTime,borderless) { //v2.0

  var leftPos=0,topPos=0,autoCloseTimeoutHandle, ontopIntervalHandle, w = 480, h = 340;  
  if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;
  if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + popHeight;
  if (winAlign && winAlign != "" && popWidth > 0 && popHeight > 0) {
    if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
		if (winAlign.indexOf("center") != -1) {topPos = (h-popHeight)/2;leftPos = (w-popWidth)/2;}
		if (winAlign.indexOf("bottom") != -1) topPos = h-popHeight; if (winAlign.indexOf("right") != -1) leftPos = w-popWidth; 
		if (winAlign.indexOf("left") != -1) leftPos = 0; if (winAlign.indexOf("top") != -1) topPos = 0; 						
    features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}
  if (document.all && borderless && borderless != "" && features.indexOf("fullscreen") != -1) features+=",fullscreen=1";
  if (window["popupWindow"] == null) window["popupWindow"] = new Array();
  var wp = popupWindow.length;
  popupWindow[wp] = window.open(theURL,winName,features);
  if (popupWindow[wp].opener == null) popupWindow[wp].opener = self;  
  if (document.all || document.layers || document.getElementById) {
    if (borderless && borderless != "") {popupWindow[wp].resizeTo(popWidth,popHeight); popupWindow[wp].moveTo(leftPos, topPos);}
    if (alwaysOnTop && alwaysOnTop != "") {
    	ontopIntervalHandle = popupWindow[wp].setInterval("window.focus();", 50);
    	popupWindow[wp].document.body.onload = function() {window.setInterval("window.focus();", 50);}; }
    if (autoCloseTime && autoCloseTime > 0) {
    	popupWindow[wp].document.body.onbeforeunload = function() {
  			if (autoCloseTimeoutHandle) window.clearInterval(autoCloseTimeoutHandle);
    		window.onbeforeunload = null;	}  
   		autoCloseTimeoutHandle = window.setTimeout("popupWindow["+wp+"].close()", autoCloseTime * 1000); }
  	window.onbeforeunload = function() {for (var i=0;i<popupWindow.length;i++) popupWindow[i].close();}; }   
  document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;
}

/* menu fade-in */

var aColors = new Array(["e"],["d"],["c"],["b"],["a"],["9"],["8"],["7"],["6"],["5"],["4"],["3"],["2"],["1"],["0"]);
var aElements = new Array();
var iElement = 0;

function setElementsToFade(){
	var aParentElements = document.getElementById("nav1_front");
	if(aParentElements){
	 aElements = aParentElements.getElementsByTagName("a");
		if (aElements && iElement<aElements.length){
			setFadeElement(iElement, 0)
			setTimeout(function(){setElementsToFade()},150)
			iElement += 1;
		}
	}
}

function setFadeElement(iObj, iIterator){
	if (iIterator < aColors.length){
		var oTmp;
		oTmp = aElements[iObj];
		sTmp = "#" + aColors[iIterator] + aColors[iIterator] + aColors[iIterator];
		oTmp.style.color = sTmp; 
		setTimeout(function(){setFadeElement(iObj, iIterator + 1)},70);
	}
}