/*
 * HIT Cyclone Platform, PERL Power Web Platform.
 * Copyright (C) 2006-2008, HIT IT Consulting & Services, individual authors
 * as indicated by the @author tags.
 *
 * This is CLOSED SOURCE SOFTWARE. You may neither read nor copy
 * and / or distribute any portion of this software without prior
 * permission by HIT IT Consulting & Services.
 *
 * @author Ben Hildebrand (ben.hildebrand [at] hit-consult.net
 */

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//

function getKey( e ){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	// alert( "code: " + e.which );
	
	if(key == 'x'){ hideModalBox(); }
}


//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }

//
// addLoadEvent()
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

function addClickEvent(func)
{	
	var oldonclick = document.onclick;
	if( typeof document.onclick != 'function' ){
    	document.onclick = func;
	} else {
		document.onclick = function(){
			oldonclick();
			func();
		}
	}
}


function deflateJSON( json ) {
	var data = eval('(' + unescape( json ) + ')');
	
	return data;
}

function getNaturalHeight( img ) {
	if( img.naturalHeight ) {
		return img.naturalHeight;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.height;
	}
}

function getNaturalWidth( img ) {
	if( img.naturalWidth ) {
		return img.naturalWidth;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.width;
	}
}

function navigateTo( navPoint, args ) {
	var newURL = navURI( navPoint, args );
	
	document.location.href = newURL;
}

function dynAction( action ) {
	document.location.href = "/run/" + dynAction;
}

function navURI( navPoint, args ) {
	var URI = "/site/" + navPoint;
	
	if( args != null ) {
		URI = URI + "?" + $H( args ).toQueryString();
	}
	
	return URI;
}

function _showError( errorText, boxOptions ) {
	if( !boxOptions ) {
		boxOptions = new Object();
	}

	boxOptions.msgKey = errorText;
	boxOptions.type = "error";

	showWindow( 
		"FEHLER", 
		navURI( "standardPopup", boxOptions ),
		{
			width: 500,
			height: 200
		}
	);
}

function _showInfo( infoText, boxOptions ) {
	showWindow( 
		"Information", 
		navURI( "standardPopup", 
			{
				type: "info",
				msgKey: infoText
			} 
		),
		{
			width: 500,
			height: 200
		} 
	);
}

function showWindow( titleRef, urlRef, extraOptions ) {
	Windows.overlayShowEffectOptions = null;
	Windows.overlayHideEffectOptions = null;
	
	var windowOptions = 
		{
			// className: "alphacube", 
			title: titleRef, 
			width:800, 
			height:600,
			maximizable: false,
			minimizable: false,
			draggable: false,
			resizable: false, 
			url: urlRef, 
			showEffect:  Element.show,
			hideEffect:  Element.hide
		};
		
	if( extraOptions ) {
			
		if( extraOptions.width ) {
			windowOptions.width = extraOptions.width;
		}
	
		if( extraOptions.height ) {
			windowOptions.height = extraOptions.height;
		}
	
	}
	
	var win = new Window( windowOptions ); 
	
	win.showCenter( true ); 
}

