/*$.fn.lightbox = function(settings) {
	this.defaults = {
		 width:"200",
		 height:"200",
		 element:null
	}
	this.store = { }
	var opts = $.extend({}, this.defaults, settings);
	var storeAttrs = $.extend({}, this.store);
	this.init = function() {
		$(this).css({
						'width':opts.width+'px',
						'height':opts.height+'px'
					});
		
	}
	this.init();
}
*/

var LightBox = {
	init:function() {
		$('A[_lightbox]').click(this.show);
	}
	,show:function(e) {
		var el = $(e.currentTarget);
		var opts = $(el).attr("_opts");
		var content = $('#'+$(el).attr("_lightbox")).clone(true);
		$(document.body).append(content);
		LightBox.backgroundFade(true,false,true);
		var cWidth = $(content).outerWidth(true);
		var cHeight = $(content).outerHeight(true);
		LightBox.lb = document.createElement('DIV');
		//var marginTop = cHeight + (parseInt($(LightBox.lb).css('padding').replace('px',''),10)*2);
		if(opts != "static")
			var marginTop = (($(window).height() - cHeight) / 2) + $(document).scrollTop();
		else
			var marginTop = 100;
		var docW = $(window).width();
		var lbLeft = ((docW-cWidth) / 2) - 16;
		
		$(LightBox.lb).css({
				  'top':-marginTop+'px',
				  //'width':$(content).outerWidth(true)+'px',
				  //'left':lbLeft+'px',
				  'width':'50px',
				  'left':((docW-100) / 2)+'px',
				  'height':$(content).outerHeight(true)+'px',
				  'position':'absolute',
				  'backgroundColor':'#FFFFFF',
				  'border':'8px solid #313131',
				  //'-moz-border-radius':'8px',
				  //'-webkit-border-radius':'8px',
				  'opacity':'0',
				  'zIndex':'9999'
				  });
		
		function createXout() {
			var xOut = document.createElement('IMG');
			$(xOut).attr("src","images/lightbox/x-out.png");
			$(xOut).addClass("lb-xOut");
			$(xOut).css({
							'top':'0px',
							'right':'0px',
							'position':'absolute',
							'padding':'5px',
							'display':'none',
							'cursor':'pointer',
							'width':'20px',
							'height':'20px'
							});
			$(xOut).click(LightBox.close);
			$(LightBox.lb).append(xOut);	
		}
		
		$(LightBox.lb).mousemove(function() {
			var xOut = $(LightBox.lb).find('.lb-xOut');
			$(xOut).fadeIn(200);
		}).mouseleave(function() {
			var xOut = $(LightBox.lb).find('.lb-xOut');
			$(xOut).fadeOut(200);
		});
		LightBox.origLocation = marginTop;
		LightBox.startScrollPos = $(document).scrollTop();
		if(opts != "static") {
			$(window).scroll(function() {
				var addMove = ($(window).height() - $(LightBox.lb).outerHeight(true)) / 2;
				$(LightBox.lb).animate({'top':addMove+$(document).scrollTop()+'px'},{queue:false},200,'linear');
			});
		}
		
		$(LightBox.lb).append($(content));
		$(document.body).append(LightBox.lb);
		$(LightBox.lb).animate({ 'top':marginTop+'px','opacity':'1','left':lbLeft+'px' },
					  { queue: false, easing:'easeOutBack', duration:1000, complete:function() { $(content).fadeIn(500); } }).animate({ 'width':(cWidth)+'px','height':(cHeight)+'px' },1000,'easeOutBack',createXout);
		
		return false;
	}
	,close:function(e) {
		$(window).unbind("scroll");
		LightBox.backgroundFade(false,false,true);
		$(LightBox.lb).animate({
						   'top':-($(LightBox.lb).outerHeight(true))+'px',
						   'opacity':'0'
						   },1000,function() { $($(e.currentTarget).parent()).remove(); });
		
	}
	,backgroundFade:function(show,callback,noAnimate) {
		if(show) {
			var setHeight = function() {
				var body = document.body,
				html = document.documentElement;
				var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
				$(Core.modalDiv).css({'height':height+'px'});
			}
			$(window).resize(setHeight);
			var body = document.body,
			html = document.documentElement;
			var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
			this.modalDiv = document.createElement('DIV');
			document.body.appendChild(this.modalDiv);
			$(this.modalDiv).css({
					'position':'absolute',
					'backgroundColor':'#000000',
					'height':height+'px',
					'width':'100%',
					'top':'0',
					'left':'0',
					'opacity':'0',
					'zIndex':'999'
					});
			if(!noAnimate) {
				$(this.modalDiv).animate({
						'opacity':'0.7'
						},400,callback);
			}
			else {
				$(this.modalDiv).css({'opacity':'0.7'});	
			}
		}
		else {
			if(!noAnimate) {
				$(this.modalDiv).animate({
						'opacity':'0'
						},function() { $(Core.modalDiv).remove(); });
			}
			else {
				$(this.modalDiv).remove();
			}
		}
	}
	
}

$(document).ready(function() {
	LightBox.init();
});
