var LightShowAjax = Class.create();
LightShowAjax.prototype = {
	initialize: function(frequency){
		this.freq = frequency;
		this.savedLightbox = [];
		this.savedLightboxDesc = [];
		this.savedLightboxTitle = [];
		this.savedLightboxHeight = [];
		this.offset = 0;
		this.pe = '';
		this.showID = 0;
		this.offsetfrommouse = [15,15];  
		this.displayduration = 0;
		this.currentimageheight = 220;
		this.drawLightShowFrame();
		this.width = 170;
		this.height = 155;
	},
	 
	getLightShowFiles: function(id) {
		if(this.freq < 1) {
			return;
		}
		
		if(this.showID == id) {
			return;
		} else {
			this.showID = id
		}
		document.onmousemove = this.followmouse.bind(this);
		Element.update("LightboxSlideShow", '<div align="center" style="display: table-cell; vertical-align: middle; padding: 5px; width:'+this.width+'px; height:'+this.height+'px; background-color: #FFF; border: 1px solid #888;"><img src="images/loading.gif"><br/>Loading 50 Samples...</div>');
		Element.show("LightboxSlideShow");

		if(this.savedLightbox[id] && this.savedLightbox[id].length) {
			this.drawLightboxFile();
			this.startLightShow();
		}
		else {
			var url = 'ajax_class_creator.html';
			var params = 'ajax_action=GetSlideShowFiles&ajax_class=lightshow&id=' + id;
			var myAjax = new Ajax.Request
				(
					url, 
					{ 
						method: 'get', 
						parameters: params,
						onComplete: this.beginLightShow.bind(this),
						onFailure: this.lightShowFailed.bind(this)
					}
				);
		}
	}, 

	lightShowFailed: function(req) {
		this.endLightShow();
	},

	beginLightShow: function(req) {
		var data = AjaxUtil.receiveArray(req.responseText);
		if(data.files.length == 0) { return; }
		this.savedLightbox[this.showID] = data.files;		
		this.savedLightboxDesc[this.showID] = data.desc;
		this.savedLightboxTitle[this.showID] = data.title;
		this.savedLightboxHeight[this.showID] = data.height;
		this.startLightShow();
		if(this.offset == 0) {
			this.drawLightboxFile(); // to avoid 3 sec delay from the periodic call
		}
	},
	
	startLightShow: function() {
		if (this.pe) { this.pe.stop(); }
		this.pe = new PeriodicalExecuter(this.drawLightboxFile.bind(this), this.freq);
	},

	drawLightboxFile: function() {
		html =  '<div style="display: table-cell; padding:5px; width:'+this.width+'px; height:'+this.height+'px; background-color: #FFF; border: 1px solid #888;">';
		html += '	<div style="display: table; width:'+this.width+'px; background-color: #FFF;">';
		html += '			<div style="height: 120px; text-align: center; padding-top: 2px; "><img src="file_thumbview_approve.php?size=1&id=' + this.savedLightbox[this.showID][this.offset] + '" /></div>';
		html += '		Single click to view lightbox<br/>';
		html +=	'		Double click to view image</div>';
		html += '	</div>'
		html += '</div>';

		Element.update("LightboxSlideShow", html);
		this.offset++;
		if(this.offset == this.savedLightbox[this.showID].length) { this.offset = 0; }
	},

	endLightShow: function() {
		if(this.pe) {
			this.pe.stop();
		}
		Element.update("LightboxSlideShow", '');
		Element.hide("LightboxSlideShow");
		document.onmousemove=""
		$("LightboxSlideShow").style.left = "-500px";
		this.showID = 0;
		this.offset = 0;
	},
	
	drawLightShowFrame: function() {
		document.write('<div id="LightboxSlideShow" style="display: table; width:'+this.width+'px; height:'+this.height+'px; vertical-align: top; position: absolute; z-index: 1;"></div>');	
	},

	truebody: function(){
		return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	},

	followmouse: function(e){

		var xcoord = this.offsetfrommouse[0]
		var ycoord = this.offsetfrommouse[1]
	
		var docwidth=document.all? this.truebody().scrollLeft+this.truebody().clientWidth : pageXOffset+window.innerWidth-15
		var docheight=document.all? Math.min(this.truebody().scrollHeight, this.truebody().clientHeight) : Math.min(window.innerHeight)
	
		
		if (typeof e != "undefined"){
			if (docwidth - e.pageX < 300){
				xcoord = e.pageX - xcoord - 180; // Move to the left side of the cursor
			} else {
				xcoord += e.pageX;
			}
			if (docheight - e.pageY < (this.currentimageheight + 110)){			
				ycoord += e.pageY - Math.max(0,(110 + this.currentimageheight + e.pageY - docheight - this.truebody().scrollTop));
			} else {
				ycoord += e.pageY;
			}
					
		} else if (typeof window.event != "undefined"){
			if (docwidth - event.clientX < 300){
				xcoord = event.clientX + this.truebody().scrollLeft - xcoord - 320; // Move to the left side of the cursor
			} else {
				xcoord += this.truebody().scrollLeft+event.clientX
			}
			if (docheight - event.clientY < (this.currentimageheight + 110)){
				ycoord += event.clientY + this.truebody().scrollTop - Math.max(0,(110 + this.currentimageheight + event.clientY - docheight));
			} else {
				ycoord += this.truebody().scrollTop + event.clientY;
			}
		}
	
		if(ycoord < 0) { ycoord = ycoord*-1; }
		
		$("LightboxSlideShow").style.left=xcoord+"px"
		$("LightboxSlideShow").style.top=ycoord+"px"	
	},
	
	toFileCloseUp : function() {
		if(this.showID <=0) { return; }
		if(this.pe) { this.pe.stop(); }		
		var offset = this.offset-1;
		if(offset < 0) { offset = 49; }
		location.href='file_closeup5445.html?id=' + this.savedLightbox[this.showID][offset];
	},
	
	toFileSearch: function(id) {
		if(this.pe) { this.pe.stop(); }
		location.href='file_searchab47.html?action=file&amp;lightboxID=' + id;
	}
		
}