var VCMUtils = {
	/**
	 * Find the last number in a string
	 * @param sNumber {String} The string containing a number
	 * @return {Number} The number
	 */
	findNumber: function(sNumber) {
		var re = /-?[0-9]+/gi;
		var matches = sNumber.match(re);
		return parseInt(matches[matches.length-1]);
	},
	
	/**
	 * Return the current language
	 * @return {String} The current language
	 */
	getLang: function() {
		return $('.langLink.selected').attr('id').substr(0, 2).toUpperCase();
	},
	
	/**
	 * Return the selected display mode
	 * @return {String} The selected display mode
	 */
	getDisplayMode: function() {
		var id = $('#filterBar a.selected').attr('id');
		// TODO JD
		if (id)
			return id.substr(0, id.length - 4);
		else
			return 'thumbnails';
	},
	
	/**
	 * Return the current offset days
	 * @return {Number} The current offset days
	 */
	getDaysOffset: function() {
		var id = $('#menuBar a.selected').attr('id');
		if (id == 'lastMinute') return -1;
		else if (id == 'allDates') return 99;		
		else return this.findNumber(id);
	},
	
	/**
	 * Return the current discipline id
	 * @return {Number} The current discipline id
	 */
	getDisciplineId: function() {
		var tmp = $('#cbDiscipline').val();
		if (tmp)
			return parseInt(tmp);
		else
			return -1;
	},
	
	/**
	 * Parse a string of parameters to an Object of parameters
	 * @param params {String} The string of parameters
	 * @return {Object} An Object of parameters
	 */
	parseCommand: function(params) {
		var out = {},
			split = params.split('&');
		
		for(i in split) {
			var str = split[i],
				name = str.substr(0, str.indexOf('=')),
				value = str.substr(str.indexOf('=') + 1);		
			out[name] = value;
		}
		
		return out;
	},
	
	/**
	 * Prepare the command URL
	 * @param params {Object} An Object of parameters
	 * @return {String} The complete URL
	 */
	prepareCommand: function(params) {
		// Default params
		params = jQuery.extend({
			url: 'doAjax',
			command: 'displayInit',
			lang: this.getLang(),
			inSefURL: (rootPath == '../' ? true : false),
			rd: Math.random()
		}, params || {});
		// Generate the url
		var url = rootPath + params.url + '?';
		delete params.url;
		// Add parameters to the url
		for(name in params)
			url += name + '=' + encodeURIComponent(params[name]) + '&';
		// 
		return url.substr(0, url.length - 1);
	},
	
	/**
	 * Capitalize the given string
	 * @param {String} str
	 */
	capitalize: function(str) {
		return str.charAt(0).toUpperCase() + str.substr(1);
	},
	
	/**
	 * Add jQuery events to an object
	 * The object must contains a _EVENTS object specifying the events in this format:
	 * _EVENTS: { eventName: ['#mySelector', 'event1'...]... }
	 * Ex. : _EVENTS: { test: ['#test', 'click'] }
	 * The object must contains method for the specifyed events:
	 * onEventNameEvent1: function(evt) { }
	 * Ex. : onTestClick: function(evt) { }
	 * @param {Object} obj The object containing the events
	 */
	addEvents: function(obj) {
		var events = obj._EVENTS;
		for(var name in events) {
			var event = events[name],
				jEl = event[event.length] = $(event[0]);
			for(var i = 1; i < event.length - 1; i++) {
				var method = 'on' + VCMUtils.capitalize(name) + VCMUtils.capitalize(event[i]);
				jEl.bind(event[i],{jEl:jEl},$.proxy(obj, method));
			}
		}
	},
	
	/**
	 * Remove jQuery events from an object
	 * @param {Object} obj
	 */
	removeEvents: function(obj) {
		var events = obj._EVENTS;
		for(var name in events) {
			var event = events[name];
			var jEl = event.pop();
			for(var i = 1; i < event.length; i++) {
				jEl.unbind(event[i]);
			}
		}
	}
};


