//=================================================================================
/*
	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.innerHTML += "<span style='position:absolute;top:2px;left:2px;cursor:pointer;font-weight:bold;text-decoration:underline;'>CLOSE</span>";

		Box.style.position = "absolute";
		Box.style.display = "none";
		Box.style.cursor = "pointer";
		Box.style.backgroundColor = "#ffffff";
		Box.style.border = "1px solid #000000";
		Box.style.zIndex = "5000";

		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.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) {
		Loading.style.display = "block";
		Blackout.style.display = "block";

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

		self.CenterBox();

		var Picture = new Image();

		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;

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

	JH_Lightbox_Instance = new JH_Lightbox();
}

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