function SimpleLightBox() {
	this.x;
	this.y;
	this.src_gallery;
	this.desc_gallery;
	this.diff=200;
	this.originalWidth=0;
	this.originalHeight=0;
	this.showThumbs = false;
	this.bgColor = '#FFFFFF';
	this.border  = '3px solid #000000';
	this.opacity  = 85;
	this.animate_overlay = true;
	this.self = this;
	this.currentIndex = 0;
	this.pageSize = function() {
	    var xScroll, yScroll;
	   
	    if (window.innerHeight && window.scrollMaxY) {
	        xScroll = window.innerWidth + window.scrollMaxX;
	        yScroll = window.innerHeight + window.scrollMaxY;
	    } else if (document.body.scrollHeight > document.body.offsetHeight) {
	        xScroll = document.body.scrollWidth;
	        yScroll = document.body.scrollHeight;
	    } else {
	        xScroll = document.body.offsetWidth;
	        yScroll = document.body.offsetHeight;
	    }
	    var windowWidth, windowHeight;
	    if (self.innerHeight) {
	        if (document.documentElement.clientWidth) {
	            windowWidth = document.documentElement.clientWidth;
	        } else {
	            windowWidth = self.innerWidth;
	        }
	        windowHeight = self.innerHeight;
	    } else if (document.documentElement && document.documentElement.clientHeight) {
	        windowWidth = document.documentElement.clientWidth;
	        windowHeight = document.documentElement.clientHeight;
	    } else if (document.body) {
	        windowWidth = document.body.clientWidth;
	        windowHeight = document.body.clientHeight;
	    }
	    if (yScroll < windowHeight) {
	        pageHeight = windowHeight;
	    } else {
	        pageHeight = yScroll;
	    }
	    if (xScroll < windowWidth) {
	        pageWidth = xScroll;
	    } else {
	        pageWidth = windowWidth;
	    }
	   
	    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	    return arrayPageSize;
	};
	
	this.onClose = function(){
		
	};
	
	
	this.toggleSelects = function(state){
		$('select').each(function(){
			$(this).css("visibility",(state=='hide'?'hidden':'visible'));
		});
	};
	
	this.toggleFlash = function(state){
		
		$('object').each(function(){
			$(this).css("visibility",(state=='hide'?'hidden':'visible'));
		});
		$('embed').each(function(){
			$(this).css("visibility",(state=='hide'?'hidden':'visible'));
		});
	};
	
	
	this.loadFile = function(file){
	   var _this = this;
	   jQuery.ajax({
		   async: true,
		   type: "GET",
		   url: file,
		   success: function(data){
				_this.loadHTML(data);			
		   }
		});
	};
	this.loadImage=function(src){
		var image = new Image();
		var self = this;
		self.loadHTML('<div style="background: url(images/admin/ajax-loader.gif) center center no-repeat;width:100px;height:100px"></div>');
		image.onload=function(){
			self.loadHTML('<div onclick="MySimpleLightBox.close();" style="cursor:pointer;position:relative;width:'+image.width+'px;height:'+image.height+'px"><div style="position:absolute;right:0px;top:0px"></div><img src="'+src+'" border="0"></div>');
		};
		image.src=src;
	};
	this.loadCode = function (code,close_description){
		var html='';
		html+= '<div style="background: url(images/admin/ajax-loader.gif) center center no-repeat">';
		html+= code;
		html+= '<div style="width:100%;text-align:right;"><a href="javascript:void(0)" onclick="MySimpleLightBox.close();">'+close_description+'</a></div>';	
		html+= '</div>';
		this.loadHTML(html);
		
	};
	this.loadGallery = function (src_array,desc_array,current){
		
		this.loadHTML('<div style="background: url(images/admin/ajax-loader.gif) center center no-repeat;width:100px;height:100px"></div>');
		this.src_gallery = src_array;
		this.desc_gallery = desc_array;
		var first_img = current!=undefined?current:this.src_gallery[0];
		
		var img = new Image();
		var self = this;
		
		img.onload = function(){
			var html ='';
			html+='<table>';
				html+='<tr>';
					html+='<td>';
						html+='<table>';
							html+='<tr>';
								html+='<td style="position:relative;">';
									html+='<div style="position:relative">';
										html+='<div id="gallery_main_box"		 style="cursor:pointer;"><img src="" id="gallery_main_image" /></div>';
										html+='<div id="gallery_description_box" style="z-index:10003;position:absolute;bottom:0px;background:#000000;color:#FFF;width:98%;height:30px;line-height:30px;text-align:right;padding-right:2%"></div>';
										html+='<div id="gallery_description"     style="z-index:10003;position:absolute;bottom:0px;color:#FFF;width:98%;height:30px;line-height:30px;text-align:right;padding-right:2%"></div>';
										html+='<div id="gallery_close"           style="z-index:10007;position:absolute;top:10px;right:10px;height: 24px;width:24px;background:url(images/admin/close_image.png) center no-repeat;cursor:pointer;" 	onclick="MySimpleLightBox.close();"></div>';
										html+='<div id="gallery_go_left"  		 style="z-index:10005;position:absolute;top:0px;left:0px;  background:url(images/admin/go_prev.png) scroll left  center no-repeat;height: 24px;width: 50%;"></div>';
										html+='<div id="gallery_go_right" 		 style="z-index:10005;position:absolute;top:0px;right:0px; background:url(images/admin/go_next.png) scroll right center no-repeat;height: 24px;width: 50%;"></div>';
									html+='</div>';
								html+='</td>';
							html+='</tr>';
						html+='<table>';	
					html+='</td>';
				html+='</tr>';
				if(self.showThumbs){
					html+='<tr>';
						html+='<td id="gallery_thumbs" style="padding-top:10px;text-align:center">'+self.showThumbs;
						if(self.src_gallery.length>1){
							for(var i=0;i<self.src_gallery.length;i++){
								html+='<img onclick="MySimpleLightBox.changeImage(\''+self.src_gallery[i]+'\');"  src="'+self.src_gallery[i]+'"  style="height:30px;float:left;margin:5px 5px 0px 0px;cursor:pointer" title="'+self.desc_gallery[i]+'"/>';
							}	
						}	
						html+='</td>';
					html+='</tr>';
				}
			html+='</table>';
			self.loadHTML(html);
			
			$("#gallery_go_left,#gallery_go_right").bind("mousemove",function(event){
				self.x = event.pageX;
				self.y = event.pageY;
			});
			
			
			//Left arrow
			$("#gallery_go_left").bind("mouseover mouseenter",function(event){
				self.showPrevIcon();
			});
			$("#gallery_go_left").bind("mouseout mouseleave",function(event){
				self.hidePrevIcon();
			});
			$("#gallery_go_left").bind("click",function(event){
				self.showPreviousImage();
			});
			
			//Right arrow
			
			$("#gallery_go_right").bind("mouseover mouseenter",function(event){
				self.showNextIcon();
			});
			
			$("#gallery_go_right").bind("mouseout mouseleave",function(event){
				self.hideNextIcon();
			});
			$("#gallery_go_right").bind("click",function(event){
				self.showNextImage();
			});
			
			self.changeImage(first_img);
			self.highlightThumb(self.getImageIndex(first_img));
			$("#gallery_thumbs img:first").css({marginLeft:'0px'});
			
			
		};
	
		img.src=first_img.replace('thumb_','large_');
	};
	
	this.initializeArrows=function(){
		this.hideArrows();
		var top = $("#gallery_main_box").offset().top;
		var bottom = $("#gallery_main_box").offset().top+$("#gallery_main_box").outerHeight();
		var left = $("#gallery_main_box").offset().left;
		var right = $("#gallery_main_box").offset().left+$("#gallery_main_box").outerWidth();
		var h = $("#gallery_main_box").outerHeight();
		var w = $("#gallery_main_box").outerWidth();
		$("#gallery_go_left").css({height: h+"px"});
		$("#gallery_go_right").css({height:h+"px"});
		
		if(this.y>top && this.y<bottom && this.x>left && this.x<right){
			if(this.x < parseInt(left+(w/2))){
				this.showPrevIcon();
			}
			else{
				this.showNextIcon();
			}
		}
	};
	
	this.showNextIcon=function(){
		var idx = this.getImageIndex($("#gallery_main_image").attr("src").replace("large_","thumb_"));
		if(idx==this.src_gallery.length-1){
			this.hideNextIcon();
		}
		else{
			$("#gallery_go_right").css("background","transparent url(images/admin/go_next.png) 95% center no-repeat scroll");
			$("#gallery_go_right").css("cursor","pointer");
		}
	};
	
	
	this.showPrevIcon=function(){
		var idx = this.getImageIndex($("#gallery_main_image").attr("src").replace("large_","thumb_"));
		if(idx==0){
			this.hidePrevIcon();
		}
		else{
			$("#gallery_go_left").css("background","transparent url(images/admin/go_prev.png) 5% center no-repeat scroll");
			$("#gallery_go_left").css("cursor","pointer");
		}
		
	};
	
	this.hidePrevIcon=function(){
		$("#gallery_go_left").css("background","url(images/admin/transparency.gif)");
	};
	
	this.hideNextIcon=function(){
		$("#gallery_go_right").css("background","url(images/admin/transparency.gif)");
	};
	
	this.highlightThumb=function(idx){
		if(self.showThumbs){
			$("#gallery_thumbs img").css("border","none");
			$("#gallery_thumbs img:eq("+idx+")").css("border","1px solid red");
		}
	};
	
	this.getImageIndex=function(image){
		for(var i=0;i<this.src_gallery.length;i++){
			if(image==this.src_gallery[i] || image.indexOf(this.src_gallery[i])!=-1){
				return i;
			}
		}
	};
	
	
	
	this.centerArrows=function(){
		var h = $("#gallery_main_box").outerHeight();
		var w = $("#gallery_main_box").outerWidth();
		
		$("#gallery_go_left").css({top:((h-$("#gallery_go_left").outerHeight())/2)+"px"});
		$("#gallery_go_right").css({top:((h-$("#gallery_go_right").outerHeight())/2)+"px",left:(w-$("#gallery_go_right").outerWidth())+"px"});
	};
	
	this.hideArrows=function(){
		this.hidePrevIcon();
		this.hideNextIcon();
	};
	
	
	this.showPreviousImage=function(){
		var idx = this.getImageIndex($("#gallery_main_image").attr("src").replace("large_","thumb_"));
		if(idx>0){
			var prev = idx-1;
			if(prev==0){
				this.hidePrevIcon();
			}
			this.showNextIcon();
			this.changeImage(this.src_gallery[prev]);
		}
		else{
			this.close();
		}
		
	};
	
	this.showNextImage=function(){
		var idx = this.getImageIndex($("#gallery_main_image").attr("src").replace("large_","thumb_"));
		if(idx<this.src_gallery.length-1){
			var next = idx+1;
			if(next==this.src_gallery.length-1){
				this.hideNextIcon();
			}
			this.showPrevIcon();
			this.changeImage(this.src_gallery[next]);
		}
		else{
			this.close();
		}
	};
	
	this.changeImage=function(image){
		var self = this;
		var idx = self.getImageIndex(image);
		var src = self.src_gallery[idx].replace('thumb_','large_');
		var desc = self.desc_gallery[idx];
		self.currentIndex = idx;
		self.hideArrows();
		$('#gallery_main_box').addAjaxLoader();
		$('#gallery_description_box').fadeTo("fast", 0);
		
		var img = new Image();
		img.onload = function(){
			if($('#gallery_main_box img').length==0){
				$('#gallery_main_box').append('<img src="'+src+'">');
			}
			else{
				$('#gallery_main_box img').attr('src',src);
			}
			$('#gallery_main_box img').attr("width",(this.width>640?640:this.width));
			$('#gallery_main_box img').attr("height",parseInt($('#gallery_main_box img').attr("width"))*this.height/this.width);
			
			
			$('#gallery_main_box img').fadeIn();
			$('#gallery_main_box img').attr("title",desc);
			$('#gallery_description_box').html(desc);
			$('#gallery_thumbs').css("width",$('#gallery_main_box img').css("width"));
			if(jQuery.trim($('#gallery_description_box').html())!=''){
				$('#gallery_description_box').fadeTo("fast", 0.8);
			}
			$("#gallery_description_box,#gallery_description").css({bottom:'0px'});
			$("#gallery_go_left,#gallery_go_right").css({height:$("#gallery_main_box").outerHeight()+"px"});
			$('#gallery_main_box').removeAjaxLoader();
			self.highlightThumb(idx);
			self.centerContent();
			self.initializeArrows();
		
			
			
		};
		img.src=src;
	};
	
	this.centerContent=function(){
		var pagesize = this.pageSize();
		$("#lboxContent").css({
            position: 'absolute',
            top: (parseInt($(document).scrollTop()) + parseInt((pagesize[3]-parseInt($('#lboxContent').height()))/2))+"px",
            left: '50%',
            marginLeft: -$('#lboxContent').width()*0.5,
            'z-index': 10001
        });
	};
	
	this.loadHTML = function(html){
		var pagesize = this.pageSize();
		var boxContent = null;
		if($("#lboxContent").length>0){
			$("#lboxContent").remove();
		}
		boxContent = document.createElement("DIV");
		$(boxContent).attr('id','lboxContent');
		$(boxContent).html(html);
		
		$('body').append($(boxContent));

		$(boxContent).hide().css({
            position: 'absolute',
            top: (parseInt($(document).scrollTop()) + parseInt((pagesize[3]-parseInt($('#lboxContent').height()))/2))+"px",
            left: '50%',
            marginLeft: -$('#lboxContent').width()*0.5,
            'z-index': 10001
        });
		
		if($('#lbox').length==0){
	        $('body').append('<div id="lbox"></div>');
	        $('#lbox').css({
	            position: 'absolute',
	            top: 0,
	            left: 0,
	            width: pagesize[0],
	            height: pagesize[1],
	            opacity: 0,
	            background: this.bgColor,
	            'z-index': 10000
	        }).animate({
	            opacity: (this.opacity/100)
	        },function(){
	        	
	        	$(boxContent).fadeIn("fast",function(){
	        		
	        	});
	        });
        }
        else{
        	$(boxContent).fadeIn();
        }
		
	};

	this.close = function(){
			
		try{
			$("body").css("overflow","visible");
			this.toggleSelects('show');
			this.toggleFlash('show');
			if($('#lbox').length !=0){
  				$('#lbox').remove();
	  		}
	  		if($('#lboxContent').length!=0){
	  			$('#lboxContent').remove();
	  		}
		}
		catch(ex){
		}
		this.onClose();
	};
}
var MySimpleLightBox = new SimpleLightBox();