/*
Created By: Chris Campbell
Modified By: Match.com 

Website: http://particletree.com
Date: 2/1/2006

Adapted By: Simon de Haan
Website: http://blog.eight.nl
Date: 21/2/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
And the lightbox gone wild by ParticleTree at http://particletree.com/features/lightbox-gone-wild/

*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 						= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function GetControlAttributeValue (control, attribute) { 	
	var _attrib; 		
	
	var _browser_IE = (document.all);
	var _browser_DOM = (document.getElementById);	
	
	var _domPrefix = 'document.';
	_domPrefix += (_browser_IE) ? 'all.' : (_browser_DOM) ? 'getElementById("' : '';	

	var _domSuffix = (_browser_DOM && !_browser_IE) ? '")' : '';
	
	if (_browser_DOM) 			
		_attrib = control.getAttribute(attribute, false); 		
	else 			
		_attrib = eval(_domPrefix + control.id + '.' + attribute + _domSuffix); 		

	return _attrib; 	
}
/*-----------------------------------------------------------------------------------------------*/

/* All browsers: Catch all functions */
Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'load', activateDefaultDiv, false);
Event.observe(window, 'load', CenterActiveLightbox, false);
Event.observe(window, 'unload', Event.unloadCache, false);
Event.observe(window, 'resize', CenterActiveLightbox, false);

/* For Mozilla */
Event.observe(window, 'DOMContentLoaded', initialize, false);
Event.observe(window, 'DOMContentLoaded', getBrowserInfo, false);
Event.observe(window, 'DOMContentLoaded', activateDefaultDiv, false);

// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
    if (this.readyState == "complete") {
	    initialize();
		getBrowserInfo();
		activateDefaultDiv();
    }
};
/*@end @*/




var __activeLightbox;

//Allow resizing of the active light box
var __resizedWidth = 0;
var __resizedHeight = 0;

var lightboxCacheItem = Class.create();
lightboxCacheItem.prototype = {
	HtmlID : 0,
	LightboxControl : null,
	
	initialize: function(htmlID, lightboxControl) {
		this.HtmlID = htmlID;
		this.LightboxControl = lightboxControl;
	}
}


var lightbox = Class.create();
lightbox.prototype = {

	yPos : 0,
	xPos : 0,
	ieBodyHeightCache : '',
	ieBodyOverflowCache : '',
	ieHTMLHeightCache : '',
	ieHTMLOverflowCache : '',
	overlayID : '',
	overlayBackgroundID : '',
	autoResizeHeight : '',
	
	// Add in markup necessary to make this work. Basically two divs:
	// Overlay holds the shadow
	// Lightbox is the centered square that the content is put into.
	addLightboxMarkup: function() {
		bod 				= document.getElementsByTagName('body')[0];
		overlay 			= document.createElement('div');
		overlay.id			= this.overlayID;

		iframe				= document.createElement('iframe');
		iframe.id			= this.overlayBackgroundID;

		bod.appendChild(iframe);
		bod.appendChild(overlay);
	},

	initialize: function(ctrl) {
		this.content = ctrl.rel;
		
		this.overlayID = GetControlAttributeValue(ctrl, 'overlayID');
		this.overlayBackgroundID = GetControlAttributeValue(ctrl, 'overlayBackgroundID');
		
		//Add the markup
		this.addLightboxMarkup();
	
		//Automatically resize height?
		if (GetControlAttributeValue(ctrl, 'autoResizeHeight') != null)
			this.autoResizeHeight = GetControlAttributeValue(ctrl, 'autoResizeHeight');

		//Set the default width
		if (GetControlAttributeValue(ctrl, 'relDefaultWidth') != null)
			$(this.content).style.width = GetControlAttributeValue(ctrl, 'relDefaultWidth') + 'px';

		//Do not set the height if auto resize height is enabled
		if (this.autoResizeHeight != 'true') 
		{
			//Set the default height
			if (GetControlAttributeValue(ctrl, 'relDefaultHeight') != null)
				$(this.content).style.height = GetControlAttributeValue(ctrl, 'relDefaultHeight') + 'px';
		}		
		
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	


	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			//this.hideSelects('hidden');
		}
		
		//Designed this way to eliminate lightbox flickering
		//	Logic: display has to be set to block prior to CenterActiveLightbox() for 
		//		AutoResizeHeight to accurately detect the $(__activeLightbox.content).scrollHeight
		//		Visibility is set to hidden to elminate flickering
		this.SetLightboxVisibility("hidden");
		this.SetLightboxDisplay("block");
		
		__activeLightbox = this;
		CenterActiveLightbox();
		
		this.SetLightboxVisibility("visible");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];

		//Cache the body styles		
		this.ieBodyHeightCache = bod.style.height;
		this.ieBodyOverflowCache = bod.style.overflow;
		
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		
		//Cache the html styles	
		this.ieHTMLHeightCache = htm.style.height;
		this.ieHTMLOverflowCache = htm.style.overflow;
		
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	revertIE: function() {
		//Revert from cache
		bod = document.getElementsByTagName('body')[0];
		//bod.style.height = this.ieBodyHeightCache;
		bod.style.overflow = this.ieBodyOverflowCache;
		
		//Revert from cache
		htm = document.getElementsByTagName('html')[0];
		//htm.style.height = this.ieHTMLHeightCache;
		htm.style.overflow = this.ieHTMLOverflowCache;
	},
	
	// No longer needed... page iframe resolves this... maintained incase we need it later on 
	//		In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
		
		//fix hidden select boxes
		var fixSelects = $(this.content).getElementsByTagName('select');
		
		for(i = 0; i < fixSelects.length; i++) {
			fixSelects[i].style.visibility = 'visible';
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	SetLightboxVisibility : function(visiblity){
		$(this.overlayID).style.visibility = visiblity;
		$(this.content).style.visibility = visiblity;
		$(this.overlayBackgroundID).style.visibility = visiblity;
	},	
	
	SetLightboxDisplay: function(display){
		$(this.overlayID).style.display = display;
		$(this.content).style.display = display;
		$(this.overlayBackgroundID).style.display = display;
		
		if(display != 'none') this.actions();		
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.revertIE();
			//this.hideSelects("visible");
		}
		
		this.SetLightboxDisplay("none");
		this.SetLightboxVisibility("hidden");
		
		//Reset the global variables
		__activeLightbox = null;
		
		__resizedWidth = 0;
		__resizedHeight = 0;
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
var __objectToActivate = null;
var __activeControlOnload = null;
var __lightboxArray = new Array();

function SetControlToActivate(controlToActivate) {
	__activeControlOnload = controlToActivate;
}

function initialize(){
	if (!arguments.callee.initialized) {
		lbox = document.getElementsByClassName('lbOn');
		
		for(i = 0; i < lbox.length; i++) {
			valid = new lightbox(lbox[i]);
			
			if (lbox[i] == $(__activeControlOnload))
				__objectToActivate = valid;
				
			//Add the lightbox to the global array incase a user wants to activate it
			__lightboxArray.push(new lightboxCacheItem(lbox[i].id, valid));
		}

		arguments.callee.initialized = true;
	}
}

function activateDefaultDiv() {
	if (__objectToActivate != null) {
		__objectToActivate.activate();
		__objectToActivate = null;
	}
}

//Allow programmatic activation of a lightbox
function ShowLightbox(controlToActivate) {

	//Make sure the control is valid
	var _indexKey = 'init_' + controlToActivate;
	
	for (i = 0; i < __lightboxArray.length; i++) {
		if (__lightboxArray[i].HtmlID == _indexKey) {
			__objectToActivate = __lightboxArray[i].LightboxControl;
			activateDefaultDiv();
			break;
		}
	}
}

//Allow resizing of the active light box
function ResizeActiveLightbox(width, height) {
	__resizedWidth = width;
	__resizedHeight = height;
	
	CenterActiveLightbox();
}

function CloseActiveLightbox() {
	if (__activeLightbox != null) {
		__activeLightbox.deactivate();
		__activeLightbox = null;
	}
}

// Automatically centers the active lightbox
function CenterActiveLightbox() {
	if (__activeLightbox != null) 
	{
		if (__activeLightbox.autoResizeHeight == "true")
			$(__activeLightbox.content).style.height = $(__activeLightbox.content).scrollHeight + 'px';

		else if (__resizedWidth > 0 && __resizedHeight > 0) 
			{
				$(__activeLightbox.content).style.width = __resizedWidth + 'px';
				$(__activeLightbox.content).style.height = __resizedHeight + 'px';
		}
		
		//Get the div positioning... remove px / em / %
		var _width = parseInt($(__activeLightbox.content).style.width);
		var _height = parseInt($(__activeLightbox.content).style.height);
		
		var _screenWidth = 0;
		var _screenHeight = 0;
		
		
		if (window.innerWidth != null) {
			_screenWidth = window.innerWidth;
			_screenHeight = window.innerHeight;
		}
		else {
			_screenWidth = document.body.offsetWidth;
			_screenHeight = (document.body.offsetHeight > screen.height) ? screen.height : document.body.offsetHeight;
		}
		
		
		var _left = (_screenWidth/2) - _width/2;
		var _top = (_screenHeight/2) - _height/2;	
		
		
		/*
		alert("activeLightBox width" + $(__activeLightbox.content).style.width);
		alert("screenWidth = " + _screenWidth + " screenHeight = " + _screenHeight);	
		alert("_left & _top " + _left + " " + _top);
		*/
		
		$(__activeLightbox.content).style.top = _top + 'px';
		$(__activeLightbox.content).style.left = _left + 'px';
	}
}

