/**
* FX-Libary
* Author: André Hoendgen <a.hoendgen@xbyte.de>
* Copyright: 2008 Xbyte GmbH
*
**/

/*
* Layer
*/

function popLayer(id) {

	poplayer = document.getElementById("poplayer_" + id);

	if(poplayer.style.display != "none") {
		poplayer.style.display = "none";
	} else {
		poplayer.style.display = "block";
	}
}

function layer() {

}

layer.prototype = {
	background : {
	"color" : "#000",
	"image" : ""
	},
	openStyle: "default",
	id: "layer",
	content: "",
	layer: null,
	contentBox: null,
	closeBox: null,
	opacity: 60,
	addLayer : function () {

		if(this.layer == null) {


			layer = document.createElement("div");
			layer.setAttribute("id","overlay");
			layer.style.display = "none";
			layer.style.width = "0";
			layer.style.background = this.background.color;
			layer.style.backgroundImage = this.background.image;
			layer.style.filter = "alpha(opacity=" + this.opacity + ");"
			layer.style.opacity = (this.opacity/100);
			layer.style.position = "absolute";
			layer.style.top = "0px";
			layer.style.left = "0px";
			layer.style.zIndex = "100000";
			layer.onclick = function() { actualLayer.hideLayer() };
			document.body.appendChild(layer);
		}

		contentBox = document.createElement("div");
		contentBox.style.display = "none";
		contentBox.setAttribute("id",this.id);
		contentBox.setAttribute("class","content-box");
		contentBox.style.position = "absolute";
		contentBox.style.top = "0";
		contentBox.style.left = "0";
		contentBox.style.zIndex = "100001";
		document.body.appendChild(contentBox);

		closeBox = document.createElement("div");
		closeBox.setAttribute("id","close_" + this.id);
		closeBox.setAttribute("class","close-box");
		closeBox.style.zIndex = "100001";
		closeBox.style.padding = "5px";
		closeBox.style.width = "573px";
		closeBox.style.position = "absolute";
		closeBox.style.background = "#fff";
		closeBox.style.textAlign = "right";
		closeBox.style.color = "#000";
		closeBox.style.opacity = 1;
		closeBox.style.filter = "alpha(opacity=100);";

		closeBoxSpan = document.createElement("span");
		closeBoxSpan.innerHTML = "X";
		closeBoxSpan.style.cursor = "pointer";
		closeBoxSpan.style.width = "46px";
		closeBoxSpan.style.textAlign = "center";
		closeBoxSpan.style.fontSize = "35px";
		closeBoxSpan.style.lineHeight = "55px";
		closeBoxSpan.style.fontWeight = "bold";
		closeBoxSpan.style.cssFloat = "right";
		closeBoxSpan.style.styleFloat = "right";
		closeBoxSpan.onclick = function() { actualLayer.hideLayer() };
		closeBox.appendChild(closeBoxSpan);

		messageBox = document.createElement("div");
		messageBox.setAttribute("id","messagebox_" + this.id);
		messageBox.style.cssFloat = "left";
		messageBox.style.styleFloat = "left";
		messageBox.style.marginRight = "0";
		closeBox.appendChild(messageBox);
		
		this.layer = document.getElementById("overlay");
		this.contentBox = document.getElementById(this.id);
		this.closeBox = closeBox;
		

	},
	updateLayer : function() {

		this.contentBox.innerHTML = this.content;

	},
	showLayer : function() {

		pageSize = getPageSize();
		this.layer.style.display = "block";
		this.contentBox.style.display = "block";
		this.layer.style.width = pageSize[0] + "px";
		this.layer.style.height = pageSize[1] + "px";

	},
	hideLayer : function() {
		this.layer.style.display = "none";
		this.contentBox.style.display = "none";
		this.layer.style.width = "0";
		this.layer.style.height = "0";
		
	},
	setContent: function(content) {
		this.content = content;
		this.updateLayer();
	},
	setContentById: function(id) {

		con = document.getElementById(id);

		if(con != null) {
			this.content = con.innerHTML;
			this.updateLayer();
		}
	},
	setContentByURL: function(id) {


	},
	setContentFrame : function(url) {
		
		pageSize = getPageSize();
		width = pageSize[0];
		marginLeft = ((width-583)/2);

		frame = document.createElement("iframe");
		frame.setAttribute("class","layerContentFrame");
		frame.src = url;
		frame.frameBorder = "0";
		frame.width = "583";
		
		if(isIE6) {
			frame.width = "573";
			height = (document.body.clientHeight-26);
		} else if (isIE7) {
			height = (document.documentElement.clientHeight-40);
		} else {
			height = window.innerHeight;
		}

		height = Math.floor((height-200));
		frame.height = height;
		
		frame.name = this.id;

		frame.style.position = "absolute";
		frame.style.left = marginLeft + "px";
		frame.style.top = "115px";
		frame.style.opacity = 1;
		frame.style.zIndex = '2000000';
		frame.style.filter = "alpha(opacity=100);";

		this.closeBox.style.left = marginLeft + "px";
		this.closeBox.style.top = (50) + "px";
		
		this.contentBox.innerHTML = "";
		this.contentBox.appendChild(frame);
		
		this.contentBox.appendChild(this.closeBox);

	}
}



//****************************************//
//             helper                     //
//****************************************//

// from huddel´s lightbox
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		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;

	//	console.log(self.innerWidth);
	//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			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;
	}

	//	console.log("xScroll " + xScroll)
	//	console.log("windowWidth " + windowWidth)

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

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

function getElementsByClassName(className) {

    results = new Array();
    eles = document.getElementsByTagName("*") 

    l = 0;
    for(i=0; i <= (eles.length-1);i++) {
        
        if(eles[i].className == className) {
            results[l] = eles[i];
        	l++;
        }    
    }
    
    return results;
}

function getElementsByAttribute(attributeName,attributeValue) {

    results = new Array();
    eles = document.getElementsByTagName("*") 

    l = 0;
    for(i=0; i <= (eles.length-1);i++) {
        
        eval("if(eles[i]." + attributeName + " == '" + attributeValue + "') { results[l] = eles[i]; }");
    }
    
    return results;
}