// JavaScript Document
function initLightbox() {	
	var objBody = document.getElementsByTagName("body").item(0);
	
	var objFade = document.createElement("div");
	objFade.setAttribute('id','fade');
	objFade.onclick = function () {changeit(); return false;}
	objBody.appendChild(objFade);
	
	var objPreview = document.createElement("div");
	objPreview.setAttribute('id','preview');
	objPreview.className = 'hide';
	objBody.appendChild(objPreview);
			
	var objIframes = document.createElement("div");
	objIframes.setAttribute('id','iframe-container');
	objIframes.style.height = '90%';
	objPreview.appendChild(objIframes);
	
	var objSidebar = document.createElement("div");
	objSidebar.style.height = '1%';
	objPreview.appendChild(objSidebar);
		
	var objEmpty = document.createElement("div");
	objEmpty.onclick = function () {changeit(); return false;}
	objEmpty.style.height = '2000px';
	objPreview.appendChild(objEmpty);
	
	var lightboxes = document.getElementsByTagName('a');
	for (var i=0;i<lightboxes.length;i++) {	
		if (lightboxes[i].className == 'lightbox') {
			lightboxes[i].id = 'e'+i;
			lightboxes[i].onclick = function () {	return changeit(this.id, this) }
			
			background = lightboxes[i].style.backgroundImage.replace("url(","").replace(")","").replace('"','').replace('"','');
			
			var objThumb = document.createElement("div");
			objThumb.onclick = function () {changeframe(false,this); return false;}
			objThumb.className = 'sm-thumbnail';
			objThumb.setAttribute('rel','e'+i);
			objSidebar.appendChild(objThumb);
			
			var objImg = document.createElement("img");
			objImg.setAttribute('src',background);
			objThumb.appendChild(objImg);
			
			//create iframes on demand
			//var objFrame = document.createElement("iframe");
			//objFrame.setAttribute('id','iframe'+i);
			//objFrame.setAttribute('frameBorder','0');
			//objFrame.className = 'preview-iframe';
			//objFrame.setAttribute('src',lightboxes[i].href);
			//objIframes.appendChild(objFrame);
		}
	}
	//browsershot testing
	//changeit('e15',false);
}

function changeframe(frame, thumb) {
	if(frame == false && thumb) {
		frame = thumb.getAttribute("rel");
	}
	
	if(frame) {
		var frames = document.getElementsByTagName('iframe');
		for (var i=0;i<frames.length;i++) {
			frames[i].style.display = 'none';
		}
		
		iframe = document.getElementById('ifram'+frame);
		
		//check if frame exists		
		if(!iframe) {
			objIframes = document.getElementById('iframe-container');
			iframeSrc = document.getElementById(frame);
			
			var objFrame = document.createElement("iframe");
			objFrame.setAttribute('id','ifram'+frame);
			objFrame.setAttribute('frameBorder','0');
			objFrame.setAttribute('allowtransparency', 'true');
			objFrame.style.background = 'transparent';
			objFrame.setAttribute('src',"document.write('<body style=\"background:#000\">');");
			objFrame.className = 'preview-iframe';			
			objFrame.setAttribute('src',iframeSrc.href);
			objIframes.appendChild(objFrame);
			
			iframe = document.getElementById('ifram'+frame);
		}
		
		iframe.style.display = 'block';
	}
	
	if(thumb) {
		var thumbs = document.getElementsByTagName('div');
		for (var i=0;i<thumbs.length;i++) {
			if (thumbs[i].className == 'sm-thumbnail') {
					thumbs[i].style.opacity = 0.5;
					thumbs[i].style.filter = 'alpha(opacity=50)';
			}
		}
	
		thumb.style.opacity = 1;
		thumb.style.filter = 'alpha(opacity=100)';
	}
}

function changeit(id, keep) {
	var preview = document.getElementById('preview');
	var fade = document.getElementById('fade');
	
	if(preview.className!='preview') {
		if(navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.match('6.0')) {
			scroll(0,0);
		}
		
		preview.className = 'preview';
		fade.className = 'fade';
		
		changeframe(id, false);
		
		if(keep) {
			var thumbs = document.getElementsByTagName('div');
			
			for (var i=0;i<thumbs.length;i++) {
				if (thumbs[i].getAttribute("rel") == id) {
					changeframe(false,thumbs[i]);
					break;
				}
			}
		}		
		document.body.style.overflow="hidden";
	} else {
		preview.className = 'hide';
		fade.className = 'hide';
		document.body.style.overflow="visible";
	}
	
	return false;
}

// 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(){
		oldonload();
		func();
		}
	}
}

addLoadEvent(initLightbox);	// run initLightbox onLoad
