/*!
 * ts_dialogin - jQuery Login Dialog
 * 
 * Utilizes jQuery & jQuery-UI to Render the Login-Dialog
 * and take Configuration from TypoScript trough PHP
 *
 * http://www.trisinus.de
 *
 * Copyright 2010, Trisinus GmbH & Co. KG
 *
 */
tx_tsdialogin = {
	//Instance of Dialog
	dialog: null,
	
	//Instances of form Input fields
	form: {
		username: null,
		password: null,
		permalogin: null
	},
	
	//Div for Error Output
	error: null,
	
	//Div for Loading Image
	loading: null,
	
	//Options Object
	options: null,
	
	/*
	* Initialize the Login-Dialog
	*
	* Takes Configuration from TypoScript trough PHP and
	* initializes the Login-Dialog
	*
	*/
	init: function(o)
	{
		this.options = o;
		
		this.dialog = jQuery("<div></div>").appendTo("body").dialog({
			resizable: this.options.dialog.resizable,
			modal: this.options.dialog.modal,
			autoOpen: false,
			title: this.options.lang.title,
			show: this.options.dialog.effects.show,
			hide: this.options.dialog.effects.hide,
			dialogClass: 'tx_tsdialogin',
			draggable: this.options.dialog.draggable,
			width: this.options.dialog.width,
			height: this.options.dialog.height,
			position: [this.options.dialog.position.x,this.options.dialog.position.y],
			buttons: {
				"Cancel": function() {
					tx_tsdialogin.show_loading(false);
					jQuery(this).dialog("close");
				},
				"Login": function() {
					tx_tsdialogin.try_login();
				}
			}
		});
		
		jQuery(".tx_tsdialogin .ui-button-text:first").text(this.options.lang.label_cancel);
		jQuery(".tx_tsdialogin .ui-button-text:last").text(this.options.lang.label_login);
		
		this.form.username = jQuery("<input type='text' id='ts_dialogin_username' />");
		this.form.password = jQuery("<input type='password' id='ts_dialogin_password' />");
		
		div_username = jQuery("<div class='form-field username'></div>").appendTo(this.dialog);
		div_password = jQuery("<div class='form-field password'></div>").appendTo(this.dialog);
		div_permalogin = jQuery("<div class='form-field permalogin'></div>").appendTo(this.dialog);
		this.error = jQuery("<div class='error'></div>").appendTo(this.dialog);
		
		div_username.append("<label>"+this.options.lang.label_username+"</label>");
		div_username.append(this.form.username);
		
		div_password.append("<label>"+this.options.lang.label_password+"</label>");
		div_password.append(this.form.password);
		
		if (this.options.permalogin = 1)
		{
			this.form.permalogin = jQuery("<input type='checkbox' checked='' id='ts_dialogin_permalogin' />");
			
			div_permalogin.append("<label>"+this.options.lang.label_permalogin+"</label>");
			div_permalogin.append(this.form.permalogin);
		}
		
		this.loading = jQuery("<img src='"+this.options.image_loading+"' title='loading...' alt='loading...' />").hide().appendTo("body");
	},
	
	/*
	* Show the Login-Dialog
	*
	* Brings the Login-Dialog up to the User.
	* You can call this Method yourself if you don't
	* want to use the Login-Button included.
	*
	*/
	open_dialog: function()
	{
		this.error.text("");
		this.dialog.dialog("open");
	},
	
	/*
	* Shows and Hides the Loading-Image
	*
	* Used to toggle the Login-Image and disable the Login-Dialog
	*
	*/
	show_loading: function(display)
	{
		if (display)
		{
			this.dialog.dialog("disable");
			this.loading.show();
			this.loading.position({
				of: this.dialog,
				my: 'center center',
				at: 'center center'
			});
			this.loading.css('z-index',2000);
		}
		else
		{
			this.loading.hide();
			this.dialog.dialog("enable");
		}
	},
	
	/*
	* Send's Ajax Request to Typo3-Backend for Login
	*
	* This Method is called by the Login-Dialog itself.
	* It tries to Login the Visitor with the given Username and Password.
	* If Login is successful it redirects the User, if not it displays an error message.
	*
	*/
	try_login: function()
	{
		this.show_loading(true);
		this.error.text("");
		
		post_data = {
			type: this.options.page_type,
			logintype: 'login',
			pid: this.options.storagePID,
			redirect_url: '',
			user: this.form.username.val(),
			pass: this.form.password.val()
		};
		if (jQuery("#ts_dialogin_permalogin:checked").val() != null)
		{
			post_data.permalogin = 1;
		}
		
		jQuery.post(location.href,post_data,function(data,textStatus,xmlHttpRequest) {
			if (data.error)
			{
				tx_tsdialogin.error.text(data.error);
			}
			else if (data.redirect)
			{
				if (data.redirect == "reload") location.reload();
				else location.href = data.redirect;
			}
			else
			{
				tx_tsdialogin.error.text(this.options.lang.unknown_error);
			}
			tx_tsdialogin.show_loading(false);
		},'json');
	},
	
	/*
	* Send's Ajax Request to Typo3-Backend for Logout
	*
	* This Method can be used to Logout the user via Ajax Request.
	* It will Reload the current Page after that.
	* You can call this Method yourself if you
	* don't want to use the Logout-Button included.
	*
	*/
	try_logout: function()
	{
		post_data = {
			logintype: 'logout'
		};
		
		jQuery.post(location.href,post_data,function(data,textStatus,xmlHttpRequest) {
			location.reload();
		});
	},
	
	foo: "bar"
}

