// JavaScript Slide Show developed by Anthony Falcone for BudgetTravel.com
// version 2.0 | December 20, 2007

// This function is for opening links in the parent window.

function parentCheck(passedURL) {
	// This tests that it can find the parent. If not, it will open a new window.
	if (window.opener) {
		window.opener.location = passedURL;
		window.opener.focus();
	}
	else {
		window.open(passedURL);
	}
}

// This function swaps the picture and caption.

function swapPicture(whichPic) {
	var picHTML = "";
	var nextPic;
	// This checks to see if we're at the end of the show. If we are, clicking on the photo takes you to the final page.
	if ((whichPic + 1) == SSLength) {
		nextPic = 99;
	}
	else {
		nextPic = whichPic + 1;
	}
	picHTML += "<a href=\"index.html?jumpToPic=" + nextPic +"\"><img src=" + SSPhoto[whichPic] + " class=border" + SSBorder[whichPic] + " alt=\"Click the image to move to the next slide\" \/><\/a>";
	document.getElementById('slidePhoto').innerHTML = picHTML;
	
	var captionHTML = "";
	// This checks to see if their is a credit for the specific photo. If not, it won't put the parentheses in.
	if (SSPhotoCredit[whichPic].length == 0) {
		captionHTML += SSPhotoCaption[whichPic];
	}
	else {
		captionHTML += SSPhotoCaption[whichPic] + " (" + SSPhotoCredit[whichPic] + ")";
	}
	document.getElementById('slideCaption').innerHTML = captionHTML;
}

// This function builds out the navigation links

function buildNavigation(thisPic) {
	var navHTML = "";
		navHTML += "<ul>";
	var prevPic = thisPic - 1;
	var nextPic = thisPic + 1;
	if (thisPic == 0) {
		navHTML += "<li class=button><a><img src=../images_v2/nav_back_inactive.gif alt=\"There are no previous slides\" border=0 \/><\/a><\/li>";
	}
	else {
		navHTML += "<li class=button><a href=\"index.html?jumpToPic=" + prevPic + "\"><img src=../images_v2/nav_back.gif alt=\"View previous slide\" border=0 \/><\/a><\/li>";
	}
	for (i=0; i<SSLength; i++) {
		if (i==thisPic) {
			navHTML += "<li class=active><a>" + eval(i + 1) + "<\/a><\/li>";
		}
		else {
			navHTML += "<li><a href=\"index.html?jumpToPic=" + i + "\">" + eval(i + 1) + "<\/a><\/li>";
		}
	}
	if (nextPic == SSLength) {
		navHTML += "<li class=button><a href=\"index.html?jumpToPic=99\"><img src=../images_v2/nav_next.gif alt=\"View next slide\" border=0 \/><\/a><\/li>";
	}
	else {
		navHTML += "<li class=button><a href=\"index.html?jumpToPic=" + nextPic + "\"><img src=../images_v2/nav_next.gif alt=\"View next slide\" border=0 \/><\/a><\/li>";
	}
		navHTML += "<\/ul>";
		
	document.getElementById('centralBodyNavContainer').innerHTML = navHTML;
}

// This is the function that builds the final page.

function endSlideShow() {
	document.getElementById('centralBodyPhotoContainer').innerHTML = finalHTML;
}

// This is the master function that actually loads the whole slide.

function setupSlide() {

	var picNumber;
	var docURL = window.document.location.href;
	var mySplitResult = docURL.split("=");

	// This tests to ensure that a valid slide number was passed in the URL; if not, it defaults to the first slide (0).
	if (mySplitResult[1]) {
		// This fixes a flaw when someone emails the slide show to a friend, and our system adds a variable to the end of the link.
		if (mySplitResult[1] == "emaillink") {
			picNumber = 0;
		}
		// If the requested slide number is higher than 99, then it is an Omniture tracking code. Default back to the first slide (0).
		else if (mySplitResult[1] > 99) {
			picNumber = 0;
		}
		else {
			picNumber = parseInt(mySplitResult[1]);
		}
	}
	else {
		picNumber = 0;
	}

	// If the slide requested is 99, then the slide show is over. This will cause it to skip to the final page.
	if (picNumber == 99) {
		endSlideShow();
	}
	else {
		swapPicture(picNumber);
		buildNavigation(picNumber);
	}
}
