dojo.declare("mf_main", null,
{
	showErrorTooltip_ids: new Array(),

	constructor : function(aArguments, aNode) {
		dojo.addOnLoad(function(){mf_main.onLoad();});
		dojo.addOnUnload(function(){mf_main.onUnload();});
		dojo.addOnWindowUnload(function(){mf_main.onWindowUnload();});
	},

	onLoad: function(){},
	
	onUnload : function(){},

	onWindowUnload : function(){},

	showErrorTooltip: function(element_id, message) {

		var target = dijit.byId(element_id);
		
		if(!target) {
			console.debug("showErrorTooltip: unknown element '"+element_id+"'");
			return;
		}
		
		this.showErrorTooltip_ids.push(element_id);


		dijit.showTooltip('<b style="color:red;">'+message+'</b>', target.domNode );

	},

	hideAllErrorTooltips: function() {
		for(c=0; c<this.showErrorTooltip_ids.length;c++) {
			var target = dijit.byId(this.showErrorTooltip_ids[c]);
			dijit.hideTooltip(target.domNode);
		}
	},
	
	/**
	 * Performs AJAX operation (with busy button)
	 *
	 * @param module {string}
	 * @param action {string}
	 * @param data {mixed} .. if not defined or null, GET request is used, else POST is used for request
	 * @param onOk {function}
	 * @param string {busy_button}
	 * @param sync {bool}
	 * @param string|array {urlParams}
	 * @returns void
	 */
	AJAXOperation: function(module_identifier, action, data, onOK, busy_button, sync, urlParams) {
		
		mf_main.hideAllErrorTooltips();
		
		if(!sync)
			sync=false;
		
		var URL = AJAX_REQUEST_ROOT_URL+module_identifier+"/"+action+"/";
		
		if(typeof(urlParams) != "undefined"){
			if(typeof(urlParams) == "object"){
				URL += urlParam.join("/");
			} else if(typeof(urlParams) == "string") {
				URL += urlParams;
			}
		}
		if(URL.charAt(URL.length - 1) != "/") URL += "/";
		
		// some proxies don't allow empty POST request
		var sendMethod = typeof(data) != "undefined" && data != null ? dojo.xhrPost : dojo.xhrGet;
		
		sendMethod({

			url: URL,
			content: data,
			handleAs: "json",	
			timeout: 60000,
			
			sync: sync,
	
			load: function(rsp, ioargs) {


		 		if(!rsp["result"])
		 			rsp["result"]="";

				
		 		if(busy_button) {
		 			if( busy_button.push!==undefined ) {
		 				for(var i=0; i<busy_button.length;i++) {
		 					dijit.byId(busy_button[i]).cancel();
		 				}
		 			} else {
		 				dijit.byId(busy_button).cancel();
		 			}
		 		}
				
				if(rsp.result!="OK") {
					mf_main._handleOperationErrorResponse( rsp );
				} else {
					if(onOK)
						onOK(rsp, ioargs);
				}

				return rsp;
			},
	
			error: function(error, ioargs){
				
		 		if(busy_button) {
		 			if( busy_button.push!==undefined ) {
		 				for(var i=0; i<busy_button.length;i++) {
		 					dijit.byId(busy_button[i]).cancel();
		 				}
		 			} else {
		 				dijit.byId(busy_button).cancel();
		 			}
		 		}
				
		 		if(ioargs.xhr.status==401) {
		 			if(ioargs.xhr.responseText=="ACL:HAVENTACCESS") {
		 				var dialog = dijit.byId("havent_admin_access_dlg");
		 			} else {
		 				var dialog = dijit.byId("session_timeout_dlg");
		 			}
		 			
		 			if(dialog)
		 				dialog.show();
		 			
		 		} else {
					console.debug("ERROR", error, ioargs);
		 		}
			}
		});
	},
	/**
	 * Performs AJAX operation (with busy button) with specific URL
	 *
	 * @param URL {string}
	 * @param data {mixed} .. if not defined or null, GET request is used, else POST is used for request
	 * @param onOk {function}
	 * @param string {busy_button}
	 * @param sync {bool}
	 * @param string|array {urlParams}
	 * @returns void
	 */
	AJAXOperationWithURL: function(URL, data, onOK, busy_button, sync, urlParams) {
		
		mf_main.hideAllErrorTooltips();
		
		if(!sync)
			sync=false;
		
		if(typeof(urlParams) != "undefined"){
			if(typeof(urlParams) == "Array" || typeof(urlParams) == "Object"){
				URL += urlParam.join("/");
			} else if(typeof(urlParams) == "string") {
				URL += urlParams;
			}
		}
		if(URL.charAt(URL.length - 1) != "/") URL += "/";
		
		// some proxies don't allow empty POST request
		var sendMethod = typeof(data) != "undefined" && data != null ? dojo.xhrPost : dojo.xhrGet;
		
		sendMethod({

			url: URL,
			content: data,
			handleAs: "json",	
			timeout: 60000,
			
			sync: sync,
	
			load: function(rsp, ioargs) {


		 		if(!rsp["result"])
		 			rsp["result"]="";

				
		 		if(busy_button) {
		 			if( busy_button.push!==undefined ) {
		 				for(var i=0; i<busy_button.length;i++) {
		 					dijit.byId(busy_button[i]).cancel();
		 				}
		 			} else {
		 				dijit.byId(busy_button).cancel();
		 			}
		 		}
				
				if(rsp.result!="OK") {
					mf_main._handleOperationErrorResponse( rsp );
				} else {
					if(onOK)
						onOK(rsp, ioargs);
				}

				return rsp;
			},
	
			error: function(error, ioargs){
				
		 		if(busy_button) {
		 			if( busy_button.push!==undefined ) {
		 				for(var i=0; i<busy_button.length;i++) {
		 					dijit.byId(busy_button[i]).cancel();
		 				}
		 			} else {
		 				dijit.byId(busy_button).cancel();
		 			}
		 		}
				
		 		if(ioargs.xhr.status==401) {
		 			if(ioargs.xhr.responseText=="ACL:HAVENTACCESS") {
		 				var dialog = dijit.byId("havent_admin_access_dlg");
		 			} else {
		 				var dialog = dijit.byId("session_timeout_dlg");
		 			}
		 			
		 			if(dialog)
		 				dialog.show();
		 			
		 		} else {
					console.debug("ERROR", error, ioargs);
		 		}
			}
		});
	},
	
	/**
	 * Performs AJAX operation (with busy button)
	 *
	 * @param operation {string}
	 * @param data {mixed}
	 * @param onOk {function}
	 * @param string {busy_button}
	 * @param string|array {urlParams}
	 * @returns void
	 */
	FileUploadAction: function(form_name, module_identifier, action, data, onOK, busy_button, urlParams) {
	
		mf_main.hideAllErrorTooltips();

		var URL = AJAX_REQUEST_ROOT_URL + module_identifier + "/" + action + "/";
		if(typeof(urlParams) != "undefined"){
			if(typeof(urlParams)=="object"){
				URL += urlParams.join("/");
			} else if(typeof(urlParams) == "string") {
				URL += urlParams;
			}
		}
		if(URL.charAt(URL.length - 1) != "/") URL += "/";
		
		 dojo.io.iframe.send({

				url: URL,
				form: dojo.byId(form_name),
				content: data,
				method: "post",
				handleAs: "json",	
		
				handle: function(rsp, ioArgs) {
			 
			 		if(!rsp["result"])
			 			rsp["result"]="";
			 
			 		if(busy_button) {
			 			if( busy_button.push!==undefined ) {
			 				for(var i=0; i<busy_button.length;i++) {
			 					dijit.byId(busy_button[i]).cancel();
			 				}
			 			} else {
			 				dijit.byId(busy_button).cancel();
			 			}
			 		}
			 		
			 		
					if(rsp.result!="OK") {
						mf_main._handleOperationErrorResponse( rsp );
					} else {
						if(onOK)
							onOK(rsp, ioArgs);
					}

					return rsp;
				}
			});
		
	},
	
	_handleOperationErrorResponse: function( rsp, responseAreaID ) {
		
		if(!rsp.message)
			return;
		
		if(rsp.message["tooltip_errors"]) {
			for(var ei in rsp.message["tooltip_errors"]) {
				if(dijit.byId(ei)) {
					mf_main.showErrorTooltip(ei, rsp.message["tooltip_errors"][ei]);
					break;
				}
				
			}
		}

		if(rsp.message["message"]) {
			if(responseAreaID){
				dojo.byId(responseAreaID).innerHTML = rsp.message["message"];
			} else {
				console.log(rsp.message["message"]);
			}
		}
	}
});

mf_main = new mf_main();

dojo.declare("mf_pager", null, {
	pager_ID: 0,

	get_page_module: "",
	get_page_action: "",
	
	pages_count: 0,
	page: 0,

	display_pages_limit: 5,
	
	action_params: {},

	_pages: {},
	
	constructor : function(
				pager_ID,
				get_page_module,
				get_page_action,
				pages_count,
				selected_page,

				display_pages_limit,
				
				action_params
			) {
		this.pager_ID = pager_ID;
		this.get_page_module = get_page_module;
		this.get_page_action = get_page_action; 
		this.pages_count = pages_count;
		this.page = selected_page;
		this.display_pages_limit = display_pages_limit;
		this.action_params = action_params;
		
	},

	nextPage: function(){
		this._setPage(this.page + 1);
	},
	
	previousPage: function(){
		this._setPage(this.page - 1);
	},
	
	setPage: function(which){
		this._setPage(which);
	},
	
	showContent: function(){
		var page_content_node = this._getPageContentNode();  
		
		page_content_node.innerHTML = this._getLoadingMsg();
		
		this.action_params["page"] = this.page;

		mf_main.AJAXOperation(
							this.get_page_module, 
							this.get_page_action, 
							this.action_params, 
							function(response) {
								page_content_node.innerHTML = response.data.content; 
							}
					); 
	},

	_setPage: function(which) {
		if(which<1 || which>this.pages_count)
			return;

		this._getPageNoNode(this.page).innerHTML =
			'<a href="#"  onclick="'+this.pager_ID+'.setPage('+this.page+');">'+this.page+'</a>';
	
		this._getPageNoNode(which).innerHTML = "<strong>"+which+"</strong>";

		var show=0;
		
		for(var pg = 1; pg <= this.pages_count; pg++){
			if(!this._pages[pg]){
				this._pages[pg] = this._getPageNoNode(pg);
			}
			this._pages[pg].style.display="none";
		}
		for(var pg = which - 2; show != this.display_pages_limit && pg <= this.pages_count; pg++){
			if(pg < 1) 
				continue;
			
			if(this._pages[pg]){
				this._pages[pg].style.display="";
				show++;
			}
		}
		
		
		this._getPrevButtonNode().style.display= which > 1 ? "" : "none";
		this._getNextButtonNode().style.display= which < this.pages_count ? "" : "none";

		this.page=which;
		this.showContent();		
	},

	_getPrevButtonNode: function() {
		return dojo.byId("pager_prev_bt_"+this.pager_ID);
	},
	_getNextButtonNode: function() {
		return dojo.byId("pager_next_bt_"+this.pager_ID);
	},
	
	_getPageNoNode: function( page_no ) {
		return dojo.byId("pager_page_no_"+this.pager_ID+"_"+page_no);
	},

	_getPageContentNode: function() {
		return dojo.byId("pager_content_"+this.pager_ID);
	}, 

	_getLoadingMsg: function() {
		return dojo.byId("pager_loading_msg_"+this.pager_ID).innerHTML;
	}
});


dojo.declare("mf_calendar", null, {
	calendar_identifier: "",
	
	AJAX_URL: "",
	
	selected_year: "",
	selected_month: "",
	action_params: {},
	object_identifier: "",
	
	constructor : function(
				calendar_identifier,
				selected_month,
				selected_year,
				AJAX_URL,
				object_identifier
			) {
		this.calendar_identifier = calendar_identifier;
		this.selected_year = selected_year;
		this.selected_month = selected_month;
		this.AJAX_URL = AJAX_URL;
		this.object_identifier = object_identifier;
	},

	nextMonth: function(){
		if(hs) {
			hs.close();
		}
		this._setMonth(this.selected_month + 1);
	},
	
	previousMonth: function(){
		this._setMonth(this.selected_month - 1);
	},
	
	setMonth: function(which){
		this._setMonth(which);
	},
	
	showContent: function(){
		var calendar_content_node = this._getCalendarContentNode();  
		
		calendar_content_node.innerHTML = this._getLoadingMsg();
		
		this.action_params["calendar_identifier"] = this.calendar_identifier;
		this.action_params["object_identifier"] = this.object_identifier;
		this.action_params["month"] = this.selected_month;
		this.action_params["year"] = this.selected_year;

		mf_main.AJAXOperationWithURL(
							this.AJAX_URL, 
							this.action_params, 
							function(response) {
								calendar_content_node.innerHTML = response.data.content; 
							}
					); 
	},

	_setMonth: function(which) {
		if(which==13) {
			which = 1;
			this.selected_year  = this.selected_year + 1;
		}
		if(which==0) {
			which = 12;
			this.selected_year  = this.selected_year - 1;
		}
		
		var show=0;
		this.selected_month=which;
		this.showContent();		
	},

	_getLoadingMsg: function() {
		return dojo.byId("calendar_loading_msg_"+this.calendar_identifier).innerHTML;
	},
	
	_getCalendarContentNode: function() {
		return dojo.byId("calendar_content_"+this.calendar_identifier);
	}
});
