if(typeof NICK == "undefined" || !NICK) var NICK = {};
NICK.namespace("games.utils");
NICK.namespace("login");

/*--David Jordan 5/26/2009 */
$(document).ready(function() {
	/* Commented out as per: http://jira.mtvi.com/browse/NICKTM-3353*/
	/*NICK.games.utils.initPersonalBest();*/
	NICK.games.utils.initCharacterCarouselTooltip();
	NICK.games.doIpadCheck();
});

NICK.games.utils.initCharacterCarouselTooltip = function(){
	$("#carousel-character-carousel .image").tooltip({
		id: "label-tooltip",
		contentTitle: true,
		followMouse: true,
		allowAccess: false,
		lockTo: "top",
		allowFlip: false,
		delay: 0
	});
};

/*Temporary removal of personal best
 * Will need to uncomment the initPersonalBest() above
 * Will need add, or uncomment add-attribute value="games.high-scores" from the games config 
 * game play page definition
 */

NICK.games.utils.initPersonalBest = function(){

	//Added an exclusion for TeenNick, this needs to be cleaned up	CL 2/28/11
	
	/* Commented out as per: http://jira.mtvi.com/browse/NICKTM-3353*/
	/*
	if(KIDS.get("site") == "TeenNick") {
		NICK.utils.doLog("initPersonalBest: teennick: skipping");
		return;
	}

	$("#games-high-scores").hide();
	$(document).bind("authStatus", function(response){
		NICK.utils.doLog("initPersonalBest: authStatus:" + NICK.login.getNickName());
		NICK.games.utils.initGamePlayPage();
		$('#userpic').html( NICK.avatar.headshot( NICK.login.getAvatar() ) );
	});

	$(document).bind("loggedIn", function(response){
		NICK.utils.doLog("initPersonalBest: loggedIn:" + NICK.login.getNickName());
		NICK.games.utils.initGamePlayPage();
	});

	$(document).trigger("authStatus");
	 */
};

NICK.games.utils.initGamePlayPage = function(){
	//Added an exclusion for TeenNick, this needs to be cleaned up	CL 2/28/11
	if(KIDS.get("site") == "TeenNick") {
		NICK.utils.doLog("initGamePlayPage: teennick: skipping");
		return;
	}

	NICK.games.utils.getPersonalBest();
};

NICK.games.utils.getPersonalBest = function() {
	if(!NICK.login.isLoggedIn()) {
		NICK.utils.doLog("getPersonalBest: not logged in: skipping");
		return;		
	}

	var nick_url = NICK.utils.getNickDomain();
	var URL = "http://"+ "www.nick.com" + "/common/score/json/getPersonalBest.jhtml";
	NICK.request.doRequest({
		dataType: "jsonp",
		url: URL,
		data: {
			user: NICK.login.getNickName(),
			id: NICK.get("cmsId")
		},
		onSuccess: function(response){
			var nickName = NICK.login.getNickName();
			if (!NICK.utils.isEmptyString(nickName)) {
				$("#hs-user-name").html(nickName);
				var score = response.data[0].myScore[0].score;
				score = score != "null" ? score : "";
				$("#my-score").html(score);
				var nickpoints = response.data[0].game[0].nickpoints;
				nickpoints = nickpoints != "null" ? nickpoints : "";
				$("#nickpoints-earned").html(nickpoints);
				var rankIndex = parseInt(response.data[0].rank[0].index) +1;
				rankIndex = isNaN(rankIndex) ?  "n/a" :rankIndex ;
				$("#current-rank").html(rankIndex);
				if(score != "" && score != "0"){
					$("#games-high-scores").show();
				}
			}
		},
		onFail: function(errors){
			for (var error in errors) {
				NICK.utils.doLog("Bad load: Error: " + error + " - " + errors[error]);
			}
		}
	});
};

NICK.games.utils.doMTVNPreloaderAd = function(gameURL,gameURLAlias,gameShowAlias,flashVars,gameParams,gameWidth,gameHeight,gamePerAd,gameTitle,gameId,gameType,gameGenre,skipAd,targetDiv,adType,siteSection,assetId,useWrapper){
	var gamePlayer = new GamePlayer();

	if(flashVars !== ""){
		if ( gameURL.indexOf("?") == -1 ) {
			gameURL += "?" + flashVars;
		} else {
			gameURL += "&" + flashVars;
		}
	}
	
	if( useWrapper === true ){
		gameURL += "&useQSFlashVar=true";
	}

    gamePlayer.gameURL = gameURL;
    gamePlayer.gameWidth = gameWidth;
    gamePlayer.gameHeight = gameHeight;
    gamePlayer.gamePerAd = gamePerAd;
    gamePlayer.adURL = 'http://ad.doubleclick.net/pfadx/' + mtvn.btg.config.AdSettings.DoubleClick.dartSite + '/c/gv/' + gameGenre + '/NA/' + gameShowAlias + '/' + gameType + '/' + gameURLAlias + ';franchise=' + gameShowAlias + ';mode=clip;mtype=gamevideo;genre=' + gameGenre + ';game_type=' + gameType + ';game_id=' + gameId + ';game_name=' + gameURLAlias + ';demo=NA;!category=clip;!category=gamevideo;!category=' + gameShowAlias + ';sz=640x480;tile=1;dcmt=text/xml;u=partner-NA|franchise-' + gameShowAlias + '|mode-clip|mtype-gamevideo|genre-' + gameGenre + '|game_type-' + gameType + '|game_id-' + gameId + '|game_name-' + gameURLAlias + '|demo-NA|!category-clip|!category-gamevideo|!category-' + gameShowAlias + '|sz-640x480|tile-1|ord=' + Math.ceil(Math.random()*1000000);
    gamePlayer.disableAds = skipAd;
    gamePlayer.gameParam = gameParams;

    //To enable FreeWheel Ad
    if( typeof adType != "undefined" && adType === "freewheel" ){
    	gamePlayer.doubleClick.doubleClickEnabled = false;
        gamePlayer.freeWheel.freeWheelEnabled = true;
        gamePlayer.freeWheel.networkId = 82125;    
        gamePlayer.freeWheel.siteSection = siteSection;
        gamePlayer.freeWheel.assetId = assetId;
    }else{
    	gamePlayer.doubleClick.doubleClickEnabled = true;
    	gamePlayer.freeWheel.freeWheelEnabled = false;
    }

    gamePlayer.quantCast.quantcastEnabled = false;
    
    gamePlayer.init(document.getElementById(targetDiv));  
};

NICK.games.utils.doGDMPreloader = function(data){
		
	com.mtvnet.games.GameSettings.Ad.gamePerAd = data.gamePerAd;
	com.mtvnet.games.GameSettings.Ad.timeDisplayEnabled = true;
	
	com.mtvnet.games.GameSettings.Ad.disableAds = data.skipAd;
	com.mtvnet.games.GameSettings.Ad.Quantcast.enabled = false;
	com.mtvnet.games.GameSettings.Ad.DoubleClick.keyValues = 'ga=' + data.gameURLAlias + ',genre=' + data.gameGenre + ',site=' + data.site;
	
	if( typeof data.adType != "undefined" && data.adType === "freewheel" ){
		com.mtvnet.games.GameSettings.Ad.FreeWheel.enabled = true;
		com.mtvnet.games.GameSettings.Ad.DoubleClick.enabled = false;
	}else{
		com.mtvnet.games.GameSettings.Ad.FreeWheel.enabled = false;
		com.mtvnet.games.GameSettings.Ad.DoubleClick.enabled = true;
	}
	
	com.mtvnet.games.GameSettings.Ad.FreeWheel.networkId = 82125;
	com.mtvnet.games.GameSettings.Ad.FreeWheel.siteSection = data.siteSection;
	com.mtvnet.games.GameSettings.Ad.FreeWheel.assetId = data.assetId;
		
	com.mtvnet.games.GameSettings.Reporting.GoogleAnalytics.enabled = true;
	com.mtvnet.games.GameSettings.Reporting.GoogleAnalytics.account = "UA-1922428-3";
	
	com.mtvnet.games.GameSettings.Game.gameEmbedTag = data.gameEmbedTag;
	com.mtvnet.games.GameSettings.Game.height = data.gameHeight;
	com.mtvnet.games.GameSettings.Game.width = data.gameWidth; 
	com.mtvnet.games.GameSettings.Game.param = data.embedParams;
	com.mtvnet.games.GameSettings.Game.metadata.gameURL = data.gameAssetUrl;
	com.mtvnet.games.GameSettings.Game.metadata.gameTitle = data.gameTitle;
	com.mtvnet.games.GameSettings.Game.metadata.gameId = data.gameId;
	
	com.mtvnet.games.GameSettings.Game.doPrecache = data.enablePrecaching;

	com.mtvnet.games.GameSettings.Game.metadata.gameGenre = data.gameGenre;
	com.mtvnet.games.GameSettings.Game.metadata.gameType = data.gameType;
	com.mtvnet.games.GameSettings.Game.metadata.gameName = data.gameURLAlias;
	com.mtvnet.games.GameSettings.Game.metadata.type = "network";

	com.mtvnet.games.GameLoader.init(document.getElementById("games-player-game"));	
};

/* Ad loader support - This is the function we handed to the games team */
NICK.games.launchGame = function() {
	NICK.utils.doLog("Nick.games.launchGame");
};

/* Retrieve related content for the end game screen when related content is not returned by the page controller */
NICK.games.getRelatedData = function(urlAlias){

	var params = {
	 	'sort':'data desc',
	 	'start': 0,
		'page': 1
	 }; 
	
	NICK.request.doRequest({
		dataType:"html",
		url: "/ajax" + urlAlias,
		data: params,
		onSuccess: function(response) {
			var filterTarget = $("#egs-more-games-list");
			filterTarget.height("auto").html(response);

			NICK.utils.initTooltip();

		},
		onFail: function(errors) {
			for(var error in errors) {
				NICK.utils.doLog("Sort Response: Error: "+error+" - "+errors[error]);
			}
		}				
	});
}

NICK.games.doIpadCheck = function() {
	if (typeof ESI === "undefined" || !ESI || ESI.os == null) {
		return;
	}

	if(ESI.os.toLowerCase() == "ipad" && $("#games-player").length > 0) {
		NickLog.debug("doIpad: iPad found");
		$("#games-player").empty().attr("id", "ipad-message-games");
	} else {
		NickLog.debug("doIpad: no iPad: "+ESI.os);
	}
}

