/*************************************************
Add to Onload
*************************************************/
function addOnload(newOnload){
	if(typeof window.onload!='function'){
		window.onload = newOnload;
	}else{
		var oldOnload = window.onload;
		window.onload = function(){
			oldOnload();
			newOnload();
		};
	}
}
/*************************************************
Fix External Blank
*************************************************/
function fixBlank(){
	var arrayLiens = document.getElementsByTagName("a");
	for(var i=0;i<arrayLiens.length;i++){
		if(/external/i.test(arrayLiens[i].getAttribute("rel"))){
			arrayLiens[i].target = "_blank";
		}
	}
}
addOnload(fixBlank);
/*************************************************
Fix PNGs
*************************************************/
function fixPng(){
	if((navigator.appVersion+'').indexOf('MSIE 6')!==-1){
		var arrayPng = document.getElementsByTagName('img');
		for(var i=0;i<arrayPng.length;i++){
			if(arrayPng[i].src.substr(arrayPng[i].src.length - 3, 3) == 'png'){
				arrayPng[i].style.width = arrayPng[i].offsetWidth;
				arrayPng[i].style.height = arrayPng[i].offsetHeight;
				arrayPng[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+arrayPng[i].src+'", sizingMethod="scale")';
				arrayPng[i].src = 'images/blank.gif';
			}
		}
	}
}
addOnload(fixPng);
/*************************************************
LightBox
*************************************************/
function showBox(){
	document.getElementById('overlay').style.height = document.body.offsetHeight + 'px';
	document.getElementById('overlay').style.display = 'block';

	document.getElementById('box').style.visibility = 'hidden';
	document.getElementById('box').style.display = 'block';

	var intWindowWidth=0,intWindowHeight=0;

	intWindowWidth = window.innerWidth?window.innerWidth:document.documentElement.clientWidth;
	intWindowHeight = window.innerHeight?window.innerHeight:document.documentElement.clientHeight;

	var intScrollX=0,intScrollY=0;

	intScrollX = window.pageXOffset?window.pageXOffset:document.documentElement.scrollLeft;
	intScrollY = window.pageYOffset?window.pageYOffset:document.documentElement.scrollTop;

	if(parseInt(document.getElementById('boxContent').offsetWidth)>700)
		document.getElementById('boxContent').width = "700";
	else
		document.getElementById('boxContent').width = parseInt(document.getElementById('boxContent').offsetWidth);

	document.getElementById('box').style.top = document.getElementById('box').offsetHeight>intWindowHeight?(intScrollY+10)+'px':(intScrollY + ((intWindowHeight - document.getElementById('box').offsetHeight) / 2))+'px';
	document.getElementById('box').style.left = document.getElementById('box').offsetWidth>intWindowWidth?(intScrollX+10)+'px':(intScrollX + ((intWindowWidth - document.getElementById('box').offsetWidth) / 2))+'px';
	document.getElementById('box').style.visibility = 'visible';
	document.getElementById('box').style.width = (parseInt(document.getElementById('boxContent').offsetWidth)) + 'px';

	if(parseInt(document.getElementById('overlay').style.height) < parseInt(document.getElementById('box').style.top) + document.getElementById('box').offsetHeight + 10){
		document.getElementById('overlay').style.height = (parseInt(document.getElementById('box').style.top) + document.getElementById('box').offsetHeight + 10) + 'px';
	}
}
function hideBox(){
	document.getElementById('overlay').style.display = 'none';
	document.getElementById('box').style.display = 'none';
	document.getElementById('box').style.width = null;
}
function initLightbox(){
	var divLightbox = document.createElement("div");
	divLightbox.innerHTML = '<div id="overlay" onclick="hideBox()"></div><table id="box" onclick="hideBox()"><tr><td style="text-align:center;"><img id="boxContent" onclick="hideBox();" /></td></table>';
	document.body.appendChild(divLightbox);
	var images = document.getElementsByTagName("img");
	var intImages = images.length;
	for(var i=0;i<intImages;i++){
		var image = images[i];
		if(image.getAttribute("alt")){
			if(image.getAttribute("alt").indexOf("box") >= 0){
				image.setAttribute("alt", "");
				image.onmouseover = function (){
					this.style.cursor = "pointer";
				}
				image.onclick = function (){
					document.getElementById('boxContent').src = this.src;
					showBox();
				}
			}
		}
	}
}
addOnload(initLightbox);