var Event = function()
{
	var EventColl = [];
	var counter = 0;
	
	function Event()
	{
		var self = this;
		
		this.addEvent = function(oElement, handler, callback, capture)
		{
			if (oElement.addEventListener)
			{
				oElement.addEventListener(handler, callback, capture);
			}
			else if(oElement.attachEvent)
			{
				var prop = [handler, callback].join("");
				if (oElement[prop]) return;
				
				if (!EventColl[oElement._eventid])
				{
					oElement._eventid = counter++;
					EventColl[oElement._eventid] = {'oElement' : oElement};
				}
				EventColl[oElement._eventid][prop] = [handler, callback];
				
				oElement[prop] = function()
				{
					callback.call(oElement, event);
				}
				oElement.attachEvent('on' + handler, oElement[prop]);
			}
		}
		
		this.removeEvent = function(oElement, handler, callback, capture)
		{
			if (oElement.removeEventListener)
			{
				oElement.removeEventListener(handler, callback, capture);
			}
			else if (oElement.detachEvent)
			{
				var prop = [handler, callback].join("");
				if (oElement[prop])
				{
					oElement.detachEvent('on' + handler, oElement[prop]);
					oElement[prop] = null;
					EventColl[oElement._eventid][prop] = null;
				}
			}
		}
		
		function breakEventLeak()
		{
			var curr = null;
			var j = null;
			for (var i = 0, len = EventColl.length; i < len; i++)
			{
				curr = EventColl[i];
				for (j in curr)
				{
					if (j != 'oElement')
					{
						self.removeEvent(curr.oElement, curr[j][0], curr[j][1]);
					}
				}
				curr.oElement._eventid = null;
			}
			EventColl = null;
			counter = null;
		}
		if (window.attachEvent) window.attachEvent('onunload', breakEventLeak);
	}
	return new Event();
}();

Event.preventDefault = function(event)
{
	event.returnValue = false;
	if (event.preventDefault)
	{
		event.preventDefault();
	}
}

Event.stopPropagation = function(event)
{
	if (event.stopPropagation)
	{
		event.stopPropagation();
		return;
	}
	event.cancelBubble = true;
}

/* 
	- Implements optgroup
	- Implements remove Option
	- Implements parse html select
	-Fix selected for IE
	-Implements disabled select
*/


function getOffsetElement(element)
{
	var top = left = 0;
	while(element)
	{
		top += element.offsetTop;
		left += element.offsetLeft;
		element = element.offsetParent;
	}
	
	var offset = [left, top];
	return offset;
}

function OptionItem(properties)
{
	var option = document.createElement('option');
	for (var i in properties)
	{
		option[i] = properties[i];
	}
	
	return option;
}

var ComboBox = function()
{
	var instance = false;
	var instance_coll = [];
	
	function ComboBox(properties, html_select)
	{		
		var custom_combo_box = document.createElement('div');
			custom_combo_box.className = properties.className || 'combo_box';
			if (properties.width) custom_combo_box.style.width = properties.width + 'px';
			if (properties.height) custom_combo_box.style.height = properties.height + 'px';
			custom_combo_box.innerHTML = (properties.src ? '<img src="' + img_combo + '" alt="" style="float: right; height: 100%" />' : '') + '<label></label>';
			
		var combo_box = html_select || document.createElement('select');
			combo_box.className = 'hidden_component';
			if (properties.name) combo_box.name = properties.name;
			if (properties.id) combo_box.id = properties.id;
			if (properties.onChange || html_select.onchange) combo_box.onchange_listener = html_select.onchange || properties.onChange;
			combo_box.custom_combo_box = custom_combo_box;
			custom_combo_box.select = combo_box;
		var curr_parent = combo_box;
		
		this.addOption = function(OptionItem)
		{
			if (window.addEventListener)
			{
				curr_parent.appendChild(OptionItem);
				return;
			}
			combo_box.add(OptionItem);
		}
		
		/*this.addOptgroup = function(label_text)
		{
			curr_parent = document.createElement('optgroup');
			curr_parent.label = label_text || '';
			combo_box.appendChild(curr_parent);
		}*/
		
		this.flushOptions = function()
		{
			combo_box.options.length = 0;
		}
		
		this.renderComboBox = function(parentCombo)
		{
			var parent_combobox = document.getElementById(parentCombo) || parentCombo;
			ComboBox.setUpdateText.call(combo_box, null, true);
			
			if (combo_box.addEventListener)
			{
				combo_box.addEventListener('keyup', combo_box.blur, false);
				combo_box.addEventListener('keyup', combo_box.focus, false);
			}
			
			Event.addEvent(combo_box, 'change', ObserverDropDown.moveKeySelected, false);
			Event.addEvent(combo_box, 'change', ComboBox.setUpdateText, false);
			Event.addEvent(combo_box, 'keydown', ObserverDropDown.checkKey, false);
			Event.addEvent(combo_box, 'focus', ComboBox.focus, false);
			Event.addEvent(combo_box, 'blur', ComboBox.blur, false);
			
			parent_combobox.appendChild(combo_box);
			
			Event.addEvent(custom_combo_box, 'click', ObserverDropDown.showDropDown, false);
			
			parent_combobox.appendChild(custom_combo_box);
		}
		
		if (!instance)
		{
			if (document.body)
			{
				ObserverDropDown.initDropDown();
			}
			else 
			{
				Event.addEvent(window, 'load', ObserverDropDown.initDropDown, false);
			}
			
			instance = true;
		}
		
		if (!window.XMLHttpRequest)
		{
			instance_coll[instance_coll.length] = custom_combo_box;
		}
	}
	
	/*Prevent memory leak in IE6*/
	if (!window.XMLHttpRequest)
	{
		window.attachEvent('onunload', function()
		{
			for (var i = 0, len = instance_coll.length; i < len; i++)
			{
				instance_coll[i].select.custom_combo_box = null;
				instance_coll[i].select = null;
			}
		});
	}
	
	return ComboBox;
}();

ComboBox.setUpdateText = function(event, onchange_flag)
{
	if (this.selectedIndex >= 0)
	{
		var option = this.options[this.selectedIndex];
		this.custom_combo_box.getElementsByTagName('label')[0].innerHTML = 
		(option.title ? '<img src="' + option.title + '" alt="" />' : '') + option.text;
		if (!onchange_flag && this.onchange_listener instanceof Function)
		{
			this.onchange_listener(this);
		}
	}
}

ComboBox.setSelectedIndex = function(index)
{
	if (index >= 0)
	{
		this.selectedIndex = index;
		ComboBox.setUpdateText.call(this);
	}
}

ComboBox.focus = function()
{
	this.custom_combo_box.getElementsByTagName('label')[0].className = 'combo_box_focus'
}

ComboBox.blur = function()
{
	this.custom_combo_box.getElementsByTagName('label')[0].className = '';
}

var ObserverDropDown = function()
{
	function ObserverDropDown()
	{
		var drop_down_id = 'drop_down_combo_box';
		var selected_class_name = 'selected_index';
		
		var drop_down = null;
		var curr_combo_box = null;
		var default_selected_index = null;
		var old_selected_index = null;
		var self = this;
		
		this.initDropDown = function()
		{
			drop_down = document.createElement('div');
				drop_down.id = drop_down_id;
			var drop_down_style = drop_down.style;
				drop_down_style.position = 'absolute';
				drop_down_style.zIndex = '100000';
				drop_down_style.display = 'none';
				
				Event.addEvent(drop_down, 'scroll', self.focusSelect, false);
				
			document.body.appendChild(drop_down);
		}
		
		this.showDropDown = function(event)
		{
			this.select.focus();
			if (this != curr_combo_box)
			{
				curr_combo_box = this;
				default_selected_index = old_selected_index = curr_combo_box.select.selectedIndex;
				positionResizeDropDown();
				renderOptions();
				drop_down.style.display = 'block';
				drop_down.scrollTop = getSelectedOffset();
				Event.stopPropagation(event);
				
				Event.addEvent(document, 'click', self.hideDropDown, false);
				Event.addEvent(window, 'resize', self.hideDropDown, false);
			}
		}
		
		this.hideDropDown = function(event) 
		{
			drop_down.style.display = 'none';
			if (curr_combo_box)
			{
				if (curr_combo_box.select.selectedIndex != default_selected_index)
				{
					curr_combo_box.select.selectedIndex = default_selected_index;
				}
				curr_combo_box.select.focus();
				curr_combo_box = null;
				Event.removeEvent(document, 'click', self.hideDropDown, false);
				Event.removeEvent(window, 'resize', self.hideDropDown, false);
			}
		}
		
		this.moveSelected = function(index)
		{
			if (index != old_selected_index)
			{
				var obj = drop_down.getElementsByTagName('label');
				obj[old_selected_index].className = '';
				obj[index].className = selected_class_name;
				curr_combo_box.select.selectedIndex = old_selected_index = index;
			}
		}
		
		this.moveKeySelected = function()
		{
			if (curr_combo_box)
			{
				default_selected_index = curr_combo_box.select.selectedIndex;
				self.moveSelected(default_selected_index);
				drop_down.scrollTop = getSelectedOffset();
			}
		}
		
		this.setSelectedIndex = function(index)
		{
			default_selected_index = index;
			ComboBox.setSelectedIndex.call(curr_combo_box.select, index);
		}
		
		this.checkKey = function(event)
		{
			var code = event.keyCode; 
			if (curr_combo_box && (code == 9 || code == 13))
			{
				self.hideDropDown();
			}
		}
		
		this.focusSelect = function()
		{
			curr_combo_box.select.focus();
		}
		
		function positionResizeDropDown()
		{
			var pos = getOffsetElement(curr_combo_box);
			drop_down.style.left = pos[0] + 'px';
			drop_down.style.top = pos[1] + curr_combo_box.offsetHeight + 'px';
			drop_down.style.width = curr_combo_box.clientWidth + 'px';
		}
		
		function getSelectedOffset()
		{
			if (default_selected_index > -1)
			{
				var obj = drop_down.getElementsByTagName('label')[curr_combo_box.select.selectedIndex];
				var scroll = drop_down.scrollTop;
				var obj_top = obj.offsetTop;
				var obj_height = obj.offsetHeight + 2;
				
				if (obj_top < scroll)
				{
					return obj_top;
				}
				
				if (obj_top > scroll + drop_down.offsetHeight - obj_height)
				{
					return obj_top - (drop_down.offsetHeight - obj_height);
				}
				return scroll;
			}
		}
		
		function renderOptions()
		{
			var index = -1;
			var option = null;
			function replaceOption(match)
			{
				index++;
				option = curr_combo_box.select.options[index];
				return '<label onmouse' + (window.ActiveXObject ? 'over' : 'move') + '=ObserverDropDown.moveSelected(' + index + ') onclick=ObserverDropDown.setSelectedIndex(' + index + ')>' + 
				(option.title ? '<img src="' + option.title + '" alt="" />' : '');
			}
			
			var html_code = curr_combo_box.select.innerHTML;
			html_code = html_code.replace(/<option[^>]+>/ig, replaceOption);
			html_code = html_code.replace(/<\/option/ig, '</label');
			drop_down.innerHTML = html_code;
			if (index > -1)
			{
				drop_down.getElementsByTagName('label')[curr_combo_box.select.selectedIndex].className = selected_class_name;
			}
			
		}
		
	}

	return new ObserverDropDown();
}();

/*Radio Button and checkbox beta version*/
function Checkbox(name, value, checked, id)
{
	this.button = null;
	this.button_flag = false;
	if (arguments.length > 1)
	{
		this.initializedComponent();
		this.button.className = 'hidden_component';
		this.button.name = name;
		this.button.value = value;
		this.button.id = id;
		this.checked = checked;
		this.button.defaultChecked = checked;
	}
	if (arguments[0])
	{
		this.button = arguments[0];
		this.checked = this.button.checked;
		this.button_flag = true;
	}
	if (arguments.length)
	{
		this.custom_button = document.createElement('span');
		this.custom_button.par_button = this.button;
		this.custom_button.className = 'checkbox' + (this.checked ? ' ch_checked' : '');
		this.custom_button.onclick = function(){this.par_button.click();};
		
		this.button.custom_button = this.custom_button;
		this.changeCheckedListener();
	}
}

Checkbox.prototype.changeCheckedListener = function()
{
	this.button.onclick = function()
	{
		this.custom_button.className = 'checkbox' + (this.checked ? ' ch_checked' : '');
	}
}

Checkbox.prototype.writeComponent = function(id_element)
{
	var obj = document.getElementById(id_element) || id_element;
	if (!this.button_flag) obj.appendChild(this.button);
	obj.insertBefore(this.custom_button, this.button);
}

Checkbox.prototype.initializedComponent = function()
{
	this.button = document.createElement('input');
	this.button.type = 'checkbox';
}
	
var radio_button_reference = [];
function changeRadioButtonChecked(sender)
{
	if (radio_button_reference[sender.name]) 
	{
		radio_button_reference[sender.name].custom_button.className = 'radio';
		radio_button_reference[sender.name].checked = false;
		radio_button_reference[sender.name].defaultChecked = false;
	}
	
	sender.checked = sender.defaultChecked = true;
	sender.custom_button.className = 'radio r_checked';
	radio_button_reference[sender.name] = sender;
}

RadioButton.prototype = new Checkbox;
RadioButton.constructor = RadioButton;

function RadioButton(name, value, checked, id)
{
	Checkbox.call(this, name, value, checked, id);
	this.custom_button.className = 'radio';
	if (this.checked)
	{
		changeRadioButtonChecked(this.button);
	}
}

RadioButton.prototype.changeCheckedListener = function()
{
	this.button.onclick = function()
	{
		changeRadioButtonChecked(this);
	}
}

RadioButton.prototype.initializedComponent = function()
{
	this.button = document.createElement((!window.ActiveXObject ? 'input' : '<input type="radio">'));
	this.button.type = 'radio';
}
/*end radio button and checkbox*/
	
	
function parseFormElements()
{
	var obj = document.getElementsByTagName('select');
	var felem = null;
	var combo_box = null;
	for (var i = 0, len = obj.length; i < len; i++)
	{
		if (!obj[i].multiple)
		{
			combo_box = new ComboBox({'className' : obj[i].className}, obj[i]);
			combo_box.renderComboBox(obj[i].parentNode);
		}
	}
	
	obj = document.getElementsByTagName('input');
	for (var i = 0, len = obj.length; i < len; i++)
	{
		if (obj[i].type.toLowerCase() == 'radio')
		{
			new RadioButton(obj[i]).writeComponent(obj[i].parentNode);
		}
		if (obj[i].type.toLowerCase() == 'checkbox')
		{
			new Checkbox(obj[i]).writeComponent(obj[i].parentNode);
		}
	}
}
Event.addEvent(window, 'load', parseFormElements, false);

/*Tabs Menu*/
/*
	var tabs_menu = new TabsMenu({
	'tabs_root_id' : 'tabs_menu', - holder id for tabs
	'tabs_tag_name' : 'a', - tabs tag name
	'active_tab_index' : 0, - default active tab, if no set 0 is defaul
	'change_event' : 'mouseover' - event chnage tabs, default is click
	'onChange' : tabChanger, - change callback pointer to function
	'next_button_id' : 'tabs_menu_next', - id for next button 
	'prev_button_id' : 'tabs_menu_prev' - id for previous button
	});
	
	setActiveTab(index) - set active tab
	nextTab() - next tab
	prevTab - prev tab
*/

function tabChangeFunction1()
{
 	
}

function TabsMenu(properties)
{
	var active_class = properties.active_class || 'active_tab';
	var active_index = properties.active_tab_index || 0;
	var tabs_holder = document.getElementById(properties.tabs_root_id);
	var tabs = tabs_holder.getElementsByTagName(properties.tabs_tag_name);
	var change_event = properties.change_event || 'click';
	var onChange = properties.onChange;
	var self = this;
	
	this.setActiveTab = function(index)
	{
		if (index < 0 || index >= tabs.length) return; 
		
		if (onChange)
		{
			onChange(properties.tabs_root_id, index, active_index); 
		}
		
		if(tabs[active_index].className.indexOf('video_item')>=0) tabs[active_index].className = 'video_item';
		else tabs[active_index].className = '';
		
		if(tabs[index].className.indexOf('video_item')>=0) tabs[index].className = 'video_item ' + active_class;
		else tabs[index].className = active_class;
		
		active_index = index;
	}
	
	this.nextTab = function()
	{
		var curr = active_index;
		this.setActiveTab((++curr >= tabs.length ? 0 : curr)); 
	}
	
	this.prevTab = function()
	{
		var curr = active_index;
		this.setActiveTab((--curr < 0 ? tabs.length - 1 : curr));
	}
	
	
	for (var i = 0, len = tabs.length; i < len; i++)
	{
		Event.addEvent(tabs[i], change_event, 
			(function(index){
				return function(e){self.setActiveTab(index); Event.preventDefault(e);}
			})(i), false);
	}
	var button_obj = null;
	if (button_obj = document.getElementById(properties.prev_button_id))
	{
		Event.addEvent(button_obj, change_event, function(e){self.prevTab(); Event.preventDefault(e);}, false);
	}
	if (button_obj = document.getElementById(properties.next_button_id))
	{
		Event.addEvent(button_obj, change_event, function(e){self.nextTab(); Event.preventDefault(e);}, false);
	}
	
	this.setActiveTab(active_index);
	
	this.getActiveTab = function()
	{
		return active_index;
	}
}


/*lightbox start*/
/*
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)
	
	Table of Contents
	-----------------
	Configuration
	
	Functions
	- getPageScroll()
	- getPageSize()
	- pause()
	- getKey()
	- listenKey()
	- showLightbox()
	- hideLightbox()
	- initLightbox()
	- addLoadEvent()
	
	Function Calls
	- addLoadEvent(initLightbox)

*/



//
// Configuration
//

function addPrintButton(target_id)
{
	var obj = document.getElementById(target_id).getElementsByTagName('div')[0];
	var button = document.createElement('button');
	button.className = 'button print_icon';
	button.onclick = function(){window.print()};
	obj.insertBefore(button, obj.firstChild);
}

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = 'themes/Main/loading.gif';		
var closeButton = 'themes/Main/close.gif';		


function showHideIframes(show)
{
	var frames = document.getElementsByTagName('iframe');
	for (var i = 0, len = frames.length; i < len; i++)
	{
		frames[i].style.visibility = (show ? 'visible' : 'hidden');
	}
}


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightbox(); }
}


//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }
	

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objCaption = document.getElementById('lightboxCaption');
	var objContent = document.getElementById('lightboxContent');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLightboxDetails = document.getElementById('lightboxDetails');

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	
	var objLinkHref = objLink.href;
	var st_pos = null;
	if ((st_pos = objLinkHref.indexOf("#")) > -1)
	{
		var oCont = document.getElementById(objLinkHref.substring(++st_pos, objLinkHref.length));
		
			objContent.appendChild(oCont.cloneNode(true));
			
			// center lightbox and make sure that the top and left values are not negative
			// and the image placed outside the viewport
			var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - oCont.offsetHeight) / 2);
			var lightboxLeft = ((arrayPageSize[0] - 20 - oCont.offsetWidth) / 2);
			
			objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
			objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


			objLightboxDetails.style.width = oCont.offsetWidth + 'px';
			
			
			if(objLink.getAttribute('title')){
				objCaption.style.display = 'block';
				//objCaption.style.width = imgPreload.width + 'px';
				objCaption.innerHTML = objLink.getAttribute('title');
			} else {
				objCaption.style.display = 'none';
			}

			if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }

			// Hide select boxes as they will 'peek' through the image in IE
			selects = document.getElementsByTagName("select");
			for (i = 0; i != selects.length; i++) {
				selects[i].style.visibility = "hidden";
			}
			showHideIframes(false);
		
			objLightbox.style.display = 'block';

			// After image is loaded, update the overlay height as the new image might have
			// increased the overall page height.
			arrayPageSize = getPageSize();
			objOverlay.style.height = (arrayPageSize[1] + 'px');
			
			// Check for 'x' keypress
			listenKey();

			return false;
		
	}
	else {
		// preload image
		imgPreload = new Image();
		imgPreload.onload=function(){
			objContent.innerHTML = '<img src="' + objLink.href + '" alt="" />';

			// center lightbox and make sure that the top and left values are not negative
			// and the image placed outside the viewport
			var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
			var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
			
			objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
			objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


			objLightboxDetails.style.width = imgPreload.width + 'px';
			
			if(objLink.getAttribute('title')){
				objCaption.style.display = 'block';
				//objCaption.style.width = imgPreload.width + 'px';
				objCaption.innerHTML = objLink.getAttribute('title');
			} else {
				objCaption.style.display = 'none';
			}
			
			// A small pause between the image loading and displaying is required with IE,
			// this prevents the previous image displaying for a short burst causing flicker.
			if (navigator.appVersion.indexOf("MSIE")!=-1){
				pause(250);
			} 

			if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }

			// Hide select boxes as they will 'peek' through the image in IE
			selects = document.getElementsByTagName("select");
			for (i = 0; i != selects.length; i++) {
				selects[i].style.visibility = "hidden";
			}
			showHideIframes(false);
		
			objLightbox.style.display = 'block';

			// After image is loaded, update the overlay height as the new image might have
			// increased the overall page height.
			arrayPageSize = getPageSize();
			objOverlay.style.height = (arrayPageSize[1] + 'px');
			
			// Check for 'x' keypress
			listenKey();

			return false;
		}

		imgPreload.src = objLink.href;
	}
}





//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	var objContent = document.getElementById('lightboxContent');
	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
	showHideIframes(true);
	// disable keypress listener
	document.onkeypress = '';
	objContent.innerHTML = '';
}




//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
			if (anchor.getAttribute("href").indexOf('#print_content') > -1)
			{
				anchor.onclick = function () {showLightbox(this); addPrintButton('lightboxContent'); return false;}
			}
		}
	}

	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay">
	//		<a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
	//	</div>
	// <div id="lightbox">
	//		<a href="#" onclick="hideLightbox(); return false;" title="Click anywhere to close image">
	//			<img id="closeButton" />		
	//			<img id="lightboxImage" />
	//		</a>
	//		<div id="lightboxDetails">
	//			<div id="lightboxCaption"></div>
	//			<div id="keyboardMsg"></div>
	//		</div>
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
	var imgPreloader = new Image();
	
	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloader.onload=function(){

		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objLoadingImageLink.appendChild(objLoadingImage);

		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloader.src = loadingImage;

	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
	// create link
	var objLink = document.createElement("div");
	//objLink.onclick = function(){hideLightbox(); return false;};
	
	objLightbox.appendChild(objLink);

	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	imgPreloadCloseButton.onload=function(){

		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButton;
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objCloseButton.onclick = function(){hideLightbox(); return false;};
		objLink.appendChild(objCloseButton);

		return false;
	}

	imgPreloadCloseButton.src = closeButton;

	// create image
	var objContent = document.createElement("div");
	objContent.setAttribute('id','lightboxContent');
	objLink.appendChild(objContent);
	
	// create details div, a container for the caption and keyboard message
	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetails');
	objLightbox.appendChild(objLightboxDetails);

	// create caption
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','lightboxCaption');
	objCaption.style.display = 'none';
	objLightboxDetails.appendChild(objCaption);

	// create keyboard message
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','keyboardMsg');
	objKeyboardMsg.innerHTML = 'press <a href="#" onclick="hideLightbox(); return false;"><kbd>x</kbd></a> to close';
	objLightboxDetails.appendChild(objKeyboardMsg);


}




//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}



addLoadEvent(initLightbox);	// run initLightbox onLoad
/*end lightbox*/