/*
 * lightbox.js 
 * Date Created: March 26, 2009
 * Requires: jquery 1.2.3
 * Author: Brandon Quintana
 */
 

jQuery(function($) {
	$.fn.lightbox = function(settings) {
		var defaults = {
 			overlayClass: 'overlay',
 			lightboxClass: 'lightbox',
			lightboxOpenSelector: '.lbOn',
			lightboxCloseSelector: '.lbClose'
		};
		
		var s = $.extend(defaults, settings);
			
		var n = {
			overlay: null,
			lightbox: null
		};
				
		var c = {
			
		};
		
		return this.each(function() {
			/* Initialize
			-------------------------------------------*/
			
			/**
	 		* @method init
	 		**/
			this.init = function(settings) {
				var $this = this;
				
				this._eventManager();
			};

			/* Private Methods
			-------------------------------------------*/
			/**
			 * @method _eventManager()
			 **/
			this._eventManager = function() {
				var $this = this;
				
				$(document.body).bind('click', function(e) {
					var target = $(e.target);

					if(target.is(s.lightboxOpenSelector)) {
						$this._activate(target);
						return false;
					}
					
					if(target.is(s.lightboxCloseSelector)) {
						$this._hide();
						return false;
					}
				});
				
				if($.browser.msie && $.browser.version < 7){
					$(window).bind('scroll', function(e) {
						var target = $(e.target);
						$this._fixIE6();
					});
				}
				
			};
			
			/* Public Methods
			-------------------------------------------*/
			/**
			 * @method _fixIE6()
			 **/
			this._fixIE6 = function() {
				s.scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
				if(n.overlay) n.overlay.css({'top': s.scrollTop + 'px'});
				if(n.iframe) n.iframe.css({'top': s.scrollTop + 'px'});
			};
			
			/**
			 * @method _activate()
			 * @param trigger
			 **/
			this._activate = function(trigger) {
				var $this = this;
				if($('.' + s.lightboxClass).length <= 0)
					this._createLightbox();
				
				n.lightbox.load(trigger.attr('href'), function() {
					$this._show(trigger.attr('rel'));
				});
			};
			
			/**
			 * @method _show()
			 * @param callback
			 **/
			this._show = function(callback) {
				if($.browser.msie) n.iframe.height($(window).height()).show();
				n.lightbox.show();
				n.overlay.height($(window).height());
				n.overlay.show();
				n.lightbox.fadeIn();
				eval(callback);
			};
			
			/**
			 * @method _hide()
			 * @param trigger
			 **/
			this._hide = function() {
				if($.browser.msie)
					n.iframe.hide();
				
				n.lightbox.hide();
				n.overlay.hide();
			};
			
			/**
			 * @method _createLightbox()
			 **/
			this._createLightbox = function() {
				$('body').append('<div class="' + s.overlayClass + '"></div>');
				$('body').append('<div class="' + s.lightboxClass + '"></div>');
				
				if($.browser.msie) {
					$('body').append('<iframe class="' + s.overlayClass + '_iframe"></iframe>');
					
					n.iframe = $('.' + s.overlayClass + '_iframe');
				
					n.iframe.css({
						'text-align' : 'left',
						'display' : 'none',
						'position' : 'fixed',
						'top' : '0',
						'left' : '0',
						'width' : '100%',
						'height' : '100%',
						'z-index' : '400'
					});
				}
				
				if($.browser.msie && $.browser.version < 7)
					n.iframe.css('position', 'absolute');
				
				n.overlay = $('.' + s.overlayClass);
				
				n.overlay.css({
					'text-align' : 'left',
					'display' : 'none',
					'position' : 'fixed',
					'top' : '0',
					'left' : '0',
					'width' : '100%',
					'height' : '100%',
					'z-index' : '500',
					'background' : '#2c3c5d',
					opacity: 0.75
				});
				
				if($.browser.msie && $.browser.version < 7)
					n.overlay.css('position', 'absolute');
				
				n.lightbox = $('.' + s.lightboxClass);
				
				n.lightbox.css({
					'display' : 'none',
					'z-index' : '600',
					'position' : 'absolute',
					'top' : '0',
					'left' : '0',
					'width': '100%',
					opacity: 1
				});
				
				if($.browser.msie && $.browser.version < 7)
					n.lightbox.css('position', 'absolute');
			};
					
			//Call initialize
			this.init(settings);
		});
	};
});

jQuery(document).ready(function($) {
	$(document).lightbox();
});
