/* Begin: TragicMedia.com LightBox JS
/* created 4/08 by Rich Rudzinski
/*------------------------------------------*/

/* Begin: LightBox
/*-------------------*/

/*
<script type="text/javascript">
	addLightBoxEvent( )  //automatically finds all links with a class of lightBox. Image tags must be wrapped in <a> with the href of the full size image, non-image tags must have a rel of the container id of the ajaxed page.
</script>

add to all ajaxed pages:
<script type="text/javascript">
	manageNonImage();
</script>
*/

//global variables
var LBelement;

function addLightBoxEvent() {
	/*if(navigator.appVersion.match('MSIE 6.0')) {
		return;
	}*/
	var lightBoxLinks = getElementsByClassName('lightbox', 'a');
	var count = 1;
	createLightBox();
	for(i=0; i<lightBoxLinks.length; i++) {
		lightBoxLinks[i].onclick = function() {
			var overlay = $('overlay');
			if(overlay.style.display == 'none') {
				overlay.style.display = 'block';
				LBelement = this;
				makeElement(this.parentNode.parentNode.parentNode.id);
			}			
			return false;
		}
	}
	window.onresize = function() { resizeLightBox(); }				
}
/* create overlay container */
function createLightBox() {
	if(!$('overlay')) {
		var overlay = document.createElement('div');
		overlay.id = 'overlay';
		if(document.all) {
			overlay.style.width = document.documentElement.clientWidth + 'px';
			overlay.style.height = document.documentElement.clientHeight + 'px';
			if(navigator.appVersion.match('MSIE 6.0')) 
				overlay.style.height = document.body.clientHeight + 27 + 'px';
		} else {
			overlay.style.width = window.innerWidth + 'px';
			overlay.style.height = window.innerHeight + 'px';
		}
		overlay.style.display = 'none';
		if(document.all) {
			var tempCont = document.body.all[0];
			document.body.insertBefore(overlay, tempCont);
		} else {
			document.body.insertBefore(overlay, document.body.firstChild);
		}
	}
}
/* reevaluate height/width on resize */
function resizeLightBox() {
	var overlay = $('overlay');
	var LBCont = $('LBContainer');
	if(overlay.style.display == 'block') {
		if(document.all) {
			overlay.style.width = document.documentElement.clientWidth + 'px';
			overlay.style.height = document.documentElement.clientHeight + 'px';
			if(navigator.appVersion.match('MSIE 6.0')) 
				overlay.style.height = document.body.clientHeight + 27 + 'px';
		}
		else {
			overlay.style.width = window.innerWidth + 'px';
			overlay.style.height = window.innerHeight + 'px';
		}
		while(LBCont.childNodes.length > 0) {
			LBCont.removeChild(LBCont.firstChild);
		}	
		makeElement(LBelement.parentNode.parentNode.parentNode.id);
	}
}
function getViewport()
{
var viewport = [];
// standars compliant browsers
if(window.innerHeight)
{
viewport["width"] = window.innerWidth;
viewport["height"] = window.innerHeight;
}
// IE6 in standards mode. IE6 in quirks mode won't work
else if(document.documentElement.clientHeight)
{
viewport["width"] = document.documentElement.clientWidth;
viewport["height"] = document.documentElement.clientHeight;
}
return viewport;
}
/* creates the lightbox elements, and handles their display */
function makeElement(eCont) {
	var overlay = $('overlay');
	var adjustPos;
	var contHeight;
	var imgTag;
	var closeBtn;
	if(!$('LBContainer')) {
		var LBCont = document.createElement('div');
		LBCont.id = 'LBContainer';
		overlay.appendChild(LBCont);
	} else {
		var LBCont = $('LBContainer');
		if(document.all) {
			overlay.style.width = document.documentElement.clientWidth + 'px';
			overlay.style.height = document.documentElement.clientHeight + 'px';
			if(navigator.appVersion.match('MSIE 6.0')) 
				overlay.style.height = document.body.clientHeight + 27 + 'px';
		}
		else {
			overlay.style.width = window.innerWidth + 'px';
			overlay.style.height = window.innerHeight + 'px';
		}
	}
	LBCont.style.visibility = 'hidden';
	overlay.onclick = function() { if(this.firstChild.getAttribute('rel') != 'noClick') {return closeLightBox();} }
	LBCont.onclick = function() { this.setAttribute('rel', 'noClick'); setTimeout('$("LBContainer").setAttribute("rel", "");', 50); }
	//create loading image
	if(!$('loading')) {
		var loading = document.createElement('img');
		loading.id = 'loading';
		loading.src = '/images/loading.gif';
		loading.style.top = parseInt(overlay.style.height.replace('px', ''))/2 - 16 + 'px'; //30 is half the height/width of the loading image
		loading.style.left = parseInt(overlay.style.width.replace('px', ''))/2 - 16 + 'px';
		overlay.appendChild(loading);
	} else {
		var loading = $('loading');
		loading.style.visibility = 'visible';
	}
	/* create container */
	var divCont = $(eCont);
	var LBDiv = document.createElement('div');
	LBDiv.setAttribute('id', 'LBDiv');
	LBDiv.style.width = '720px';
	LBDiv.style.height = '677px';
	var topCont = document.createElement('div');
	topCont.setAttribute('id', 'topCont');
	LBDiv.appendChild(topCont);
	var clientHeader = document.createElement('h3');
	clientHeader.innerHTML = divCont.getElementsByTagName('h3')[0].innerHTML;
	topCont.appendChild(clientHeader);
	/* create image array */
	var imageArray = divCont.getElementsByTagName('img');
	var elArray = [];
	var caption = document.createElement('p');
	caption.setAttribute('id','caption');
	for(i=0; i<imageArray.length; i++) {
		if(i == 0) continue;
		var temp = new Image();
		temp.src = imageArray[i].src;
		temp.alt = imageArray[i].alt;
		if(i == 1) {
			temp.className += ' show';
			if(temp.alt == '' || temp.alt == undefined) temp.alt = '';
			caption.innerHTML = temp.alt;
		}
		temp.className += ' image' + i;
		temp.style.width = '686px';
		temp.style.height = '450px';
		elArray.push(temp);
	}
	for(m=0; m < elArray.length; m++) {
		topCont.appendChild(elArray[m]);
	}
	/* create bottom section */
	var bottomCont = document.createElement('div');
	bottomCont.setAttribute('id', 'bottomCont');
	LBDiv.appendChild(bottomCont);
	bottomCont.appendChild(caption);
	var imgLinkCont = document.createElement('div');
	imgLinkCont.id = 'imgLinkCont';
	bottomCont.appendChild(imgLinkCont);
	var imageLinks = [];
	for(n=0; n < elArray.length; n++) {
		imageLinks.push(document.createElement('a'));
		imageLinks[n].setAttribute('href', elArray[n].src);
		if(n == 0) imageLinks[n].className = 'current';
		imageLinks[n].innerHTML = n + 1;
		imageLinks[n].onclick = function() {
			var cont = $('topCont');
			var imgArray = cont.getElementsByTagName('img');
			var linkArray = this.parentNode.getElementsByTagName('a');
			for(i=0; i<imgArray.length; i++) {
				if(imgArray[i].className.match('show')) imgArray[i].className = imgArray[i].className.replace('show', '');
				if(linkArray[i].className.match('current')) linkArray[i].className = linkArray[i].className.replace('current', '');
				if(imgArray[i].src == this.href /*&& !imgArray[i].className.match('show')*/) {
					imgArray[i].className += ' show';
					if(temp.alt) {
						$('caption').innerHTML = imgArray[i].alt;
					} else {
						$('caption').innerHTML = '';
					}
					linkArray[i].className += ' current';
				}
			}
			return false;
		}
		imageLinks[n].className += ' button';
		imgLinkCont.appendChild(imageLinks[n]);		
		
	}
	var hr = document.createElement('img');
	hr.src = '/images/hr_simple.jpg';
	hr.setAttribute('id', 'hr');
	bottomCont.appendChild(hr);
	var clientSummary = document.createElement('p');
	clientSummary.innerHTML = divCont.getElementsByTagName('p')[0].innerHTML;
	clientSummary.id = 'clientSummary';
	bottomCont.appendChild(clientSummary);
	LBCont.appendChild(LBDiv);	
	manageNonImage();
}
function manageNonImage() {
	var LBCont = $('LBContainer');
	var overlay = $('overlay');
	var loading = $('loading');
	var ajaxCont = $(LBCont.firstChild);
	var adjustPos;
	var MAXheight = parseInt(overlay.style.height.replace('px', ''));
	var MAXwidth = parseInt(overlay.style.width.replace('px', ''));	
	LBCont.style.width = ajaxCont.offsetWidth + 'px';
	LBCont.style.height = ajaxCont.offsetHeight + 'px';
	if(overlay.style.height < LBCont.style.height) {
		var height = getViewport();
		LBCont.firstChild.style.height = height['height'] + 'px';
		LBCont.style.height = height['height'] + 'px';
		LBCont.style.overflowY = 'scroll';
		LBCont.style.overflowX = 'hidden';
		LBCont.style.top = 0;
	} else {
		LBCont.style.overflowY = 'visible';
		LBCont.style.overflowX = 'visible';
		if(MAXheight > ajaxCont.offsetHeight) {
			adjustPos = (MAXheight - ajaxCont.offsetHeight)/2; // center vertically
		} else {
			adjustPos = 0; // center vertically
		}
		LBCont.style.top = adjustPos + 'px';
		LBCont.style.width = ajaxCont.offsetWidth + 'px';
		LBCont.style.height = ajaxCont.offsetHeight + 'px';
	}
	makeButtons(LBCont); 
	loading.style.visibility = 'hidden';
	LBCont.style.visibility = 'visible';
}
/* functions makes the close, next, previous buttons */
function makeButtons(wrapper) {
	var parentId = LBelement.parentNode.id;
	var siblings = getElementsByClassName('lightbox', '*', parentId);
	for(i=0; i<siblings.length; i++) {
		if(siblings[i] == LBelement) {
			var elNum = i;
		}
	}
	//create close button
	closeBtn = document.createElement('a'); // create close button
	closeBtn.setAttribute('id', 'closeBtn');
	closeBtn.setAttribute('href', '#');
	closeBtn.innerHTML = 'Close &nbsp;X';
	closeBtn.title = 'CLOSE';
	//append close button
	wrapper.appendChild(closeBtn);
	closeBtn.style.right = (closeBtn.offsetWidth)/2 + 'px';
	closeBtn.style.top = closeBtn.offsetHeight + 'px';
	//manage click event
	closeBtn.onclick = function() { return closeLightBox(); }
	if(elNum != 0) {
		//create left arrow
		arrowLeft = document.createElement('a');
		arrowLeft.setAttribute('id', 'arrowLeft');
		arrowLeft.className += ' button';
		arrowLeft.setAttribute('href', '#');
		arrowLeft.innerHTML = 'PREVIOUS CLIENT';
		var arrowLImg = document.createElement('img');
		arrowLImg.src = '/images/ico_linkArrowLeft.jpg';
		arrowLeft.appendChild(arrowLImg);
		arrowLeft.title = 'PREVIOUS CLIENT';
		//append left arrow
		wrapper.appendChild(arrowLeft);
		//manage click event
		arrowLeft.onclick = function() {
			var siblings = getElementsByClassName('lightbox', '*', parentId);
			for(i=0; i<siblings.length; i++) {
				if(siblings[i] == LBelement) {
					var elNum = i;
				}
			}
			var sibRef = siblings[(elNum-1)];
			var LBCont = $('LBContainer');
			while(LBCont.childNodes.length > 0) {
				LBCont.removeChild(LBCont.firstChild);
			}			
			LBelement = siblings[(elNum-1)];
			makeElement(sibRef.parentNode.parentNode.parentNode.id);
			return false;
		}
	} 
	if(elNum != siblings.length - 1) {
		//create right arrow
		arrowRight = document.createElement('a');
		arrowRight.setAttribute('id', 'arrowRight');
		arrowRight.className += ' button';
		arrowRight.setAttribute('href', '#');
		arrowRight.innerHTML = 'NEXT CLIENT';
		var arrowRImg = document.createElement('img');
		arrowRImg.src = '/images/ico_linkArrowRight.jpg';
		arrowRight.appendChild(arrowRImg);
		arrowRight.title = 'NEXT CLIENT';
		//append right arrow
		wrapper.appendChild(arrowRight);
		//manage click event
		arrowRight.onclick = function() {
			var siblings = getElementsByClassName('lightbox', '*', parentId);
			for(i=0; i<siblings.length; i++) {
				if(siblings[i] == LBelement) {
					var elNum = i;
				}
			}
			var sibRef = siblings[(elNum+1)];
			var LBCont = $('LBContainer');
			while(LBCont.childNodes.length > 0) {
				LBCont.removeChild(LBCont.firstChild);
			}			
			LBelement = siblings[(elNum+1)];
			makeElement(sibRef.parentNode.parentNode.parentNode.id);
			return false;
		}
	} 
}
/* removes elements inside overlay container */
function closeLightBox() {
	var overlay = $('overlay');
	var LBCont = $('LBContainer');
	overlay.style.display = 'none';
	var LBCont = $('LBContainer');
	while(LBCont.childNodes.length > 0) {
		LBCont.removeChild(LBCont.firstChild);
	}
	return false;
}

addAnEvent(window, 'load', addLightBoxEvent);

/*------------- BEGIN: addAnEvent  ----------------*/
function addAnEvent(node, type, listener) {
	if(!(node=$(node))) {return false;}
	if(node.addAnEventListener) {
		// W3C method
		node.addAnEventListener(type, listener, false);
		return true;
	} else if(node.attachEvent) {
		//MSIE method
		node['e' + type + listener] = listener;
		node[type + listener] = function() {
			node['e' + type + listener](window.event);
		}
		node.attachEvent('on' + type, node[type + listener]);
		return true;
	}
	//Didn't have either so return false
	return false;
}
/*------------- END: addAnEvent  ----------------*/
/*------------- BEGIN: getElementsByClassName  ----------------*/
function getElementsByClassName(className, tag, parent) {
	parent = parent || document;
	if(!(parent = $(parent))) {return false;}
	// Locate all the matching tags
	var allTags = (tag == "*" && parent.all) ? parent.all : parent.getElementsByTagName(tag);
	var matchingElements = new Array();
	// Create a regular expression to determine if the className is correct
	className = className.replace(/\-/g, "\\-");
	var regex = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var element;
	// Check each element
	for(var i=0; i<allTags.length; i++) {
		element = allTags[i];
		if(regex.test(element.className)) {
			matchingElements.push(element);
		}
	}
	// Return any matching elements
	return matchingElements;
}

/*------------- END: getElementsByClassName  ----------------*/
/*------------- BEGIN: $  ----------------*/
function $() {
	var elements = new Array();
	// Find all elements supplied as arguments
	for(var i=0; i<arguments.length; i++) {
		var element = arguments[i];
		//If the argument is a string assume it's an id
		if(typeof element == 'string') {
			element = document.getElementById(element);
		}
		// If only one argument was supplied, return the element immediately
		if(arguments.length == 1) {
			return element;
		}
		// Otherwise add it to the array
		elements.push(element);
	}
	// Return the array of multiple requested elements
	return elements;
}
/*------------- END: $  ----------------*/
