// JavaScript Document
var Error = new function(){
	
	this.init = function(){
		var self=Error;
		self.checkforTimeout();
		self.clearPageTimeOut = window.setInterval(function(){
										self.checkforTimeout();
										}	
										,1800000); 
	};
	
	//error handling
	this.errFunc = function(t) {	
		var self=Error;
		var div = document.createElement("div");
		var err = document.createElement("div");
		var err2 = document.createElement("div");
		var x = document.createElement("span");
		x.innerHTML = '<img src="images/close.png" id="imgClose" title="Close preview window" onclick="Error.closeErrorMsg()"/>';
		div.id = "error";
		err.id = "errorMsg";
		err.appendChild(x);
		err2.innerHTML = t.responseText;
		err.appendChild(err2);
		div.appendChild(err);
		var offset = document.viewport.getScrollOffsets();
		div.style.top = offset.top + 'px';
		div.style.left = offset.left + 'px';	
		div.style.display = 'none';	
		
		document.body.appendChild(div);	
		Effect.Appear(div,{duration:2});	
		
		self.clearError = window.setTimeout(function(){
												var self=Error;	
												self.closeErrorMsg();
												}	
												,30000); 
	};
	this.throwErrorMsg = function(str, timeOut){	
		var self=Error;
		if ($("error")) {self.closeErrorMsg();}; //close any existing error msg's
		var t = 7000;
		var div = document.createElement("div");
		var err = document.createElement("div");
		var err2 = document.createElement("div");
		var imgSpan = document.createElement('img');
		imgSpan.src ="images/close.png";
		imgSpan.id = "imgClose";
		imgSpan.title="Close preview window";
		imgSpan.onclick=function(){Error.closeErrorMsg()};
		div.id = "error";
		err.id = "errorMsg";
		err2.innerHTML = str;
		err.appendChild(imgSpan);
		err.appendChild(err2);
		div.appendChild(err);
		var offset = document.viewport.getScrollOffsets();
		div.style.top = offset.top + 'px';
		div.style.left = offset.left + 'px';	
		div.style.display = 'none';	
		document.body.appendChild(div);
		Effect.Appear(div,{duration:1});		

		if (timeOut>-1) {
			t = timeOut;
		};
		if (timeOut!=false){
		self.clearError = window.setTimeout(function(){
												var self=Error;	
												self.closeErrorMsg();
												}	
												,t); 
		};
	};
	
	this.fullScreenErrorMsg = function(str){
		var self=Error;
		document.body.style.overflow = 'hidden';
		var div1 = document.createElement('div');
		var div2 = document.createElement('div');
		var div2a = document.createElement('div');
		var div3 = document.createElement('div');
		var imgSpan = document.createElement('span');
		imgSpan.onclick=function(){Error.closefullScreenErrorMsg()};
		imgSpan.innerHTML = '<img src="images/close.png" id="imgClose" title="Close preview window" />';
		div1.id = 'fullScreenErrorMsgWrapper';
		div2.id = 'fullScreenErrorMsgWindowWrapper';
		div2a.id = 'fullScreenErrorMsgWindow';
		div3.id = 'fullScreenErrorMsgWindowContent';
		div3.innerHTML =str;		
		div2a.appendChild(imgSpan);
		div2.appendChild(div2a);
		div2a.appendChild(div3);
		div1.style.display = 'none';	
		div2.style.display = 'none';	
		document.body.appendChild(div1);
		document.body.appendChild(div2);
		var offset = document.viewport.getScrollOffsets();
		div1.style.top = offset.top + 'px';
		div1.style.left = offset.left + 'px';
		div2.style.top = offset.top + 'px';
		div2.style.left = offset.left + 'px';
		
		
		Effect.Appear(div2,{duration:1});		
	};
	this.removefullScreenErrorMsg = function(){
		var self=Error;
		self.removeElement($('fullScreenErrorMsgWrapper'));
		self.removeElement($('fullScreenErrorMsgWindowWrapper'));
	};
	
	this.closeErrorMsg = function(){
		var self=Error;
		if ($("error")){
			window.clearTimeout(self.clearError);
			Error.removeElement("error");
		};
	};
	this.closefullScreenErrorMsg = function(){
		var self=Error;
		if ($("fullScreenErrorMsgWrapper")){
			window.clearTimeout(self.clearPageTimeOut);
			Error.removeElement("fullScreenErrorMsgWrapper");
			Error.removeElement("fullScreenErrorMsgWindowWrapper");
			document.body.style.overflow = 'visible';
		};
	};

	this.removeElement = function(id){
		var div = $(id);
		var parent = div.parentNode;
		parent.removeChild(div);
	};

	//end error handling


// preview windows

	this.lrgPreview = function(){
		document.body.style.overflow = 'hidden';
		var div1 = document.createElement('div');
		var div2 = document.createElement('div');
		var div2a = document.createElement('div');
		var div3 = document.createElement('div');
		var imgSpan = document.createElement('span');
		imgSpan.innerHTML = '<img src="images/close.png" id="imgClose" title="Close preview window" onclick="Error.closePreview()"/>';
		div1.id = 'previewWrapper';
		div2.id = 'previewWindowWrapper';
		div2a.id = 'previewWindow';
		div3.id = 'holder';
		
		div2a.appendChild(imgSpan);
		document.body.appendChild(div1);
		div2.style.display='none';
		document.body.appendChild(div2);
		div2.appendChild(div2a);
		div2a.appendChild(div3);
		var offset = document.viewport.getScrollOffsets();
		div1.style.position = 'absolute';
		div2.style.position = 'absolute'
		div1.style.top = offset.top + 'px';
		div1.style.left = offset.left + 'px';
		div2.style.top = offset.top + 'px';
		div2.style.left = offset.left + 'px';
		
		
		Effect.Appear(div2,{duration:1});		
	};

	this.closePreview = function(){
		var self=Error;
		//check to see if large preview
		if($('thumbNail_' + Macchiato.currentPreview)){
			Macchiato.closePreview();// do large preview close cleanup
		};
		// remove the Div
		$('previewWrapper').remove();
		$('previewWindowWrapper').remove();
		document.body.style.overflow = 'visible';
	}
	
	
// end preview windows
	this.checkforTimeout = function(){
		var self=Error;	
		new Ajax.Request('ajax.asp?checkingForTimeOut', {onSuccess:self.checkforTimeoutHandlerFunc, onFailure:self.errFunc}); 
	};

	this.checkforTimeoutHandlerFunc = function(t){
		var self=Error;
		var str = t.responseText;
		if (str.indexOf("sessionTimeOut")>-1) {
			//document.location.href='login.asp';
			str = '<h3>Opps...</h3>'
			str += '<p>It looks suspiciously like you are away from your computer or busy elsewhere on the interweb thingy.</p>'
			str += '<p> Terribly sorry my dear but you\'ll have to log in again. </p>'
			str += "<div align='right'><button type='button' id='btnCloseTimeout' class='btnOrangeSml'  onclick='Error.timeOut()'>Run along now</button></div>";
			document.body.innerHTML="";
			self.fullScreenErrorMsg(str);
			window.clearTimeout(self.clearPageTimeOut);
		};
	};
	this.timeOut = function(){
		var self=Error;
		self.removefullScreenErrorMsg();
		document.location.href='login.asp';
		};

};


/**
 * Ajax.Request.abort
 * extend the prototype.js Ajax.Request object so that it supports an abort method
 */
Ajax.Request.prototype.abort = function() {
    // prevent and state change callbacks from being issued
    this.transport.onreadystatechange = Prototype.emptyFunction;
    // abort the XHR
    this.transport.abort();
    // update the request counter
    Ajax.activeRequestCount--;
};




