$(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	// $("#dialog").dialog("destroy");

	$(".loginLink").click(loginForm);

	function loginForm() {

		$("#dialog-message").dialog({
			modal: true,
			buttons: {
				Cancel: function() {
					$(this).dialog('close');
				},
				Login: function() {
					jQuery.post(
						"/index.php?controller=login&cmd=login",
						{username: document.getElementById("username").value, password: document.getElementById("password").value, response_type:"ajax"},
						ajaxResponse
					);
				}
			}
		});
		
		// Set custom classes for form buttons:
		$(".ui-dialog-buttonpane button:contains('Login')").addClass('btn-login');
		$(".ui-dialog-buttonpane button:contains('Cancel')").addClass('btn-cancel');

		
	}
	
	function ajaxResponse(data, textStatus, XMLHttpRequest) {
		var result=parseInt($('auth_result',data).text());
		if (result==1){
		    location.reload();
		}else{
		    alert($('auth_msg',data).text());
		}
		//alert("answer:"+data);
	}
	
	
});

