// JavaScript Document

$(document).ready(function(){

var pArray = new Array();
var photoFolder='/images/tour/';
var photoArray = new Array();
var captionArray = new Array();
var qstring=location.search;
qstring=qstring.substring(1, qstring.length);

var next=0;
// look in qstring to get image= value and set default image in array.
var image_id=0;

for (var i=0; i<qstring.split('&').length; i++) {
	if (qstring.split('&')[i].indexOf('image=')!=-1) {
	image_id=qstring.split('&')[i].substring(6, qstring.split('&')[i].length);
	image_id=parseInt(image_id);
	if (isNaN(image_id)) {image_id=0;}
	}
}

//if (qstring.length>1) {
//Check query string to see if ajax should load data
$.ajax({
type: "GET",
url: "/tour/tour_data.aspx",
data: qstring,
dataType: "xml",
success: function(xml) {
$(xml).find('photo').each(function(){
var name_text = $(this).attr('name');
photoArray.push(name_text);
var caption_text = $(this).attr('caption');
captionArray.push(caption_text);
// var name_text = $(this).find('name').text() this would find any nested tags
}); //close each(
if (image_id>photoArray.length) {image_id=photoArray.length-1;}
$("#photo_large").attr("src", photoFolder + photoArray[image_id]);
$("#summary").text(captionArray[image_id]);
$("#numbers_total").text(photoArray.length);
$("#numbers").text(image_id+1);
}
}); //close $.ajax

//}

 



$("#next_button").click(function(event){
var photoSrc=$("#photo_large").attr("src");
photoSrc=decodeURI(photoSrc);
for (var i=0; i<photoArray.length; i++) {
if (photoSrc.indexOf('/' + photoArray[i])!=-1) {
 if (i==photoArray.length-1) {
 next=0;
 }else{
  next=(i+1);
 }
}
}
	$("#photo_large").fadeOut("normal", loadNext);
	return false;
});

$("#prev_button").click(function(event){
var photoSrc=$("#photo_large").attr("src");
photoSrc=decodeURI(photoSrc);
for (var i=0; i<photoArray.length; i++) {
if (photoSrc.indexOf('/' + photoArray[i])!=-1) {
 if (i==0) {
 next=(photoArray.length-1);
 }else{
  next=(i-1);
 }
}
}
	$("#photo_large").fadeOut("normal", loadNext);
	return false;
});

$('#photo_large').click(function(){
var photoSrc=$("#photo_large").attr("src");
photoSrc=decodeURI(photoSrc);
for (var i=0; i<photoArray.length; i++) {
if (photoSrc.indexOf('/' + photoArray[i])!=-1) {
 if (i==photoArray.length-1) {
 next=0;
 }else{
  next=(i+1);
 }
}
}
	$("#photo_large").fadeOut("normal", loadNext);
	return false;
});

function loadNext(){
	$("#numbers").text(next+1);
	$("#summary").text(captionArray[next]);
		$("#photo_large").load(function () {
			$(this).fadeIn("fast");
		}).error(function() {
			// notify the user that the image could not be loaded
		}).attr('src', photoFolder + photoArray[next]);
}

});