// parses any query string passed into an array of name value pairs
var qString = location.search.substr(1);
if (qString.indexOf("#") > -1) {
	chopHash = qString.split("#");
	qString = chopHash[0]
}
var nvPairs = new Array();
nvPairs = qString.split('&');
var passedVal = "";
var passedCreator = false;
if (nvPairs.length > -1) {
	for (i = 0; i < nvPairs.length; i++) {
		tempArr = nvPairs[i].split('=');
		if (tempArr[0] == "id") {
			passedVal = tempArr[1];
		}
		if (tempArr[0] == "getCreator") {
			passedCreator = tempArr[1];
		}
	}
}

// receives requests from the swf
// the first param is the type of action requested
// other params carry any other necessary info
function swfReceiver (rAction,param01,param02,param03) {
	if (rAction == "swf_loaded") {
		if (passedCreator != false) {
			swapContent ('browse','getGamesByCreator','Creator.name=' + passedCreator + '&numberOfItemsPerPage=10&pageToDisplay=1&sortByColumn=1&sortBy=0&Game.statistic.timesShared=0&Game.statistic.timesPlayed=0');
			var s=s_gi('carnetnmcom');
			s.linkTrackVars="prop11,events";  
			s.linkTrackEvents="event2";        
			s.prop11='bgc1_allbrowse_go';
			s.events="event2";
			s.tl('byCreator','o','bgc1_allbrowse_go');
			//location.href = "#swfTop";
			window.scrollTo(0,200);
		} else {
			initFeatured ();
		}
	} else if (rAction == "browse") {
		swapContent ('browse','getNew','.1','');
	} else if (rAction == "full_profile") {
		swapContent ('profile');
	} else if (rAction == "chains") {
		swapContent ('chains','getNewGameChains','.1','');
	} else if (rAction == "tutorial") {
		window.open ('tutorial/index.html');
	} else if (rAction == "get_help") {
		window.open ('faq/index.html?section=help');
	} else if (rAction == "for_parents") {
		window.open ('faq/index.html?section=forparents');
	} else if (rAction == "ben10gc") {
		location.href = "http://gamecreator.cartoonnetwork.com/";
	} else if (rAction == "starwarsgc") {
		location.href = "http://starwarsgamecreator.cartoonnetwork.com/";
	}
}

// generate a random number
function getRandom () {
	mill = new Date ();
	randomNum = Math.round(Math.random() * mill.getMilliseconds());
	return randomNum;
}

function openModTest (gameId) {
	window.open ('http://batmangamecreator.cartoonnetwork.com/index.html?id='+gameId);
}

// simple image swapper
function rollImg (imgName, imgSrc) {
	if (document.images) {
		document[imgName].src = imgSrc;
	}
}

function rollStyle (styleObj, styleName) {
	if (styleObj.className) {
		styleObj.className = styleName;
	} else {
		document.getElementById(styleObj).className = styleName;
	}
}

function rollNavStyle (styleObj, styleName, assocFilter) {
	if (currentFilter != assocFilter) {
		if (styleObj.className) {
			styleObj.className = styleName;
		} else {
			document.getElementById(styleObj).className = styleName;
		}
	}
}

function rollBrowseClass (styleObj, styleName) {
	if (styleObj != activeFilterShell) {
		rollStyle (styleObj, styleName);
	}
}

// determines if the browser is IE or other
function isGCIE () {
	var hasMS = navigator.appName.indexOf("Microsoft") != -1;
	return hasMS;
}

// determines the DOM name of the swf object
function thisMovie(movieName) {
	if (isGCIE ()) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

// sends the swf to the build state
function swfSendGame (gameID,gameCreator,gameFilter) {
	thisMovie("gamecreator").swfCallGame(gameID,gameCreator);
	var s=s_gi('carnetnmcom');
	s.linkTrackVars="prop11,events";  
	s.linkTrackEvents="event2";        
	s.prop11=gameFilter;
	s.events="event2";
	s.tl(gameID,'o',gameFilter);
//	location.href = "#swfTop";
	window.scrollTo(0,200);
}

// sends the swf the ID and thumbnail of a game to be added to a
// game chain
function swfAddToChain (gameID,gameImg) {
	thisMovie("gamecreator").swfAddToChain(gameID,gameImg);
//	location.href = "#swfTop";
	window.scrollTo(0,700);
}

// sends game IDs into the swf
function swfCall() {
	thisMovie("gamecreator").fromJS();
//	location.href = "#swfTop";
	window.scrollTo(0,200);
}

function getMoreGames (one, two, three, four, five) {
	swapContent(one,two,three,four);
	var s=s_gi('carnetnmcom');
	s.linkTrackVars="prop11,events";  
	s.linkTrackEvents="event2";        
	s.prop11=five;
	s.events="event2";
	s.tl(one,'o',five);
}

var currentContent;
var currentFilter;
var currentArgs;
var activeFilterShell;
var currentTypeId;
// stupid workaround, like most of the rest of this page
function initFeatured () {

	CSIManager.getInstance().useDelayedCSI=true;

	new Request("getMostPopularAllTime","catMostPopAll",".1","").send();
	new Request("getMostPopularThisWeek","catMostPopWeek",".1","").send();
	new Request("getNew","catNew",".1","").send();
	new Request("getNewGameChains","catNewChains",".1","").send();
	new Request("getBestRatedByHero","catTopBeetle",".1",".1").withArgs(".2").send();
	new Request("getBestRatedByHero","catTopArrow",".1",".2").withArgs(".1").send();
	new Request("getBestRatedByHero","catTopAqua",".1",".3").withArgs(".3").send();
	new Request("getBestRatedByHero","catTopTornado",".1",".4").withArgs(".4").send();
	new Request("getMostPopularGameChains","catPopChains",".1","").send();
	new Request("getBestRatedByGoal","catTopBattle",".1",".3").withArgs(".3").send();
	new Request("getBestRatedByGoal","catTopCollect",".1",".2").withArgs(".2").send();
	new Request("getFeaturedCreator","catPicks",".1","").send();

	currentFilter = undefined;
	currentArgs = undefined;
	currentContent = "featured";
	rollImg ('featuredGamesBtn', 'http://i.cdn.turner.com/toon/games/gamecreatorbm/tools/img/unv.chains/nav.btn.featured.on.jpg');
	document.getElementById('featuredMasterShell').className = "masterShellOn";
	if ((passedVal != undefined) && (passedVal != "")) {
		var s=s_gi('carnetnmcom');
		s.linkTrackVars="prop11,events";  
		s.linkTrackEvents="event2";        
		s.prop11="bgc1_from_shared_link";
		s.events="event2";
		s.tl(gameID,'o','bgc1_from_shared_link');
//		location.href = "#swfTop";
		window.scrollTo(0,900);
	}
}

//
// keeps track of which data set is currently displayed
// defaults to featured content
// requestedContent corresponds to the tab and button, i.e. "browse"
// contentFilter is the subcategory to requestedContent, i.e "browse" "getBestRatedByHero"
// otherArgs holds any other arguments necessary to make the request
// typeNum tracks subcategories of subcategories, i.e "browse" "getBestRatedByHero","1" where one is Blue Beetle
function swapContent (requestedContent, contentFilter, otherArgs, typeNum) {
	previousContent = currentContent + "MasterShell";
	nextContent = requestedContent + "MasterShell";
	if (contentFilter == "repeat") {
		contentFilter = currentFilter;
	}
	if (requestedContent != currentContent || ((requestedContent == currentContent) && ((contentFilter != currentFilter) || (contentFilter == currentFilter)))) {
		if (currentContent != undefined) {
			previousMainNav = currentContent + "GamesBtn";
			nextMainNav = requestedContent + "GamesBtn";
			rollImg (previousMainNav, 'http://i.cdn.turner.com/toon/games/gamecreatorbm/tools/img/unv.chains/nav.btn.' + currentContent + '.off.jpg');
			rollImg (nextMainNav, 'http://i.cdn.turner.com/toon/games/gamecreatorbm/tools/img/unv.chains/nav.btn.' + requestedContent + '.on.jpg');
			document.getElementById(previousContent).className="masterShellOff";
		} else {
			nextMainNav = requestedContent + "GamesBtn";
			rollImg (nextMainNav, 'http://i.cdn.turner.com/toon/games/gamecreatorbm/tools/img/unv.chains/nav.btn.' + requestedContent + '.on.jpg');
		}
		if (requestedContent == "featured") {
			new Request("getMostPopularAllTime","catMostPopAll",".1","").send();
			new Request("getMostPopularThisWeek","catMostPopWeek",".1","").send();
			new Request("getNew","catNew",".1","").send();
			new Request("getNewGameChains","catNewChains",".1","").send();
			new Request("getBestRatedByHero","catTopBeetle",".1",".1").withArgs(".2").send();
			new Request("getBestRatedByHero","catTopArrow",".1",".2").withArgs(".1").send();
			new Request("getBestRatedByHero","catTopAqua",".1",".3").withArgs(".3").send();
			new Request("getBestRatedByHero","catTopTornado",".1",".4").withArgs(".4").send();
			new Request("getMostPopularGameChains","catPopChains",".1","").send();
			new Request("getBestRatedByGoal","catTopBattle",".1",".3").withArgs(".3").send();
			new Request("getBestRatedByGoal","catTopCollect",".1",".2").withArgs(".2").send();
			new Request("getFeaturedCreator","catPicks",".1","").send();
			currentFilter = undefined;
			currentArgs = undefined;
		} else if (requestedContent == "profile") {
			if (otherArgs == undefined) {
				new oldRequest("getMyGames","profileShell").withArgs("Game.status=1,0&numberOfItemsPerPage=10&pageToDisplay=1&sortByColumn=1&sortBy=0&displayName=" + getRandom()).send();
				otherArgs = "Game.status=1,0&numberOfItemsPerPage=10&pageToDisplay=1&sortByColumn=1&sortBy=0&displayName=" + getRandom();
			} else {
				otherArgs += "&displayName = " + getRandom();
				new oldRequest("getMyGames","profileShell").withArgs(otherArgs).send();
			}
			currentFilter = undefined;
			currentArgs = otherArgs;
		} else if (requestedContent == "chains") {
			new Request(contentFilter,"chainsShell",otherArgs,"").send();
			currentFilter = contentFilter;
			currentArgs = otherArgs;
		} else {
			if (contentFilter == undefined) {
				new Request("getNew","browseGamesShell",".1","").send();
			} else if (contentFilter == "getGamesByCreator") {
				new newRequest(contentFilter,"browseGamesShell").withArgs(otherArgs).send();
			} else {
				new Request(contentFilter,"browseGamesShell",otherArgs,typeNum).send();
			}
			currentFilter = contentFilter;
			currentArgs = otherArgs;
			currentTypeId = typeNum;
			if (contentFilter == "getBestRatedByHero") {
				if (typeNum == ".1") {
					textFiller = "Blue Beetle";
				} else if (typeNum == ".2") {
					textFiller = "Green Arrow";
				} else if (typeNum == ".3") {
					textFiller = "Aquaman";
				} else if (typeNum == ".4") {
					textFiller = "Red Tornado";
				} else {
					textFiller = "Batman";
				}
				document.getElementById('allGamesImageShell').className = "allGamesImageShellOff";
				document.getElementById('byGoalsImageShell').className = "menuGamesImageShellOff";
				document.getElementById('byGoalsImageShell').innerHTML = "Browse by Goal";
				document.getElementById('byHeroesImageShell').className = "menuGamesImageShellOn";
				document.getElementById('byHeroesImageShell').innerHTML = textFiller;
				document.nameSearch.freeText.className = "textFieldOff";
				document.nameSearch.freeText.valuee = "Search by Creator Name";
				activeFilterShell = "byHeroesImageShell";
			} else if (contentFilter == "getBestRatedByGoal") {
				if (typeNum == ".1") {
					textFiller = "Get to the Door";
				} else if (typeNum == ".2") {
					textFiller = "Collect All Orbs";
				} else if (typeNum == ".3") {
					textFiller = "Battle All Enemies";
				} else {
					textFiller = "Battle & Collect";
				}
				document.getElementById('allGamesImageShell').className = "allGamesImageShellOff";
				document.getElementById('byGoalsImageShell').className = "menuGamesImageShellOn";
				document.getElementById('byGoalsImageShell').innerHTML = textFiller;
				document.getElementById('byHeroesImageShell').className = "menuGamesImageShellOff";
				document.getElementById('byHeroesImageShell').innerHTML = "Browse by Hero";
				activeFilterShell = "byGoalsImageShell";
				document.nameSearch.freeText.className = "textFieldOff";
				document.nameSearch.freeText.value = "Search by Creator Name";
			} else if (contentFilter == "getGamesByCreator") {
				document.getElementById('allGamesImageShell').className = "allGamesImageShellOff";
				document.getElementById('byGoalsImageShell').className = "menuGamesImageShellOff";
				document.getElementById('byGoalsImageShell').innerHTML = "Browse by Goal";
				document.getElementById('byHeroesImageShell').className = "menuGamesImageShellOff";
				document.getElementById('byHeroesImageShell').innerHTML = "Browse by Hero";
				document.nameSearch.freeText.className = "textFieldOn";
				activeFilterShell = "byCreatorBtnShell";
			} else {
				document.getElementById('allGamesImageShell').className = "allGamesImageShellOn";
				document.getElementById('byGoalsImageShell').className = "menuGamesImageShellOff";
				document.getElementById('byGoalsImageShell').innerHTML = "Browse by Goal";
				document.getElementById('byHeroesImageShell').className = "menuGamesImageShellOff";
				document.getElementById('byHeroesImageShell').innerHTML = "Browse by Hero";
				document.nameSearch.freeText.className = "textFieldOff";
				document.nameSearch.freeText.value = "Search by Creator Name";
				activeFilterShell = "allGamesImageShell";
			}
		}
		currentContent = requestedContent;
	}
	document.getElementById(nextContent).className = "masterShellOn";
//	location.href = "#htmlTop";
	window.scrollTo(0,900);
}

function doFreeTextRequest () {
	creatorArg = document.nameSearch.freeText.value;
	if (creatorArg != "") {
		swapContent ('browse','getGamesByCreator','Creator.name=' + creatorArg + '&numberOfItemsPerPage=10&pageToDisplay=1&sortByColumn=1&sortBy=0&Game.statistic.timesShared=0&Game.statistic.timesPlayed=0');
		var s=s_gi('carnetnmcom');
		s.linkTrackVars="prop11,events";  
		s.linkTrackEvents="event2";        
		s.prop11='bgc1_allbrowse_go';
		s.events="event2";
		s.tl('byCreator','o','bgc1_allbrowse_go');
//		location.href = "#htmlTop";
		window.scrollTo(0,900);
	}
}

function doNameSearch (creatorArg) {
	swapContent ('browse','getGamesByCreator','Creator.name=' + creatorArg + '&numberOfItemsPerPage=10&pageToDisplay=1&sortByColumn=1&sortBy=0&Game.statistic.timesShared=0&Game.statistic.timesPlayed=0');
	var s=s_gi('carnetnmcom');
	s.linkTrackVars="prop11,events";  
	s.linkTrackEvents="event2";        
	s.prop11='bgc1_allbrowse_go';
	s.events="event2";
	s.tl('byCreator','o','bgc1_allbrowse_go');
//	location.href = "#htmlTop";
	window.scrollTo(0,900);
}

function rollMainNav (imgName, imgSrc) {
	if (imgName.indexOf(currentContent) < 0) {
		rollImg (imgName, imgSrc);
	}
}

var currentBrowseFilterBtn;
function rollBrowseNav (imgName, imgSrc) {
//	if (imgName != currentBrowseFilterBtn) {
		rollImg (imgName, imgSrc);
//	}
}

// page slider available to the swf
function scrollPage() {
	window.scrollTo(0,200);
}

var activeModalGame;
var activeModalShell;
function launchModal (gameId, elementId, newClassName,gameCreator) {
	if (activeModalShell != undefined) {
		document.getElementById(activeModalShell).className = "modalOff";
	}
	document.getElementById(elementId).className = "modalOn";
	document.cnpSTFForm.directLinkText.value = "http://batmangamecreator.cartoonnetwork.com/index.html?id=" + gameId;
	stfWin.location.href = "/stf/index.html?id=" + gameId;
	activeModalGame = gameId;
	activeModalShell = elementId;
	location.href = "#htmlTop";
//	alert("activeModalShell = " + activeModalShell);
	activeModalShell = elementId;
}

function closeModal () {
	document.getElementById(activeModalShell).className = "modalOff";
	activeModalGame = undefined;
	activeModalShell = undefined;
	swapContent('profile','repeat','Game.status=1,0&numberOfItemsPerPage=10&pageToDisplay=1&sortByColumn=1&sortBy=0');
}

function launchRegSwf (entryPoint) {
	if (entryPoint == "register") {
		thisMovie("gamecreator").swfCallReg();
	} else {
		thisMovie("gamecreator").swfCallLogin();
	}
	//location.href = "#swfTop";
	window.scrollTo(0,200);
}

var tenDigitString = "";
function getRandom10 () {
	for (i=0; i<10; i++) {
		tenDigitString += (Math.floor(Math.random() * 10)).toString();
	}
	return tenDigitString;
}

var my10Digit = getRandom10();


var cookieVal = null;
function getCookieByName (cookieName) {
	var cookieDomain = self.location.host;
	if (/^[^.]+\.[^.]+\.[^.]+$/.test(cookieDomain)) {
		cookieDomain = cookieDomain.substring(cookieDomain.indexOf('.')+1);
	}
	var allCookies = unescape(document.cookie);
	var cookieArray = allCookies.split(";");
	for (i=0; i<cookieArray.length; i++) {
		if (cookieArray[i].indexOf(cookieName) > -1) {
			cookieCrumbs = cookieArray[i].split("=");
			cookieVal = cookieCrumbs[1];
		} else {
			cookieVal = null;
		}
	}
	return cookieVal;
}

function cartoonLogOut(){
	document.cookie = "TEGid=null; 			expires=Thu, 01-Jan-70 00:00:01 GMT path=/; domain=.cartoonnetwork.com";
	document.cookie = "authid=null; 		expires=Thu, 01-Jan-70 00:00:01 GMT path=/; domain=.cartoonnetwork.com";
	document.cookie = "authz=null; 			expires=Thu, 01-Jan-70 00:00:01 GMT path=/; domain=.cartoonnetwork.com";
	document.cookie = "authpass=null; 		expires=Thu, 01-Jan-70 00:00:01 GMT path=/; domain=.cartoonnetwork.com";
	document.cookie = "displayname=null; 	expires=Thu, 01-Jan-70 00:00:01 GMT path=/; domain=.cartoonnetwork.com";
	initFeatured();
	document.location.href = document.location.href;
}

