function SimpleLightBox() {
	this.x;
	this.y;
	this.src_gallery;
	this.desc_gallery;
	this.diff=200;
	this.originalWidth=0;
	this.originalHeight=0;
	this.bgColor = '#CCC';
	this.border  = '3px solid #000000';
	this.opacity  = 85;
	this.animate_overlay = true;
	this.self = this;
	this.showTumbs = false;
	this.phpResizeFile = '';
	this.flvplayer = '';
	this.flvplayerWidth 	= 320;
	this.flvplayerHeight 	= 240;
	this.borderTopHeight 	= 38;
	this.borderBottomHeight = 38;
	this.borderLeftWidth 	= 38;
	this.borderRightWidth 	= 38;
	this.imageDataHeight 	= 34;
	
	
	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.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 class="lbox-ajax-loader"></div>');
		image.onload=function(){
			$(".lboxContent:last").remove();
			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.toggleSelects('hide');
		this.toggleFlash('hide');
		pageSize = this.pageSize();
		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;
		var html ='';
		
		
		html+='<table class="simpleLightBoxBorders" border="0" cellspacing="0" cellpadding="0">';
			html+='<tr>';
				html+='<td class="simpleLightBoxBorderTopLeft png"></td>';
				html+='<td class="simpleLightBoxBorderTopCenter png"></td>';
				html+='<td class="simpleLightBoxBorderTopRight png"></td>';
			html+='</tr>';
			html+='<tr>';
				html+='<td class="simpleLightBoxBorderMiddleLeft"></td>';
				html+='<td>';
					
					html+='<table style="" border="0" cellspacing="0" cellpadding="0">';
							html+='<tr>';
								html+='<td id="gallery_main_box_content">';
											if(src_array.length > 1){
												html+='<div id="gallery_main_box"	style="cursor:pointer;overflow:hidden;position:relative;">';
													html+='<div id="gallery_go_left"   style="z-index:10005;position:absolute;top:0px;left:0px;  height: 24px;width: 50%;"></div>';
													html+='<div id="gallery_go_right"  style="z-index:10005;position:absolute;top:0px;right:0px; height: 24px;width: 50%;"></div>';
												html+='</div>';
											}else{
												html+='<div id="gallery_main_box"	style="cursor:default;overflow:hidden;position:relative;">';
												html+='</div>';
											}
											html+='<div class="simpleLightBoxImageData">';
												html+='<div class="simpleLightBoxImageDataDescription"></div>';
												html+='<div class="simpleLightBoxImageDataClose" onclick="MySimpleLightBox.close();" style="display:none;"></div>';
												html+='<div class="simpleLightBoxImageDataImages"></div>';
											html+='</div>';
											
								html+='</td>';
							html+='</tr>';
					html+='</table>';
					
				html+='</td>';
				html+='<td class="simpleLightBoxBorderMiddleRight"></td>';
			html+='</tr>';
			html+='<tr>';
				html+='<td class="simpleLightBoxBorderBottomLeft"></td>';
				html+='<td class="simpleLightBoxBorderBottomCenter"></td>';
				html+='<td class="simpleLightBoxBorderBottomRight"></td>';
			html+='</tr>';
		html+='</table>';	
		
		//html='<table style="margin:0 auto;padding:0;position:absolute;left:0;width:'+pageSize[2]+'px;height:'+pageSize[3]+'px;"><tr><td>'+html+'</td></tr></table>';
		
		self.loadHTML(html,false);
		$("#gallery_main_box").css({height:200,width:200});
		self.showLoadingBox($('#gallery_main_box'));
		self.centerContent();
		
		$("#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();
		});
		
		if(self.getFileExtension(first_img)=='flv'){
			self.changeImage(first_img);
		}
		else{
			self.changeImage(first_img);
		}
        
	};
	
	this.initializeArrows=function(isFlv){
		isFlv = isFlv==undefined?false:isFlv;
		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-(isFlv?25:0))+"px"});
		$("#gallery_go_right").css({height:(h-(isFlv?25:0))+"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.getCleanImagePath=function(image){
		if(image.indexOf("url(")!=-1){
			image = image.substr(4,image.length-5);
		}
		return image;
	};
	
	this.getMainImagePath=function(){
		var src = $("#gallery_main_box .simpleLightBoxMainImage:first").attr('src');
		if(src == undefined || src == ''){
			src = $("#gallery_main_box .simpleLightBoxMainImage:first").data("src");
		}
		src = this.getCleanImagePath(src);
		
		return src;
	};
	
	this.showNextIcon=function(){
		if($('#gallery_main_box .simpleLightBoxMainImage').length > 1 || $('#gallery_main_box .loading-box').length > 0){
			this.hideNextIcon();
		}
		else{
			if(!$("#gallery_go_right").hasClass("active")){
				$("#gallery_go_right").addClass("active");
			}
		}	
		
	};
	
	
	this.showPrevIcon=function(){
		if($('#gallery_main_box .simpleLightBoxMainImage').length > 1 || $('#gallery_main_box .loading-box').length > 0){
			this.hidePrevIcon();
		}
		else{
			if(!$("#gallery_go_left").hasClass("active")){
				$("#gallery_go_left").addClass("active");
			}
		}
		
	};
	
	this.hidePrevIcon=function(){
		$("#gallery_go_left").removeClass("active");
	};
	
	this.hideNextIcon=function(){
		$("#gallery_go_right").removeClass("active");
	};
	
	this.highlightThumb=function(idx){
		$("#gallery_thumbs img").css("border","none");
		$("#gallery_thumbs img:eq("+idx+")").css("border","1px solid red");
	};
	
	this.getImageIndex=function(image){
		image = image.replace(/%20/g," ");
		for(var i=0;i<this.src_gallery.length;i++){
			
			if(this.src_gallery[i].indexOf(image)!=-1 || image.indexOf(this.src_gallery[i])!=-1 || image == this.src_gallery[i] ){
				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(){
		if($('#gallery_main_box .loading-box').length == 0){
			var idx = this.getImageIndex(this.getMainImagePath());
			if(idx>0){
				var prev = idx-1;
				if(prev==0){
					this.hidePrevIcon();
				}
				this.showNextIcon();
				this.changeImage(this.src_gallery[prev]);
			}
			else{
				this.changeImage(this.src_gallery[this.src_gallery.length-1]);
			}
		}
	};
	
	this.showNextImage=function(){
		if($('#gallery_main_box .loading-box').length == 0){
			var idx = this.getImageIndex(this.getMainImagePath());
			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{
				idx = 0;
				this.changeImage(this.src_gallery[0]);
			}
		}
	};
	
	this.getImageMaxDimensions = function(w,h){
		var pagesize = this.pageSize();
		var allowedW = pagesize[2] - this.borderLeftWidth - this.borderRightWidth;
		var allowedH = pagesize[3] - this.borderTopHeight - this.borderBottomHeight - this.imageDataHeight;
		var maxw = w;
		var maxh = h;
		
		if(w > allowedW){
			maxw = allowedW;
			maxh = maxw*h/w;
		}
		else if (h > allowedH) {
			maxh = allowedH;
			maxw = maxh*w/h;
		}
		
		return [maxw,maxh];
	};
	
	this.resizeGalleryContent=function(filename,w,h,callback){
		callback = callback==undefined?false:callback;
		var self = this;
		var idx = self.getImageIndex(filename);
		var src = self.src_gallery[idx];
		var desc = self.desc_gallery[idx];
	
		self.hideArrows();
		
		var pagesize = self.pageSize();
		var allowedW = pagesize[2] - this.borderLeftWidth - this.borderRightWidth;
		var allowedH = pagesize[3] - this.borderTopHeight - this.borderBottomHeight - this.imageDataHeight;
		var auxwidth=0,auxheight=0;
		var maxw = w;
		var maxh = h;
		
		if(w > allowedW){
			maxw = allowedW;
			maxh = maxw*h/w;
		}
		else if (h > allowedH) {
			maxh = allowedH;
			maxw = maxh*w/h;
		}
		
		var imageBoxHeight = maxh+this.borderTopHeight+this.borderBottomHeight+this.imageDataHeight;
        var imageBoxWidth = maxw+this.borderLeftWidth+this.borderRightWidth;
        var currentMarginLeft = isNaN(parseInt($('.simpleLightBoxBorders').css("left")))?0:parseInt($('.simpleLightBoxBorders').css("left"));
        
        $('.simpleLightBoxImageData').hide("fast");
        $('.simpleLightBoxImageDataClose').hide("fast");
        $('.simpleLightBoxBorders').css({position:'absolute'});
		
        $('#gallery_main_box .simpleLightBoxMainImage:first').animate({height:'120%',width:'120%',marginLeft:'-10%',marginTop:'-10%',opacity:0},1000);
		var current = $('#gallery_main_box .simpleLightBoxMainImage:first');
		
		
		$('#gallery_main_box').animate({width:maxw+"px", height:maxh+"px"},{
			duration: 1000,
			step:function(){
			    $('.simpleLightBoxBorders').css({top: (((pagesize[3]-($(this).height()+self.borderTopHeight+self.borderBottomHeight+self.imageDataHeight))/2)+$(document).scrollTop())+"px"});
			    $('.simpleLightBoxBorders').css({left: ((pagesize[2]-($(this).width()+self.borderLeftWidth+self.borderRightWidth))/2)+"px"});
				$('.simpleLightBoxImageData').css({width:$(this).width()+"px"});
			},
			complete: function(){
				var func = function(){
					$("#gallery_go_left,#gallery_go_right").css({height:$("#gallery_main_box").outerHeight()+"px"});
					$(".simpleLightBoxImageDataDescription").html(self.desc_gallery[idx]);
					$(".simpleLightBoxImageDataImages").html(self.src_gallery.length>1?((idx+1)+"/"+self.src_gallery.length):"");
					self.updateHeight();
					
					$('.simpleLightBoxImageData').show("fast",function(){
						$('.simpleLightBoxImageDataClose').show("fast");
						callback.apply(this,[]);	
					});
				};
				if($('#gallery_main_box .simpleLightBoxMainImage').length > 1){
					current.animate({opacity:0},"fast" ,function(){
						$(this).remove();
						self.initializeArrows(self.getFileExtension(filename) == 'flv'?true:false);
						func.apply(this,[]);
					});
				}
				else{
					func.apply(this,[]);
				}
				
				
			}
		});
		
		return [maxw,maxh];
	};
	this.showLoadingBox=function(el){
		el.prepend('<div class="loading-box" style="position:relative"><div style="background-color:#FFFFFF;z-index:10000;position:absolute;top:0;left:0;width:'+el.width()+'px;height:'+el.height()+'px;" class="gallery_loading"></div>');
		el.find(".gallery_loading").css({opacity:.5});
	};
	
	this.changeImage=function(filename,resized){
		resized = resized==undefined?false:true; 
		var self = this;
		if(filename!=undefined || resized){
			
			if($('#gallery_main_box .simpleLightBoxMainImage').length <= 1){
				self.hideArrows();
				self.showLoadingBox($('#gallery_main_box'));
				if(self.getFileExtension(filename) == 'flv'){
					maxw = self.flvplayerWidth;
					maxh = self.flvplayerHeight;
					self.resizeGalleryContent(filename,maxw,maxh);
					var mainImage = document.createElement("DIV");
					$(mainImage).addClass("simpleLightBoxMainImage");
					$(mainImage).css({opacity:0,width:'100%',height:'100%'});
					$(mainImage).html('<div id="flv-box"></div>');
					$('#gallery_main_box').append($(mainImage));
					$(mainImage).animate({
						opacity:1,
						height:maxh+'px'
					},1000,function(){
						$('#gallery_main_box').find('.loading-box').remove();
					});
					self.showFlv(filename,"flv-box",maxw,maxh);
					$(".simpleLightBoxMainImage").data("src",filename);
				}
				else{
					
					
					var idx = self.getImageIndex(filename);
					var src = self.src_gallery[idx];
					var img = new Image();
					
					img.onload = function(){
						
						var box_dimensions = self.getImageMaxDimensions(this.width,this.height);
						if((this.width > box_dimensions[0] || this.height > box_dimensions[1]) && src.indexOf(self.phpResizeFile)==-1){
							if(this.width > this.height){
								self.src_gallery[idx] = self.phpResizeFile+"?pic="+src+"&w="+Math.round(box_dimensions[0]);
							}
							else{
								self.src_gallery[idx] = self.phpResizeFile+"?pic="+src+"&h="+Math.round(box_dimensions[1]);
							}
							
							self.changeImage(self.src_gallery[idx],resized);
						}
						else{
							var callback = function(){
								var mainImage = document.createElement("IMG");
								$(mainImage).addClass("simpleLightBoxMainImage");
								$(mainImage).css({opacity:0,width:'100%',height:'100%'});
								$(mainImage).attr("src",img.src);
								$('#gallery_main_box').append($(mainImage));
					            
								$(mainImage).animate({
									opacity:1,
									height:box_dimensions[1]+'px'
								},1000,function(){
									if($('#gallery_main_box img').length>1){
										$('#gallery_main_box img:first').remove();
									}
									self.initializeArrows();
									
								});
							};
							$('#gallery_main_box').find('.loading-box').remove();
							self.resizeGalleryContent(filename,this.width,this.height,callback);
							
							
						}	
						
					};
					
					img.src=src;
				}
			}
		}	
	};
	
this.showFlv = function(filename,content_id,w,h){
		
		var flashvars = {
		  name: "flv player",
		  file: filename,
		  image: "preview.jpg"
		};
		
		var params = {
		  wmode: "opaque",
		  allowfullscreen:"true",
		  allowscriptaccess:"always"
		};
		
		var attributes = {
		  id: "flvPlayer",
		  name: "flvPlayer"
		  
		};
		
		swfobject.embedSWF(this.flvplayer, content_id, w, h, "9.0.0","expressInstall.swf", flashvars, params, attributes);
		self.initializeArrows(true);
	};
	
	this.centerContent=function(){
		var pagesize = this.pageSize();
		$(".lboxContent").css({
			paddingTop: (parseInt($(document).scrollTop()) + parseInt((pagesize[3]-parseInt($('.lboxContent').height()))/2))+"px",
            margin:'0 auto',
            zIndex: 10001
        });
	};

	this.updateHeight = function(){
		$('#lbox').css({height:$(document).height()});
	};
	
	this.loadHTML = function(html,center){
		var self = this;
        var pagesize = this.pageSize();
		center = center == undefined?true:center;

		if(!center){
			$('body').append('<table class="lboxContentTable" border="0" cellpadding="0" cellspacing="0" style="width:100%;position:absolute;left:0;top: 0px;z-index:10001"><tr><td style="text-align:center"><div class="lboxContent"  style="margin:0 auto;display:none;"></div></td></tr></table>');
			$(".lboxContent:last").html(html);
		}
		else{
			var div = document.createElement("DIV");
			$('body').append('<div class="lboxContent"  style="position:absolute;text-align:center;"></div>');
            $(".lboxContent:last").html(html);
		}
		
        try{
        	$('.simpleLightBoxBorders').pngFix();
        }
        catch(ex){
        	
        }
       
		if(center){
			$(".lboxContent").hide();
			$(".lboxContent:last").hide().css({
				top: (parseInt($(document).scrollTop()) + parseInt((pagesize[3]-parseInt($('.lboxContent:last').height()))/2))+"px",
				left: parseInt((pagesize[2]-parseInt($('.lboxContent:last').width()))/2)+"px" ,
	            zIndex: 10001
	        });
		}
		
		
		if($('#lbox').length==0){
	        $('body').append('<div id="lbox"></div>');
	        $('#lbox').css({
	            position: 'absolute',
	            top: 0,
	            left: 0,
	            width: '100%',
	            height:$(document).height(),
	            opacity: 0,
	            background: this.bgColor,
	            'z-index': 10000
	        }).animate({
	            opacity: (this.opacity/100)
	        },function(){
	        	$(".lboxContent:last").fadeIn("fast",function(){
	        		//self.showLoadingBox($("#gallery_main_box"));
	        		
	        	});
	        	
	        });
        }
        else{
        	
        	$(".lboxContent:last").fadeIn("fast",function(){
        		//self.showLoadingBox($("#gallery_main_box"));
        	});
        }
	};

	this.close = function(closeAll){
		closeAll = closeAll==undefined?false:closeAll;
		try{
			
			if($('.lboxContentTable').length == 1 || $('.lboxContent').length == 1 || closeAll){
				$("body").css("overflow","visible");
				this.toggleSelects('show');
				this.toggleFlash('show');
				if($('#lbox').length !=0){
	  				$('#lbox').remove();
		  		}
		  		if($('.lboxContentTable').length!=0){
		  			$('.lboxContentTable').remove();
		  		}
		  		if($('.lboxContent').length!=0){
		  			$('.lboxContent').remove();
		  		}
			}
			else{
				this.closeLastContentBox();
			}
		}
		catch(ex){
		}
	};

	this.closeLastContentBox = function(){
		
		try{
			if($('.lboxContentTable:last').length!=0){
	  			$('.lboxContentTable:last').remove();
	  		}
	  		if($('.lboxContent:last').length!=0){
	  			$('.lboxContent:last').remove();
	  		}
	  		if($('.lboxContent').length==0){
	  			$("body").css("overflow","visible");
	  		}
	  		$('.lboxContent:last').show();
	  		$('.lboxContentTable:last').show();
	  		
		}
		catch(ex){
		}
	};
	
	this.getFileExtension=function(filename){ 
		if( filename.length == 0 ) return ""; 
	 	var dot = filename.lastIndexOf("."); 
	 	if( dot == -1 ) return ""; 
	 	var extension = filename.substr(dot+1,filename.length); 
	 	return extension.toLowerCase(); 
	}; 
	
};


var MySimpleLightBox = new SimpleLightBox();
