/* KIDS namespace: shared across nick, nickr, nicktoons, etc */

if(typeof KIDS == "undefined" || !KIDS) var KIDS = {};

KIDS.IS_DEV = window.location.hostname.indexOf("localhost") >= 0 || window.location.hostname.indexOf('d.mtvi.com') > 0;
KIDS.IS_QA = window.location.hostname.indexOf('q.mtvi.com') > 0;
KIDS.IS_DEV_ENV = KIDS.IS_DEV || KIDS.IS_QA;
KIDS.IS_LIVE = !KIDS.IS_DEV_ENV;
KIDS.IS_DEBUG = KIDS.IS_DEV_ENV && window && typeof window.console != "undefined";
//KIDS.IS_DEBUG = window && typeof window.console != "undefined";

KIDS.namespace = function(nodes, root) {
	root = root == null ? KIDS : root;

    var a = nodes, o = null, i, j, d;

    d = nodes.split(".");
    o = root;

    // root is implied, so it is ignored if it is included
    for (j = (d[0] == root) ? 1 : 0; j < d.length; j++) {
        o[d[j]]=o[d[j]] || {};
        o=o[d[j]];
    }
    return o;
};

KIDS.namespace("utils", KIDS);

KIDS.utils.URL_DEV_NICK = 'www.nick-jd.mtvi.com';
KIDS.utils.URL_QA_NICK = 'www.nick-jq.mtvi.com';
KIDS.utils.URL_LIVE_NICK = 'www.nick.com';

KIDS.utils.isEmptyString = function(str) {
	//return str == null || KIDS.utils.trim(str) == "" || str === 'undefined';
	return str == null || str == undefined || str == "" || str === 'undefined';
}

KIDS.utils.trim = function(str) {
	return str == null ? "" : str.replace(/^\s+|\s+$/g, '');
}

KIDS.utils.getCookie = function(name) {
	if(KIDS.utils.isEmptyString(name)) return null;

	var cook = document.cookie.toString();
	if(cook.length > 0) {
		var begin = cook.indexOf(name+"=");
		if(begin != -1) {
			begin += name.length+1;
			var end = cook.indexOf(";", begin);
			if(end == -1){
			   end = cook.length;
			}
			return unescape(cook.substring(begin, end));
		}
	}
	return null;
}

KIDS.utils.getDomain = function() {
	return window.location.hostname;
}

KIDS.utils.getNickDomain = function() {
	if(KIDS.IS_LIVE){
		return KIDS.utils.URL_LIVE_NICK;
	} else if(KIDS.IS_QA) {
		return KIDS.utils.URL_QA_NICK;
	} else {
		return KIDS.utils.URL_DEV_NICK;
	}
}

KIDS.utils.getQueryString = function(data) {
	if(data == null) return "";
	var qs = "";
	for(var key in data) {
		if(!key || !data[key]) continue;

		if(qs != '') qs += '&';
		qs += key +"="+ escape(data[key]);
	}
	return qs;
}

/*
	Utility to submit xml into the GDC without incurring an html redirect to a "successURL"
	Shouldn't need to wrap this in xml. Inquire about a fix.
*/
KIDS.utils.getGdcXml = function(id, obj) {
	var gdc = "<answers collectionID=\""+id+"\">";
	for(var key in obj) {
		gdc += "<answer tag=\""+ key +"\"><![CDATA["+ obj[key] +"]]></answer>";
	}
	gdc += "</answers>";
	return gdc;
}

KIDS.utils.getSwf = function(id) {
    if(navigator.appName.indexOf("Microsoft") != -1) {
        return window[id];
    } else {
    	if(document[id].length != undefined) {
			return document[id][1];
    	}
    	return document[id];
	}
}

KIDS.utils.trimArray = function(arr, toLower) {
	if(arr == null) return null;

	for(var i = 0; i < arr.length; i++) {
		arr[i] = toLower ? arr[i].toLowerCase() : arr[i];
		arr[i] = KIDS.utils.trim(arr[i]);
	}
	return arr;
}

KIDS.utils.getUrlParts = function(url) {
	var urlInfo = url.replace('//', '/').split('/');
	return urlInfo;
}

KIDS.utils.getUrlPath = function(url) {
	var urlInfo = KIDS.utils.getUrlParts(url);
	var domain;

	if(url.indexOf("http://") > -1) domain = urlInfo.splice(0, 2).join("/");

	var docInfo = urlInfo.join("/");
	//KIDS.utils.doLog("getUrlPath: "+domain+" | "+docInfo);
	return docInfo;
}

KIDS.utils.getContextPath = function(url, context, removeExtension, removeTrailingIndex) {
	var path = getUrlPath(url);
	
	// remove the file extension if a page is specified: file.jhtml
	if(removeExtension && path.lastIndexOf(".") >= 0) {
		path = path.substring(0, path.lastIndexOf("."));
	}
	
	// remove assigned context from url. context = "games" > url = /games/spongebob/game.jhtml > spongebob/game
	context = context == null || context == "" ? "" : context;
	var contextMatch = new RegExp("^"+context+"\/", "i")
	path = path.replace(contextMatch, "");;

	// remove trailing "/index" - to combine - "/some/url/index" & "/some/url/"
	if(removeTrailingIndex) path = path.replace(/\/index$/i, "");

	// remove trailing forward slash
	path = path.replace(/\/$/, "");
	
	if(path.indexOf("/") >= 0){
		path = path.replace(/\/$/, "");
	}
		
	if(path == ""){
		path = "hub";
	}
	//alert("getContextType: "+path+" | "+url);
	path = context + "_" + path;
	return path;
}

KIDS.properties = null;
KIDS.add = function(name, value) {
	if(name == null || name == '') return;
	if(KIDS.properties == null) KIDS.properties = {};

	KIDS.properties[name] = value;
};

KIDS.get = function(name) {
	if(name == null || name == '') return null;
	if(KIDS.properties == null) return null;

	return KIDS.properties[name];
};

KIDS.utils.printObj = function(obj) {
	if(!obj) KIDS.utils.doLog('>>PrintObj: invalid: '+ typeof(obj));

	KIDS.utils.doLog('>>PrintObj: type: '+ typeof(obj));
	if(typeof(obj) != 'object') return;

	for(var key in obj) {
		KIDS.utils.doLog('>>PrintObj: key: '+ key +" | "+obj[key]);
	}
}

KIDS.utils.doLog = function() {
	if (!KIDS.IS_DEBUG) return; // flag should already verify window && window.console. Fixes an odd bug

	if (typeof window.console.debug != "undefined") {
		// firebug error console
		window.console.debug.apply(window.console, arguments);
		// IE 6 -  CompanionJS - Bug in version 0.5 - Revert to 0.4.2 - Treats window.console.debug as a string instead of a function
	} else if (typeof window.console.log != "undefined") {
		if(window.console.log.apply === "function") {
			// safari error console
			window.console.log.apply(window.console, arguments);
	    } else {
	    	// IE8
			window.console.log(arguments[0]);
	    }
	}
}

KIDS.utils.openBumper = function(wBumper, advertiser, onair, customBumper, customHeight, newWindow, isInHouse, customWidth) {
	KIDS.utils.doLog("openBumper: site specific override missing!");
	window.open(advertiser);
}
