/*
 * jstrimpel@buzztime.com, 11/15/2010
 * DO NOT use BUZZTIME.cookie.set and BUZZTIME.cookie.del.
 * These functions rerernce other lib function that have not been included.
 * These functions were not modified in case that the full lib is loaded 
 * there will not be an descrepencies bewteen functions. 
 * 
 */

/*
 * @namespace
 * BUZZTIME is a global object that can be extended indefinitely. 
 */
var BUZZTIME = BUZZTIME || {};

/*
 * @namespace
 * @static 
 * BUZZTIME.cookie performs basic cookie functions
 * @return void
 */
BUZZTIME.cookie = {
	/*
	 * BUZZTIME.cookie.set sets a cookie.
	 * @param {Object} cookie contains cookie values.
	 * @return {Boolean} true if cookie successfully set.
	 */
	set : function(cookie){		
		if(!cookie.name)
		{
			BUZZTIME.debug.error('cookie.name is a required parameter for BUZZTIME.cookie.set()');
		}	
		else if(!cookie.val)
		{
			BUZZTIME.debug.error('cookie.val is a required parameter for BUZZTIME.cookie.set()');
		}		
		else
		{
			var _cookie_string = cookie.name + '=' 
			+ cookie.val + ';'
			+ (cookie.expires ? 'expires=' + cookie.expires + ';' : '')
			+ (cookie.path ? 'path=' + cookie.path + ';' : '')
			+ (cookie.domain ? 'domain=' + cookie.domain + ';' : '')
			+ (cookie.secure ? 'secure=' + cookie.secure + ';' : '')
			+ (cookie.httponly ? 'htpponly=' + cookie.httponly + ';' : '')
			;
	
			try
			{
				document.cookie = _cookie_string;
				return true;
			}
			catch(e)
			{
				BUZZTIME.debug.error('an error occured while writing cookie, ' + cookie.name + ': ' + e);	
			}
		}
		
		return false;
	},
	/*
	 * BUZZTIME.cookie.get gets a cokkies value.
	 * @param {String} name of cookie.
	 * @return {String || Boolean (false)} cookie value or false if unable to retrieve cookie value.
	 */
	get : function(name){						
		var _cookie = document.cookie;
		
		if(_cookie)
		{		
			var _name = _cookie.indexOf(name + '=');
			
			if(_name >= 0)
			{
				var _val_start = (name.length + 1) + _name;
				var _val_end = _cookie.indexOf(';', _val_start);
								
				if(_val_end === -1)
					_val_end = _cookie.length;				
				
				if(_val_end >= 0)
					return _cookie.substring(_val_start, _val_end);
				else
					BUZZTIME.debug.warn('cookie, ' + name + ', value could not be extracted');
			}
		}
		
		return false;
	},
	/*
	 * BUZZTIME.cookie.del deletes a cookie by setting the expiration date to the past.
	 * @param {String} name of cookie.
	 * @return {Boolean} true for successful deletion.
	 */
	del : function(c_name){
		var _cookie = {
			name : c_name,
			val : ' ',
			expires : 'Thu, 01-Jan-70 00:00:01 GMT'
		};
		
		if(this.set(_cookie))
		{
			return true;
		}
		else
		{
			BUZZTIME.debug.warn('cookie, ' + c_name + ', could not be deleted');
			return false;
		}
	}
};

BUZZTIME.micro = { 	
	redirect : function(){
		var _cookie_name = 'get_bt_micro_site';
		
		// put together default 'home' url for comparison to window.location.href
		var _host = window.location.host;
		var _protocol = window.location.protocol;
		var _pathname = '/'; // set path to web root
		var _home = _protocol + '//' + _host + _pathname;
		
		var _href = window.location.href; // where am i?	
		var _micro_site = BUZZTIME.cookie.get(_cookie_name); // am i a micro site?		
		
		if(_href === _home && _micro_site) // if micro site cookie set and user went to default home then redirect
			window.location = '/' + _micro_site;
	}
};
