//=================================================================================
/*
	JH Lightbox

	5/8/2008

	Author: Tim Jones
*/
//=================================================================================
var JH_Lightbox = function() {
	var self = this;

	var Box = null;
	var Blackout = null;
	var Loading = null;

	//=============================================================================
	//Private
	var OnInit = function() {
		var Elements = document.getElementsByTagName("a");

		for(var i = 0;i < Elements.length;i++) {
			if(Elements[i].rel === "JH_Lightbox") {
				Elements[i].onclick = function() {
					if(this.href.charAt(this.href.length - 1) != "#") {
						this.ImageSrc = this.href;
						this.href = "#";
					}

					self.ViewImage(this);
				}
			}
		}

		var Content = "<img src='' alt='' id='JH_Lightbox_Image'/>";

		Box = document.createElement("div");

		Box.id = "JH_Lightbox_Box";
		Box.className = "JH_Lightbox_Box";
		Box.innerHTML = Content;

		Box.style.position = "absolute";
		Box.style.display = "none";
		Box.style.cursor = "pointer";

		Box.onclick = self.HideAll;

		Blackout = document.createElement("div");

		Blackout.id = "JH_Lightbox_Blackout";
		Blackout.className = "JH_Lightbox_Blackout";

		Blackout.style.position = "absolute";
		Blackout.style.display = "none";

		Blackout.style.left = "0px";
		Blackout.style.top = "0px";
		Blackout.style.width = "100%";
		Blackout.style.height = "100%";

		Blackout.style.backgroundColor = "black";
		Blackout.style.MozOpacity = 0.5;
		Blackout.style.filter = "alpha(opacity=50)";
		Blackout.style.opacity = 0.5;

		Blackout.onclick = self.HideAll;

		Loading = document.createElement("div");

		Loading.id = "JH_Lightbox_Loading";
		Loading.className = "JH_Lightbox_Loading";

		Loading.style.position = "absolute";
		Loading.style.display = "none";

		Loading.style.left = "0px";
		Loading.style.top = "0px";

		Loading.innerHTML = "<img src='./gfx/loader.gif' alt=''/>";

		document.body.appendChild(Loading);
		document.body.appendChild(Blackout);
		document.body.appendChild(Box);
	}

	//=============================================================================
	self.ViewImage = function(Element) {
		if(Element.ImageSrc.charAt(Element.ImageSrc.length - 1) == "/") {
			return;
		}

		Loading.style.display = "block";
		Blackout.style.display = "block";

		document.getElementById("JH_Lightbox_Image").src = "";

		self.CenterBox();

		var Picture = new Image();

		Picture.onerror = function() {
			Loading.style.display = "none";
			Blackout.style.display = "none";
		}

		Picture.onload = function() {
			Loading.style.display = "none";

			Box.style.display = "block";

			self.CenterBox();
		}

		Picture.src = Element.ImageSrc;
		document.getElementById("JH_Lightbox_Image").src = Picture.src;

		self.CenterBox();
	}

	//-----------------------------------------------------------------------------
	self.HideAll = function() {
		Box.style.display = "none";
		Blackout.style.display = "none";
		Loading.style.display = "none";
	}

	//-----------------------------------------------------------------------------
	self.CenterBox = function() {
		Box.style.left = ((DocGetWidth() / 2) - (GetWidth(Box) / 2)) + "px";
		Box.style.top = ((DocGetHeight() / 2) - (GetHeight(Box) / 2))  + "px";

		Loading.style.left = ((DocGetWidth() / 2) - (GetWidth(Loading) / 2)) + "px";
		Loading.style.top = ((DocGetHeight() / 2) - (GetHeight(Loading) / 2))  + "px";
	}

	//=============================================================================
	var DocGetWidth = function() {
		var Width = 0;

		if(typeof(window.innerWidth) == 'number') {
			//Non-IE
			Width = window.innerWidth;
		}else if(document.documentElement && document.documentElement.clientWidth) {
			//IE 6+ in 'standards compliant mode'
			Width = document.documentElement.clientWidth;
		}else if(document.body && document.body.clientWidth) {
			//IE 4 compatible
			Width = document.body.clientWidth;
		}

		return Width;
	}

	//-----------------------------------------------------------------------------
	var DocGetHeight = function() {
		var Height = 0;

		if(typeof(window.innerWidth) == 'number') {
			//Non-IE
			Height = window.innerHeight;
		}else if( document.documentElement && document.documentElement.clientHeight) {
			//IE 6+ in 'standards compliant mode'
			Height = document.documentElement.clientHeight;
		}else if( document.body && document.body.clientHeight) {
			//IE 4 compatible
			Height = document.body.clientHeight;
		}

		return Height;
	}

	//-----------------------------------------------------------------------------
	var GetWidth = function(Element) {
		if(Element.style && Element.style.width) {
			var Width = Element.style.width.replace("px", "");
				Width = Width.replace("%", "");

			return parseInt(Width);
		}else
		if(Element.offsetWidth) {
			return Element.offsetWidth;
		}else
		if(Element.innerWidth) {
			return Element.innerWidth;
		}

		return 0;
	}

	//-----------------------------------------------------------------------------
	var GetHeight = function(Element) {
		if(Element.style && Element.style.height) {
			var Height = Element.style.height.replace("px", "");
				Height = Height.replace("%", "");

			if(Height > 0) {	
				return parseInt(Height);
			}
		}

		if(Element.offsetHeight) {
			return Element.offsetHeight;
		}else
		if(Element.innerHeight) {
			return Element.innerHeight;
		}

		return 0;
	}

	//=============================================================================

	OnInit();
};

//=================================================================================
var JH_Lightbox_Instance = null;
var JH_Lightbox_OldWindowOnLoad = window.onload;
var JH_Lightbox_OldWindowOnResize = window.onresize;
var JH_Lightbox_OldWindowOnScroll = window.onscroll;

//---------------------------------------------------------------------------------
window.onload = function() {
	if(JH_Lightbox_OldWindowOnLoad) {
		JH_Lightbox_OldWindowOnLoad();
	}

	JH_Lightbox_Instance = new JH_Lightbox();
}

//---------------------------------------------------------------------------------
window.onresize = function() {
	if(JH_Lightbox_OldWindowOnResize) {
		JH_Lightbox_OldWindowOnResize();
	}

	if(JH_Lightbox_Instance) {
		JH_Lightbox_Instance.CenterBox();
	}
}

//=================================================================================
