// This is the base page controller class. Since we are only using basic
// inheritence, the constructor must only set value and define other methods.
// The constructor will not be called with arguments.
function PageControllerClass(){
	this.PageBusy = false;
	this.LoadingReady = false;
	this.NotificationReady = false;
	this.AlertReady = false;
	this.LoadingBar = '';
	this.NotificationBar = '';
	this.AlertBox = '';
};

jQuery.fn.slideToggle = function(speed, easing, callback) {
	var current = (this.css('bottom') != '-60px' ? '-60px' : '0px');
	return this.animate({bottom: current}, speed, easing, callback);
};

PageControllerClass.prototype.showNotification = function(msg, obj, speed){
	if(!this.NotificationReady){
		this.NotificationBar = $('<div id="pagenotification"></div>').appendTo("body");
		this.NotificationReady = true;
	};
	this.updateNotification(msg, obj);
	if(msg != false){this.toggleNotification(speed);};
};

PageControllerClass.prototype.updateNotification = function(msg, obj) {
	this.NotificationBar.empty();
	if(msg != false) {
		var updated = $('<h1>'+msg+'</h1>');
		if(obj != 'undefined' && obj != null){updated.click(function(objEvent){obj.getNewMessage();});};
		updated.appendTo(this.NotificationBar);
	};
};

PageControllerClass.prototype.toggleNotification = function(s) {
	var objSelf = this;
	var speed = (s == 'undefined' ? 2000 : s);
	if(objSelf.NotificationReady){
		objSelf.NotificationBar.slideToggle(speed,'swing',setTimeout(function(){objSelf.NotificationBar.slideToggle(),3000,'swing'},10000));
	};
};


PageControllerClass.prototype.ShowAlert = function(msg) {
	if(!this.AlertReady){
		this.requireUI('both');
		this.AlertBox = $('<div id="alertBox"></div>').appendTo('body');
		this.AlertBox.dialog({autoOpen:false,buttons:{"Close":function(){$(this).dialog("close");}},title:"Information:"});
		this.AlertReady = true;
	};
	this.AlertBox.text(msg);
	this.AlertBox.dialog('open');
};

PageControllerClass.prototype.toggleLoading = function(swtch) {
	if(!$("#loading").length) {
		$('<img id="loading" src="/assets/images/ajax-loader-bar.gif" width="43" height="11" alt="Loading...">').appendTo('body');
	};
	switch(swtch) {
		case true:
			$("img#loading").css('display','block');
		break;
		case false:
			$("img#loading").css('display','none');
		break;
	};
};


PageControllerClass.prototype.GetBusy = function(){
	return(this.PageBusy);
};

PageControllerClass.prototype.SetBusy = function(blnBusy){
	this.PageBusy = blnBusy;
	this.toggleLoading(blnBusy);
};


PageControllerClass.prototype.requireUI = function(part) {
	switch(part) {
		case 'css': if(!$("head > link[href$=dialog.css]").length) { $("head").append('<link rel="stylesheet" type="text/css" href="/linked/ui/streetsv9/jquery-ui-1.7.custom.css" />'); }; break;
		case 'js': if(!$("head > script[src$=jquery-ui.js]").length) { $("head").append('<script type="text/javascript" src="/linked/jquery-ui.js"></script>'); }; break;
		case 'both': if(!$("head > link[href$=dialog.css]").length) { $("head").append('<link rel="stylesheet" type="text/css" href="/linked/ui/streetsv9/jquery-ui-1.7.custom.css" />'); }; if(!$("head > script[src$=jquery-ui.js]").length) { $("head").append('<script type="text/javascript" src="/linked/jquery-ui.js"></script>'); }; break;
	};
};


PageControllerClass.prototype.AlertBusy = function(){
	this.ShowAlert("The page is currently loading, please be patient.");
};

PageControllerClass.prototype.ShowAPIErrors = function( arrErrors, blnStayBusy ){
	var strResponse = arrErrors.join(".").split(".").join("<br>");
	this.ShowAlert(strResponse);
	// We are going to assume that after alerting errors, we want to
	// turn off the busy flag. However, use the optional argument to 
	// bypass this mechanism.
	if(!blnStayBusy){this.SetBusy(false);};
};

PageControllerClass.prototype.DestroyElement = function(expr) {
	try{ $(expr).remove(); }
	catch(e) {};
};