// Flipline.com / PapaLouie.com 
// Game Page Link Button Randomizer
// v. 0.1



/*
Link and Button information:
============================

Add foldernames to the array (ie. "cactusmccoycot").

The script will automatically assign the correct full path (ie. "flipline.com/games/cactusmccoycot"),
and will automatically grab the correct image (located in foldername/images/banner_s.jpg)

*/


var games = [

"cactusmccoycot",
"papasburgeria",
"papaspizzeria",
"rockgarden",
"steakandjake",
"papalouie",
"midnightmarch",
"papastacomia",
"papasfreezeria",
"cactusmccoy2"

];



/*
Game-Specific Defaults:
=======================

Add array pairs of default top buttons for any game.
Use this format:

[thisGameName, buttonToShow, useInsteadOfNewestGame(true/false)]

*/

var defaultButtons = [

	["papasburgeria", "papaspizzeria", false],
	["steakandjake", "midnightmarch", false],
	["midnightmarch", "steakandjake", false],
	["papaspizzeria", "papasburgeria", false],
	["papasburgeria", "papastacomia", false],
	["papastacomia", "papasfreezeria", false],
	["papasfreezeria", "papastacomia", false],
	["cactusmccoycot", "cactusmccoy2", true],
	["cactusmccoy2", "cactusmccoycot", true]
	

];



/*
Newest Featured Game:
=====================

Enter a game folder name to use as the featured game, which will appear as the top entry on all pages.
Also set whether this should be enabled, true/false.

NOTE: Any game-specific defaults with the useInsteadOfNewestGame set to true will NOT show this featured game.

*/

var featuredGame = "cactusmccoy2";
var showFeaturedGame = true;



//-------------------------------------------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------------------------------//
//                                                                                                                                     //
//                                                                                                                                     //
//    DO NOT EDIT BELOW THIS LINE                                                                                                      //
//                                                                                                                                     //
//                                                                                                                                     //
//-------------------------------------------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------------------------------//


//Generator:
function createGameLinks(){
	
	setupArrayIndexing();
	
	var foldername = getFolderName(location.href);
	var domain = getDomainName(location.href);
	var i;
	
	var useGames = games.concat();

	//remove THIS GAME from the list:
	if (useGames.indexOf(foldername) > -1){
		useGames.splice(useGames.indexOf(foldername), 1);
	}
	
	var pageLinks = [];
	var linksToRandomize = 3;
	
	//check if there's a default one to use
	var useDefault = getDefaultGame(foldername);
	
	if (useDefault != ""){
		pageLinks.push(useDefault);
		linksToRandomize --;
		
		//remove FEATURED GAME from the list:
		if (useGames.indexOf(useDefault) > -1){
			useGames.splice(useGames.indexOf(useDefault), 1);
		}
	}
	
	for (i=0; i<linksToRandomize; i++){
		
		//pick a random one:
		var newIndex = Math.floor(Math.random() * useGames.length);
		pageLinks.push(useGames[newIndex]);
		useGames.splice(newIndex, 1);
		
	}
	
	
	
	//create the HTML to write into the document!
	var writeString = "";
	
	
	for (i=0; i<pageLinks.length; i++){
		writeString += "<tr><td height=\"60\" class=\"bk_bg\"><a href=\"../"+pageLinks[i]+"/index.html\">";
		writeString += "<img src=\"../"+pageLinks[i]+"/images/banner_s.jpg\" alt=\"Play Now!\" width=\"285\" height=\"60\" border=\"0\"></a></td></tr>";
	  
	  if (i < pageLinks.length-1){
		  writeString += " <tr><td height=\"10\" class=\"bk_bg\"></td></tr>";
	  }
		
	}
	
	
	//Write it:
	document.write(writeString);

	
}


function getFolderName(pageURL){
	
	var parts = pageURL.split("/");
	var folderpart = parts[parts.length-2];
	return folderpart;
	
}

function getDomainName(pageURL){
	var parts = pageURL.split("/");
	return parts[2];
}

function getDefaultGame(thisGame){
	
	var gotDefault = "";
	var shouldOverride = false;
	
	//search through the defaults:
	for (var i=0; i<defaultButtons.length; i++){
		
		if (defaultButtons[i][0] == thisGame){
			gotDefault = defaultButtons[i][1];
			shouldOverride = defaultButtons[i][2];
		}
	}
	
	if (showFeaturedGame == true && featuredGame != "" && featuredGame != thisGame){
		if (shouldOverride == false){
			gotDefault = featuredGame;
		}
	}
	
	return gotDefault;

}


function setupArrayIndexing(){
	
	//Javascript may not have Array.indexOf built in.
	//This will allow it to work correctly.
	
	if (!Array.prototype.indexOf)
	{
	  Array.prototype.indexOf = function(elt /*, from*/)
	  {
		var len = this.length;
	
		var from = Number(arguments[1]) || 0;
		from = (from < 0)
			 ? Math.ceil(from)
			 : Math.floor(from);
		if (from < 0)
		  from += len;
	
		for (; from < len; from++)
		{
		  if (from in this &&
			  this[from] === elt)
			return from;
		}
		return -1;
	  };
	}
	
}


function onload(){
	
	//createGameLinks();
}


