// JavaScript Document
$(document).ready(function() { 	
	m_shell = new shell();
	m_shell.debug = false;
	m_shell.init();	
	m_shell.equalizeBoxHeight();
});

function shell()
{
	var self = this;
	this.debug = false; // Debug Mode
	this.id = "Hooray!";
	this.mainWindow = $("#ui-global-main-window");
	this.mainWindowTitle = $("#ui-global-main-window-title");
	this.taskbar = $("#ui-global-main-window-taskbar");
	this.message = $("#global-flash-message");
	this.messageContainer = this.message.parent();
	this.content = $("#global-content");
	this.mainMenuReady = false;
	this.shellReady = false;
	this.leaveWarning = false; 	// Used to warn a user of something before he leaves the page. 
	this.formErrorShown = false; 
	
	this.init = function()
	{
		self.messageContainer.click(function() {
			self.closeFlashMessage();
		});
		
		self.processForms();
	}	

	this.setPageContent = function(newContent)
	{
		self.mainWindowContent.fadeOut(250,function() {
			self.mainWindowContent.html(newContent);
			self.mainWindowContent.fadeIn(250);
		});
	}
	
	this.load = function(url,callback)
	{
		if(!url)
			return;
		self.content.mask("Loading...");
		self.closeFlashMessage();
		self.mainWindowContent.load(url,function() {	
			self.prepareContent();
			if(callback)
				callback();			
		});			
	}
	
	this.loadContent = function(url,callback)
	{
		if(!url)
			return false;
		self.content.mask("Loading...");
		self.content.load(url,function() {
			self.prepareContent();
			if(callback)
				callback();
		});
	}
	
	this.setContent = function(html,callback)
	{
		if(!html)
			return false;
		else
		{
			self.resetStatusbar();
			self.content.html(html);
			if(callback)
				callback();
		}
	}
	
	this.loadInto = function(target,url,callback)
	{
		//alert("Loading "+url+" Into: "+target);
		if(!url || !target)
			return;
		var t = $(target); 
		t.fadeOut(250,function() {
			t.load(url,function(data){
				//alert(data);
				t.fadeIn(250,callback);
			});
		});
	}
	
	this.flashMessage = function(msg,msgClass)
	{
		if(!msgClass)
			msgClass = 'notice';
					
		$().toastmessage("showToast",{
			text: msg,
			sticky: true,
			position: "top-right",
			type: msgClass,
			close: function() {
				if(msgClass == 'error')
					self.flashErrorShown = false; 
			}
		});
	}
	
	this.closeFlashMessage = function()
	{
		
		self.messageContainer.hide(function() {
			self.message.html("");
			self.message.removeClass();
		});
	}
	
	this.flashError = function(msg,altHeading)
	{
		heading = altHeading ? altHeading : "";
		self.flashMessage("<strong>"+heading+" </strong>"+msg,"error");
	}
	
	this.flashSuccess = function(msg,altHeading)
	{
		heading = altHeading ? altHeading : "";
		self.flashMessage("<strong>"+heading+" </strong>"+msg,"success");
		setTimeout(m_shell.closeFlashMessage,7500);
	}
	
	this.flashInfo = function(msg)
	{
		self.flashMessage(msg,"notice");
	}
	
	this.flashAttention = function(msg)
	{
		self.flashMessage(msg,"warning");
	}
	
	this.equalizeBoxHeight = function()
	{
		// Iterate Between Rows:
		$("div.row").each(function() { 
			// Iterate between boxes in the row to get their heights:
			var heights = new Array();
			$.each($(this).find(".box"),function() {
				heights.push($(this).height());
			});
			var max_height = 0;
			for(var i=0;i<heights.length;i++)
			{
				max_height = Math.max(max_height,heights[i]);
			}
			if(max_height > 0)
			{
				$(this).find(".box").height(max_height);
			}
		});
	}
	
	// FORM VALIDATION METHODS
	this.processForms = function()
	{
		$("form .field.date").each(function() {
			$(this).datepicker({dateFormat: "d MM, yy"});
		});
		$("form:not(.novalidate)").each(function() {
			var form = $(this);
			form.find(":submit").click(function(e) {
				if(self.validateForm(form) == false)
				{
					e.preventDefault();
				}
			});
		});
		
		$("form input:checkbox.select-all").click(function() {
			var form = $(this).parents("form");
			var isChecked = $(this).is(":checked");
			form.find("input:checkbox:not(.select-all)").attr("checked",isChecked);
		});
	}
	
	this.validateForm = function(form)
	{
		var isJQuery = form instanceof jQuery;
		
		if(isJQuery == false)
			return false;
			
		var errors = 0;
		form.find(".field").each(function() {
			var field = $(this);
			if(field.attr("required") && field.val() == "")
			{
				field.addClass("error");
				field.click(function() { field.removeClass("error"); });
				errors++;
			}
		});
		if(errors > 0)
		{
			self.flashError("All fields marked red are mandatory.");
			return false; 
		}
		else
			return true; 
	}
}
