(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);
            gobj=$(this).first();
			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');
			}
			if (gobj.get(0)==undefined)
				return gobj
			tagName=gobj.get(0).tagName.toLowerCase();
			if (tagName!='a')
				obj=options.url;			
			else 	
				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('');
				$('#lightWindowBox').show();
				$('<div onclick="$.lightBoxClose()" class="lightCancel"> </div><div class="light_navContainer"><div onclick="$.lightBoxPrev()" class="light_prevButton"></div><div onclick="$.lightBoxNext()"  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;
				}
				options.sclass=gobj.attr('rel');
				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;
					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});
			$('#lightWindowBox').show();
			$('#lightWindowOverlay').css('cursor','progress');
			$('#lightWindowOverlay').fadeIn();
			

			$('#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');
		}
$.lightBoxText= function(t) {
			options=$('#lightWindowBox').data('options');
			$temp=$('<div></div>').appendTo('body').hide().html(t);
			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(){});
			$('#lightWindowBox').data('options',options);

			$('#lightWindowBox').html(t);
		}
$.lightBoxNext= function(t) {
			options=$('#lightWindowBox').data('options');
			npoz=options.cpos;
			npoz++;
			if ($($('[rel='+options.sclass+']')[npoz]).length>0) {
				options.cpos=npoz;
				gobj=$($('[rel='+options.sclass+']')[npoz]);
			} else {
				npoz=0;
				options.cpos=npoz;
				gobj=$($('[rel='+options.sclass+']')[npoz]);
			};
			if (gobj.get(0)==undefined)
				return gobj
			tagName=gobj.get(0).tagName.toLowerCase();

			if (tagName!='a')
				obj=options.url;			
			else 	
				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 onclick="$.lightBoxPrev()" class="light_prevButton"> </div><div onclick="$.lightBoxNext()"  class="light_nextButton"> </div></div><img src="'+obj+'" />').appendTo('#lightWindowBox');

				npoz=$('.'+options.sclass).index(gobj);
				if ($($('.'+options.sclass)[npoz]).length>0) {
					options.cpos=npoz;
				}
				var newImg = new Image();
				newImg.src = obj;
				$(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;
					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');
				});
			}
			
			$('#lightWindowBox').data('options',options);

		}

$.lightBoxPrev= function(t) {
			options=$('#lightWindowBox').data('options');
			npoz=options.cpos;
			npoz--;
			if ($($('[rel='+options.sclass+']')[npoz]).length>0) {
				options.cpos=npoz;
				gobj=$($('[rel='+options.sclass+']')[npoz]);
			} else {
				npoz=0;
				options.cpos=npoz;
				gobj=$($('[rel='+options.sclass+']')[npoz]);
			};
			if (gobj.get(0)==undefined)
				return gobj
			tagName=gobj.get(0).tagName.toLowerCase();

			if (tagName!='a')
				obj=options.url;			
			else 	
				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 onclick="$.lightBoxPrev()" class="light_prevButton"> </div><div onclick="$.lightBoxNext()"  class="light_nextButton"> </div></div><img src="'+obj+'" />').appendTo('#lightWindowBox');

				npoz=$('.'+options.sclass).index(gobj);
				if ($($('.'+options.sclass)[npoz]).length>0) {
					options.cpos=npoz;
				}
				var newImg = new Image();
				newImg.src = obj;
				$(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;
					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');
				});
			}
			
			$('#lightWindowBox').data('options',options);

		}

