(function($){
	var a=$(window);
	$.fn.viewportOffset = function(){var b=$(this).offset();return{left:b.left-a.scrollLeft(),top:b.top-a.scrollTop()}}

})(jQuery);
(function($){
    $.fn.extend({
        lightbox: function(options) {
            var defaults = {
                width: '200',
                height: '60',
				position: 'fixed',
				overstate:'hidden',
				sclass:'lightbox',
				size:'auto',
				cpos:'0',
				url:false
            };
			var options = $.extend(defaults, options);
			if ($('#lightWindowOverlay').length==0) {
				$('<div id="lightWindowOverlay" style="display:none;position:fixed;float:none;height:100%; left:0px; background-color:#000; top:0px;width:100%; z-index:9500;-moz-opacity: 0.7;opacity:.70;text-align:center; ; filter: alpha(opacity=70);_height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight+\'px\')"></div>').appendTo('body');
				$('<div id="lightWindowBox" style="display:none; position:'+options.position+'; width:365px; z-index:10000; font-family:Tahoma,Helvetica,sans-serif;background:#FFFFFF none repeat scroll 0 0;padding:0px;overflow:'+options.overstate+'; border:1px solid #333;"></div>').appendTo('body');
			}
            objs=$(this);
			objs.each(function(idx,e){
				$(e).data('options',options);
				$(e).click(function(){
					return $(this).lightBoxShow();
				});
			});
		
		},
		lightBoxShow:function(){
			gobj=$(this).first();
			if (gobj.get(0)==undefined)
				return gobj
			var options=gobj.data('options');
			tagName=gobj.get(0).tagName.toLowerCase();
			if (tagName=='img')
				obj=gobj.attr('src');			
			else 	
				if (tagName=='a')
					obj=gobj.attr('href');
			if (!obj)
				return ;
			$('#lightWindowBox').html('<p style="text-align:center; padding:20px;">Loading...</p>');
			
			if ($(obj).endsWith('jpg') || $(obj).endsWith('png') || $(obj).endsWith('gif'))  {
				$('#lightWindowBox').html('');
				$('<div onclick="$.lightBoxClose()" class="lightCancel"> </div><div class="light_navContainer"><div class="light_prevButton"></div><div class="light_nextButton"></div></div><img src="'+obj+'" />').appendTo('#lightWindowBox');
				npoz=$('[rel='+gobj.attr('rel')+']').index(gobj);
				if ($($('[rel='+gobj.attr('rel')+']')[npoz]).length>0) {
					options.cpos=npoz;
				}
				
				lpoz=npoz+1;
				if ($($('[rel='+gobj.attr('rel')+']')[lpoz]).length>0) {
					lobj=$($('[rel='+gobj.attr('rel')+']')[lpoz]);
				} else {
					lpoz=0;
					lobj=$($('[rel='+gobj.attr('rel')+']')[lpoz]);
				};

				if (gobj.get(0)!=undefined) {
					$('.light_nextButton').click(function(){
						lobj.lightBoxShow();
					})
				}
				
				
				lpoz=npoz-1;
				if ($($('[rel='+gobj.attr('rel')+']')[lpoz]).length>0) {
					lobj=$($('[rel='+gobj.attr('rel')+']')[lpoz]);
				} else {
					lpoz=0;
					lobj=$($('[rel='+gobj.attr('rel')+']')[lpoz]);
					
				};
				if (gobj.get(0)!=undefined) {
					$('.light_prevButton').click(function(){
						lobj.lightBoxShow();
					})
				}
				
				var newImg = new Image();
				newImg.src = obj+'?rand='+Math.random()*100;
				$(newImg).load(function(){
					var windowHeight=$(window).height();
					var windowWidth=$(window).width();
					boxTop=parseInt(windowHeight/2)-parseInt(this.height/2);
					if (boxTop<=5) boxTop=5;
					if (options.position!='fixed') 
						boxTop+=$(document).scrollTop();
					boxLeft=parseInt(windowWidth/2)-parseInt(this.width/2);
					if (boxLeft<=5) boxLeft=5;
									console.log('x2');
					if ($('#lightWindowBox').css('position')!='fixed') 			
						boxLeft+=$(document).scrollLeft();
						$('#lightWindowBox').stop();
						$('#lightWindowBox').animate({opacity:1,left:boxLeft,top:boxTop,width:this.width,height:this.height},600,function(){});
						$('#lightWindowOverlay').css('cursor','pointer');
				});
			}
			 else 
			{
            	var local = {};
				if (gobj.attr('options')!=undefined)
					options = $.extend(options, $.parseJSON(gobj.attr('options').replace(/'/g,"\"")));
				 $.ajax({url:obj,success:function(data){
					if ($('#lightWindowOverlay').is(':visible'))
					if (options.size=='auto') {
						$temp=$('<div></div>').appendTo('body').hide().html(data);
						cheight=$temp.height();
						cwidth=$temp.width();
						$temp.remove();
						options.height=cheight;
						options.width=cwidth;

						var windowHeight=$(window).height();
						var windowWidth=$(window).width();
						
						boxTop=parseInt(windowHeight/2)-parseInt(cheight/2);
						if (boxTop<=5) boxTop=5;
						if (options.position!='fixed') 
							boxTop+=$(document).scrollTop();
		
						boxLeft=parseInt(windowWidth/2)-parseInt(cwidth/2);
						if (boxLeft<=5) boxLeft=5;
						if ($('#lightWindowBox').css('position')!='fixed') 			
							boxLeft+=$(document).scrollLeft();
						//$('#lightWindowBox').stop();
						
						$('#lightWindowBox').animate({opacity:1,left:boxLeft,top:boxTop,width:cwidth,height:cheight},600,function(){});
						$('#lightWindowOverlay').css('cursor','pointer');
					}
					$('#lightWindowBox').html(data);
				}})		
			}
			var offset=gobj.viewportOffset();
				offset.top+=parseInt(gobj.height()/2);
				offset.left+=parseInt(gobj.width()/2);			
			var windowHeight=$(window).height();
			var windowWidth=$(window).width();
			
			boxTop=parseInt(windowHeight/2)-parseInt(options.height/2);
			if (boxTop<=5) boxTop=5;
			if (options.position!='fixed') 
				boxTop+=$(document).scrollTop();

			boxLeft=parseInt(windowWidth/2)-parseInt(options.width/2);
			if (boxLeft<=5) boxLeft=5;
			if (options.position!='fixed') 			
				boxLeft+=$(document).scrollLeft();
			$('#lightWindowBox').css({'top':offset.top,'left':offset.left,'width':'0px','height':'0px','position':options.position,'overflow':options.overstate});
			$('#lightWindowOverlay').css('cursor','progress');
			$('#lightWindowOverlay').fadeIn();
			$('#lightWindowBox').show();
			//$('#lightWindowBox').animate({opacity:1,left:boxLeft,top:boxTop,width:options.width,height:options.height},600,function(){});
			$('#lightWindowOverlay').click(function() { 
				$('#lightWindowOverlay').fadeOut('slow');
				$('#lightWindowBox').fadeOut('fast');
			} )

			$('#lightWindowBox').data('options',options);
			return false;
		},
		
		endsWith: function(pattern) {
			o=this.selector;
			var d = o.length - pattern.length;
			return d >= 0 && o.lastIndexOf(pattern) === d;
		}
		})
})(jQuery);

jQuery.lightBoxClose = function() {
				$('#lightWindowOverlay').fadeOut('slow');
				$('#lightWindowBox').fadeOut('fast');
		}

