/* ------------------------------------------------------------------------------------------------------------ */
/* Lightbocks 0.5 by Kristofer Ryken																			*/
/*																												*/
/* Known issues: 																											*/
/* Sets the HTML & BODY of IE to 0																											*/
/* Doesn't work in Opera



/* To Do:																										*/
/* 																												*/
/* Add Little (X) to close lightbocks																			*/
/* Add extra modification properties (i.e. rounded corners, drop shadow, border, padding)						*/
/* Fix for Opera																								*/
/* Add support for animated transitions																			*/
/* Clean up the way it resets the contents of the bocks (make it removeChild Nodes instead of using innerHTML	*/
/* Add support for flash																						*/
/* Add support for loading external HTML snippets																*/
/* Add support for loading XML																					*/
/* Create function for resetting or clearing the overlay & bocks												*/
/* Comment better and make it degrade on even older browsers & browsers without Javascript support				*/
/* Add support for google maps integration																		*/
/* Add feature to allow for moving/resizing the bocks															*/
/* Make it an option to disable clicking the overlay to close (and to flash to white, when the user clicks it)	*/
/* Add option to make not centered																				*/
/* ------------------------------------------------------------------------------------------------------------ */

var paddycake = "0px";
var overlayColor = "#555"; /* "#bbb"; */
var overlayOpacity = "0.7";

/* var closeBocks = "<a href='#' onclick='javascript:alert()'>(X) close crazy window</a>"; */

function initLightbocks() {

	/* alert(document.getElementById('textbocks').rel); */
	var elementHTML = document.getElementsByTagName('HTML')[0];
  var elementBody = document.getElementsByTagName('BODY')[0];
	var overlay = document.createElement('DIV');

	if (document.all) { 
	  elementHTML.style.margin = "0";
		elementHTML.style.padding = "0";
		elementHTML.style.overflow = "auto";
		elementHTML.style.height = "100%";
		elementBody.style.margin = "0";
		elementBody.style.padding = "0";
		elementBody.style.overflow = "auto";
		elementBody.style.height = "100%";		/* for IE */
//		elementBody.style.margin = "0px";
		/* elementBody.style.padding = "0px"; */
	}
	
	overlay.id = "overlay";
	/* overlay.style.backgroundImage="url(lightbocks.png)"; */
	/* overlay.style.background = "transparent url(.images/overlay-blur.png) center center no-repeat"; */
	overlay.style.backgroundColor = overlayColor;
	if (document.getElementById && !document.all) { overlay.style.cursor = "pointer"; } /* to hide from IE4 */
	overlay.style.display = "none";
	overlay.style.top = "0px";
	overlay.style.left = "0px";
	overlay.style.width = "100%";
	overlay.style.height = "100%";
	overlay.style.margin = "0px";
	overlay.style.opacity = overlayOpacity; 			if (document.all) {	overlay.style.filter = "alpha(opacity=" + overlayOpacity*100 + ")"; } /* for IE */
	overlay.style.padding = "0px";
	overlay.style.position = "fixed";					if (document.all) { overlay.style.position = "absolute"; } /* for IE */
	overlay.style.zIndex = "1000";
	overlay.title = "Click to close";
	elementBody.appendChild(overlay);

	var bocks = document.createElement('DIV');
	if(paddycake){
		bocks.style.padding = paddycake;
	}
	bocks.id = "bocks";
	bocks.style.backgroundColor = "white";
	//bocks.style.backgroundColor = "transparent";
	bocks.style.cursor = "auto";
	bocks.style.display = "none";
	bocks.style.overflow = "auto";
	bocks.style.top = "50%";
	bocks.style.left = "50%";
	bocks.style.padding = "10px";
	bocks.style.position = "fixed";						if (document.all) { bocks.style.position = "absolute"; } /* for IE */
	bocks.style.zIndex = "1001";
	elementBody.appendChild(bocks);

	// Make a little (x) appear:
	// var closeBocks = document.createElement('DIV');
	// closeBocks.id = "closeBocks";
	// closeBocks.style.display = "none";
	// closeBocks.style.position = "absolute";
	// closeBocks.style.left = "-10px";
	// closeBocks.style.top = "-10px";
	// closeBocks.style.width = "20px";
	// closeBocks.style.height = "20px";
	// closeBocks.style.visibility = "hidden";
	// closeBocks.innerHTML = "<a href='javascript:closeLightbocks()' border='none'><img src='x.png' border='none' /></a>"; /* Make DOM instead of innerHTML */
	// bocks.appendChild(closeBocks);

	attachEvent(document.getElementById('overlay'), "mouseup", function() {
		closeLightbocks();
	});
}

//$ function openLightbocks(kind, what, color, w, h, blurb, a) {
function openLightbocks(kind, what, w, h) {
	if (!document.getElementById('overlay')) {
		initLightbocks();
	}
	drawOverlay();
//$	drawBocks(kind, color, what, w, h, blurb, a);
	drawBocks(kind, what, w, h);
}

function drawOverlay() {
	//$ Effect.Appear('overlay', { duration: 0.1, from:0.0, to:0.7 });
		document.getElementById('overlay').style.display = "block";
		document.getElementById('overlay').style.visibility = "visible"; 
}

//$ function drawBocks(kind, color, what, w, h, blurb, a) {
function drawBocks(kind, what, w, h) {

	//$ if (color) { 
		//$ document.getElementById('overlay').style.backgroundColor = color;
		//document.getElementById('overlay').style.background = "transparent url(./images/overlay-blur.png) center center no-repeat";
	//$ }

	var bocks = document.getElementById('bocks');

	bocks.className = "lightBocks";
	//bocks.style.border = "1px dotted #ff0000";
	bocks.style.height = h + "px";
	bocks.style.marginTop = -h/2 + "px";
	bocks.style.marginLeft = -w/2 + "px";

	bocks.style.width = w + "px";

	if ( bocks.hasChildNodes() ) {
		bocks.innerHTML = "";
		/* var kids = bocks.childNodes; */
		/*	for ( i=1; i < kids.length; i++ ) { alert(kids[i]);	kids.parentNode.removeChild(kids[i]); } */
		/* kids.parentNode.removeChild(kids); */
	}

	if ( kind == "text" ) {
		var e = document.createElement('P');
		var t = document.createTextNode(what);
		/* e.innerHTML = what; */
		bocks.appendChild(e);
		e.appendChild(t);
	}
	
	if ( kind == "image" ) {
		bocks.style.backgroundImage = "url('loading.png')";								
		bocks.style.backgroundRepeat = "no-repeat";
		bocks.style.backgroundPosition = "center center";
		if (document.getElementById && !document.all) { bocks.style.cursor = "pointer"; } /* to hide from IE4 */

		function loadImage() {
			bocks.innerHTML = "";														
			bocks.appendChild(e);
			bocks.style.backgroundImage = "none";
		}
		
		var e = document.createElement('IMG');											
		e.onload = loadImage;
		e.onerror = loadImage;
		e.src = what;
		e.width = w;
		e.height = h;
		//e.title = "Click to Close";
		//$ e.title = blurb;		
		
		attachEvent(document.getElementById('bocks'), "mouseup", function() {
			closeLightbocks();
		});
	}

	if ( kind == "link" ) {
		//$ bocks.style.backgroundImage = "url('loading.png')";								
		//$ bocks.style.backgroundRepeat = "no-repeat";
		//$ bocks.style.backgroundPosition = "center center";
		if (document.getElementById && !document.all) { bocks.style.cursor = "pointer"; } /* to hide from IE4 */

		function loadImage() {
			bocks.innerHTML = "";	
			bocks.appendChild(d);	
			d.appendChild(e);												
			//bocks.appendChild(e);
			bocks.style.backgroundImage = "none";
		}

		var d = document.createElement('A');
		d.border = "none";
		d.href = a;
		d.display = "block";
		d.width = w;
		d.height = h;
		d.target = "_blank";
		//d.title = "Click to open in a new window.";
		
		var e = document.createElement('IMG');	
		e.border = "none";									
		e.onload = loadImage;
		e.onerror = loadImage;
		e.src = what;
		e.width = w;
		e.height = h;
		//e.title = "Click to Close";		
		e.title = "Click to open in a new window.";
		
		attachEvent(document.getElementById('bocks'), "mouseup", function() {
			closeLightbocks();
		});
	}

	
	
	if (kind == "html") {
		bocks.innerHTML = what;
	}

	if (kind == "external") {
		var e = document.createElement('IFRAME');
		e.frameBorder = "0";
		//$ e.style.background = "url('loading.gif') center center no-repeat";	
		e.style.height = "100%";
		e.style.width = "100%";
		e.style.scrolling = "auto";
		e.src = what;
		bocks.appendChild(e);
	}

		document.getElementById('bocks').style.display = "block";
		document.getElementById('bocks').style.visibility = "visible";
		//$ Effect.Appear('bocks', { duration: 0.25 });

	//document.getElementById('closeBocks').style.display = "block";
	//document.getElementById('closeBocks').style.visibility = "visible";
}

function closeLightbocks() {
	eraseOverlay();
	eraseBocks();
  // var elementHTML = document.getElementsByTagName('HTML')[0];
  //   var elementBody = document.getElementsByTagName('BODY')[0];
  // elementHTML.style.overflow = "auto";
  // elementBody.style.overflow = "auto";
}

function eraseOverlay() {
		document.getElementById('overlay').style.display = "none";
		document.getElementById('overlay').style.visibility = "hidden";
	//$ Effect.Fade('overlay', { duration: 0.25 });
}

function eraseBocks() {
		document.getElementById('bocks').style.display = "none";
		document.getElementById('bocks').style.visibility = "hidden";
	//$ Effect.Fade('bocks', { duration: 0.1 });
}






/* The right way to add load events. By Simon Willison */
/* ------------------------------------------------------------------------------------------ */

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}




/* Better than window.onload and init b/c it can add multiple functions */
/* ------------------------------------------------------------------------------------------ */

/* addLoadEvent(initLightbocks); */




/* attach an event properly */
/* ------------------------------------------------------------------------------------------ */

function attachEvent(element, type, event)	{	
  if(element.attachEvent)	{
    element.attachEvent('on' + type, event);
	}
  else	{
   element.addEventListener(type, event, false);
	}
}
