function SimpleLightBox() {
	this.src_gallery;
	this.desc_gallery;
	this.diff=200;
	this.originalWidth=0;
	this.originalHeight=0;
	this.bgColor = '#FFFFFF';
	this.border  = '3px solid #000000';
	this.opacity  = 85;
	this.animate_overlay = true;
	this.self = this;
	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;
		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){
		
		this.src_gallery = src_array;
		this.desc_gallery = desc_array;
		
		var first_img = this.src_gallery[0];
		var img = new Image();
		var self = this;
		img.onload = function(){
			
			var html ='';
			html+='<table>';
			html+='<tr>';
			html+='<td>';
			html+='<div style="position:relative;">';
			html+='<div id="gallery_main_box" style="cursor:pointer;position:absolute:top:0px;left:0px;"><img id="gallery_main_image" onclick="MySimpleLightBox.close();"/></div>';
			html+='<div id="gallery_description_box" style="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="position:absolute;bottom:0px;color:#FFF;width:98%;height:30px;line-height:30px;text-align:right;padding-right:2%"></div>';
			html+='</div>';
			html+='</td>';
			html+='</tr>';
			html+='<tr>';
			html+='<td id="gallery_thumbs" style="padding-top:10px;">';
			
			for(var i=0;i<self.src_gallery.length;i++){
				html+='<img onclick="MySimpleLightBox.changeImage(this);"  src="'+self.src_gallery[i]+'" height="30" style="float:left;margin:5px 5px 0px 0px;cursor:pointer" title="'+self.desc_gallery[i]+'"/>';
			}	
			
			html+='</td>';
			html+='</tr>';
			html+='</table>';
			self.loadHTML(html);
			self.changeImage($("#gallery_thumbs img:first"));
			$("#gallery_thumbs img:first").css({marginLeft:'0px'});
			
		};
	
		img.src=first_img.replace('thumb_','large_');
	};
	
	this.changeImage=function(el){
		var self =this;
		var src = $(el).attr("src").replace('thumb_','large_');
		var desc = $(el).attr("title");
		addAjaxLoader('gallery_main_box');
		$('#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("title",desc);
			
			
			

			$('#gallery_description_box').html(desc);
			$('#gallery_thumbs').css("width",$('#gallery_main_box img').css("width"));
			if(jQuery.trim($('#gallery_description').html())!=''){
				$('#gallery_description_box').fadeTo("fast", 0.8);
			}
				
			$("#gallery_description_box,#gallery_description").css({bottom:'0px'});
			removeAjaxLoader('gallery_main_box');
			self.centerContent();
		};
		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': '999'
        });
	};
	
	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': '999'
        });
		
		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': 100
	        }).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){
		}
	};
}
var MySimpleLightBox = new SimpleLightBox();
