/* 
 * This code allows you to use console.x() functions without them making it into production 
 * or into browsers that don't support them.
 */
(function(global){
	var prod = global.location.href.match(/^http:\/\/(www\.)?lds.org/i) !== null,
		api = ["log","debug","info","warn","error","assert","dir","dirxml", "trace","group","groupCollapsed","groupEnd","time","timeEnd", "profile","profileEnd","count","exception","table"],
		log, i, len;

	if (typeof global.console == "undefined" || !global.console) {
		try { global.console = {}; } catch (err) { }
	}

	log = (!prod && typeof global.console.log != "undefined") ? global.console.log : function(){};

	for (i=0, len=api.length; i<len; i++) {
		if (prod || typeof global.console[api[i]] == "undefined" || !global.console[api[i]]) {
			try { global.console[api[i]] = log; } catch (err) { }
		}
	}
})(window);


/*
 * This is a place to store lang, country, resource strings, etc. for use via javascript.
 */
var DATA = {
	
	LANG: 'eng',	// Overridden by page
	CLANG: 'eng',	// Overridden by page
	COUNTRY: '',	// Overridden by page
	PATH:'',	// optional value to set for use in prototypes
	CDNPATH:'',	// optional value to set for use in prototypes
	toolBarOptions:{}, // Used for study toolbar
	
	LOCALE: function() {
		return DATA.LANG + (DATA.COUNTRY != '' ? '-' + DATA.COUNTRY : '');
	},
	
	localeParams: function() {
		var params = [];
		if (DATA.LANG != '') params.push('lang=' + DATA.LANG);
		if (DATA.CLANG != '') params.push('clang=' + DATA.CLANG);
		if (DATA.COUNTRY != '') params.push('country=' + DATA.COUNTRY);
		
		return params.join('&');
	},
	
	
	
	RESOURCES: {},
	
	// Added by each page
	addResources: function(newResources) {
		DATA.RESOURCES = jQuery.extend(DATA.RESOURCES, newResources);
	},
	
	
		
	/*
	 * Stores a pseudo user profile in a cookie.  Supports storing info for 
	 * multiple users.  If the profile hasn't been set yet for the given 
	 * account ID, returns the default profile.  
	 * 
	 * Usages:
	 * DATA.PROFILE.profile('123456');	// Returns the profile for user with account ID 123456
	 * DATA.PROFILE.profile('123456', 'nbToolbar');	// Returns the setting that determines if the toolbar should be shown for user with account ID 123456
	 * DATA.PROFILE.profile('123456', 'nbToolbar', 0);	// Set's the nbToolbar value to 0 for user with account ID 123456 and returns the new value
	 */
	profile: function(accountId, key, value) {
		var cookieName = 'ldsorg_profile';
		
		// Start with default profile
		var profile = {  
			nbIntro: 1,		// notebook - show the intro?
			nbMode: 1,		// notebook - study mode on?
			nbToolbar: 1,	// notebook - show toolbar?
			nbColor: 'hl-color-1',		// notebook - index of current highlight color
			nbFolder: null,	// notebook - guid of current folder
			editId: null,	// notebook - id of the highlight to open in edit mode
			nbView: 1		// notebook - what is the default view for the study notebook? 1=list, 2=detail
		};
		
		// Is there a profile in the cookie?
		var wrapper = jQuery.parseJSON(jQuery.cookie(cookieName));
		var hasWrapper = wrapper && wrapper.users && wrapper.lastUser;
		var foundProfile = hasWrapper && accountId && wrapper.users[accountId];
		if (foundProfile) {
			profile = wrapper.users[accountId];
			console.debug('Found cookie profile: ', profile);
		}
		
		// Set and return a value
		if (key && (value || value === false || value === 0)) {
			// Set the value
			profile[key] = value;
			
			// Save the profile in the cookie
			if (!hasWrapper) {
				wrapper = { lastUser: accountId, users: {} };
			}
			wrapper.users[accountId] = profile;
			
			var json = JSON.stringify(wrapper);
			if (json.length > 1000) {
				console.debug('Profile may be too large to fit in a cookie.');
			}
			
			jQuery.cookie(
				cookieName, 
				json, 
				{ path: "/", expires: 365 }
			);
			
			console.debug('Set cookie profile value "' + key + '" to: ', value);
			return value;
			
		// Return a value
		} else if (key) {
			console.debug('Returning cookie profile value for "' + key + '": ', profile[key]);
			return profile[key];
		
		// Return the whole profile
		} else {
			console.debug('Returning profile: ', profile);
			return profile;
		}
	}
};
