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.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: 15%;"></div>';
													html+='<div id="gallery_go_right"  style="z-index:10005;position:absolute;top:0px;right:0px; height: 24px;width: 15%;"></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();"></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;top:'+$(document).scrollTop()+'px;left:0;width:'+pageSize[2]+'px;height:'+pageSize[3]+'px;"><tr><td>'+html+'</td></tr></table>';
		self.loadHTML(html,false);
		
		$("#gallery_main_box").append('<div class="gallery_loading" style="width:200px;height:200px"></div>');
		
		$("#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{
		
			img.onload = function(){
				
				
				self.changeImage(first_img);
			};
	       
			img.src=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(){
		//return this.getCleanImagePath($("#gallery_main_box .simpleLightBoxMainImage:first").css('backgroundImage'));
		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){
			this.hideNextIcon();
		}
		else{
			if(!$("#gallery_go_right").hasClass("active")){
				$("#gallery_go_right").addClass("active");
			}
		}	
		/* 
		var idx = this.getImageIndex(this.getMainImagePath());
		if(idx==this.src_gallery.length-1){
			this.hideNextIcon();
		}
		else{
			$("#gallery_go_right").addClass("active");
			
		}
		*/
	};
	
	
	this.showPrevIcon=function(){
		if($('#gallery_main_box .simpleLightBoxMainImage').length > 1){
			this.hidePrevIcon();
		}
		else{
			if(!$("#gallery_go_left").hasClass("active")){
				$("#gallery_go_left").addClass("active");
			}
		}
		/* 
		var idx = this.getImageIndex(this.getMainImagePath());
		if(idx==0){
			this.hidePrevIcon();
		}
		else{
			$("#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){
		for(var i=0;i<this.src_gallery.length;i++){
			if(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(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(){
		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.resizeGalleryContent=function(filename,w,h){
		var self = this;
		var idx = self.getImageIndex(filename);
		var src = self.src_gallery[idx];
		var desc = self.desc_gallery[idx];
		var borderTopHeight 	= 38;
		var borderBottomHeight  = 38;
		var borderLeftWidth 	= 38;
		var borderRightWidth 	= 38;
		var imageDataHeight 	= 34;
		self.hideArrows();
		$('.simpleLightBoxImageData').hide("fast");
		var pagesize = self.pageSize();
		var allowedW = pagesize[2] - borderLeftWidth - borderRightWidth;
		var allowedH = pagesize[3] - borderTopHeight - borderBottomHeight -100;
		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+borderTopHeight+borderBottomHeight+imageDataHeight;
        var imageBoxWidth = maxw+borderLeftWidth+borderRightWidth;
        var currentMarginLeft = isNaN(parseInt($('.simpleLightBoxBorders').css("marginLeft")))?0:parseInt($('.simpleLightBoxBorders').css("marginLeft"));
        //$('.simpleLightBoxBorders').animate({marginTop: ((pagesize[3]-imageBoxHeight)/2)+"px"},100);
        $('.simpleLightBoxBorders').animate({marginLeft: ((pagesize[2]-imageBoxWidth)/2)+"px"},currentMarginLeft==0?1:1000);
        
		$('#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(){
				$('.simpleLightBoxImageData').css({width:$(this).width()+"px"});
			},
			complete: function(){
				if($('#gallery_main_box .simpleLightBoxMainImage').length > 1){
					current.animate({opacity:0},"fast" ,function(){
						$(this).remove();
						self.initializeArrows(self.getFileExtension(filename) == 'flv'?true:false);
					});
				}
				$("#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();
				self.initializeArrows();
				$('.simpleLightBoxImageData').show("fast");
				
			}
		});
		return [maxw,maxh];
	};
	
	this.changeImage=function(filename){
		var self = this;
		if($('#gallery_main_box .simpleLightBoxMainImage').length <= 1){
			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 .gallery_loading").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.resizeGalleryContent(filename,this.width,this.height);
					if(this.width > box_dimensions[0] || this.height > box_dimensions[1]){
						
						if(this.width > this.height){
							this.src = self.phpResizeFile+"?pic="+src+"&w="+box_dimensions[0];
						}
						else{
							this.src = self.phpResizeFile+"?pic="+src+"&h="+box_dimensions[1];
						}
					}
					else{
						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(){
							
							$("#gallery_main_box .gallery_loading").remove();
						});
					}	
				};
				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);
	};
	
	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(){
		var pagesize = this.pageSize();
		$('#lbox').css({height:pagesize[1]});
	};
	
	this.loadHTML = function(html,center){
        var pagesize = this.pageSize();
		center = center == undefined || center == true?true:false;

		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{
            $('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().css({
				top: (parseInt($(document).scrollTop()) + parseInt((pagesize[3]-parseInt($('.lboxContent').height()))/2))+"px",
	            left: parseInt((pagesize[2]-parseInt($('.lboxContent').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: pagesize[1],
	            opacity: 0,
	            background: this.bgColor,
	            'z-index': 10000
	        }).animate({
	            opacity: (this.opacity/100)
	        },function(){
	        	$(".lboxContent:last").fadeIn("fast",function(){});
	        });
        }
        else{
        	
        	$(".lboxContent:last").fadeIn();
        }
	};

	this.close = function(){
		
		try{
			
			if($('.lboxContentTable').length == 1 || $('.lboxContent').length == 1){
				$("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();
