/**
 * Create a new instance of VCMSideBar
 * @classDescription Class for the side bar
 * @param {VCMPage} page Reference to the VCMPage object
 * @return A VCMSideBar object
 * @constructor
 */
function VCMSideBar(page) {
	this._page = page;
}

VCMSideBar.prototype = {
	/**
	 * Reference to a VCMPage object
	 * @type {VCMPage}
	 */
	_page: null,
	
	/**
	 * Reference to a VCMLogin object
	 * @type {VCMLogin}
	 */
	_login: null,
	
	/**
	 * Reference to a VCMProfileBar object
	 * @type {VCMProfileBar}
	 */
	_profileBar: null,
	
	/**
	 * jQuery reference to the content container
	 * @type {jQuery}
	 */
	_content: null,
	
	/**
	 * jQuery reference to the display profile link
	 * @type {jQuery}
	 */
	_jLnkDisplayProfile: null,
	
	_cmsMenuItems: null,
	_jSearch: null,
	_searchModal: null,
	
	/**
	 * Initialize
	 */
	init: function() {
		console.log('VCMSideBar.init()');
		this._login = new VCMLogin(this._page);
		this._profileBar = new VCMProfileBar(this._page);
		this._searchModal = new VCMSearchModal(this._page);
		this._content = $('#sideBar');
		this.onLoad();
	},	
	
	/**
	 * Destroy
	 */
	destroy: function() {
		console.log('VCMSideBar.destroy()');
		this._jSearch = null;
		this._cmsMenuItems = null;
		this._login = null;
		this._profileBar = null;
		this._searchModal = null;
		this._content = null;
	},
	
	/**
	 * Action performed when the sidebar is loaded
	 */
	onLoad: function() {
		console.log('VCMSideBar.onLoad()');
		
		this._jSearch = $('#searchButton').click($.proxy(this, 'onSearchClick'));
		
		if ($('#cmsSidebar').length == 1) {
			this._login.init();
			this._cmsMenuItems = $(".menuItem").click($.proxy(this, 'onCMSMenuItemClick'));		
		} else if ($('#profileSidebar').length == 1) {
			this._profileBar.init();
		}
	},
	
	/**
	 * On CMS menu item click
	 */	
	onCMSMenuItemClick: function(evt) {
		console.log('VCMSideBar.onCMSMenuItemClick(', evt, ')');
		var jEl = $(evt.target);
		// Show CMS content
		var target = jEl.attr('target');
		if (target == '_blank') {
			window.open(jEl.attr('href'));
		} else {
			var id = VCMUtils.findNumber(jEl.attr('id'));
			this._page.getContent().showCMSContent(id, true);
		}
		// Cancel default event
		return false;
	},
	
	/**
	 * On search click
	 * @param {Object} evt The Event object
	 */
	onSearchClick: function(evt) {
		// Show search modal
		this._searchModal.init();
		// Cancel default event
		return false;
	},

	/**
	 * Action performed when the sidebar is unloaded
	 */
	onUnload: function() {
		console.log('VCMSideBar.onUnload()');
		
		this._jSearch.unbind('click');
		this._jSearch = null;
		
		if ($('#cmsSidebar').length == 1) {
			this._login.destroy();
			this._cmsMenuItems.unbind('click');
			this._cmsMenuItems = null;
		} else if ($('#profileSidebar').length == 1) {
			this._profileBar.destroy();
		}
	},
	
	/**
	 * Load the content of the sidebar
	 * @param {Object} params
	 */
	load: function(params) {
		console.log('VCMSideBar.load()');
		this.onUnload();
		var me = this;
		this._content.load(VCMUtils.prepareCommand(params), function() {
			me.onLoad();
		});
	},
	
	/**
	 * Load the CMS content
	 */
	displayCMS: function() {
		console.log('VCMSideBar.displayCMS()');
		if ($('#cmsSidebar').length == 0) {
			this.load({command:'displayCMSSidebar'});
		}
	},
	
	/**
	 * Load the Profile content
	 * @param {Boolean} force
	 */
	displayProfile: function(force) {
		console.log('VCMSideBar.displayProfile()');
		if ($('#profileSidebar').length == 0 || force === true) {
			this.load({command:'displayProfileSidebar'});
		}
	},
	
	/**
	 * @return {VCMLogin} Returns the VCMLogin object
	 */
	getLogin: function() {
		return this._login;
	}
};


