/**
 * Page Manager.
 * 1. First responsibility is loading content through ajax. This library is used by menu.
 * 2. Clean error div when there is no data.
 */


var PageManager = Class.create({
	contentContainterId: null,
	initialize: function(contentContainterId){
		this.contentContainterId = contentContainterId;
		if($(this.contentContainterId) == null){
			alert("Loading content through ajax module: no content\' container found with id: " + contentContainterId);
		}
	},
	
	ajaxLoad: function(href){
		if($(this.contentContainterId) != null){
			/**
			@todo: find module name from link or menu AND
			check if this module support loading content without layout mode.
			*/
			while(href.endsWith('/')){
				href = href.substr(0,(href.length -1));
			}
			if(href.include("?")){
				var queryString = href.split("?");
				queryString = queryString[1];
				if(queryString.length > 0){
					href += "&enable_layout=0";
				} else{
					href += "enable_layout=0";
				}		
			} else{
				href += "?enable_layout=0";
			}
			new Ajax.Updater(this.contentContainterId,href,{method:'get',evalScripts:true});
		} else{
			//load as normal, no ajax
			window.location = href;
		}
	},
	
	hideErrorDivs: function(){
		var errorDivs = $$("div.error");
		errorDivs.each(function (errorDiv){
			if(errorDiv.empty()){
				errorDiv.setStyle({
					maxWidth:0,
					border: 0
				});
//				console.log(errorDiv.readAttribute("max-width"));
			}	
		});
	}	
});
var pageManager = null;
/**
 * @todo: the id of container is fixed now. Later should standardize the name of content container id.
 */ 
document.observe("dom:loaded",function(){
	pageManager = new PageManager("content");
	pageManager.hideErrorDivs();
});

