
var activeButtons = new Array(
	'estore',
	'login',
	'backyard',
	'games',
	'digSite',
	'shop',
	'fieldGuide',
	'rewards',
	'register',
	'codeWord',
	'classroom',
	'yearbook',
	'avClub',
	'dinocyclopedia',
	'hatchery',
	'citysaurus');


function getQueryStringValue(key)
{
	loc = window.location.search.substring(1);
	bits = loc.split('&');
	for (i=0; i<bits.length; i++)
	{	keyValue = bits[i].split("=");
		if (keyValue[0] == key)
		{	return keyValue[1];
		}
	}
	return null;
}


function getDinoRunStartLevel()
{
	startLevel = getQueryStringValue('level');
	return startLevel ? startLevel : 0;
}


function trace(txt)
{	
	// log(txt);
}

function log(txt)
{
	var openURL = '/log.php?txt=' + escape(txt);
	
	var xmlHttp = new XMLHttpRequest();

	var d = new Date();
	var sep = '&';
	if (!/\?/.exec(openURL))
	{	sep = '?';
	}
	openURL = openURL + sep + 'ieCache=' + d.getTime();

	var urlParts = /^(.*?)\?(.*)$/.exec(openURL);
	var url = urlParts[1];
	var params = urlParts[2];
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}


function in_array(needle, haystack)
{
	var found = false;
	for (var i=0; i < haystack.length; i++)
	{	if (needle == haystack[i])
		{	found = true;
			break;
		}
	}
	return found;
}



function showingHelp(clipName)
{
	deactivateButton('fieldGuide');
}


function hidingHelp()
{
	activateButton('fieldGuide');
}


function activateButton(sectionID)
{	
	if (!in_array(sectionID, activeButtons))
	{	activeButtons.push(sectionID);
	}
}

function deactivateButton(sectionID)
{	
	var temp = new Array();
	for (var i=0; i < activeButtons.length; i++)
	{
		if (activeButtons[i] != sectionID)
		{	temp.push(activeButtons[i]);
		}
	}
	activeButtons = temp;
}


function navClicked(sectionID)
{
	if (!in_array(sectionID, activeButtons))
	{	return false;
	}

	var gotoURL = '';

	if (sectionID == 'login')
	{	gotoURL = "login.html";
	}else if (sectionID == 'register')
	{	gotoURL = "register.html";
	}else if (sectionID == 'backyard')
	{	gotoURL = 'backyard.html';
	}else if (sectionID == 'games')
	{	gotoURL = 'games.html';
	}else if (sectionID == 'digSite')
	{	gotoURL = 'dig-site.html';
	}else if (sectionID == 'codeWord')
	{	gotoURL = 'codeword.html';
	}else if (sectionID == 'shop')
	{	gotoURL = 'store.html';
	}else if (sectionID == 'citysaurus')
	{	gotoURL = 'city-saurus.html';
	}else if (sectionID == 'fieldGuide')
	{	var swf = getMainFlashMovie();
		swf.callFlashEvent("openDigBook");
	}else if (sectionID == 'rewards')
	{	showRewards();
	}else if (sectionID == 'estore')
	{	window.open('http://dinodan.shop.musictoday.com/', '_blank');
	}else
	{	// alert("The button " + sectionID + " is not active yet.");
		gotoURL = sectionID + ".html";
	}

	if (gotoURL != '')
	{	window.location.href = gotoURL;
	}
}



function playGame(gameType, gameID)
{
	var gameFile = '';

	var miniGames = new Array('tailswipe', 'dino-glider');
	var marqueeGames = new Array('city-saurus');

	if (gameType == 'minigame')
	{	gameFile = miniGames[gameID-1];
	}else if (gameType == 'game')
	{	gameFile = marqueeGames[gameID-1];
	}

	if (gameFile != '')
	{	window.location.href = gameFile + '.html';
	}
}

function gameButtonClicked(sectionID)
{
	var gotoURL = '';

	if (sectionID == 'mini_1')
	{	gotoURL = "tailswipe.html";
	}else if (sectionID == 'marquee_1')
	{	gotoURL = 'city-saurus.html';
	}

	if (gotoURL != '')
	{	window.location.href = gotoURL;
	}
}


function getMainFlashMovie()
{	return getFlashMovie("contentSWF");
}

function getFlashMovie(movieName)
{	
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}



function updatePointsFromDB()
{	
	var flash = (navigator.appName.indexOf("Microsoft") != -1) ?  window.wrapperSWF : window.document.wrapperSWF;
	flash.updatePointsFromDB(true);
}

function updatePoints(points)
{	setPoints(points);
}
function setPoints(points)
{
	var flash = (navigator.appName.indexOf("Microsoft") != -1) ?  window.wrapperSWF : window.document.wrapperSWF;
	flash.setPoints(points);
}

function adjustPoints(points)
{
	var flash = (navigator.appName.indexOf("Microsoft") != -1) ?  window.wrapperSWF : window.document.wrapperSWF;
	flash.adjustPoints(points);
}


function showRewards(forceFieldGuideReopen)
{
	var flash = (navigator.appName.indexOf("Microsoft") != -1) ?  window.wrapperSWF : window.document.wrapperSWF;
	flash.showRewards(forceFieldGuideReopen == "1" ? true : false);
}


function printWindow()
{	window.print();
}


function setDigSiteDinoID(dinoID)
{	document.cookie = "digSiteDinoID=" + escape(dinoID);
}

function getDigSiteDinoID()
{	// return 'trex'; // getCookie("digSiteDinoID");

	var cookies = document.cookie.split(";");
	var key, val;
	for (var i = 0; i < cookies.length; i++)
	{
		key = cookies[i].substr(0, cookies[i].indexOf("="));
		val = cookies[i].substr(cookies[i].indexOf("=") + 1);
		key = key.replace(/^\s+|\s+$/g,"");
		if (key == 'digSiteDinoID')
		{	return unescape(val);
		}
	}
	return '';
}


function clearDigSiteDinoID()
{	// not used
	if (getDigSiteDinoID() != '')
	{	// document.cookie = 'digSiteDinoID=empty;expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
}

