/**
 * 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();
		/*
		création du cookie pour le load du popup video
		if(this.readCookie("videoCookie") == null){
			this._modal.show($('#embeddedVideocontainer').html());
			this.createCookie("videoCookie", "1",null);
		}*/
		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'));
	},
	
	/**
	 * 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;
	},
	
	createCookie: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},

	readCookie: function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	eraseCookie:function (name) {
		createCookie(name,"",-1);
	}

	
};


