/**
	Kailash Nadh,	http://kailashnadh.name
	February 2011
	Smooth popup dialog for jQuery

	License:	GNU Public License: http://www.fsf.org/copyleft/gpl.html
	
	v1.3	February 6 2011	-	Rewrote the whole plugin to comply with jQuery's plugin standards
	v1.2	September 2 2009
**/

;(function($) {
	
	$.jqDialog = {
		//________button / control labels
		labels: {
			ok: 'Ok',
			yes: 'Yes',
			no: 'No',
			cancel: 'Cancel',
			x: 'X',
			ingresar: 'INGRESAR',
		},

		//________element ids
		ids: {
			div_modal:	'jqDialog_modal',
			div_box:	'jqDialog_box',
			div_content:	'jqDialog_content',
			div_options: 'jqDialog_options',
			bt_close: 'jqDialog_close',
			bt_yes: 'jqDialog_yes',
			bt_no: 'jqDialog_no',
			bt_ok: 'jqDialog_ok',
			bt_cancel: 'jqDialog_cancel',
			input: 'jqDialog_user',
			input_pass: 'jqDialog_pass',
			bt_ingresar: 'jqDialog_ingresar',
		},
		
		//________confirm dialog
		confirm: function(message, callback_yes, callback_no) {
			var t = this;
			
			t.create(message);
			
			t.parts.bt_ok.hide();
			t.parts.bt_cancel.hide();
			
			t.parts.bt_yes.show();
			t.parts.bt_no.show();
			t.parts.bt_yes.focus();
			
			// just redo this everytime in case a new callback is presented
			t.parts.bt_yes.unbind().click( function() {
				t.close();
				if(callback_yes) callback_yes();
			});

			t.parts.bt_no.unbind().click( function() {
				t.close();
				if(callback_no) callback_no();
			});
		},
		
		//________prompt dialog
		prompt: function(message, content, callback_ok, callback_cancel) {
			var t = this;

			t.create(
				$("<div>").
					append($("<div id='prompt_texto'><div id='prompt_seccion'>Secci&oacute;n exclusiva para distribuidores denimed.</div><div id='prompt_pedi_mail'>Ped&iacute; usuario y contrase&ntilde;a enviandonos un email a:</div></div>"))
					.append($("<div id='prompt_inputs'>")
						.append($("<p class='porta_jqDialog_user'><label class='label_jqDialog_user' for='jqDialog_user'>Usuario</label>")
							.append(t.parts.input.val(content)))
						.append($("<p class='porta_jqDialog_pass'><label class='label_jqDialog_pass' for='jqDialog_pass'>Password</label>")
							.append(t.parts.input_pass.val(content)))
						.append( t.parts.bt_ingresar ))
			);
			
			// activate appropriate controls
			t.parts.bt_yes.hide();
			t.parts.bt_no.hide();

			t.parts.bt_ok.hide();
			t.parts.bt_cancel.hide(); 
			
			t.parts.bt_ingresar.show();
			
			t.parts.bt_close.show();
			
			// just redo t everytime in case a new callback is presented
			t.parts.bt_ingresar.unbind().click( function() {
				t.close();
				if(callback_ok) callback_ok( t.parts.input.val(), t.parts.input_pass.val() );
			});
			t.parts.input.bind('keypress', function(e) {
				if(e.keyCode==13){
					t.parts.input_pass.focus();
				}
			});
			t.parts.input_pass.bind('keypress', function(e) {
				if(e.keyCode==13){
					t.parts.bt_ingresar.click();
				}
			});
		},
		
		//________alert dialog
		alert: function(content, callback_ok, close_seconds) {
			var t = this;
			
			t.create(content);

			// activate appropriate controls
			t.parts.bt_cancel.hide();
			t.parts.bt_yes.hide();
			t.parts.bt_no.hide();
			
			t.parts.bt_close.hide();
			
			// just redo this everytime in case a new callback is presented
			if(callback_ok) {
				t.parts.bt_ok.show();
				t.parts.bt_ok.focus();
				t.parts.bt_ok.unbind().click( function() {
					t.close();
					if(callback_ok)
						callback_ok();
				});
			} else {
				t.parts.bt_ok.hide();
			}
			
			if(close_seconds)
				t.close_timer = setTimeout(function() { t.close(); }, close_seconds*1000 );

		},

		//________content
		content: function(content, close_seconds) {
			var t = this;
			
			t.create(content);
			t.parts.div_options.hide();
		},

		//________auto-hiding notification
		notify: function(content, close_seconds) {
			var t = this;
			
			t.content(content);
			t.parts.bt_close.focus();
			if(close_seconds)
				t.close_timer = setTimeout(function() { t.close(); }, close_seconds*1000 );
		},

		//________create a dialog box
		create: function(content) {
			var t = this;
			
			t.check();
			
			t.maintainPosition( t.parts.div_box );
			
			clearTimeout(t.close_timer);
			t.parts.div_content.html(content);
			t.parts.div_options.show();
			t.parts.div_box.fadeIn('fast');
			t.parts.div_modal.fadeIn('fast');
		},
		//________close the dialog box
		close: function() {
			var t = this;
			t.parts.div_box.fadeOut('fast');
			t.parts.div_modal.fadeOut('fast');
			t.clearPosition();
		},

		//________position control
		clearPosition: function() {
			$(window).unbind('scroll.jqDialog');
		},
		makeCenter: function(object) {
			object.css({
				top: ( (($(window).height() / 2) - ( object.height() / 2 ) )) + ($(document).scrollTop()) + 'px',
				left: ( (($(window).width() / 2) - ( object.width() / 2 ) )) + ($(document).scrollLeft()) + 'px',
			});
		},
		maintainPosition: function(object) {
			var t = this;

			t.makeCenter(object);
			
			$(window).bind('scroll.jqDialog', function() {
				t.makeCenter(object);
			} );
		},

		//________
		init_done: false,
		check: function() {
			var t = this;
			if(t.init_done)
				return;
			else {
				t.init_done = true;
			}
			
			$('body').append( t.parts.div_modal );
			$('body').append( t.parts.div_box );
		},
		init: function() {
			var t = this;
		
			t.parts = {};
			
			// create the dialog components
			t.parts.div_modal = $("<div>").attr({ id: t.ids.div_modal });
			t.parts.div_box = $("<div>").attr({ id: t.ids.div_box });
			t.parts.div_content = $("<div>").attr({ id: t.ids.div_content });
			t.parts.div_options = $("<div>").attr({ id: t.ids.div_options });

			t.parts.bt_yes = $("<button>").attr({ id: t.ids.bt_yes }).append( t.labels.yes );
			t.parts.bt_no = $("<button>").attr({ id: t.ids.bt_no }).append( t.labels.no );
			t.parts.bt_ok = $("<button>").attr({ id: t.ids.bt_ok }).append( t.labels.ok );
			t.parts.bt_cancel = $("<button>").attr({ id: t.ids.bt_cancel }).append( t.labels.cancel );
			t.parts.bt_ingresar = $("<div>").attr({ id: t.ids.bt_ingresar });

			t.parts.input = $("<input>").attr({ id: t.ids.input, type:"text" });
			t.parts.input_pass = $("<input>").attr({ id: t.ids.input_pass, type:"password" });
			t.parts.bt_close = $("<button>").attr({ id: t.ids.bt_close })
										   .append( t.labels.x ).click(
												function() {
													t.close();
												}
											);

			// assemble the parts
			t.parts.div_box.append( t.parts.bt_close )
					.append( t.parts.div_content )
					.append(
						t.parts.div_options.append(t.parts.bt_yes)
										   .append(t.parts.bt_no)
										   .append(t.parts.bt_ok)
										   .append(t.parts.bt_cancel)
					);

			// add to body
			t.parts.div_box.hide();
			t.parts.div_modal.hide();
		}
	};
	$.jqDialog.init();
})(jQuery);
