/**
 * VCM Content
 * @constructor
 */
function VCMContent(page) {
	this._page = page;
}

VCMContent.prototype = {
	/**
	 * Reference to the VCMPage parent
	 * @type {VCMPage}
	 */
	_page: null,
	
	/**
	 * Reference to the VCMActivityList
	 * @type {VCMActivityList}
	 */
	_activityList: null,
	
	/**
	 * Reference to the VCMDetails
	 * @type {VCMDetails}
	 */
	_details: null,
	
	/**
	 * Reference to the VCMProfileContent
	 * @type {VCMProfileContent}
	 */
	_profile: null,
	
	_cmspage: null,
	
	/**
	 * Last params from load
	 * @type {Object}
	 */
	_params: null,
	
	/**
	 * Initialize the content
	 */
	init: function(params) {
		console.log('VCMContent.init()');
		this._params = params;
		this._activityList = new VCMActivityList(this);
		this._details = new VCMDetails(this);
		this._profile = new VCMProfileContent(this._page);
		//this._cmspage = new VCMCMSPage(this._page);
		this.onLoad();
	},

	/**
	 * Destroy the content
	 */
	destroy: function() {
		console.log('VCMContent.destroy()');
		this._activityList = null;
		this._details = null;
		this._profile = null;
		//this._cmspage = null;
	},
	
	/**
	 * Action performed when the content is loaded
	 */
	onLoad: function() {
		console.log('VCMContent.onLoad()');
		VCMHistory.setParams(this._params);		
		if ($('#detailContainer').length == 1) {
			this._details.init();
		} else if ($('#thumbnails, #lignesListe, #homePage').length == 1) {
			this._activityList.init();
		} else if ($('#welcomePage, #homePage').length == 1) {
			this._profile.init();
		} else if ($('#cmsPage').length == 1){
			//this._cmspage.init();
		}
	},
	
	/**
	 * Action performed just before the content is unloaded
	 */
	onUnload: function() {
		console.log('VCMContent.onUnload()');
		if ($('#detailContainer').length == 1) {
			this._details.destroy();
		} else if ($('#thumbnails, #lignesListe, #homePage').length == 1) {
			this._activityList.destroy();
		} else if ($('#welcomePage, #homePage').length == 1) {
			this._profile.destroy();
		} else if ($('#cmsPage').length == 1){
			//this._cmspage.destroy();
		}
		//this._page._RechercheGoogle.hideResults();
		// Remove the content and store it in history
		VCMHistory.setContent($('#content').remove().get(0));
	},
	
	/**
	 * Load the content
	 * @param {Object} params
	 */
	load: function(params, reset) {
		console.log('VCMContent.load(', params, ')');
		this._params = params;
		// Call onUnload
		this.onUnload();
		// History
		if(!(reset === undefined)) {
			VCMHistory.reset();
		} else {		
			VCMHistory.forward();
		}
		// Create new content with loading
		$('#afterContent').before('<div id="content"><div id="loading"></div></div>');
		// Load content and call onLoad
		$("#content").load(VCMUtils.prepareCommand(params), $.proxy(this, 'onLoad'));
	},
	
	/**
	 * Show activities
	 */
	showActivities: function() {
		console.log('VCMContent.showActivities()');
		this.load({
			mode: this.displayMode,
			daysOffset: VCMUtils.getDaysOffset(),
			disciplineId: this.disciplineId,
			sectorId: this.sectorId
		}, true);
	},
	
	/**
	 * Load content with details of an activity
	 * @param {Number} id Activity id
	 */
	showActivity: function(id) {
		this.load({
			command: 'displayActivity',
			id: id
		});
	},
	
	/**
	 * Load content with details of an event
	 * @param {Number} id event id
	 */
	showEvent: function(id) {
		this.load({
			command: 'displayEvent',
			id: id
		});
	},
	
	/**
	 * Load content with details of a location
	 * @param {Object} id Location id
	 */
	showLocation: function(id) {
		this.load({
			command: 'displayLocation',
			id: id
		});
	},
	
	/**
	 * Load content with CMS Menu on selected menuItem
	 * @param {Number} id
	 * @param {Boolean} drawMenu
	 */
	showCMSContent: function(id,cmsId,drawMenu) {
		this.load({
			command: 'displayContent',
			id: id,
			cmsId : cmsId,
			drawMenu: drawMenu
		}, true);
	},
	
	/**
	 * Load content with the home page
	 */
	showHomePage: function() {
		this.load({
			command: 'displayHomePage'
		}, true);
	},
	
	showSearchResults: function(params) {
		this.load($.extend({command:'search'}, params), true);
	},
	
	/**
	 * @return {VCMPage} The VCMPage parent
	 */
	getPage: function() {
		return this._page;
	},
	
	/**
	 * @return {Object} Returns last params from load
	 */
	getLastParams: function() {
		if (VCMUtils.isHistoryEmpty()) return VCMUtils.parseCommand(hiddenCommand);
		else return VCMUtils.getLastHistory().params;
	},
	
	/**
	 * Reset
	 */
	reset: function() {
		this.displayMode = 'thumbnails';
		this.disciplineId = -1;
		VCMHistory.reset();
	},
	
	/**
	 * Go back
	 * @param {Object} evt The Event object
	 */
	goBack: function(evt) {
		// Call onUnload
		this.onUnload();
		// If we have old content
		if (VCMHistory.back()) {
			// Put back the old content
			this._params = VCMHistory.getParams();
			$('#afterContent').before(VCMHistory.getContent());
			// Call onLoad
			this.onLoad();
		} else {
			// Simulate today click
			$('#day0').click();
		}
		// Cancel default event
		return false;
	}
};

