/*global YAHOO, SKYPE, pageTracker, s_gi, sc_custom_local, s, s_account*/
/*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, white: true, widget: true, undef: true, indent: 4*/

/* Init SKYPE.wanalytics namespace */
if (typeof SKYPE.wanalytics === "undefined" || !SKYPE.account) {
    SKYPE.namespace("wanalytics");
}

SKYPE.wanalytics.Core = (function ()
{
	var page_url = location.href;
	
	return {
	
		/* remove empty elements from Array */
		removeEmptiesFromArray: function (someArray)
		{
		    return SKYPE.wanalytics.Core.removeRegexMatchingElementfromArray(/^$/, someArray);
		},
	
		/* remove occurences of matching regexElement from Array */
		removeRegexMatchingElementfromArray: function (regexElemToRemove, someArray)
		{
		    var newArray = [],
		    	element;
		
		    for (element in someArray) {
		        if ((typeof someArray[element] === 'string') && !(someArray[element].match(regexElemToRemove))) 
				{
		            newArray.push(someArray[element]);
		        }
		    }
		    return newArray;
		},
	
		getCookieValue: function (name) 
		{
			var nameEQ = name + "=", 
				ca = document.cookie.split(';'), 
				c, i;
			
			for (i = 0; i < ca.length ; i = i + 1) 
			{
				c = ca[i];
				while (c.charAt(0) === ' ') {
					c = c.substring(1, c.length);
				}
				
				if (c.indexOf(nameEQ) === 0) {
					return c.substring(nameEQ.length, c.length);	
				} 
			}
			return null;
		},

		getThePageName: function ()
		{
			var the_path = location.pathname,
				end_path = SKYPE.wanalytics.Core.removeEmptiesFromArray(the_path.split("/")),
				result = "";

			/* cleans the intl/en */
			if (the_path.match(/intl/))
			{			
				end_path.shift();
				end_path.shift();
			}

			result = end_path.join("/");

			/* if is without html and without php, then add a forward slash */
			if ((result.indexOf(".html") === -1) && (result.indexOf(".php") === -1)) {
				result += "/";				
			}

			return result;
		},

		getCategoryFromPath: function ()
		{
			var the_path = location.pathname,
				end_path = SKYPE.wanalytics.Core.removeEmptiesFromArray(the_path.split("/"));

			/* cleans the intl/en */
			if (the_path.match(/intl/))
			{			
				end_path.shift();
				end_path.shift();
			}

			/* if has html or php page, cleans it from end */
			if (the_path.match(/\.html$/) || the_path.match(/\.php$/))
			{			
				end_path.pop();
			}

			return end_path.join(",").replace(/-/g, "");
		},

		getLocalization: function (the_url)
		{
			var langCode   = "";
			
			//checks a globally defined var to see if a custom local is defined
			if ((typeof sc_custom_local !== "undefined") && sc_custom_local !== "") {
				/* Cleans it up */
				sc_custom_local = sc_custom_local.replace(/-?_?/g, "");
				return sc_custom_local.toLowerCase();
			}
	
			/* Figure out the language */
	        langCode = location.pathname.replace(/^\/intl\/([^\-\/]+)-?([^\/]*)\/.*/, "$1$2");

	        if (!langCode.length || langCode.search(/[^a-z]/) !== -1) {
		    	langCode = "enus"; //if there's no intl assumes US, carefull with this.
			}
	 
			return langCode.toLowerCase();
		},
		
		getCountry: function ()
		{
			var PREF_DEFAULT_CC = 'US',
				ccMatch;

			/* for debugging */
			if (location.search) {
			    ccMatch = location.search.match(/debug-country=([A-Z]+)/);
			    if (ccMatch) {
			        PREF_DEFAULT_CC = ccMatch[1];
			    }
			}

			return PREF_DEFAULT_CC;
		},

		isGoogleAnalyticsEnabled: function ()
		{
			return (typeof pageTracker !== "undefined");
		},

		isOmnitureEnabled: function ()
		{
			return (typeof s_gi !== "undefined");
		},
		
		makeOmnitureMandatoryVariables: function (the_app_name, language_code)
		{
			
			/* REQ1.9, language */
			if (typeof language_code !== "undefined" && language_code !== "") {
				s.eVar5 = s.prop5 = language_code;				
			} else {
				s.eVar5 = s.prop5 = SKYPE.wanalytics.Core.getLocalization();
			}

			/* REQ1.8. */
			s.channel = the_app_name;
		},
		
		/* use: SKYPE.wanalytics.Core.debug(); */
		debug: function (the_url)
		{
			try {
				var scdebug_vars = ["channel",
									"eVar5",
									"prop5",
									"pageName",
									"hier1",
									"pageType",
									"products",
									"events",
									"campaign",
									"eVar1",
									"eVar2",
									"prop8",
									"prop12",
									"prop13"],
					debstr = s_account.replace(",", "&"),
					iter;

				for (iter in scdebug_vars) 
				{
					if ((typeof scdebug_vars[iter] === "string") && (typeof s !== "undefined")) {
						debstr += "," + (s[scdebug_vars[iter]] || "").replace(/,/g, '-');
					}
				}
			
				return debstr;
			} catch (err) { 
				return err.toString();
			}
		}
	};
}());