SPCL.lightbox = {
	el_back: null,
	el_wrap: null,
	el_body: null,
	el_foot: null,
	el_temp: null,
	el_exit: null,
	
	el_oldParent:null,
	
	init: function() {
		var O = SPCL.lightbox;
		O.el_back = O.el_back || jQuery('<div class="spcl--lightbox-bg"></div>').appendTo(document.body);
		O.el_wrap = O.el_wrap || jQuery('<div class="spcl--lightbox-el"></div>').appendTo(document.body);
		O.el_body = O.el_body || jQuery('<div class="spcl--lightbox-body"></div>').appendTo(O.el_wrap);
		O.el_foot = O.el_foot || jQuery('<div class="spcl--lightbox-controls"></div>').appendTo(O.el_wrap);
		O.el_exit = O.el_exit || jQuery('<a href="" class="spcl--close">CLOSE</a>').click(O.hide).appendTo(O.el_foot);
		O.el_temp = O.el_temp || jQuery('<div class="spcl--lightbox-temp" id="SpclTemp"></div>').appendTo(document.body);
	},
	
	/**
	 * Shows the lightbox
	 *
	 * @param   P   Parameters for the lightbox
	 *						bgColor:		Color of lightbox background
	 *											default:#000
	 *						bgOpacity:	Opacity of lightbox background
	 *											default: 0.6
	 *						width:		Width of the lightbox body
	 *											default: width of src (best guess)
	 *						height:		Height of the lightbox body
	 *											default: height of src (best guess)
	 *						src:			What to display in lightbox body
	 *											[type==dom: id of element to display]		ex: #content
	 *											[type==flv: path to flv movie (sans .flv)	ex: /common/flash/flvFileName
	 *											required.
	 *						type:			Type of content to lightbox
	 *											[dom|flv]
	 *											default: dom
	 *						color:		Color of the lightbox body background
	 *											default: '#fff'
	 *						border:		Width of border around lightbox body
	 *											default: 2
	 *						crop:			Whether to crop or scroll body
	 *											[true=crop body | false=scroll body]
	 *											default: true
	 *						showFooter:	Whether to show footer under lightbox body
	 *											[true=show footer | false=hide footer]
	 *											default: true
	 */
	show: function(P) {
		var O = SPCL.lightbox;
		var E = null;
		// Get initial variables
		var x = jQuery('body')[0].scrollLeft;
		var y = document.documentElement.scrollTop;
		var w = O.el_back[0].offsetWidth;
		var h = self.innerHeight || (document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
		/* page height */
		var pageH = jQuery('html')[0].scrollHeight;
//		var test1 = document.body.scrollHeight;
//		var test2 = document.body.offsetHeight;
//		var pageH = test1 > test2 ? document.body.scrollHeight : document.body.offsetHeight;
		
		var src = null;
		
		// If the 'type' parameter is set, override the 'src' parameter
		switch (P.type) {
			case 'flv':
				var so = new SWFObject("/common/flash/FLVPlayer_Progressive.swf", "SpclFlvPlayer", P.width, P.height, "8", "#FFFFFF");
				so.addParam("flashvars", "&MM_ComponentVersion=1&skinName=/common/flash/Clear_Skin_2&streamName="+P.src+"&autoPlay=true&autoRewind=false");
				so.write("SpclTemp");
				src = jQuery('#SpclFlvPlayer');
				O.el_oldParent = null;
				break;
			case 'swf':
				var so = new SWFObject(P.src, "SpclSwfPlayer", P.width, P.height, "8", "#ffffff");
				so.write("SpclTemp");
				src = jQuery('#SpclSwfPlayer');
				O.el_oldParent = null;
				break;
			case 'dom':
				src = jQuery(P.src);
				O.el_oldParent = src.parent();
				break;
		}
		
		// If width or height not defined, find them
		if (!P.width || !P.height) {
			O.el_temp.empty().append(src);
			if (!P.width)  P.width = O.el_temp[0].offsetWidth;
			if (!P.height) P.height= O.el_temp[0].offsetHeight;
		}
		
		// Setup footer control bar
		if (P.showFooter) {
			O.el_foot.css({display:'', width:P.width+'px'});
		} else {
			O.el_foot.css('display', 'none');
		}
		
		// Setup body
		O.el_body.append(src).css({width:P.width+'px', height:P.height+'px', overflow:P.crop?'hidden':'auto', margin:P.border});
		
		// Position and show back
		/*opacity:P.bgOpacity, */
		O.el_back.css({visibility:'visible', height:pageH});
		// There are problems with FF/Mac and opacity and flash, so don't do it there
		if (!(jQuery.browser.mozilla  &&  navigator.platform.indexOf('Mac') >= 0)) {
			O.el_back.css({'background-color':P.bgColor, 'opacity':P.bgOpacity});
		}
		
		// Position body wrapper
		var bw = O.el_wrap[0].offsetWidth;
		var bh = O.el_wrap[0].offsetHeight;
		var bx = x + parseInt(w/2 - bw/2);
		var by = y + parseInt(h/2 - bh/2);
		// Check for invalid size or position (in case browser window is obscenely small)
		if (bx < 0) bx = 0; if (bw < 0) bw = 100;
		if (by < 0) by = 0; if (bh < 0) bh = 100;
		O.el_wrap.css({visibility:'visible', left:bx+'px', top:by+'px', 'background-color':P.color});
		
		// Listen for ESCAPE key
		jQuery('html').keypress(O.onKeyPress);
	},
	
	/**
	 * Called when any element with a 'lightbox' attribute is clicked
	 */
	onElementClick: function() {
		var O = SPCL.lightbox;
		O.show(O.parseParams(this.getAttribute('lightbox')));
		
		return false;
	},
	
	/**
	 * Called when a key is pressed while the lighbox is visible.
	 * Checks for escape key and hides lightbox if ESC was key pressed.
	 */
	onKeyPress: function(e) {
		if (e.which == 27) SPCL.lightbox.hide();
	},
	
	/**
	 * Hides the object
	 */
	hide: function() {
		var O = SPCL.lightbox;
		O.el_wrap.css('visibility', 'hidden');
		O.el_back.css('visibility', 'hidden');
		// Restore lightboxed content if able to
		if (!O.el_oldParent || O.el_oldParent[0] == O.el_temp[0])
		{
			O.el_body.empty();
		}
		else
		{
			O.el_body.contents().appendTo(O.el_oldParent);
		}
		// Remove key listener
		jQuery('body').unbind('keypress', O.onKeyPress);
		
		return false;
	},
	
	/**
	 * Parses element's 'lightbox' attribute string into object
	 *
	 * @param   el   Element to parse
	 */
	parseParams: function(attrStr) {
		var params = {};
		
		// Read user parameters from 'lightbox' attribute
		jQuery.each(attrStr.split(';'), function(i, n) {
			n = jQuery.trim(n).split(':');
			params[jQuery.trim(n[0])] = jQuery.trim(n[1]);
		});
		
		// Combine user parameters with default parameters
		params = jQuery.extend({
			type:			'dom',
			bgColor:		'#000',
			bgOpacity:	0.6,
			color:	'#fff',
			border:	2,
			crop:	true,
			showFooter:	true
		}, params);
		
		// Turn true/false strings into actual boolean values
		params.showFooter = eval(params.showFooter);
		params.crop = eval(params.crop);
		
		return params;
	}
};

jQuery(window).load(function() {
	// Setup the lightbox
	SPCL.lightbox.init();
	
	// Look for any lightbox elements
	jQuery('*[lightbox]').each(function(i) {
		jQuery(this).click(SPCL.lightbox.onElementClick);
	});
});
