//iLightBox v 0.2
//Modified Version
//DONT USE ON OTHER PROJECTS THAN richardharvell.com
//Lightbox Script with iFrame Support
//(c) 08/2010 by Stefan Oechslein

Event.observe(window, 'load', function(event){ //On Page load, initialize components 
	var links = $$('a'); //Get all <a> tags  in the document
	for(var i = 0; i < links.length; i++){ //for each tag...
		if(links[i].rel == 'iBox'){ //check if rel argument is iBox							
			links[i].onclick = function () {
				showLB(this.href); //Show Lightbox with URL
				return false;
			}};
		}
});

function showLB(linkURL){
	var LBox_height = 350; 
	var LBox_width = 280;
	
	var LBox_pos_x = (getDocWidth()-LBox_width) / 2;
	var LBox_pos_y = (getDocHeight()-LBox_height) / 2;
	
	//Create the components
	var fadeDIV = Builder.node('div', {id:'LB_fade_div', style:'position:absolute; margin:0px 0px 0px 0px; background-color:#000000; border:none; display:none; z-index:1; top:0; left:0; opacity:0; width:100%;'});
	var LB = Builder.node('div', {id:'iLightBox', style:'position:absolute; background-color:#FFFFFF; border:3px solid #999999; z-index:2; visibility:hidden; height:'+LBox_height+'px; width:'+LBox_width+'px;'});
		var headerDIV = Builder.node('div', {id:'LB_header_div', style:'background-color:#DEDEDE; text-align:right; width:'+LBox_width+'px; height:15px; cursor:move;'});
			var headerIMG = Builder.node('img', {src:'http://www.richardharvell.com/img/close.png', alt:'Close', height:'15', width:'15', style:'cursor:pointer', onclick:'closeLB();'});
    	var mainDIV = Builder.node('div', {id:'LB_main_div', style:'display:none'});
			var iframe = Builder.node('iframe', {id: 'LB_iframe', src:linkURL, scrolling:'no', border:'0', framborder:'0', style:'height:'+(LBox_height-15)+'px; width:'+(LBox_width)+'px; border:none;'});
	
	headerDIV.appendChild(headerIMG);
	LB.appendChild(headerDIV);
	
	mainDIV.appendChild(iframe);
	LB.appendChild(mainDIV);
	
	document.body.appendChild(LB);
	document.body.appendChild(fadeDIV);

	
	//Show the components
	$('LB_fade_div').style.display = 'block';
	centerDiv(LBox_width, LBox_height, 'iLightBox');
	$('LB_fade_div').style.height = getFullDocHeight() +'px';
	new Effect.Opacity('LB_fade_div', {from:0.0, to:0.8, duration:1}); //Darken the Background
	$('iLightBox').style.visibility = 'visible';
	new Effect.Appear('LB_main_div', {from:0.0, to:1.0, duration:0.5}); //Show the Content
	new Draggable('iLightBox', {handle:'LB_header_div'});
}

function closeLB(){
	new Effect.Fade('iLightBox', {from:1.0, to:0.0, duration:0.5, queue:'front', afterFinish:function(){
																									  $('iLightBox').style.visibility = 'hidden';
																									  }}); //Hide the Content
	new Effect.Opacity('LB_fade_div', {from:0.8, to:0.0, duration:1, queue:'end', afterFinish:function(){
																							$('LB_fade_div').style.display = 'none';
																							$('iLightBox').parentNode.removeChild($('iLightBox'));
																							$('LB_fade_div').parentNode.removeChild($('LB_fade_div'));
																						  }}); //Lighten the Background
}


function getFullDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
function getFullDocWidth() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
}

function getDocHeight(){
	var viewportheight;
	if (typeof window.innerHeight != 'undefined'){
		viewportheight = window.innerHeight;
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0){
		viewportheight = document.documentElement.clientHeight;
	}else{
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	return viewportheight;
}

function getDocWidth(){
	var viewportWidth;
	if (typeof window.innerWidth != 'undefined'){
		viewportWidth = window.innerWidth;
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		viewportWidth = document.documentElement.clientWidth;
	}else{
		viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
	}
	return viewportWidth;
}

function centerDiv(Xwidth,Yheight,divid) {
	var scrolledX, scrolledY;
	if(self.pageYOffset){
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
	}else if(document.documentElement && document.documentElement.scrollTop){
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	}else if(document.body){
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}

	var centerX, centerY;
	if(self.innerHeight){
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
	}else if(document.body){
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
	}

	var leftOffset = scrolledX + (centerX - Xwidth) / 2;
	var topOffset = scrolledY + (centerY - Yheight) / 2;

	var o = $(divid);
	var r = o.style;
	r.top = topOffset + 'px';
	r.left = leftOffset + 'px';
	r.display = "block";
} 


