/**
 * VCM Header
 * @classDescription Class for the header
 * @param {VCMPage} page Reference to the VCMPage parent
 * @return {VCMHeader} Returns a new VCMHeader object
 * @constructor
 */
function VCMHeader(page) {
	this._page = page;
}

VCMHeader.prototype = {
	/**
	 * Reference to a VCMPage object
	 * @type {VCMPage}
	 */
	_page: null,
	/**
	 * jQuery reference to the languages links
	 * @type {jQuery}
	 */
	_jLangLinks: null,
	/**
	 * jQuery reference to the home link
	 * @type {jQuery}
	 */
	_jHome: null,
	/**
	 * jQuery reference to the menu links
	 * @type {jQuery}
	 */
	_jMenuLinks: null,
	/**
	 * jQuery reference to the sector
	 * @type {jQuery}
	 */
	_jSector: null,
	/**
	 * jQuery reference to the sectors
	 * @type {jQuery}
	 */
	_jSectors: null,
	
	/**
	 * Initialize the header
	 */
	init: function() {
		console.log('VCMHeader.init();');
		// Get jQuery references
		this._jLangLinks = $('.langLink');
		this._jHome = $('#home');
		this._jMenuLinks = $('#menuBar a, #allDates');
		this._jSector = $('#sectorsComboBox > span');
		this._jSectors = $('#sectorsComboBox > select');
		// Add events
		this._jLangLinks.click($.proxy(this, 'onLangClick'));
		this._jHome.click($.proxy(this, 'onHomeClick'));
		this._jMenuLinks.click($.proxy(this, 'onMenuClick'));
		this._jSectors.change($.proxy(this, 'onSectorChange'));
		
		var urlParamSector = decodeURI(
		        (RegExp("sector" + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
		    );
		if(urlParamSector != null && urlParamSector != "null") {
			//alert(urlParamSector);
			this._jSectors.attr("value", urlParamSector);
			this._jSector.text(this._jSectors[0].options[this._jSectors[0].selectedIndex].text);
			this._page.getContent().sectorId = urlParamSector;
			$("#allDates").addClass("selected");
		}
		
	},
	
	/**
	 * Destroy the header
	 */
	destroy: function() {
		console.log('VCMHeader.destroy();');
		// Remove events
		this._jLangLinks.unbind('click');
		this._jHome.unbind('click');
		this._jMenuLinks.unbind('click');
		this._jSectors.unbind('change');
		// Destroy jQuery reference
		this._jLangLinks = null;
		this._jHome = null;
		this._jMenuLinks = null;
		this._jSector = null;
		this._jSectors = null;
	},
	
	/**
	 * On sector change
	 * @param {Event} evt The Event object
	 */
	onSectorChange: function(evt) {
		this._jSector.text(this._jSectors[0].options[this._jSectors[0].selectedIndex].text);
		this._page.getContent().sectorId = this._jSectors.val();
		this._page.getContent().showActivities();
	},
	
	selectSectorById: function(id) {
		for(var i = 0; i < this._jSectors[0].options.length; i++) {
			if (this._jSectors[0].options[i].value == id) {
				this._jSectors[0].selectedIndex = i;
				this._jSector.text(this._jSectors[0].options[this._jSectors[0].selectedIndex].text);
				break;
			}
		}
	},
	
	/**
	 * On language link click
	 * @param {Event} evt The Event object
	 * @private
	 */
	onLangClick: function(evt) {
		console.log('VCMHeader.onLangClick();');
		var params = VCMHistory.getParams();
		params.lang = $(evt.currentTarget).attr('id').substr(0, 2).toUpperCase();
		this._page.load(params);
		return false;
	},
	
	/**
	 * On home link click
	 * @param {Event} evt The Event object
	 * @private
	 */
	onHomeClick: function(evt) {
		console.log('VCMHeader.onHomeClick();');
		this._page.getContent().reset();
		this._page.load();
		return false;
	},

	/**
	 * On menu link click
	 * @param {Event} evt The Event object
	 * @private
	 */
	onMenuClick: function(evt) {
		console.log('VCMHeader.onMenuClick();');
		this._jMenuLinks.removeClass('selected');
		$(evt.currentTarget).addClass('selected');
		this._page.getContent().showActivities();
		this._page.getSideBar().displayCMS();
		return false;
	},
	
	deselectDay: function() {
		this._jMenuLinks.removeClass('selected');
	}
	
};


