var bustcachevar=0 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var bustcacheparameter=""

initLayout();

/*
window.onload = init;
function init(){

	var st="";
	
	for( var i=0; i < document.links.length; i++ ){

		var link = document.links[ i ];

		if( link.target == "layout" || link.target == "calculator" ){

			link.target = "_self";
			link.href = "#";
		}
	}
}
*/

function initLayout(){

	document.writeln( '<div id="raise"></div>' );
}

function ajaxpage(url, containerid){

	var page_request = false

	if (window.XMLHttpRequest){ // if Mozilla, Safari etc

		page_request = new XMLHttpRequest()

	} else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e){}
		}
	} else {
		return false
	}

	page_request.onreadystatechange=function(){

		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){

			document.getElementById(containerid).innerHTML=page_request.responseText
			postProcessShowLayout();
		}
	}

	if (bustcachevar){ //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	}
	page_request.open('GET', url+bustcacheparameter, true)
	page_request.send(null)
}

function showLayout( id ){

	var raiser = document.getElementById( 'raise' );

	raiser.style.display='block';
	raiser.style.height=(getPageSize()[1] + 'px');
	ajaxpage( '/index.php/layout-' + id, 'raise' );


	//window.scroll( 0, 0 );
}
function postProcessShowLayout(){

	var layoutContainer = document.getElementById( 'layout_container' );
	var closeLink1 = document.getElementById( 'closelink1' );
	var closeLink2 = document.getElementById( 'closelink2' );

	var t = getPageScroll()[1]+100;
	layoutContainer.style.top = (t < 0) ? "0px" : t + "px";

	closeLink1.setAttribute( 'href', '#' );
	closeLink1.onclick = function () {hideLayout(); return false;}
	closeLink2.setAttribute( 'href', '#' );
	closeLink2.onclick = function () {hideLayout(); return false;}
					 
}
function hideLayout(){

	var y = getPageScroll()[ 1 ];

	document.getElementById( 'raise' ).style.display='none';
	document.getElementById( 'raise' ).innerHTML='';
}

function showCalculator( lang ){

	var calc = window.open( "/calculator.php?lang="+lang, "calculator", "innerWidth=650,innerHeight=600,dependent=yes,menubar=no,status=no,scrollbars=yes,resizable=yes,location=no,toolbar=no" );
	calc.focus();
}

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

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

