// Check if the CLICK_CS global namespace object exists.  If not, create it.
// Create The CLICK_CS object by creating a function and immediately invoking it.
// This function will return an object with both public and private variables and methods.

var CLICK_CS = CLICK_CS || function() {
	// private variable. set this to the relaticve location of the "determine login status" template
	var loginStatusFile = "/templates/determine_login_status",
	// Creating cookiejar and subcookiejar as private functions so as not to have to change any of the code
	// the private functions are later called as public methods of the CLICK_CS object.
	/**
	 * Cookies.js, providing easy access to cookies thru the cookiejar object. Enabling so-called "subcookies" thru the subcookiejar 
	 * object.
	 * See this related blogpost for more information on how to use these objects:
	 * 	<http://www.whatstyle.net/articles/28/subcookies>
	 * Check out this other blogpost for information about the new version:
	 *  <http://www.whatstyle.net/articles/46/subcookies_v2>
	 * 
	 * @author Harmen Janssen <http://www.whatstyle.net>
	 * @version 2.0
	 * 
	 */
	/* based on http://www.quirksmode.org/js/cookies.html, by Peter-Paul Koch */
	cookiejar = {
		/* set a cookie */
		bake: function(cookieName,cookieValue,days,path) {
			var expires='';
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				expires = "; expires="+date.toGMTString();
			}
			var thePath = '; path=/';
			if (path) {
				thePath = '; path=' + path;
			}
			document.cookie = cookieName + '=' + escape(cookieValue)+expires+thePath;
			return true;
		},
		/* get a cookie value */
		fetch: function(cookieName) {
			var i, c,
			nameEQ = cookieName + '=',
			ca = document.cookie.split(';');
			for (i=0; i<ca.length; i++)	{
				c = ca[i];
				while (c.charAt(0) == ' ') {
					c = c.substring(1, c.length);
				}
				if (c.indexOf(nameEQ) === 0) {
					return unescape(c.substring(nameEQ.length, c.length));
				}
			}
			return null;
		},
		/* delete a cookie */
		crumble: function(cookieName) {
			return cookiejar.bake(cookieName,'',-1);
		}
	},

	/* circumventing browser restrictions on the number of cookies one can use */
	subcookiejar = {
		nameValueSeparator: '$$:$$',
		subcookieSeparator: '$$/$$',
		/* set a cookie. subcookieObj is a collection of cookies to be. Every member of subcookieObj is the name of the cookie, its value
		 * the cookie value
		 */
		bake: function(cookieName,subcookieObj,days,path) {
			var i, existingCookie;
			/* check for existing cookie */
			if (existingCookie == subcookiejar.fetch(cookieName)) {
				/* if a cookie by the same name is found, 
				 * append its values to the subcookieObj.
				 */
				for (i in existingCookie) {
					if (!(i in subcookieObj)) {
						subcookieObj[i] = existingCookie[i];
					}
				}
			}
			var cookieValue = '';
			for (i in subcookieObj)	{
				cookieValue += i + subcookiejar.nameValueSeparator;
				cookieValue += subcookieObj[i];
				cookieValue += subcookiejar.subcookieSeparator;
			}
			/* remove trailing subcookieSeparator */
			cookieValue = cookieValue.substring(0,cookieValue.length-subcookiejar.subcookieSeparator.length);
			return cookiejar.bake(cookieName,cookieValue,days,path);
		},
		/* get a subcookie */
		fetch: function(cookieName,subcookieName) {
			var i, sclen, cookieValue = cookiejar.fetch(cookieName);
			/* proceed only if a cookie was found */
			if (!cookieValue) {
				return null;
			}
			var subcookies = cookieValue.split(subcookiejar.subcookieSeparator);
			var cookieObj = {};
			for (i=0,sclen=subcookies.length; i<sclen; i++)	{
				var sc = subcookies[i].split(subcookiejar.nameValueSeparator);
				cookieObj [sc[0]] = sc[1];
			}
			/* if subcookieName is given, return this subcookie if available, or null.
			 * else, return the entire cookie as an object literal
			 */
			if (subcookieName !== undefined) {
				if (subcookieName in cookieObj) {
					return cookieObj[subcookieName];
				}
				return null;
			}
			return cookieObj;
		},
		/* delete a subcookie */
		crumble: function(cookieName,subcookieName,days,path) {
			var cookieValue = cookiejar.fetch(cookieName);
			if (!cookieValue) {
				return false;
			}
			var newCookieObj = {};
			var subcookies = cookieValue.split(subcookiejar.subcookieSeparator);
			for (var i=0, sclen=subcookies.length; i<sclen; i++)	{
				var sc = subcookies[i].split(subcookiejar.nameValueSeparator);
				if (sc[0] != subcookieName) {
					newCookieObj[sc[0]] = sc[1];
				}
			}
			return subcookiejar.bake(cookieName,newCookieObj,days,path);
		}
	};
	// This is the return object. The CLICK_CS namespace will have all of the properties listed below
	// plus access to the private variables and functions mentioned above
	return {
		//add cookiejar/subcookiejar
		cookiejar : cookiejar,
		subcookiejar : subcookiejar,
		setUserInfo : function () {
		 try {

			if (CLICK_CS.subcookiejar.fetch('curUserInfo', 'name') && CLICK_CS.subcookiejar.fetch('curUserInfo', 'name') != null) {
				document.getElementById('CLICK_CS_support_box_loggedin').style.display = 'block';
				document.getElementById('CLICK_CS_support_box_loggedout').style.display = 'none';
				document.getElementById('CLICK_CS_breadcrumb_loggedin').style.display = 'inline';
				// document.getElementById('CLICK_CS_support_info_loggedin').style.display = 'block';
			} else {
				document.getElementById('CLICK_CS_support_box_loggedin').style.display = 'none';
				document.getElementById('CLICK_CS_support_box_loggedout').style.display = 'block';
				document.getElementById('CLICK_CS_breadcrumb_loggedin').style.display = 'none';
				// document.getElementById('CLICK_CS_support_info_loggedin').style.display = 'none';				
			}
		     } catch (err){
           
   		     }

		     
		}
	};
}();
