/*===============================================================================================
	
	Mayomo
	base.lib.js		Base classes definition

	Modifications: 
	20090831		XXX		Initial implementation
					ADD		Base controller
																		
===============================================================================================*/

if (typeof console == "undefined") var console = { log: function() {}, time: function() {}, timeEnd: function() {} };

var BaseController = {
	//
	queryString: {
		category : 		9,
		state : 		'',		
		keywords: 		'',
		fd: 			'',
		td: 			'',
		page:	 		1
	},
	
	/**
	 * request Uri
	 * 
	 * @type String
	 * @default the empty string
	 */
	requestUri : '',
	
	/**
	 * initializes the query string object with data from the location.href
	 * 
	 * @public
	 * @param {Void} 
	 * @return {Void}
	 */
	initQueryString: function() 
	{
		var tmp = '';
		//
		if ( this.requestUri != '' ) {
			tmp = this.requestUri.split('#')[1].replace(/\//g, '');
		}
		//
		if (typeof location.href.split('#')[1] != 'undefined')
			tmp = location.href.split('#')[1].replace(/\//g, '');
		// split them by ;
		var arr = tmp.split(";");
		var arrLength = arr.length;
		
		for (var i=0; i < arrLength; i++) {
			var spl = arr[i].split("=");
			if (spl[0] == "category") {
				BaseController.queryString.category = +spl[1];
			} else if (spl[0] == "state") {
				BaseController.queryString.state = spl[1];
			} else if (spl[0] == "fd") {
				BaseController.queryString.fd = spl[1];
			} else if (spl[0] == "td") {
				BaseController.queryString.td = spl[1];
			} else if (spl[0] == 'keywords') {
				BaseController.queryString.keywords = spl[1];	
			} else if (spl[0] == 'page') {
				BaseController.queryString.page = +spl[1];	
			}
		}// end of cycle
	},
	
	/**
	 * Return the current inf_id
	 * 
	 * @return {int}
	 */
	getMaterialId : function() 
	{
		var materialId = location.pathname.substring(1);
		materialId = materialId.split('-')[0];
		
		return materialId;
	},	
	
	/**
	 * Set the query string object values
	 * 
	 * @param {Object} oQueryString
	 */
	setQueryString: function(oQueryString) 
	{
		if (typeof oQueryString.category != 'undefined') {
			BaseController.queryString.category = oQueryString.category;
		}	
		if (typeof oQueryString.state != 'undefined' ) {
			BaseController.queryString.state = oQueryString.state;
		}	
		if (typeof oQueryString.fd != 'undefined') {
			BaseController.queryString.fd = oQueryString.fd;
		}		
		if (typeof oQueryString.td != 'undefined') {
			BaseController.queryString.td = oQueryString.td;			
		}
		if (typeof oQueryString.keywords != 'undefined') {
			BaseController.queryString.keywords = oQueryString.keywords;			
		}
		if (typeof oQueryString.page != 'undefined') {
			BaseController.queryString.page = oQueryString.page;			
		}
	},

	/**
	 * Rewrite the uri, after the #
	 */
	rewriteUrl : function() 
	{
		arrQueryString = []; 
		
		if ( BaseController.queryString.category != '' ) {
			arrQueryString.push('category='+BaseController.queryString.category);
		}
		if ( BaseController.queryString.state != '' ) {
			arrQueryString.push('state='+BaseController.queryString.state);
		}
		if ( BaseController.queryString.fd != '' ) {
			arrQueryString.push('fd='+BaseController.queryString.fd);
		}
		if ( BaseController.queryString.td != '' ) {
			arrQueryString.push('td='+BaseController.queryString.td);
		}
		if ( BaseController.queryString.keywords != '' ) {
			arrQueryString.push('keywords='+BaseController.queryString.keywords);
		}
		if ( BaseController.queryString.page != '' ) {
			arrQueryString.push('page='+BaseController.queryString.page);
		}
		strQueryString = '#/'+arrQueryString.join(';')+'/';
		BaseController.setQueryStringToCookie(strQueryString);
		location.href = strQueryString;	
	},
	//
	getQueryString: function() {
		return BaseController.queryString;
	},
	//
	setQueryStringToCookie: function(urlParameter) {
		$.cookie('queryString', urlParameter);
	},
	//
	getQueryStringFromCookie: function() {
		if ($.cookie('queryString') != null) {
			this.requestUri = $.cookie('queryString');
			this.initQueryString();
		}
	}			
};
