/**
 * Create a new instance of VCMPage
 * 
 * @classDescription	This class is the main class of the VCM
 * @return {VCMPage}	Returns a new VCMPage object
 * @constructor
 */
function VCMPage() {
}

VCMPage.prototype = {
	/**
	 * Reference to a jQuery container
	 * @type {jQuery}
	 */
	_container: null,
	
	/**
	 * Reference to a VCMHeader object
	 * @type {VCMHeader}
	 */
	_header: null,
	
	/**
	 * Reference to a VCMContent object
	 * @type {VCMContent}
	 */
	_content: null,
	
	/**
	 * Reference to a VCMSideBar object
	 * @type {VCMSideBar}
	 */
	_sideBar: null,
	
	/**
	 * Reference to a Modal object
	 * @type {VCMModal}
	 */
	_modal: null,
	
	_RechercheGoogle: null,
	
	_params: null,
	
	/**
	 * Initialize the page
	 */
	init: function() {
		console.log('VCMPage.init()');
		this._container = $('#mainContainer');
		this._header = new VCMHeader(this);
		this._content = new VCMContent(this);
		this._sideBar = new VCMSideBar(this);
		this._modal = new VCMModal();
		this._RechercheGoogle = new VCMGoogleSearch(this);
		this._modal.init();
		this._params = VCMUtils.parseCommand(hiddenCommand);
		VCMHistory.reset();
		this.onLoad();
		// Session poller
		setInterval($.proxy(this, 'pollSession'), 4.5 * 60 * 1000);
	},
	
	/**
	 * Destroy the page
	 */
	destroy: function() {
		console.log('VCMPage.destroy()');
		this._container = null;
		this._header = null;
		this._content = null;
		this._sideBar = null;
		this._modal.destroy();
		this._modal = null;
		this._RechercheGoogle = null;
	},
	
	/**
	 * Load new content into the page
	 * @param {Object} params
	 */
	load: function(params) {
		console.log('VCMPage.load(', params, ')');
		this.onUnload();
		params = params || {};
		this._params = params;
		params.url = 'do';		
		$('#content').html('<div id="loading"></div>');
		this._container.load(VCMUtils.prepareCommand(params) + " #mainContainer>*", $.proxy(this, 'onLoad'));
	},
	
	loadWithCallBack: function(params) {
		console.log('VCMPage.load(', params, ')');
		//this.onUnload();
		params = params || {};
		this._params = params;
		params.url = 'do';		
		$('#content').html('<div id="loading"></div>');
		var _me = this;
		this._container.load(VCMUtils.prepareCommand(params) + " #mainContainer>*", function() {
			$.proxy(_me._content, 'onLoad');
			if(params.callback) {
				params.callback();
			}
		});
	},
	
	/**
	 * Method called when the content of the page is loaded
	 * @private
	 */
	onLoad: function() {
		console.log('VCMPage.onLoad()');
		this._header.init();
		// Don't remember the language
		delete this._params.lang;
		this._content.init(this._params);
		this._sideBar.init();
		this._RechercheGoogle.init();
	},
	
	/**
	 * Method called when the content of the page is unloaded
	 * @private
	 */
	onUnload: function() {
		console.log('VCMPage.onUnload()');
		this._header.destroy();
		this._content.destroy();
		this._sideBar.destroy();
		this._RechercheGoogle.destroy();
	},
	
	pollSession: function() {
		console.log('VCMPage.pollSession()');
		$.getJSON(rootPath + 'doJSON?command=sessionPoll&rd=' + Math.random(), $.proxy(this, 'onSessionPoll')); 
	},
	
	onSessionPoll: function(json) {
		console.log('VCMPage.onSessionPoll(', json,')');
		if (json) {
			if (json.status == -1) {
				// No session
				this.load();
			} else if (json.status == 0) {
				// Guest
			} else {
				// User
			}
		} else {
			// An error occured
		}		
	},
	
	/**
	 * @return {VCMContent} The VCMContent object
	 */
	getContent: function() {
		return this._content;
	},
	
	/**
	 * @return {VCMModal} The VCMModal object
	 */
	getModal: function() {
		return this._modal;
	},
	
	/**
	 * @return {VCMSideBar} The VCMSideBar object
	 */
	getSideBar: function() {
		return this._sideBar;
	},
	
	/**
	 * @return {VCMHeader} The VCMHeader object
	 */
	getHeader: function() {
		return this._header;
	}
};


