// JavaScript Document
//
// * LabXLightBox 0.1.2
// * http://www.labx.com
// *
// * Copyright (c) 2009 LabX
// *
// * Date: Jan 22 2009
// * TESTED ON
// * ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
// * FF 3.5.5
// * MSIE 6/7/8
// * Chrome 3.0.x
// * Safari 4.0.4
// *
// * DEPENDENCIES
// * ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
// * jQuery JavaScript Library v1.3.2
// * http://jquery.com/
// * jquery.event.drag.js - rev 10
// * http://threedubmedia.com
// *
// * USE
// * ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
// *	$('#element').LabXLightBox({
// *		openElement: ".lnkApplyMore",
// *		title: "My New LightBox",
// *		height: 430,
// *		width: 450,
// *		position: "center",
// *		blanketColour: "#00007f",
// *		blanketOpacity: 0.5,
// *		zIndex: 100,
// *		paddingOffset: 0
// *	});
// *

// LabXLightBox 
(function($) { 
	$.fn.LabXLightBox = function(options) {
		return new LabXLightBoxCore().init(this,options);
	};
	function LabXLightBoxCore() {
		return {
			defaults : {
				openElement: "a",		// object: the element which will open the box
				title: "LightBox",		// string: the title of the lightbox, displays in title bar
				height: "auto",			// int/"auto": the height of the box, excepts auto 
				width: "auto",			// int/"auto": the width of the box, excepts auto 
				position: "center",		// { x:int, y:int }/"center": the start position of the box, excepts center
				blanketColour: "#333",	// string: the colour of the blanket - default: "#333"
				blanketOpacity: 0.6,	// double: the opacity of the blanket (0.0 - 1.0) - default: 0.6
				zIndex: 1000,			// int: the zIndex of the Blanket, LightBox is this +1
				paddingOffset: 0		// int: value of Padding added to content box to even everything out.
			}, // defaults
		 	init: function(form,options){
				var _Me = form;
				var _LightBoxID = "LabXLightBox_" + _Me.attr('id');
				var opts = $.extend(LabXLightBoxCore().defaults, options);

				// Hide the LightBoxed Element
				_Me.hide();
				
				// Set the Open Element.Click
				$(opts.openElement).live("click", function(e) {
					var myWidth = 0;
					var myHeight = 0;
					$('body').append("<div id='LabXLightBox_Blanket'></div><div id='" + _LightBoxID + "' class='LabXLightBox_Container'><div class='LabXLightBox_Title'><span>" + opts.title + "</span></div><div class='LabXLightBox_Content'><span><span><span>" + _Me.clone(true).html() + "</span></span></span></div><div class='LabXLightBox_CloseContainer'><a href='' class='LabXLightBox_Close'><span>close</span></a></div></div>");

					if (opts.height == "auto") { myHeight = _Me.outerHeight(true) + $('.LabXLightBox_Title').outerHeight(true) + $('.LabXLightBox_CloseContainer').outerHeight(true); }
					else { myHeight = opts.height; }
					if (opts.width == "auto") { myWidth = _Me.outerWidth(true) + opts.paddingOffset; }
					else { myWidth = opts.width + opts.paddingOffset; }

					if (opts.position != "center") {
						var PosLeft = opts.position.x;
						var PosTop = opts.position.y;
					}
					else {
						var PosLeft = (($(window).width() - 7) - myWidth) / 2;
						var PosTop = (($(window).height() - 6) - myHeight) / 2;
					}
				
					$('#' + _LightBoxID).stop(true,true);

					// DETECT BROWSER - IF MSIE 6, DO NOT ANIMATE
					if ((jQuery.browser.msie) && (jQuery.browser.version < 7)){
						$('#' + _LightBoxID).css({
							position: "absolute",
							left: PosLeft,
							width: myWidth,
							top: PosTop,
							height: myHeight,
							"z-index": opts.zIndex
						}).show();
					}
					else {
						$('#LabXLightBox_Blanket').css({
							position: "fixed",
							top: 0,
							left: 0,
							width: $(document).width() - 7,
							height: $(document).height() - 6,
							opacity: 0,
							"background-color": opts.blanketColour,
							"z-index": opts.zIndex
						}).animate({ opacity: opts.blanketOpacity }, 200, function() {
							$(window).resize( function() {
								$('#LabXLightBox_Blanket').css({
									width: $(document).width() - 7,
									height: $(document).height() - 6
								});
							});
						});
						$('#' + _LightBoxID).css({
							position: "fixed",
							top: PosTop + (myHeight / 2),
							left: PosLeft + (myWidth / 2),
							width: 0,
							height: 4,
							"z-index": opts.zIndex+1				
						}).animate({
							left: PosLeft,
							width: myWidth
						}, 200).animate({
							top: PosTop,
							height: myHeight
						}, 300, function() {
							// Once the Window has Loaded, Activate Controls... 
							$(".LabXLightBox_Title").mouseup(
								function() { $(this).css( { "cursor":"default" } ); }
							).mousedown( 
								function() { $(this).css( { "cursor":"move" } ); }
							);
							$(".LabXLightBox_Title").bind('drag', function(e) { 
								var _MeTop = eval($('#' + _LightBoxID).css("top").replace(/px/gi,""));
								var _MeLeft = eval($('#' + _LightBoxID).css("left").replace(/px/gi,""));
								if (_MeTop >= 0) {
									var posY = (e.offsetY - $(document).scrollTop());
									if (posY < 0) { posY = 0; }
									if ((posY+$('#' + _LightBoxID).outerHeight(true)) > $(window).height()) {
										posY = $(window).height() - $('#' + _LightBoxID).outerHeight(true);
									}
								}
								if (_MeLeft >= 0) { 
									var posX = (e.offsetX - $(document).scrollLeft());
									if (posX < 0) { posX = 0; }
									if ((posX+$('#' + _LightBoxID).outerWidth(true)) > $(window).width()) {
										posX = $(window).width() - $('#' + _LightBoxID).outerWidth(true);
									}
								}
								$('#' + _LightBoxID).css({ 
									top: posY,
									left: posX 
								}); 
							}); // Close Drag
							$('.LabXLightBox_Close').bind('click', function() {
								var _MeTop = eval($('#' + _LightBoxID).css("top").replace(/px/gi,""));
								var _MeLeft = eval($('#' + _LightBoxID).css("left").replace(/px/gi,""));
								if ((jQuery.browser.msie) && (jQuery.browser.version < 7)){
									$('#LabXLightBox_Blanket').remove();
									$(this).parents('.LabXLightBox_Container').remove();
								}
								else {
									$('#LabXLightBox_Blanket').fadeOut(500, function() { $(this).remove() });
									$(this).parents('.LabXLightBox_Container').animate({
										top: _MeTop + (myHeight / 2),
										height: 4
									}, 300).animate({
										left: _MeLeft + (myWidth / 2),
										width: 0,
										opacity: 0	
									}, 200, function() { $(this).remove() });
								}
								return false;
							}); // Close .LabXLightBox_Close.click
						}); // Close Position Lightbox
					}
					return false; // Prevent Element Default Actions 
				}); // Close openElement.click()
			} // Close init
		}// Close Return
	} // Close LabXUploaderCore
})(jQuery);
