var jsDialog = new function() {

	//________button / control labels
	this.strOk = 'Ok';
	this.strYes = 'Yes';
	this.strNo = 'No';
	this.strX = 'X';

	this.max_opacity = 95;		// maximum opacity
	this.min_opacity = 0;		// minimum opacity
	this.speed	= 300;	// transition speed in milliseconds

	//________
	this.timers = [];
	this.width = 0; this.height = 0;
	
	this.divBoxName =	'jsDialog_box';		this.divBox = null;
	this.divContentName =	'jsDialog_content';	this.divContent = null;
	this.divOptionsName = 'jsDialog_options'; this.divOptions = null;
	this.hrefCloseName = 'jsDialog_close'; this.hrefClose = null;
	this.btYesName = 'jsDialog_yes'; this.btYes = null;
	this.btNoName = 'jsDialog_no'; this.btNo = null;
	this.btOk = 'jsDialog_ok'; this.btOk = null;

	this._ = function(id) {
		return document.getElementById(id);
	};
	

	//________create a confirm box
	this.confirm = function(content, callback_yes, callback_no) {		
		this.makeInvisible(this.btOk); this.makeVisible(this.btYes, 'inline'); this.makeVisible(this.btNo, 'inline');
		this.createDialog(content);
		this.btYes.focus();
		
		// just redo this everytime in case a new callback is presented
		this.btYes.onclick = function() {
			jsDialog.close();
			if(callback_yes) {
				window.setTimeout(function() {callback_yes();}, jsDialog.speed+150);
			}
		};

		this.btNo.onclick = function() {
			jsDialog.close();
			if(callback_no) {
				window.setTimeout(function() {callback_no();}, jsDialog.speed+150);
			}
		};

	};
	
	//________create an alert box
	this.alert = function(content, callback_ok) {
		this.makeVisible(this.btOk, 'inline'); this.makeInvisible(this.btYes); this.makeInvisible(this.btNo);
		this.createDialog(content);
		this.btOk.focus();
		
		// just redo this everytime in case a new callback is presented
		this.btOk.onclick = function() {
			jsDialog.close();
			if(callback_ok)
				callback_ok();
		};
	};
	
	
	//________create a dialog with custom content
	this.content = function(content, close_seconds) {
		this.makeInvisible(this.btOk); this.makeInvisible(this.btYes); this.makeInvisible(this.btNo);
		this.createDialog(content);
	};

	//________create an auto-hiding notification
	this.notify = function(content, close_seconds) {
		this.content(content);

		if(close_seconds)
			window.setTimeout(function() { jsDialog.close(); }, close_seconds*1000 );
	};

	//________dialog control
	this.createDialog = function(content) {
		this.makeVisible(this.divBox);
		this.maintainPosition(this.divBox);
		this.divContent.innerHTML = content;
		this.fadeIn(this.divBox);
	};
	
	this.close = function() {
		this.fadeOut(this.divBox);
		window.setTimeout(function() {	// after the fadeout, hide the box
			jsDialog.makeInvisible(jsDialog.divBox);
		}, this.speed+100);
		this.clearPosition(this.divBox);
	};

	//________
	this.init = function() {
		this.divBox = document.createElement('div');			// dialog box
			this.divBox.setAttribute('id', this.divBoxName);
		this.divContent = document.createElement('div');		// content area
			this.divContent.setAttribute('id', this.divContentName);

		this.divOptions = document.createElement('div');		// options (buttons) area
			this.divOptions.setAttribute('id', this.divOptionsName);

		this.btYes = document.createElement('button');		// Yes button
			this.btYes.setAttribute('id', this.btYesName);
			this.btYes.appendChild(document.createTextNode(this.strYes));
			
		this.btNo = document.createElement('button');		// No button
			this.btNo.setAttribute('id', this.btNoName);
			this.btNo.appendChild(document.createTextNode(this.strNo));
			
		this.btOk = document.createElement('button');		// No button
			this.btOk.setAttribute('id', this.btOkName);
			this.btOk.appendChild(document.createTextNode(this.strOk));
		
		this.divOptions.appendChild(this.btYes);
		this.divOptions.appendChild(this.btNo);
		this.divOptions.appendChild(this.btOk);	// add controls
		
		this.hrefClose = document.createElement('a');			// close link
			this.hrefClose.setAttribute('id', this.hrefCloseName)
			this.hrefClose.innerHTML = this.strX;
			this.hrefClose.href = "#";
			this.hrefClose.style.margin = "2";
			this.hrefClose.onclick = function() {
				jsDialog.close();
			};

		// add controls to the main box
		this.divBox.appendChild(this.hrefClose);
		this.divBox.appendChild(this.divContent);
		this.divBox.appendChild(this.divOptions);
		
		this.makeInvisible(this.divBox);	// hide the box
		document.getElementsByTagName("body")[0].appendChild(this.divBox);	// add box to document
	};

	//________transition methods
	this.fadeOut = function(obj) {
		this.opacity(obj, this.max_opacity, this.min_opacity, this.speed);
	};
	
	this.fadeIn = function(obj, max_opacity) {
		this.opacityChange(obj.id, this.min_opacity);
		this.makeVisible(obj);
		this.opacity(obj, this.min_opacity, max_opacity ? max_opacity : this.max_opacity, this.speed);
	};

	this.opacity = function(obj, opac_start, opac_end, millisec) {
		var speed = Math.round(millisec / 100);
		var timer = 0;

		if(opac_start > opac_end) {
			for(i = opac_start; i >= opac_end; i--) {
				this.timers[obj] = window.setTimeout("jsDialog.opacityChange('" + obj.id + "', " + i + ")", (timer * speed));
				timer++;
			}
		} else if(opac_start < opac_end) {
			for(i = opac_start; i <= opac_end; i++) {
				this.timers[obj] = window.setTimeout("jsDialog.opacityChange('" + obj.id + "', " + i + ")", (timer * speed));
				timer++;
			}
		}
	};

	this.opacityChange = function (obj_id, opacity) {
		var s = this._(obj_id).style;
		s.opacity = (opacity / 100);
		s.MozOpacity = (opacity / 100);
		s.KhtmlOpacity = (opacity / 100);
		s.filter = "alpha(opacity=" + opacity + ")";
	};
	
	this.makeVisible = function(obj, display) {
		obj.style.display = display ? display : 'block';
		obj.style.visibility = 'visible';
	};
	this.makeInvisible = function(obj) {
		obj.style.display = 'none';
		obj.style.visibility = 'hidden';
	};
	
	
	//________position control
	this.clearPosition = function() {
		window.onscroll = null;
		window.onresize = null;
	};
	this.maintainPosition = function(obj) {
		obj.style.position = 'absolute';	// obviously
	
		makeCenter(obj);
		
		window.onscroll = function() { makeCenter(obj); };
		window.onresize = function() { makeCenter(obj); };
		
		function makeCenter(obj) {
			var scrollPos = new getScrollPos();
			var pageSize = new getPageSize();
			var emSize = new getObjectSize(obj);
			var toX = Math.round(pageSize.width/2) - (emSize.width/2) + scrollPos.scrollX;
			var toY = Math.round(pageSize.height/2) - (emSize.height/2) + scrollPos.scrollY;
			// change the css properties and center the object

			obj.style.left = toX+'px';
			obj.style.top = toY+'px';
		}
		
		function getScrollPos() {
			var docElem = document.documentElement;
			this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
			this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
		}

		function getPageSize() {
			var docElem = document.documentElement
			this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
			this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
		}

		function getObjectSize(obj) {
			this.width = obj.offsetWidth ||  obj.style.pixelWidth;
			this.height = obj.offsetHeight || obj.style.pixelHeight;
		}
	
	}
	
	this.addEvent = function(obj, evType, fn){
		if (obj.addEventListener){ 
			obj.addEventListener(evType, fn, false); 
			return true; 
		} else if (obj.attachEvent){ 
			var r = obj.attachEvent("on"+evType, fn); 
			return r; 
		} else { 
			return false; 
		}
	}
	
};

jsDialog.addEvent(window, 'load', function() { jsDialog.init(); });