$(document).ready(function() {
   // add markup to container and apply click handlers to anchors
   //var $j = jQuery.noConflict();
   
	$("#login").click(function(e){      
		e.preventDefault();	
		
		login();
		
	});
	
	registerComponent();
	
 });
 
 function login() {
	showLoading();

	if($("#username").val() == "" 
			|| $("#password").val() == "") {
			Ext.MessageBox.alert('Warning', 'Please input username and password.');
			
	} else {
		$("#loginForm").submit();
	}
 }
 
 function logout() {
	$.post("./login/login.php?rand=" + (Math.random() * 100), {method:"logout"},
			function(data){
				 window.location = "./index.php?op=home"
			});	
 }
 
 function registerComponent() {
	$("a[href*=/indexx.php]").click(function(e) {
		
		e.preventDefault();	
		url = $(this).attr("href");
		
		var param = getParameter(url);

		retrievePage(param);
	});
}

function getParameter(url) {
	return url.substr(url.indexOf("?", 0) + 1, url.length);
}

function retrievePage(parameter) {	
	$.get("./control/pageController.php?rand=" + (Math.random() * 100) + "&" + parameter ,
			function(data){
				$("#content").html(data);
			});
			
	moveToTop();
}

function moveToTop() {
	window.scrollTo(0,0);
}