/**
 * @author Réjean Bouchard
 */

function cmsPage(page) {
	this._page = page;
}

cmsPage.prototype = {
	/**
	 * Reference to a VCMPage object
	 * @type {VCMPage}
	 */
	_page: null,
	
	/**
	 * jQuery reference to the content container
	 * @type {jQuery}
	 */
	_content: null,
	
	_title: null,
	
	_subtitle: null,
	
	_cmsMenuItems: null,
	
	
	/**
	 * Initialize
	 */
	init: function() {
		console.log('cmsPage.init()');
		this._content = $('#cmsPage');
		this._title = $('#cmsContentTitle');
		this._subtitle = $('#cmsContentSubtitle');
		this._cmsMenuItems = $('.niv1,.niv2');
		this.addEvents();
		this.onLoad();
	},	
	
	/**
	 * Destroy
	 */
	destroy: function() {
		console.log('cmsPage.destroy()');
		this.removeEvents();
	},
	
	/**
	 * Action performed when the sidebar is loaded
	 */
	onLoad: function() {
		console.log('cmsPage.onLoad()');
		//$("#cmsContentTitle").html($(this).html());
	},
	
	/**
	 * Action performed when VCMcmsPage is unloaded
	 */
	onUnload: function() {
		console.log('cmsPage.onUnload()');
	},
	
	/**
	 * Load the content of the sidebar
	 * @param {Object} params
	 */
	load: function(params) {
		console.log('cmsPage.load()');
		/*
		this.onUnload();
		var me = this;
		this._content.load(VCMUtils.prepareCommand(params), function() {
			me.onLoad();
		});
		*/
	},
	
	/**
	 * Add events
	 */	
	addEvents: function() {
		console.log('cmsPage.addEvents()');
		var me = this;

		this._cmsMenuItems.click(function(){
			console.log('Click on Niv1 or Niv2');
			var ret = false;
			var obj = $(this).nextAll(".niveau_2");
			if ($(this).attr('target') == '_self') {
				//console.log("id = " + VCMUtils.findNumber($(this).attr('id')));
				$('#cmsPageContent').load(VCMUtils.prepareCommand({command: 'displayContent',id: VCMUtils.findNumber($(this).attr('id')),drawMenu: "0"}) + " #cmsItemContent");
				if($(this).attr('class') == 'niv1'){
					$("#cmsContentTitle").html($(this).html());	//on place le titre
					$("#cmsContentSubtitle").html("");			//on efface le sous-titre
					$(".niveau_ouvert").addClass("niveau_2");			//on cache le vieux sous-menu...
					$(".niveau_ouvert").removeClass("niveau_ouvert");	//...
					obj.addClass("niveau_ouvert");	//on ouvre le nouveau sous-menu...
					obj.removeClass("niveau_2");	//...
					if($(".niv1Selected").size() > 0){
						$(".niv1Selected").removeClass("niv1Selected");	//on efface l'ancienne selection...
					}
					if ($(".niv2Selected").size() > 0) {
						$(".niv2Selected").removeClass("niv2Selected"); //... et sous-selection
					}
					$(this).addClass("niv1Selected");	//on marque la nouvelle selection
				}
				if ($(this).attr('class') == 'niv2') {
					$("#cmsContentSubtitle").html($(this).html());	//on place le sous-titre
					if ($(".niv2Selected").size() > 0) {
						$(".niv2Selected").removeClass("niv2Selected"); //... et sous-selection
					}
					$(this).addClass("niv2Selected");	//on marque la nouvelle selection
				}
			}
			else{
				ret = true;
			}
			return ret;
		});
	},

	/**
	 * Remove events
	 */	
	removeEvents: function() {
		console.log('cmsPage.removeEvents()');
		this.niv1.unbind('click');
	},

	/**
	 * Load the CMS content
	 */
	displayCMS: function() {
		console.log('cmsPage.displayCMS()');
		/*
		if ($('#cmsSidebar').length == 0) {
			this.load({command:'displayCMSSidebar'});
		}
		*/
	}
};
