// JavaScript Document

function RandCSS() {
a = Math.round(Math.random() * 3 + 1)
if (a > 3) { RandCSS() } else { WriteCSS() }
}

function WriteCSS() {
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"style" + a + ".css\">")
}

RandCSS();

function newImage(arg) {
	return;
}

// Internet Exploder is a pain. If it did what other browsers did, we could
// just redirect to the download here, but nooo... Microsoft has to prevent
// downloads and perform a page refresh to authorize them. Here comes some
// JavaScript voodoo...
function finished(tour) {
	// Take the current URL
	var url = document.location.href;

	// Strip off any sort of query string...
	url = url.replace(/[#?].*?$/, '');

	// Append '?download' to it (the tour stuff is a moot point right now
	// since we can use the current URL).
	url += '?download'; //+(tour != undefined ? '&tour='+tour : '');

	// And redirect...
	document.location.href = url;

	// Simple code for non-MS browsers:
	//document.location.href = '/regions/FPDI/download.php'+(tour != undefined ? '?tour='+tour : '');
}

// Function stolen from: http://phpjs.org/functions/basename:360
function basename (path, suffix) {
    var b = path.replace(/^.*[\/\\]/g, '');
     if (typeof(suffix) == 'string' && b.substr(b.length - suffix.length) == suffix) {
        b = b.substr(0, b.length - suffix.length);
    }
 
    return b;
}

// Look for a download flag...
if (document.location.href.indexOf('?download') != -1) {
	// Grab the current URL and use it for the PDF name...
	var url = document.location.href;
	url = url.replace(/\.[^\.\/]+\?.*/, '');

	// Clobber the window onLoad event. We redirect after the page is
	// loaded because if we do it before, the page will stop loading
	// somewhere in the middle.
	window.onload = function() {
		document.location.href = '/regions/FPDI/download.php?tour='+basename(url)
	};
}

