// 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="prop12,events";  
			s.linkTrackEvents="event2";        
			s.prop12='swgc1_allbrowse_go';
			s.events="event2";
			s.tl('byCreator','o','swgc1_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://ben10gamecreator.cartoonnetwork.com/";
	} else if (rAction == "batmangc") {
		location.href = "http://batmangamecreator.cartoonnetwork.com/";
	} else if (rAction == "fbPost") {
		var targetGame;
		if (param02 == "html") {
			targetGame = activeModalGame;
		} else {
			targetGame = param01;
		}
		var targetLoc = '/fb/index.html?gameId=' + targetGame + '&s_cid=swGC_FB';
		window.open (targetLoc,'fbBufferWin','width=635,height=310');
	}
	
}

// generate a random number
function getRandom () {
	mill = new Date ();
	randomNum = Math.round(Math.random() * mill.getMilliseconds());
	return randomNum;
}

function openModTest (gameId) {
	window.open ('http://starwarsgamecreator.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="prop12,events";  
	s.linkTrackEvents="event2";        
	s.prop12=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="prop12,events";  
	s.linkTrackEvents="event2";        
	s.prop12=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","catATTEAnakin",".1",".4").withArgs(".4").send();
	new Request("getBestRatedByHero","catATRTAnakin",".1",".5").withArgs(".5").send();
	new Request("getBestRatedByHero","catBARCAnakin",".1",".6").withArgs(".6").send();
	new Request("getBestRatedByHero","catATTECadBane",".1",".7").withArgs(".7").send();
	new Request("getBestRatedByHero","catATRTCadBane",".1",".8").withArgs(".8").send();
	new Request("getBestRatedByHero","catBARCCadBane",".1",".9").withArgs(".9").send();
	new Request("getBestRatedByHero","catATTERex",".1",".10").withArgs(".10").send();
	new Request("getBestRatedByHero","catATRTRex",".1",".11").withArgs(".11").send();
	new Request("getBestRatedByHero","catBARCRex",".1",".12").withArgs(".12").send();
	new Request("getBestRatedByHero","catBARCAhsoka",".1",".13").withArgs(".13").send();
	new Request("getBestRatedByHero","catATTEAhsoka",".1",".14").withArgs(".14").send();
	new Request("getBestRatedByHero","catATRTAhsoka",".1",".15").withArgs(".15").send();
	new Request("getBestRatedByHero","catNOVehicle",".1",".17").withArgs(".17").send();
	new Request("getMostPopularGameChains","catPopChains",".1","").send();
	new Request("getBestRatedByGoal","catDoor",".1",".1").withArgs(".1").send();
	new Request("getBestRatedByGoal","catHolocrons",".1",".2").withArgs(".2").send();
	new Request("getBestRatedByGoal","catEnemies",".1",".3").withArgs(".3").send();
	new Request("getBestRatedByGoal","catSurvival",".1",".4").withArgs(".4").send();
	new Request("getBestRatedByGoal","catDestruction",".1",".5").withArgs(".5").send();
	new Request("getFeaturedCreator","catPicks",".1","").send();
	new Request("getMostShared","catMostShare",".1","").send();

	currentFilter = undefined;
	currentArgs = undefined;
	currentContent = "featured";
	rollImg ('featuredGamesBtn', '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="prop12,events";  
		s.linkTrackEvents="event2";        
		s.prop12="swgc1_from_shared_link";
		s.events="event2";
		s.tl(gameID,'o','swgc1_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, 'tools/img/unv.chains/nav.btn.' + currentContent + '.off.jpg');
			rollImg (nextMainNav, 'tools/img/unv.chains/nav.btn.' + requestedContent + '.on.jpg');
			document.getElementById(previousContent).className="masterShellOff";
		} else {
			nextMainNav = requestedContent + "GamesBtn";
			rollImg (nextMainNav, '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","catATTEAnakin",".1",".4").withArgs(".4").send();
		new Request("getBestRatedByHero","catATRTAnakin",".1",".5").withArgs(".5").send();
		new Request("getBestRatedByHero","catBARCAnakin",".1",".6").withArgs(".6").send();
		new Request("getBestRatedByHero","catATTECadBane",".1",".7").withArgs(".7").send();
		new Request("getBestRatedByHero","catATRTCadBane",".1",".8").withArgs(".8").send();
		new Request("getBestRatedByHero","catBARCCadBane",".1",".9").withArgs(".9").send();
		new Request("getBestRatedByHero","catATTEAhsoka",".1",".10").withArgs(".10").send();
		new Request("getBestRatedByHero","catATRTAhsoka",".1",".11").withArgs(".11").send();
		new Request("getBestRatedByHero","catBARCAhsoka",".1",".12").withArgs(".12").send();
		new Request("getBestRatedByHero","catNOVehicle",".1",".17").withArgs(".17").send();
		new Request("getMostPopularGameChains","catPopChains",".1","").send();
		new Request("getBestRatedByGoal","catDoor",".1",".1").withArgs(".1").send();
		new Request("getBestRatedByGoal","catHolocrons",".1",".2").withArgs(".2").send();
		new Request("getBestRatedByGoal","catEnemies",".1",".3").withArgs(".3").send();
		new Request("getBestRatedByGoal","catSurvival",".1",".4").withArgs(".4").send();
		new Request("getBestRatedByGoal","catDestruction",".1",".5").withArgs(".5").send();
		new Request("getFeaturedCreator","catPicks",".1","").send();
		new Request("getMostShared","catMostShare",".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 == ".4") {
					textFiller = "Anakin";
				} else if (typeNum == ".7") {
					textFiller = "Cad Bane";
				} else if (typeNum == ".13") {
					textFiller = "Ahsoka";
				} else {
					textFiller = "Captain Rex";
				}
				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 Holocrons";
				} else if (typeNum == ".3") {
					textFiller = "Battle All Enemies";
				} else if (typeNum == ".4") {
					textFiller = "Survive";
				} else {
					textFiller = "Destroy the Base";
				}
				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 Character";
				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 Character";
				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 Character";
				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="prop12,events";  
		s.linkTrackEvents="event2";        
		s.prop12='swgc1_allbrowse_go';
		s.events="event2";
		s.tl('byCreator','o','swgc1_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="prop12,events";  
	s.linkTrackEvents="event2";        
	s.prop12='swgc1_allbrowse_go';
	s.events="event2";
	s.tl('byCreator','o','swgc1_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://starwarsgamecreator.cartoonnetwork.com/index.html?id=" + gameId;
	stfWin.location.href = "http://starwarsgamecreator.cartoonnetwork.com/stf/index.html?id=" + gameId;
	activeModalGame = gameId;
	activeModalShell = elementId;
	var s=s_gi('carnetnmcom');
	s.linkTrackVars="prop12,events";  
	s.linkTrackEvents="event2";        
	s.prop12='swgc_share';
	s.events="event2";
	s.tl('sendbyhtml','o','swgc_share');
	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();
	}
	var s=s_gi('carnetnmcom');
	s.linkTrackVars="prop12,events";  
	s.linkTrackEvents="event2";        
	s.prop12='swgc_login';
	s.events="event2";
	s.tl(entryPoint,'o','swgc_login');
	//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;
}

