/**
	Kailash Nadh,	http://kailashnadh.name
	August 2009
	Smooth popup Dialog2 for jQuery

	License:	GNU Public License: http://www.fsf.org/copyleft/gpl.html
	
	v1.2	September 2 2009
**/

var jqDialog2 = new function() {

	//________button / control labels
	this.strOk = 'Ok';
	this.strYes = 'Yes';
	this.strNo = 'No';
	this.strCancel = 'Cancel';
	this.strX = 'X';


	//________
	this.closeTimer = null;
	this.width = 0; this.height = 0;
	
	this.divBoxName =	'jqDialog2_box';	this.divBox = null;
	this.divContentName =	'jqDialog2_content';	this.divContent = null;
	this.divOptionsName = 'jqDialog2_options'; this.divOptions = null;
	this.btCloseName = 'jqDialog2_close'; this.btClose = null;
	this.btYesName = 'jqDialog2_yes'; this.btYes = null;
	this.btNoName = 'jqDialog2_no'; this.btNo = null;
	this.btOkName = 'jqDialog2_ok'; this.btOk = null;
	this.btCancel = 'jqDialog2_ok'; this.btCancel = null;
	this.inputName = 'jqDialog2_input'; this.input = null;


	//________create a confirm box
	this.confirm = function(message, callback_yes, callback_no) {		
		this.createDialog2(message);
		this.btYes.show(); this.btNo.show();  this.btYes.focus();
		this.btOk.hide(); this.btCancel.hide(); 
		
		// just redo this everytime in case a new callback is presented
		this.btYes.unbind().click( function() {
			jqDialog2.close();
			if(callback_yes) callback_yes();
		});

		this.btNo.unbind().click( function() {
			jqDialog2.close();
			if(callback_no) callback_no();
		});

	};
	
	//________prompt Dialog2
	this.prompt = function(message, content, callback_ok, callback_cancel) {
		this.createDialog2($("<p>").append(message).append( $("<p>").append( $(this.input).val(content) ) ));
		
		this.btYes.hide(); this.btNo.hide();
		this.btOk.show(); this.input.focus(); this.btCancel.show(); 
		
		// just redo this everytime in case a new callback is presented
		this.btOk.unbind().click( function() {
			jqDialog2.close();
			if(callback_ok) callback_ok(jqDialog2.input.val());
		});

		this.btCancel.unbind().click( function() {
			jqDialog2.close();
			if(callback_cancel) callback_cancel();
		});

	};
	
	//________create an alert box
	this.alert = function(content, callback_ok) {
		this.createDialog2(content);
		this.btCancel.hide(); this.btYes.hide(); this.btNo.hide(); this.btOk.show();
		this.btOk.focus();
		
		// just redo this everytime in case a new callback is presented
		this.btOk.unbind().click( function() {
			jqDialog2.close();
			if(callback_ok)
				callback_ok();
		});
	};

	
	//________create a Dialog2 with custom content
	this.content = function(content, close_seconds) {
		this.createDialog2(content);
		this.divOptions.hide();
	};

	//________create an auto-hiding notification
	this.notify = function(content, close_seconds) {
		this.content(content);
		this.btClose.focus();
		if(close_seconds)
			this.closeTimer = setTimeout(function() { jqDialog2.close(); }, close_seconds*1000 );
	};

	//________Dialog2 control
	this.createDialog2 = function(content) {
		clearTimeout(this.closeTimer);
		this.divOptions.show();
		this.divContent.html(content);
		this.divBox.fadeIn('fast');
		this.maintainPosition();
	};
	
	this.close = function() {
		this.divBox.fadeOut('fast');
		this.clearPosition();
	};

	//________position control
	this.clearPosition = function() {
		$(window).scroll().remove();
	};
	this.makeCenter = function() {
		$(jqDialog2.divBox).css (
			{
				top: ( (($(window).height() / 2) - ( ($(jqDialog2.divBox).height()) / 2 ) )) + ($(document).scrollTop()) + 'px',
				left: ( (($(window).width() / 2) - ( ($(jqDialog2.divBox).width()) / 2 ) )) + ($(document).scrollLeft()) + 'px'
			}
		);
	};
	this.maintainPosition = function() {
		$(window).scroll( function() {
			jqDialog2.makeCenter();
		} );
		
	}

	//________
	this.init = function() {
		this.divBox = $("<div>").attr({ id: this.divBoxName });
		this.divContent = $("<div>").attr({ id: this.divContentName });
		this.divOptions = $("<div>").attr({ id: this.divOptionsName });
		this.btYes = $("<button>").attr({ id: this.btYesName }).append( document.createTextNode(this.strYes) );
		this.btNo = $("<button>").attr({ id: this.btNoName }).append( document.createTextNode(this.strNo) );
		this.btOk = $("<button>").attr({ id: this.btOkName }).append( document.createTextNode(this.strOk) );
		this.btCancel = $("<button>").attr({ id: this.btCancelName }).append( document.createTextNode(this.strCancel) );
		this.input = $("<input>").attr({ id: this.inputName });
		this.btClose = $("<button>").attr({ id: this.btCloseName }).append( document.createTextNode(this.strX) ).click(
							function() {
								jqDialog2.close();
							});

		this.divBox.append( this.btClose ).append( this.divContent ).append(
			this.divOptions.append(this.btYes).append(this.btNo).append(this.btOk).append(this.btCancel)
		);
		
		this.divBox.hide();
		$('body').append( this.divBox );
		this.makeCenter();
	};
	
};

$(window).load(function () {
	jqDialog2.init();
});