YAHOO.namespace('YAHOO.lib');
// Constructor
YAHOO.lib.Page = function(config) {
	this.contentElement = null;
	this.config = (YAHOO.lang.isUndefined(config)) ? {} : config;
	YAHOO.util.Event.addListener(window, 'load', this.init, this, true);
}
YAHOO.lib.Page._LOADING_FAILURE_DIALOG 	= 'LoadingFailureDialog';
YAHOO.lib.Page._CONTENT_ELEMENT_ID 		= 'content';
YAHOO.lib.Page._LOGIN_FORM_ELEMENT_ID 	= 'loginContainer';
YAHOO.lib.Page._ID = 'doc';
// Prototype
YAHOO.lib.Page.prototype = {
	config: null, 
	loader: null, 
	waitControl: null, 
	// Init
	init: function() {
		this.element = YAHOO.util.Dom.get(YAHOO.lib.Page._ID);
		var config = this.config;
		if (YAHOO.lang.isUndefined(config.haswait)) config.haswait = true;
		this.render();
		this.createControls(config);
	}, 
	// Render
	render: function() {
		if (!this.contentElement) {
			this.contentElement = YAHOO.util.Dom.get(YAHOO.lib.Page._CONTENT_ELEMENT_ID);
		}
	},
	// Create Calendar
	createCalendar: function() {
		if (YAHOO.lib && YAHOO.lib.Calendar) {
			if (!YAHOO.util.Dom.get(YAHOO.lib.Calendar._CONTROL_ID)) {
				var tmpDiv = document.createElement("div");
				tmpDiv.setAttribute('id', YAHOO.lib.Calendar._CONTROL_ID);
				YAHOO.util.Dom.addClass(tmpDiv, 'hidden');
				this.element.appendChild(tmpDiv);
			}
			this.calendar = new YAHOO.lib.Calendar(YAHOO.lib.Calendar._CONTROL_ID, {
				'visible':			false, 
				'page':				this, 
				'parent':			this
			});
		}
	}, 
	// Show wait
	showWait: function() {if (this.wait) this.wait.show();}, 
	// Hide wait
	hideWait: function() {if (this.wait) this.wait.hide();}, 
	// Create controls
	createControls: function(config) {
		if (config.haswait) this.createWait();
		this.createCalendar(); 
		this.createSiteMenu();
		this.createLoginForm();
	}, 
	// Create wait
	createWait: function() {
		if (!this.wait) this.wait = YAHOO.lib.Wait.create();
	}, 
	// Create site menu
	createSiteMenu: function() {
		if (!this.siteMenu) this.siteMenu = YAHOO.lib.SiteMenu.create(YAHOO.lib.SiteMenu._ID, {page: this});
	}, 
	// Create login form
	createLoginForm: function() {
		this.loginFormElement = YAHOO.util.Dom.get(YAHOO.lib.Page._LOGIN_FORM_ELEMENT_ID);
		if (this.loginFormElement && (!YAHOO.lang.isUndefined(YAHOO.lib.LoginForm))) {
			this.loginForm = YAHOO.lib.SingleForm.create('login', {
				page: this, proxy: 'login_proxy.php', draggable: false, messager: true
			});
			this.loginForm.load('login', 'enter', 'form[type]=2');
		}
	}, 
	// Loading failure dialog confirm
	loadingFailureDialogConfirm: function() {
		this.loadingFailureDialog.hide();
	}, 
	// Create loading failure dialog
	createLoadingFailureDialog: function() {
		if (!this.loadingFailureDialog) {
			var conf = {width: '300px', caption: 'System error', text: 'Couldn\'t connect to server', 
				icon: YAHOO.widget.SimpleDialog.ICON_BLOCK, 
				buttons: [
					{text: 'OK', handler: {fn: this.loadingFailureDialogConfirm, obj: this, scope: this}, isDefault:true}
				]};
			this.loadingFailureDialog = YAHOO.lib.Dialog.create(YAHOO.lib.Page._LOADING_FAILURE_DIALOG, conf);
		}
	}, 
	// Loading Failure
	loadingFailure: function() {
		this.createLoadingFailureDialog();
		this.loadingFailureDialog.show();
	}, 
	// Get Query
	getQuery: function(aname, aphase, query) {
		var _query = 'action[name]=' + escape(aname);
		if (!YAHOO.lang.isUndefined(aphase) && !YAHOO.lang.isNull(aphase)) 
			_query += '&' + 'action[phase]=' + escape(aphase);
		if (!YAHOO.lang.isUndefined(query) && !YAHOO.lang.isNull(query)) 
			_query += '&' + query;
		return _query;
	}, 
	// Get JS Object
	getJSObject: function(js) {return eval('new Object(' + js + ')');},	
	// Redirect
	redirect: function(location) {if (this.loader) this.loader.show(); window.location = location;}, 
	// To String
	toString: function() {return 'Page Object';} 
}