// -----------------------------------------------------------------------------
// 
//  ShadowBox v1.0
//  by Shannon Moeller: http://www.shannonmoeller.com
//  July, 17 2005
// 
//  Inspired by and partly adapted from Lightbox v2.01 by Lokesh Dhakar, et al:
//  http://huddletogether.com/projects/lightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License:
//  http://creativecommons.org/licenses/by/2.5/
//
// -----------------------------------------------------------------------------

function ShadowBox()
{
	var m_gallery = new Array();
	var m_temp = new Image();
	var m_current = -1;
	var m_next = -1;
	var m_previous = -1;
	
	this.Initialize = Initialize;
	this.Load = Load;
	this.Previous = Previous;
	this.Close = Close;
	this.Next = Next;
	this.Update = Update;
	
	function Initialize()
	{
		// Show Them the Way
		var anchors = document.getElementsByTagName("a");
		var xx = 0;
		for (var x = 0; x < anchors.length; ++x)
		{
			if (anchors[x].getAttribute("rel") != null && anchors[x].getAttribute("rel").indexOf("shadowbox") == 0)
			{
				var imageID = anchors[x].getAttribute("href").replace(/^.*\/ph(\d+)\.(jpg|gif|png)$/i, "$1");
				m_gallery[xx] = new Array(imageID, anchors[x].getAttribute("href"), anchors[x].getAttribute("title"));
				anchors[x].href = "#gallery" + imageID;
				anchors[x].onclick = function () { myShadowBox.Load(this.href); }
				++xx;
			}
		}
		
		// What the Right Hand Doeth
		document.onkeydown = KeyboardEvents;
		
		// Prepare a Place for Shadows
		SetupMarkup();
		
		// Yay, a picture show... Oh. You Mean Now?
		if (window.location.hash.indexOf("#gallery") == 0) Load(window.location.hash);
	}
	
	function KeyboardEvents(e)
	{
		// No more arrow keys for you. You Safari. You fat. You eat more "n" and "p."
		var isSafari = navigator.userAgent.toLowerCase().indexOf("safari") > -1 ? true : false;
		
		// What Are You Doing, Dave?
		if (e == null) keypressed = event.keyCode; //ie
		else keypressed = e.which; //moz
		
		if ("27,88".indexOf(keypressed) > -1) Close(); // Ain't Gunna Be No Pictures
		if (keypressed == "78" || ("39,40".indexOf(keypressed) > -1 && !isSafari)) Next(); // More
		if (keypressed == "80" || ("37,38".indexOf(keypressed) > -1 && !isSafari)) Previous(); // Again
	}
	
	function SetupMarkup()
	{
		/*
		<div id="shadowbox" style="display: none;">
			<div id="shadowshade" style="display: none;">&nbsp;</div>
			<div id="shadowwrapper" style="display: none;"><div id="shadowcontent">
				<h3 id="shadowheader"></h3>
				<div id="shadownavigation">
					<a href="#" id="shadowprev">Previous</a>
					<a href="#" id="shadowclose">Close</a>
					<a href="#" id="shadownext">Next</a>
				</div>
				<div class="rule_down_dark"><hr/></div>
				
				<div id="shadowcontainer">
					<div id="shadowimagecontainer"><img src="" alt="" id="shadowimage"/></div>						
					<div id="shadowdescription">&nbsp;</div>
				</div>
				
				<div class="rule_up_dark"><hr/></div>
			</div></div>
		</div>
		*/
		
		var objBody = document.getElementsByTagName("body").item(0);
		
		var divShadowBox = document.createElement("div");
		divShadowBox.setAttribute('id','shadowbox');
		divShadowBox.style.display = "none";
		var tempText = '<div id="shadowshade" style="display: none;">&nbsp;</div><div id="shadowwrapper" style="display: none;"><div id="shadowcontent"><h3 id="shadowheader"></h3><div id="shadownavigation"><a href="#" id="shadowprev">Previous</a><a href="#" id="shadowclose">Close</a><a href="#" id="shadownext">Next</a></div><div class="rule_down_dark"><hr/></div><div id="shadowimagecontainer"><img src="" alt="" id="shadowimage"/></div><div id="shadowdescription">&nbsp;</div><div class="rule_up_dark"><hr/></div></div></div>';
		if (divShadowBox.innerText) { divShadowBox.innerText = tempText; }
		else divShadowBox.innerHTML = tempText;
		objBody.appendChild(divShadowBox);
		
		document.getElementById('shadowprev').onclick = Previous;
		document.getElementById('shadowclose').onclick = Close;
		document.getElementById('shadownext').onclick = Next;
	}
	
	function Load(flag)
	{
		for (var x = 0; x < m_gallery.length; ++x) if (flag.replace(/^.*#gallery(\d.*)$/, "$1") == m_gallery[x][0]) m_current = x;
		
		if (m_current > -1)
		{
			if (m_gallery[m_current][0] != null)
			{
				document.getElementById('shadowimage').style.display = "none";
				var box = document.getElementById('shadowbox');
				if (box.style.display == "none")
				{
					box.style.display = "block";
					new Effect.Appear('shadowshade', {duration: 0.175, from: 0, to: 0.97});
					setTimeout("new Effect.Appear('shadowwrapper', {duration: 0.175});", 175);
				}
				
				m_temp = new Image();
				m_temp.src = m_gallery[m_current][1];
				Update();
			}
		}
	}
	
	function Update()
	{
		if (m_temp.complete || m_temp.loaded)
		{
			var image = document.getElementById('shadowimage');
			image.src = m_temp.src;
			
			var container = document.getElementById('shadowimagecontainer');
			container.style["width"] = m_temp.width + "px";
			container.style["height"] = m_temp.height + "px";
			
			var paragraph = document.getElementById('shadowdescription');
			paragraph.style["width"] = m_temp.width + "px";
			if (m_gallery[m_current][2].length > 0)
			{
				if (paragraph.innerText) paragraph.innerText = m_gallery[m_current][2];
				else paragraph.innerHTML = m_gallery[m_current][2];
				paragraph.style.display = "block";
			}
			else paragraph.style.display = "none";
			
			var newHeader = "Image " + (m_current + 1) + " of " + m_gallery.length;
			var header = document.getElementById('shadowheader');
			if (header.innerText) header.innerText = newHeader;
			else header.innerHTML = newHeader;
			
			image.style.display = "block";
		
			if ((m_current + 1) < m_gallery.length)
			{
				document.getElementById('shadownext').style.display = "block";
				m_next = m_gallery[m_current + 1][0];
				document.getElementById('shadownext').href = "#gallery" + m_next;
				
				// Preload Next Image
				var nextImg = new Image();
				nextImg.src = m_gallery[m_current + 1][1];
			}
			else
			{
				document.getElementById('shadownext').style.display = "none";
				m_next = -1;
			}
			
			if (m_current > 0)
			{
				document.getElementById('shadowprev').style.display = "block";
				m_previous = m_gallery[m_current - 1][0];
				document.getElementById('shadowprev').href = "#gallery" + m_previous;
				
				// Preload Previous Image
				var nextImg = new Image();
				nextImg.src = m_gallery[m_current - 1][1];
			}
			else
			{
				document.getElementById('shadowprev').style.display = "none";
				m_previous = -1;
			}
		}
		else setTimeout("myShadowBox.Update();", 500);
	}
	
	function Close()
	{
		new Effect.Fade('shadowwrapper', {duration: 0.175});
		setTimeout("new Effect.Fade('shadowshade', {duration: 0.175});", 175);
		setTimeout('document.getElementById("shadowbox").style.display = "none";', 400);
		window.location.href = "#";
	}
	
	function Next()
	{
		if (m_next > -1)
		{
			window.location = "#gallery" + m_next;
			Load("#gallery" + m_next);
		}
	}
	
	function Previous()
	{
		if (m_previous > -1)
		{
			window.location = "#gallery" + m_previous;
			Load("#gallery" + m_previous);
		}
	}
}