/*
 * jQuery Lightbox_Me
 * By: Trent Richardson (jQuery impromptu)
 * Adapted By : Buck Wilson
 * Version : .8
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


(function($) {
	
	$.fn.lightbox_me = function(options) {
		var defaults = {
			 prefix: 'lb', 
			loaded:function(){}, 
			callback:function(){}, 
			opacity:0.6, 
			zIndex: 999,
			closeButton: '#close',
			overlayspeed:'slow',
			Lightboxspeed:'fast', 
			show:'show'
		}
		
		var o = $.extend(defaults, options);
		
		return this.each(function() {
			var ie6 = ($.browser.msie && jQuery.browser.version < 7);	
			var $b = $(document.body);
			var $w = $(window);
			var ele_id = $(this).attr('id');

			var $new_eles = $('<div id="' + o.prefix + '_container"></div>');

			if(ie6) $('select').css('visibility','hidden');
			$new_eles.append($(this)).append($('<div class="' + o.prefix + '_overlay" id="' + o.prefix + '_overlay"></div>'));


			var $lb_b = $b.append($new_eles).children('#' + o.prefix + '_container');
			var $lb_e = $lb_b.children('#'+ ele_id);
			var $lb_f = $lb_b.children('#' + o.prefix + '_overlay');

			var getWindowScrollOffset = function(){ 
				return (document.documentElement.scrollTop || document.body.scrollTop) + 'px'; 
			};		

			var getWindowSize = function(){ 
				var size = {
					width: window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth),
					height: window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight)
				};
				return size;
			};

			var ie6scroll = function(){ 
				$lb_b.css({ top: getWindowScrollOffset() }); 
			};

			var escapeKeyCloseLightbox = function(e){
				console.log(e.which)
				//if(e.which == 27 || (e.DOM_VK_ESCAPE == 27 && e.which==0)) removeLightbox();
			};

			var positionLightbox = function(){
				var wsize = getWindowSize();
				$lb_b.css({ position: (ie6)? "absolute" : "fixed", height: wsize.height, width: "100%", top: (ie6)? getWindowScrollOffset():0, left: 0, right: 0, bottom: 0 });
				$lb_f.css({ position: "absolute", height: wsize.height, width: "100%", top: 0, left: 0, right: 0, bottom: 0 });
				$lb_e.css({ position: "absolute", top: "40px", left: "50%", marginLeft: (((($lb_e.css("paddingLeft").split("px")[0]*1) + $lb_e.width())/2)*-1) });					
			};

			var styleLightbox = function(){
				$lb_f.css({ zIndex: o.zIndex, display: "none", opacity: o.opacity });
				$lb_e.css({ zIndex: o.zIndex+1, display: "none" });
			}

			var removeLightbox = function(clicked){
				$('body').prepend($lb_e.hide());
				if(ie6) $b.unbind('scroll',ie6scroll);//ie6, remove the scroll event
				$w.unbind('resize',positionLightbox);			
				$lb_f.fadeOut('normal',function(){
					$lb_f.remove();
					if(o.callback()) o.callback();
					$w.unbind('keypress',escapeKeyCloseLightbox);
					if(ie6) $('select').css('visibility','visible');
					$lb_b.remove();
				});
			};

			positionLightbox();
			styleLightbox();

			if(ie6) $w.scroll(ie6scroll);//ie6, add a scroll event to fix position:fixed
			$w.resize(positionLightbox);
		//	$w.keypress(escapeKeyCloseLightbox);
			$lb_e.find(o.closeButton).click(removeLightbox);
			
			//Show it
			$lb_f.fadeIn(o.overlayspeed);
			$lb_e[o.show](o.Lightboxspeed,o.loaded);	
		});
		
	}
})(jQuery);
