//author: Christian Kroeger (ckroeger [AT] picturesafe [dot] de)

//
// Configuration
//
var layerName = 'overlay2';
var layerNames = new Array("overlay", "overlay2");
var actualLayerPos = -1;
var initialBorder = "solid #bbb 1px";
var changedBorder = "solid #787878 1px";
var oldLayerPos = 1000;


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


function initLightsOut(){
    // if does not support this - goodbye
    if (!document.getElementsByTagName) return;

    //alert("init on " +window.name);
    var objBody = document.getElementsByTagName("body").item(0);
    if(objBody == null)return;

    if( document.getElementById(layerName) == null ){
        // create overlay div
        var objOverlay = document.createElement("div");
        objOverlay.setAttribute('id',layerName);
        objOverlay.style.display = 'none';
        objOverlay.style.position = 'absolute';
        objOverlay.style.top = '0';
        objOverlay.style.left = '0';
        objOverlay.style.zIndex = '90';
        objOverlay.style.width = '100%';
        objBody.insertBefore(objOverlay, objBody.firstChild);
    }
}

setOrAddEvent(document, "window.onload", "initLightsOut();");
//addLoadEvent(initLightsOut);	// run initLightsOut onLoad

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
//function addLoadEvent(func)
//{
//	var oldonload = window.onload;
//	if (typeof window.onload != 'function'){
//    	window.onload = func;
//	} else {
//		window.onload = function(){
//		if(oldonload!=null)oldonload();
//		func();
//		}
//	}
//
//}

function startLO(){
    if (!document.getElementsByTagName) return;
    lightsOut(actualLayerPos)
}

function lightsOut(){

    if (!document.getElementsByTagName) return;
    var args = lightsOut.arguments;

    var objLoadingImage = document.getElementById('loadingImage');
    if(objLoadingImage)objLoadingImage.style.display = 'none';

    //if(window.name=="content")alert("oldLayerPos = " + oldLayerPos);

    //dispArgs("lightsOut",args);

    //alert("layerName = " + layerName+ "\nactualLayerPos = " + actualLayerPos);

    if(args==null || args.length==0){

        //alert("no args");

        switch(actualLayerPos){
            case -1:
                actualLayerPos = 0;
                break;
            default:
                actualLayerPos++;
                if(actualLayerPos>layerNames.length-1){
                    actualLayerPos=-1;
                }
                break;
        }

        notifyAllFrames(top.window, actualLayerPos, window.name);

    }
    else {
        actualLayerPos = args[0];
    }

    focusSqares();

    // HIDDENFIELD setzen
    if(window.document.WebForm){
        var fieldForLayerPos = window.document.WebForm['actualLayerPos'];
        if(fieldForLayerPos)fieldForLayerPos.value=""+actualLayerPos;
    }

    if(actualLayerPos==-1){
        hideOverlay();
	    return;
    }

    layerName = layerNames[actualLayerPos];

    //change style
    var objOverlay = document.getElementsByTagName("div")[0];
    if(objOverlay == null) return;
	objOverlay.setAttribute('id',layerName);

	var objOverlay = document.getElementById(layerName);
    if(objOverlay == null) return;

	var arrayPageSize = getPageSize();
	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}

function notifyAllFrames(frame, actualLayerPos, calledFrom){

    if (!document.getElementsByTagName) return;

    if(!frame) return;
    if(calledFrom==frame.name)return;

    //alert("in frame = " + frame.name);

    if(frame.lightsOut)frame.lightsOut(actualLayerPos);

    if(!frame.frames) return;

    var pos=0;
    /*
    if(frame.frames.length>0){
        txt="Frame " + frame.name + " contains following frames:";
        for(pos=0;pos<frame.frames.length;pos++){
            txt+="\n"+frame.frames[pos].name;
        }
        alert(txt);
    }
    //if(confirm("break?")) return;
    */

    for(pos=0;pos<frame.frames.length;pos++){
        notifyAllFrames(frame.frames[pos],actualLayerPos,calledFrom);
    }
}

function hideOverlay(){

    if (!document.getElementsByTagName) return;

    //alert("in frame = " + window.name);
    objOverlay = document.getElementById(layerName);
    if(objOverlay == null) return;
    // hide overlay
    if(objOverlay)objOverlay.style.display = 'none';

    restoreStyles();
}

function getElemByName(name){

    if (!document.getElementsByTagName) return;

    var imageEl = document.getElementsByName(name);

    if(imageEl==null||imageEl.length==0)return null;
    //alert("found " + imageEl.length + " images");
    if(imageEl==null || imageEl.length==0){
        imageEl = new Array();
        arr = document.getElementsByTagName("div");
        ix=0;
        for(i=0;i<arr.length;i++){
            if(arr[i].getAttribute("name")==name){
                /*
                alert(
                   "arr[0].style.left = " + arr[i].style.left
                   +"\narr[0].style.top = " + arr[i].style.top
                   +"\narr[0].style.width = " + arr[i].style.width
                   +"\narr[0].style.height = " + arr[i].style.heigt
                   +"\narr[0].x = " + arr[i].x
                   +"\narr[0].offsetHeight = " + arr[i].offsetHeight
                   +"\narr[0].offsetWidth = " + arr[i].offsetWidth

                );
                */
                imageEl[ix++]=arr[i];
            }

        }
        //alert("found " + imageEl.length + " images");
    }

    return imageEl;
}

function focusSqares(){

    var imageEl = getElemByName("image");

    if(imageEl==null) return;

    for(i=0;i<imageEl.length;i++){
        imageEl[i].style.zIndex=150;
        if(imageEl[i].style.border!=""){
            if(changedBorder)imageEl[i].style.border = changedBorder;
        }
    }

}

function restoreStyles(){

    var imageEl = getElemByName("image");

    if(imageEl==null) return;

    for(i=0;i<imageEl.length;i++){
        if(imageEl[i].style.border!="")imageEl[i].style.border = initialBorder;
    }

}
/////-------------------------- LIGHTSOUT -------------------------------------------------
