// Param
var _path = "../js/";
var _w = 900;
var _h = 600;

var _isIE = document.all;
var _isIE6 = _isIE && document.implementation;

__addLoadEvent(lb_init);

// ________________________________________________________
// Init lightbox
function lb_init(){
	var arr = document.getElementsByTagName("a");
	var j = 1;
	for(var i = 0; i < arr.length; i++){
		if(arr[i].rel == "lightbox"){
			arr[i].onclick = lb_display;
		}
	}
	
	var div = document.createElement('div');
	div.setAttribute("id", "filter");
	div.onclick = lb_hide;
	document.body.appendChild(div);
}

// ________________________________________________________
// Display lightbox
function lb_display(e){
	var obj = window.event ? window.event.srcElement : e ? e.target : null;
	var html = "";
	while(obj.nodeName != "A"){
		obj = obj.parentNode;
	}
	var ext = obj.href.substring(obj.href.lastIndexOf(".") + 1, obj.href.length);
	ext = ext.substring(0, ext.indexOf("?"));
	if(ext == "html" || ext == "php"){
		lb_get(obj);
	}else{
		html = "<p><a href=\"javascript:void(0);\" onclick=\"lb_hide();\"><img src=\"" + obj.href + "\" alt=\"" + obj.title + "\" /></a></p>";
		html += "<p>" + obj.title + "</p>";
		lb_create(obj, html);
	}
	
	window.onresize = lb_set_dim;
	document.getElementById('filter').style.display = "block";
	for(i = 0; i < document.getElementsByTagName('object').length; i++){
		document.getElementsByTagName('object')[i].style.visibility = "hidden";
	}
	for(i = 0; i < document.getElementsByTagName('embed').length; i++){
		document.getElementsByTagName('embed')[i].style.visibility = "hidden";
	}
	for(i = 0; i < document.getElementsByTagName('select').length; i++){
		document.getElementsByTagName('select')[i].style.visibility = "hidden";
	}
	
	return false;
}

// ________________________________________________________
// Create lightbox
function lb_create(obj, html){
	var div = document.createElement('div');
	html = "<p class=\"close\"><a href=\"javascript:void(0);\" onclick=\"lb_hide();\"><img src=\"" + _path + "/btn_close_en.gif\" alt=\"Close\" /></a></p>" + html;
	div.innerHTML = html;
	div.setAttribute("id", "lightbox");
	document.body.appendChild(div);
	
	lb_set_dim();
}

// ________________________________________________________
// Get filename
function lb_get(obj){
	var xmlHttp = lb_get_xmlHttp();
	if(xmlHttp != null){
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
				lb_create(obj, xmlHttp.responseText);
			}
		}
		xmlHttp.open("GET", obj.href, true);
		xmlHttp.send(null);
	}else{
		window.location.href = obj.href;
	}
}

// ________________________________________________________
// Get XmlHttpObject
function lb_get_xmlHttp(){
	var xmlHttp = null;
	try{
		xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
	}catch(e){
		try{
			xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
		}catch(e){
			xmlHttp = new XMLHttpRequest();
		}
	}
	return xmlHttp;
}

// ________________________________________________________
// Set lightbox dimensions
function lb_set_dim(){
	var arrSize = __getPageSize();
	var arrPos = __getPageScroll();
	var obj = document.getElementById('lightbox');
	var x = (arrSize[2] - _w) / 2 + arrPos[0];
	var y = (arrSize[3] - _h) / 2 + arrPos[1];
	
	document.getElementById('filter').style.height = arrSize[1] + "px";
	obj.style.display = "block";
	obj.style.width = _w + "px";
	obj.style.height = _h + "px";
	obj.style.top = y + "px";
	obj.style.left = x + "px";
}

// ________________________________________________________
// Hide lightbox
function lb_hide(){
	window.onresize = null;
	document.body.removeChild(document.getElementById("lightbox"))
	document.getElementById('filter').style.display = "none";
	for(i = 0; i < document.getElementsByTagName('object').length; i++){
		document.getElementsByTagName('object')[i].style.visibility = "visible";
	}
	for(i = 0; i < document.getElementsByTagName('embed').length; i++){
		document.getElementsByTagName('embed')[i].style.visibility = "visible";
	}
	for(i = 0; i < document.getElementsByTagName('select').length; i++){
		document.getElementsByTagName('select')[i].style.visibility = "visible";
	}
}

// ________________________________________________________
// Get page size
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;
	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;
	}
	// For small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	}else{
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

// ________________________________________________________
// Get page scroll
function __getPageScroll(){
	var xScroll, yScroll;
	if(self.pageYOffset){
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	}else if(document.documentElement && document.documentElement.scrollTop){
		// Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	}else if(document.body){
		// All other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

// ________________________________________________________
// Add load event
function __addLoadEvent(fct){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = fct;
	}else{
		window.onload = function(){
			if(oldonload){
				oldonload();
			}
			fct();
		}
	}
};