
if(typeof NICK == "undefined" || !NICK) var NICK = {};

NICK.namespace = function(nodes) {
	KIDS.namespace(nodes, NICK);
}
NICK.namespace("utils", NICK);

/* Expose some kids functions that got moved to utils-kids */
NICK.utils.isEmptyString = KIDS.utils.isEmptyString;
NICK.utils.trim = KIDS.utils.trim;
NICK.utils.getCookie = KIDS.utils.getCookie
NICK.utils.getDomain = KIDS.utils.getDomain;
NICK.utils.printObj = KIDS.utils.printObj;
NICK.utils.getQueryString = KIDS.utils.getQueryString;
NICK.utils.getGdcXml = KIDS.utils.getGdcXml;
NICK.utils.getSwf = KIDS.utils.getSwf;
NICK.utils.trimArray = KIDS.utils.trimArray;
NICK.utils.getUrlParts = KIDS.utils.getUrlParts;
NICK.utils.getUrlPath = KIDS.utils.getUrlPath;
NICK.utils.getContextPath = KIDS.utils.getContextPath;
NICK.add = KIDS.add;
NICK.get = KIDS.get;
NICK.utils.getNickDomain = KIDS.utils.getNickDomain;
/* end: Expose */

/* legacy - just in case */
NICK.IS_DEV = KIDS.IS_DEV;
NICK.IS_QA = KIDS.IS_QA;
NICK.IS_DEV_ENV = KIDS.IS_DEV_ENV
NICK.IS_LIVE = KIDS.IS_LIVE;
NICK.IS_DEBUG = NICK.IS_DEV_ENV && window && typeof window.console != "undefined";

NICK.utils.URL_DEV_NICK = KIDS.utils.URL_DEV_NICK;
NICK.utils.URL_QA_NICK = KIDS.utils.URL_QA_NICK;
NICK.utils.URL_LIVE_NICK = KIDS.utils.URL_LIVE_NICK;

NICK.utils.URL_SPRING_NICK = 'spring.nick.com';
NICK.utils.URL_MTVN_D = "nick.mtvnimages-d.mtvi.com";
NICK.utils.URL_MTVN_Q = "nick.mtvnimages-q.mtvi.com";
NICK.utils.URL_MTVN_L = "nick.mtvnimages.com";

NICK.utils.URL_MTVN = null;

NICK.utils.initMtvnImageUrl = function() {
	var url = document.location.href;
	var urlParts = NICK.utils.getUrlParts(url);

	if ( urlParts[1] == 'localhost:9090' ) {
		urlParts[1] = NICK.utils.URL_DEV_NICK;
	}

	switch(urlParts[1]) {
		case NICK.utils.URL_DEV_NICK : {
			NICK.utils.URL_MTVN = NICK.utils.URL_MTVN_D;
			break;
		} case NICK.utils.URL_QA_NICK : {
			NICK.utils.URL_MTVN = NICK.utils.URL_MTVN_Q;
			break;			
		} default : {
			NICK.utils.URL_MTVN = NICK.utils.URL_MTVN_L;
		}
	}
}

NICK.utils.getImage = function(url,h,w) {
	h = h || null;
	w = w || null;
	
	return NICK.utils.getMtvnImageUrl(url,h,w);
}

NICK.utils.getMtvnImageUrl = function(image,h,w) {
		
	if(NICK.utils.isEmptyString(NICK.utils.URL_MTVN)) {
		NICK.utils.initMtvnImageUrl(); 
	}

	var path = NICK.utils.getUrlPath(image);

	path = path != null && path.indexOf("/") == 0 ? path : "/" + path;
	if(h) path+='?height='+h;
	if(w && !h) {
		path += '?'
	} else if(w) {
		path += '&';
	}

	if(w) path+='width='+w;
	
	return "http://" + NICK.utils.URL_MTVN + path;
}

NICK.utils.doLog = function(message) {
	//KIDS.utils.doLog("DOLOG: "+NICK.IS_LIVE +" | "+NICK.IS_DEBUG);
	KIDS.utils.doLog(message);
}

NICK.utils.getUsername = function() {
	return NICK.utils.getCookie("loggedInScreenName");
}
getUsername = NICK.utils.getUsername;

NICK.utils.getSessionId = function() {
	return NICK.utils.getCookie("JSESSIONID");
}
getSessionId = NICK.utils.getSessionId;


NICK.utils.hideElement = function(id) {
	if(!id || !$(id)) return;
	$(id).setStyle('visibility', 'hidden');
}

NICK.utils.showElement = function(id) {
	if(!id || !$(id)) return;
	$(id).setStyle('visibility', 'visible');

	//NICK.utils.doLog('vis: '+id+' | '+($(id).getStyle('display') == 'none'));
	if($(id).getStyle('display') == 'none') $(id).setStyle('display', 'block');
}

NICK.utils.toggleButton = function(id, toggle) {
	if(!$(id)) return;
	$(id).set("disabled", !toggle);
}

// 2008-06-27:carrillos
NICK.utils.validateDivIds = function() {
	//if(window.location.hostname.indexOf('mtvi.com') < 0) return;
	if(!NICK.IS_DEBUG) return;

	var dupes = new Object();

	// NodeList, not an array. Can't join divs and objects
	var divs = document.getElementsByTagName('div');
	for(var i = 0; i < divs.length; i++) {
		if(divs[i].id == null || divs[i].id == "" || divs[i].id == "FLASH_AD") continue;

		if(dupes[divs[i].id] != null) {
			alert("Warning: Duplicate element ID found: ["+divs[i].id+"]");
		}
		dupes[divs[i].id] = divs[i].id;
	}

	var objects = document.getElementsByTagName('object');
	for(var i = 0; i < objects.length; i++) {
		if(objects[i].id == null || objects[i].id == "" || objects[i].id == "FLASH_AD") continue;

		if(dupes[objects[i].id] != null) {
			alert("Warning: Duplicate element ID found: ["+objects[i].id+"]");
		}
		dupes[objects[i].id] = objects[i].id;
	}
	NICK.utils.doLog('validateDivIds: valid');
}

NICK.utils.doBookmark = function() {
	var title = 'Nick.com';
	var url = 'http://www.nick.com';
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
	else if (window.sidebar&&window.sidebar.addPanel)
		window.sidebar.addPanel(title,url,"");
}

NICK.utils.initTooltip = function(){
	
	$('.with-nick-tooltip').unbind("mouseenter")
						   .unbind("mouseleave")
	// Initial the tooltip for any items
	$('.with-nick-tooltip').tooltip({
    	contentSelector: ".tooltip-content",
		id: "nick-tooltip",
		allowAccess: false,
		followMouse: true,
		lockTo: "side",
		lockToElement: "img:first",
		offsetX: -13
	});

		
	
}

NICK.utils.addTooltip = function(){
	
}



NICK.utils.openNewBumper = function(wBumper, advertiser) {
	NICK.utils.openBumper(wBumper, advertiser, null, null, null, true);
} 

NICK.utils.openBumper = function(wBumper, advertiser, onair, customBumper, customHeight, newWindow, isInHouse, customWidth) {
	/*var bumperUrl = "/utils/bumper/frameset.html?";
	var fullPageBumperUrl = "/utils/bumper/full-page.html?";*/
	var bumperUrl = "http://www.nick.com/common/bumpers/bumperFrameset.jhtml?";
	if(NICK.config.SITE_NAME == "nicktoons"){
		var fullPageBumperUrl = "http://www.nick.com/common/bumpers/fullpage/nicktoons.jhtml?";
	}else{
		var fullPageBumperUrl = "http://www.nick.com/common/bumpers/fullpage/index.jhtml?";
	}
	var bumperParam = "wBumper";
	var targetUrlParameter = "advertiser";
	onair = onair != null && onair;
	isInHouse = isInHouse == undefined ? true : isInHouse;

	switch(wBumper) {
		case "external" : {
			bumperUrl = "http://www.nick.com/common/bumpers/go.jhtml?";
			bumperUrl += customBumper == null ? "" : ("bumper=" + escape(customBumper) + "&");
			bumperUrl += customHeight == null ? "" : ("bumperHeight=" + customHeight + "&");
			bumperUrl += isInHouse == undefined ? "" : ("isInHouse=" + isInHouse + "&");
			targetUrlParameter = "destination";
			break;
		} case "fullPage" : {
			bumperUrl = fullPageBumperUrl;
			targetUrlParameter = "destination";
			break;
		} case "paysite" : {
			break;
		} case "grown-ups" : {
			break;
		} case "launchpad" : {
			break;
		} case "ag" : {
			break;			
		} case "sponsor" : {
			break;
		} case "full-redirect" : {
			bumperUrl = '/utils/bumper/half-page.html?'	
			break;
		} default : {
			wBumper = "sponsor";
			break;
		}
	}

	var bumperHref = bumperUrl +
					 bumperParam +"="+ wBumper +"&"+
					 targetUrlParameter +"="+ escape(advertiser);

	if(onair) bumperHref += "&onair_landing=true";
	if(onair && wBumper == "external") {
		bumperHref = fullPageBumperUrl + targetUrlParameter +"="+ escape(bumperHref);
	}
	if(newWindow) {
		window.open(bumperHref, 'bumper', 'status=1,scrollbars=1,location=1,resizable=1,toolbar=1,menubar=1');
	} else {
		window.location.href = bumperHref;
	}
}

NICK.utils.wrapOPABanner = function() {
/*
	if ( $("#ad-728x90Div table").width() > 728 ) {
		$("#ad-728x90Div").wrapInner("<div class=\"AdSize-OPA\"></div>");
	} else {
		$("#ad-728x90Div").wrapInner("<div class=\"AdSize-728\"></div>");
	}
*/
}

/* Utils on Document ready function*/
$(document).ready(function() {
	NICK.utils.initTooltip();
	NICK.utils.validateDivIds();
	NICK.utils.initComcast();
	$(document).bind("tooltip.OPEN", function(e, data){NICK.utils.displayScreenShot(data)});
	$(document).bind("tooltip.CLOSE", function(){ $(".tooltip-screenshot").attr("src", "");})	
});

NICK.utils.displayScreenShot = function(data){
	if(data.screenShot != ""){
		if($(".tooltip-screenshot-" + data.cmsid)){
			$(".tooltip-screenshot-" + data.cmsid).attr("src", data.screenShot);
		}
	}
}

/* Provided for yahoo ads */
KIDS.utils.openBumper = NICK.utils.openBumper;

NICK.utils.hideSwfs = function(container) {
	if (container) container = container + " "; else container = "";
	$(container + ".swf").not(".swfnohide").find("> *").each(function() {
		obj = $(this);
		//obj.css("display", "none");
		//obj.hide();
		obj.css('visibility', 'hidden');
	});
}

NICK.utils.showSwfs = function(container) {
	if (container) container = container + " "; else container = "";
	$(container + ".swf > *").each(function() {
		obj = $(this);
		//obj.css("display", "block");
		//obj.show();
		obj.css('visibility', 'visible');
	});
}

/* Not working as expected due to the way the mtv script grabs the highest div layer.
 * It doesn't take into account divs created after page load (such as our swfs)
 * This, coupled with possible timing issues makes this unreliable.
 * Adding in checks after the swf is loaded to see if the overlay is present. See: doSwfEmbedCompleteNick(); 
 */
NICK.utils.initComcast = function() {
	if(typeof projX === "undefined" || !projX) {
		NICK.utils.doLog("NICK.utils.initComcast: projX not found");
		return;
	}
 
	NICK.utils.doLog("NICK.utils.initComcast:3: overriding functions");

	projX.displayEventPre = function() {
		NICK.utils.doLog("NICK.utils.initComcast: hide swfs");
		NICK.utils.hideSwfs();
		return true;
	};

	projX.closeEvent = function(){
		NICK.utils.doLog("NICK.utils.initComcast: close overlay");
		NICK.utils.showSwfs();
	};
}

/* This can be safely removed after 01/01/10
 * See
 */
doSwfEmbedCompleteNick = function (swfObj) {
	if(swfObj == null || swfObj.id == 'GamePlayer') return; // Let the ad loader ('GamePlayer') play - otherwise we won't get to the game creation

	var projAlert = document.getElementById('projxalert');
	if(projAlert == null || projAlert.style.display == 'none') {
		return;
	}
	KIDS.utils.doLog("doSwfObjectEmbed (nick override): alert found: hiding swfs");
	NICK.utils.hideSwfs();
}

NICK.utils.openOverlay = function() {
	NICK.utils.doLog("NICK.utils.openOverlay(): Deprecated. Use: NICK.overlay.open().");
}

NICK.utils.closeOverlay = function() {
	NICK.utils.doLog("NICK.utils.closeOverlay(): Deprecated. Use: NICK.overlay.close().");
}

NICK.utils.initAdFreePage = function() {
	//NICK.utils.doLog("initAdFreePage: "+KIDS.get("adfree"));
	if(KIDS.get("adfree") !== "true") return;

	$("a").not(".nick-overlay").not(".no-adfree").not(".o_popup_close").not("[href^=javascript]").bind("click", NICK.utils.doBumperOverride);
	$('.brand-mamabar-more-list .bumper').remove();
}

NICK.utils.initAdFreeLinks = function(){
	NICK.utils.initAdFreePage();

	$('#top-menu a').bind("click", NICK.utils.doBumperOverride);
	$('#page-menu a').bind("click", NICK.utils.doBumperOverride);
	$('#submenu-wrapper a').bind("click", NICK.utils.doBumperOverride);
	if($('.nick-footer-siteblock')){
		$('.nick-footer-siteblock a').bind("click", NICK.utils.doBumperOverrideNewWin);
		$('.nick-footer-text a').bind("click", NICK.utils.doBumperOverride);
		$('.nick-footer-menu a').bind("click", NICK.utils.doBumperOverride);
	}
}

NICK.utils.initExternalLinks = function() {
	//NICK.utils.doLog(" $$$ $$$ init ex links");
	$("a[href^='http']").attr('target','_top');
}

NICK.utils.initAGLinks = function() {
	$("a[href^='http://www.addictinggames.com']").bind("click", NICK.utils.doAGBumperOverride);
}

NICK.utils.doAGBumperOverride = function(e){
	NICK.utils.openBumper('ag', this.href,null,null,null,true); 
	return false;
}

NICK.utils.doBumperOverride = function(e){
	NICK.utils.openBumper('fullPage', this.href,null,null,null,false);
	return false;
}

NICK.utils.doBumperOverrideNewWin = function(e){
	NICK.utils.openBumper('fullPage', this.href,null,null,null,true);
	return false;
}

NICK.utils.random = function(X) {
	    return Math.floor(X * (Math.random() % 1));
}
	
NICK.utils.randomBetween = function(MinV, MaxV) {
	  return MinV + NICK.utils.random(MaxV - MinV + 1);
}

NICK.utils.getNoRepeatRandoms = function(total, cap){
	var rands = new Array();
	var total = Math.min(total, cap);
	while (rands.length < total){
		var val = NICK.utils.randomBetween(0, cap);
		var duplicate = false;
		for(var i=0; i<rands.length; i++){
			//NICK.utils.doLog(rands[i] + "could= " + val);
			if(rands[i] == val){
				duplicate = true;
			}
		}
		if(duplicate == false){
			rands.push(val);
		}
	}

	return rands;
}

NICK.utils.timezone = function() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}
	return std_time_offset;
}

NICK.utils.getTimezone = function () {
	if (NICK.utils.timezone() == -5)
		return "east";
	else return "west";
}

NICK.utils.goMyNick = function(url) {
	if(KIDS.get("adfree") === 'true') {
		NICK.utils.openBumper('fullPage', url,null,null,null,false);
	} else {
		document.location.href = url;
	}
}

NICK.utils.thisMovie = function(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		if(document[movieName].length != undefined) {
			return document[movieName][1];
		}
		return document[movieName];
	}
}


NICK.utils.setCookie = function(name, value) {
	//alert("calling setLobbyCookie- name:" +name + "value:" + value);
    this.setCookie2(name, value, "", "", "", ""); 
}

NICK.utils.setCookie2 = function(name, value, expires, path, domain, secure){
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  	document.cookie = curCookie;
	//alert("setLobbyCookie2- curCookie:" +curCookie)
}