/**
 * @author DREDroot
 *
 */
dojo.declare("dojom.classes.ParametersClass", null, 
{
	parameters: {},
	log	:null,
	
	constructor: function ()
	{
		this.log = LOG.addClass("ParametersClass");
	},
	
	getParameters: function() 
	{	    
		var obj = this;
		var parameters = {};
		try{													
			if(window.location.hash && JSON.count(this.parameters)==0) 
			{		       
				obj.log.debug('getParameters. Parse hash - '+window.location.hash);
				var paramArray = window.location.hash.substr(1).split('&');
				var length = paramArray.length;
			 
				for (var index = 0;index <length; index++ ) 
				{
					var param = paramArray[index].split('=');
					var name = param[0];
		
					var value =	typeof param[1] == 'string'
				                ? decodeURIComponent(param[1].replace(/\+/g, ' '))
				                : null;
					this.parameters[name] = value;
				}
			}
						
			for(var i in this.parameters)
				parameters[i] = this.parameters[i];
								
		}catch(ex){obj.log.error('getParameters.'+ex)}	
	    return parameters;
	},
	
	setParameters: function(params)
	{		
		var obj = this;
		obj.log.debug('setParameters.'+JSON.stringify(params));
		try{
			if(params)
			{
				for(var key in params)
				{
					this.parameters[key] = params[key];
				}
			}
			window.location.hash = '#';
			for(var key in this.parameters)
			{
				if(key=='login')
					continue;				
										
				if (this.parameters[key]!=null)
				{
					if (window.location.hash && window.location.hash != '#')
						window.location.hash+='&';
					window.location.hash += key + '=' + this.parameters[key];
				}
				//else window.location.hash += key;
				
			}
		}catch(ex){obj.log.error('setParameters.'+ex)}
	},
	
	setCookie: function (params) 
	{
		var name = params['name'];
		var value = params['value'];
		var expires = params['expires'];
		var path = params['path'];
		var domain = params['domain'];
		var secure = params['secure'];
		
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		
		/*
		if the expires variable is set, make the correct 
		expires time, the current script below will set 
		it for x number of days, to make it for hours, 
		delete * 24, for minutes, delete * 60 * 24
		*/
		if ( expires )
		{
		expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		var str = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
		
		document.cookie = str;
	},
	
	getCookie: function ( check_name ) 
	{
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_value = null; 
		
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );
			
			
			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
			// if the extracted name matches passed check_name
			if ( cookie_name == check_name )
			{				
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
					if(cookie_value) b_cookie_value = cookie_value;	
					else b_cookie_value	= ''; 						
				}
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		return b_cookie_value;
	},	
	
	deleteCookie: function ( name,path,domain ) {
	
		if ( this.getCookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		(( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		
		//document.cookie = null;
	},
	
	isLogin: function(msgShow)
	{	
		if(this.parameters['login']!= null && this.parameters['login']!='')
			return true;
		else
		{
			this.log.debug('isLogin = false');
			if(msgShow)
				this.log.msg('You must log in first in order to perform requested action','warning');
			return false;
		}
	}
});
