/* --------------------------------------------------------------------
NAME: siteFunctions.js
DESC: Includes variables and functions used site-side to render layout.

* sitePageNames - [Array of arrays] Stores filenames and display names
	of primary site pages.  The order in which the page names appear
	in this array determines the order in which the pages will appear
	in the site.  The first element in each page array indicates the
	type of page: 1=standard, 2=photo gallery.  This designation is
	critical for enabling the writeGlobalSubnav method to render the 
	subnav on the photo gallery subpages, which don't appear in the 
	sitePageNames array.
* galleryPageNames - [Array of strings] Stores filenames of photo 
	gallery pages in the order in which they should appear in the site.
-------------------------------------------------------------------- */
var STANDARD_PAGE = 1;
var GALLERY_PAGE = 2;
var EPISODE_PAGE = 3;
var VIDEO_PAGE = 4;

var sitePageNames = new Array();
sitePageNames.push(new Array(STANDARD_PAGE, "javascript:launchInteractive()", "World at Six Degrees"));
sitePageNames.push(new Array(GALLERY_PAGE, "mitigation.html", "Stopping the Mercury's Rise"));
sitePageNames.push(new Array(VIDEO_PAGE, "videos.html", "Videos"));
sitePageNames.push(new Array(STANDARD_PAGE, "book.html", "<em>Six Degrees</em> Book"));
sitePageNames.push(new Array(STANDARD_PAGE, "call2recycle/index.html", "Recycling"));

/* sitePageNames.push(new Array(STANDARD_PAGE, "http://nakedscience.kp10.net/", "Sweepstakes")); */


var episodePageNames = new Array();
episodePageNames.push("episodes.html");
episodePageNames.push("episodes_best.html");

var videoPageNames = new Array();
videoPageNames.push("videos.html");
videoPageNames.push("videos_1.html");
videoPageNames.push("videos_2.html");
videoPageNames.push("videos_3.html");
videoPageNames.push("videos_4.html");
videoPageNames.push("videos_5.html");
videoPageNames.push("videos_6.html");
videoPageNames.push("videos_7.html");
videoPageNames.push("videos_8.html");

var galleryPageNames = new Array();
galleryPageNames.push("mitigation.html");
galleryPageNames.push("mitigation_1.html");
galleryPageNames.push("mitigation_2.html");
galleryPageNames.push("mitigation_3.html");
galleryPageNames.push("mitigation_4.html");
galleryPageNames.push("mitigation_5.html");
galleryPageNames.push("mitigation_6.html");
galleryPageNames.push("mitigation_7.html");

var flashPlayer;
function detectFlashPlayer(){
	if(navigator.appName.indexOf("Microsoft") != -1){
		flashPlayer = window.sixDegrees;
	}else{
		flashPlayer = window.document.sixDegrees;
	}
}

function launchInteractive(param){
	var windowWidth = 961;
	var windowHeight = 535;
	var xPos = (screen.width - windowWidth)/2;
	var yPos = (screen.height - windowHeight)/2;
	var windowOptions = "toolbar=0,menubar=0,location=0,directories=0,status=false,scrollbars=0,resizable=0,copyhistory=0,width=" + windowWidth + ",height=" + windowHeight + ",top=" + yPos + ",left=" + xPos  +",screeny=" + yPos + ",screenx=" + xPos;
	var windowName = "SixDegreesInteractive";
	var pageName = "interactive.html";
	if (param == "from_flash"){
		pageName += "?nav=NGC-sixdegrees-splash";
	}
	if (document.location.toString().indexOf("comcast") != -1){
		pageName = "../" + pageName;
		if (param == "from_flash"){
			pageName += "-Comcast";
		}
	}
	remote = window.open(pageName, windowName, windowOptions);
	if(param != "from_flash" && document.location.toString().indexOf("index") != -1){
		flashPlayer.launch();
	}
}

// Writes out site-wide global sub navigation.  Looks to name of 
// current page and compares to the pageNames array to determine what 
// the previous and next pages will be.
function writeGlobalSubnav(){
	var pageName = getPageName(true);
	var output = '';

	output += '<a href="index.html" class="mainNav">Main</a>';

	var totalPages = sitePageNames.length;
	for (i=0; i < totalPages; i++){
		output += '<span class="mainNavDivider">|</span>';
		
		// CASE: Page names don't match.  However, if this
		// is a subpage of the section, don't make the nav
		// item a link.
		if (sitePageNames[i][1] != pageName){
			if (sitePageNames[i][0] == STANDARD_PAGE){
				output += '<a href="' + sitePageNames[i][1] + '" class="mainNav">' + sitePageNames[i][2] + '</a>';
			} else {
				if (sitePageNames[i][0] == EPISODE_PAGE){
					var isSubPage = getIsSubPage(pageName, episodePageNames);
				} else if (sitePageNames[i][0] == VIDEO_PAGE){
					var isSubPage = getIsSubPage(pageName, videoPageNames);
				} else if (sitePageNames[i][0] == GALLERY_PAGE){
					var isSubPage = getIsSubPage(pageName, galleryPageNames);
				}
				if (isSubPage){
					output += '<span class="mainNavCurrentSection">' + sitePageNames[i][2] + '</span>';
				} else {
					output += '<a href="' + sitePageNames[i][1] + '" class="mainNav">' + sitePageNames[i][2] + '</a>';
				}			
			}
			
		} else {
			output += '<span class="mainNavCurrentSection">' + sitePageNames[i][2] + '</span>';
		}
	}

	document.write(output);
}

// Determines whether or not the current page is part
// of a given group of pages.  Returns true or false.
function getIsSubPage(currentPageName, pageNames){
	var isSubPage = false;
	var totalSubPages = pageNames.length;
	for (var i=0; i < totalSubPages; i++){
		if (currentPageName == pageNames[i]){
			return true;
		}
	}
	return false;
}

// Writes out Photo Gallery page-to-page navigation.  Looks to 
// name of current page and compares to the pageNames array to
// determine what the previous and next pages will be.
function writeGalleryNav(){
	var pageName = getPageName(true);

	var totalPages = galleryPageNames.length;
	for (i=0; i < totalPages; i++){
		if (galleryPageNames[i] == pageName){
			pageID = i
			pageNumber = pageID + 1;
		}
	}
	if (pageNumber > 1 && pageNumber < totalPages){
		var output = '<a href="' + galleryPageNames[pageID-1] + '#photo" class="galleryLink">&#60;&#60;</a><span class="galleryPageNumbers"> ' + pageNumber + ' of ' + totalPages + ' </span><a href="' + galleryPageNames[pageID+1] + '#photo" class="galleryLink">&#62;&#62;</a>';
	} else if (pageNumber == 1){
		var output = '<span class="galleryPageNumbers">1 of ' + totalPages + ' </span><a href="' + galleryPageNames[pageID+1] + '#photo" class="galleryLink">&#62;&#62;</a>';
	} else if (pageNumber == totalPages){
		var output = '<a href="' + galleryPageNames[pageID-1] + '#photo" class="galleryLink">&#60;&#60;</a><span class="galleryPageNumbers"> ' + totalPages + ' of ' + totalPages + '</span>';
	}
	
	document.write(output);
}


// Determine name of current HTML page, sans the anchor 
// name if it exists.  The location prop has to be converted
// to a string, otherwise, calling lastIndexOf won't work.
function getPageName(stripSearchStr){
	var pageName = document.location.toString();
	var pageName = pageName.substr(pageName.lastIndexOf("/")+1);
	if (pageName.indexOf("#") != -1){
		pageName = pageName.substr(0, pageName.indexOf("#"));
	}
	if (stripSearchStr){
		if (pageName.indexOf("?") != -1){
			pageName = pageName.substr(0, pageName.indexOf("?"));
		}
	}
	return pageName;
}
