/**
	Loads and returns the file at the given url.
	Throws an exception in case of error.
 */	
function loadFile(url)
{
	try {
		// create the new request
		var request = null;
		if ( window.ActiveXObject != null ) {
			// the browser is Internet explorer 
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if ( window.XMLHttpRequest ) {
			// the browser is Mozilla
			request = new XMLHttpRequest();
		}
		else {
			// unable to get the HTTP request object
			throw new Error("Unable to get an HTTP request object");
		}

		// start the request
		request.open("GET", url, false);
		request.setRequestHeader("Cache-Control", "no-cache");
		request.send(null);

		// validate the status
		if ( request.status != 200 ) {
			throw new Error("Unable to load the URL. The request status is " + request.status);
		}

		// return the response text
		return request.responseText;
	}
	catch ( exception ) {
		throw new Error("Unable to load the URL \"" + url + "\" due to the following exception: \"" + exception.message + "\"");
	}	
}

function loadPictureInfo(url)
{
	var pictures = new Array();
	// load the picture info file
	var ssFile = loadFile(url);
	// split the result
	var lines = ssFile.split('\n');
	// get the picture base url
	var pictureBaseUrl = lines[0];
	pictureBaseUrl = pictureBaseUrl.substr(0, pictureBaseUrl.length-1);
	// get the small picture base url
	var smallBaseUrl = lines[1];
	smallBaseUrl = smallBaseUrl.substr(0, smallBaseUrl.length-1);
	// get the big picture base url
	var bigBaseUrl = lines[2];
	bigBaseUrl = bigBaseUrl.substr(0, bigBaseUrl.length-1);

	// parse the picture info
	arrayIndex = 0;
	for ( i = 3; i < lines.length; i += 2 ) {
		// get the picture name
		pictureName = lines[i];
		pictureName = pictureName.substr(0, pictureName.length-1);
		if ( pictureName == "" ) break;

		// get the caption
		caption = "";
		if ( i+1 < lines.length ) {
			caption = lines[i+1];
			if ( caption.charCodeAt(caption.length-1) == 13 ) {
				caption = caption.substr(0, caption.length-1);
			}
		}

		pictures[arrayIndex] = new Picture(pictureBaseUrl+pictureName, smallBaseUrl+pictureName, bigBaseUrl+pictureName, caption);
		arrayIndex++;
	}

	return pictures;
}

function showError(exception)
{
	settings="toolbar=no,location=no,directories=no,status=not,menubar=no,scrollbars=yes,resizable=no";
	theWindown=window.open('thebananatours.com','slides',settings);

	with ( theWindown.document ) {
 		writeln('<!doctype html public "-//w3c//dtd html 4.0 transitional//en">');
		writeln('<html>');
		writeln('	<head>');
		writeln('		<title>thebananatours.com</title>');
		writeln('		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>');
 		writeln('	</head>');
		writeln('	<body onload="self.focus()">');
		writeln('	<body>');
		writeln('		The following error occurred: <br><br>');
		writeln('			<b>exception</b>:  ' + exception + '<br>');
		writeln('			<b>message</b>:    ' + exception.message + '<br>');
		writeln('			<b>fileName</b>:   ' + exception.fileName + '<br>');
		writeln('			<b>lineNumber</b>: ' + exception.lineNumber + '<br>');
		writeln('	</body>');
		writeln('</html>');
		close();		
	}

	theWindown.focus();
}

function show(url, index)
{
	try {
		// load the pictures info
		var picturesInfo = loadPictureInfo(url);
		init(picturesInfo, parseInt(index));
	}
	catch (exception) {
		showError(exception);
	}
}

function getPictureIndex()
{
	var searchArray = getUrlParameters();
	var index = searchArray['picture'];	
	if ( index == null ) {
		index = '0';
	}
	return index;	
}

function getUrlParameters()
{
	var searchStr = location.search;
	var searchArray = new Array();

	while ( searchStr != '' ) {
		var name, value;
		// strip off leading ? or &
		if ( (searchStr.charAt(0) == '?' ) || ( searchStr.charAt(0) == '&' ) ) 
			searchStr = searchStr.substring(1,searchStr.length);
		// find name
		name = searchStr.substring(0,searchStr.indexOf('='));
		// find value
		if ( searchStr.indexOf('&')!= -1 ) 
			value = searchStr.substring(searchStr.indexOf('=')+1,searchStr.indexOf('&'));
		else 
			value = searchStr.substring(searchStr.indexOf('=')+1,searchStr.length);
		// add pair to an associative array
		searchArray[name] = value;
		// cut first pair from string
		if (searchStr.indexOf('&')!=-1) searchStr =  searchStr.substring(searchStr.indexOf('&')+1,searchStr.length);
		else searchStr = '';
	}

	return searchArray;
}


function showPicture(url, name)
{
	alert('The web page is trying to invoke an obsolete function. Please refresh your browser and try again. If the problem persist, please report the problem by dropping an email to postmaster@thebananatours.com.\n\n La pagina web sta cercando di invocare una funzione non piu\' dispobinibile perche vecchia\'. Fai un refresh della pagina e prova di nuovo. Se il problema persiste, ti prego di segnalarlo via email all\'indirizzo postmaster@thebananatours.com.');
}


function Picture(picture, small, big, caption) 
{
	this.picture = picture;
	this.small = small;
	this.big = big;
	this.caption = caption;
}


