var $Application = new Class({
	initialize: function() {
		this.blog_id = 0;
		this.setElement();
		this.periodical_key = [];
	},
	
	setElement: function(){
		var self = this;
		
		this.messages_container = window.findElementById("app_msg");
		this.confirm_container = window.findElementById("ws-area");
//		var preloader = window.findElementById("preloader");
		this.messages_template = {
			success: 	window.findElementById("app-message-success"),
			error: 		window.findElementById("app-message-error"),
			notify: 	window.findElementById("app-message-notify")
		}
		this.confirm_dlg = window.findElementById("app-confirm");
		if (Browser.Engine.version == 4) {
			window.addEvent("scroll", function(){
				var height = window.findElementById("body").getScroll().y;
				self.messages_container.setStyle("top",height);
			});
		}
		
	},
	
	
	alert: function (message, type) {
		type = type ? type : $MESSAGE.NOTIFY;
		var self = this;
		var msg_html = '';
		
		switch (type) {
			case $MESSAGE.SUCCESS:
				msg_html = this.messages_template.success.cloneWithId();
				break;
			case $MESSAGE.ERROR:
				msg_html = this.messages_template.error.cloneWithId();
				break;
			default:
				msg_html = this.messages_template.notify.cloneWithId();
		}
		
		msg_html.removeAttribute("id");
		msg_html.getElement("p").set("html", message);
		msg_html.inject(this.messages_container, "top");
		if (Browser.Engine.version == 4) {
			var height = window.findElementById("body").getScroll().y;
			this.messages_container.setStyle("top",height);
		}
		
		var timeout = $getInstance($Config).getKey("timeout", "messages");
		
		if(timeout == null) {
			timeout = 15000;
		}
		
		setTimeout(function () { self.deleteLastMessage(); }, timeout);
		
		return msg_html;
	},
	
	deleteLastMessage: function(){
		if(this.messages_container.getChildren().length > 0){
			this.messages_container.getChildren()[this.messages_container.getChildren().length-1].destroy();
		}
	},
	
	confirm: function (message, exp_yes, exp_no) {
		this.confirm_dlg = window.findElementById("app-confirm");
		
		this.confirm_dlg.getElement("#text").set("html", message);
		this.confirm_dlg.getElement("#ok").removeEvents('click');
		this.confirm_dlg.getElement("#cancel").removeEvents('click');
		this.confirm_dlg.getElement("#ok").addEvent("click", function() {
			$getInstance($DlgContainer).unloadForm();
			if(exp_yes) {
				exp_yes();
			}
			return false;
		});
		this.confirm_dlg.getElement("#cancel").addEvent("click", function() {
			$getInstance($DlgContainer).unloadForm();
			if(exp_no) {
				exp_no();
			}
			return false;
		});
		$getInstance($DlgContainer).loadForm(this.confirm_dlg);
		this.confirm_dlg.show();
		return true;
	},
	
	aclConfirm: function (message) {
		
		var confirm_dlg = window.findElementById("acl-confirm");
		
		confirm_dlg.getElement("#acl_confirm_text").set("text", message);
		
		var auth = confirm_dlg.getElement("#login_acl");
		auth.removeEvents("click");
		
		auth.addEvent("click", function() {
			$getInstance($DlgContainer).button_close.fireEvent("click");
			$getInstance($DlgLogin).show();
			return false;
		});
		
		var signup = confirm_dlg.getElement("#reg_acl");
		signup.removeEvents("click");
		
		signup.addEvent("click", function() {
			$getInstance($DlgContainer).button_close.fireEvent("click");
			$getInstance($DlgUserSignup).show();
			return false;
		});
		
		var close = confirm_dlg.getElement("#close_reg");
		close.removeEvents("click");
		
		close.addEvent("click", function() {
			$getInstance($DlgContainer).button_close.fireEvent("click");
			return false;
		});
		
		$getInstance($DlgContainer).loadForm(confirm_dlg);
		confirm_dlg.show();
		
		return true;
	},
	
	location: function (href) {
		document.location = href;
	},
	
	locationReload: function () {
		var href = document.location;
		
		var arr = document.location.toString().split("/");
		if(arr[3] == "#"){
			href = arr[0]+arr[1];
		}
		
		this.location(href);
	},
	
	getServerTime: function() {
		var time_zone = 0;
		
		var response = null;
		$URL.ModPublic.onGetServerDate({async: false, onSuccess: function(json){
			response = json;
		}}).POST({timezone: time_zone});
		
		return response.date;
	},
	
	getTimezoneOffset: function () {
		return (new Date()).getTimezoneOffset();
	},
	
	showCreatePostDialog: function() {
		var blog_id = $getInstance($TabNavigator).getActiveTabId();
		
		$getInstance($DlgCreatePost).onOpen(blog_id);
		return false;
	},
	
	setInterval: function(callback, interval) {
		return setInterval(callback, interval);
	},
	
	reloadWorkspace: function () {
		this.location("/");
	},
	
	checkUserConfurmResult: function (result) {
		if(result == 1) {
			this.alert($Locale.ModUsers.MSG_CONFIRM_OK(), $MESSAGE.SUCCESS);
			clearTimeout($getInstance($User).user_confirm_time_out);
			$getInstance($Application).location("/");
		} else if(result == -1) {
			this.alert($Locale.ModUsers.MSG_CONFIRM_ERROR(), $MESSAGE.ERROR);
		}
	},

	checkUserDeleteTabs: function (result) {
		if(result == 1) {
			this.alert($Locale.ModUsers.MSG_DELETE_TABS(), $MESSAGE.NOTIFY);
		}
	},

	getWsHost: function(ws_name){
		var current_ws_name = $getInstance($Workspace).name;
		var url = '';
		
		if(current_ws_name == ws_name){
			url = 'http://' + ws_name + '.' + $HTTP_HOST + '/';
		} else {
			url = 'http://' + ws_name + '.' + $project_domain + '/';
		}
		return url;
	},
	
	getHomeHost: function(){
		return 'http://' + $HOME_SUBDOMAIN + '.' + $HTTP_HOST + '/';
	},
	
	periodicalStart: function(func, key, timeout, bind, args){
		var value = func.periodical(timeout, bind, args);
		this.periodical_key[key] = value;
	},
	
	periodicalStop: function(key){
		$clear(this.periodical_key[key]);
	}
});

$Application.getInstance = function() {
	return $getInstance($Application);
}