// JavaScript Document

$(document).ready(function(){

var pArray = new Array();
var photoFolder='/images/golf_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 to go to specific image.
var image_id=0;

if (qstring.indexOf('image=')!=-1) {
image_id=qstring.substring(6, qstring.length);
image_id=parseInt(image_id);
if (isNaN(image_id)) {image_id=0;}
}


$.ajax({
type: "GET",
url: "/golf/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=0;}
$("#photo_large").attr("src", photoFolder + photoArray[image_id]);
$("#photo_large").attr("usemap", "#f9Map");
//$("#summary").text(captionArray[image_id]);
$("#numbers a").css('color','#000000');
$("#numbers a:eq(" + (image_id) + ")").css('color','#993300');
}
}); //close $.ajax


$('#numbers a').click(function(){
	//next=parseInt($(this).text())-1;
	next=$('#numbers a').index(this);
	$("#photo_large").fadeOut("normal", loadNext);
	return false;
	
});


$('.holemap').click(function(){
	next=parseInt($('.holemap').index(this))+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;
}); */

 
$("#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;
});

function loadNext(){
if (next==0){
$("#photo_large").attr("useMap", "#f9Map");
	$("#f9_nav").attr("src", "/images/golf_nav/nav_front_nine_over.gif");
} else if (next==10) {
$("#photo_large").attr("useMap", "#b9Map");
	$("#b9_nav").attr("src", "/images/golf_nav/nav_back_nine_over.gif");
}else{
	$("#photo_large").removeAttr("useMap");
	$("#b9_nav").attr("src", "/images/golf_nav/nav_back_nine.gif");
	$("#f9_nav").attr("src", "/images/golf_nav/nav_front_nine.gif");
}

$("#numbers a").css('color','#000000');
$("#numbers a:eq(" + (next) + ")").css('color','#993300');
	$("#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]);
}

$('#f9_nav').hover(function(){
	var temp=$(this).attr('src')
	if (temp.indexOf('_over')==-1) {
	temp=temp.replace('.gif', '_over.gif');
	$(this).attr('src', temp);
	}
	}, 
	function() {
	var temp=$(this).attr('src')
	if (temp.indexOf('_over')!=-1) {
	temp=temp.replace('_over.gif', '.gif');
	$(this).attr('src', temp);
	}
	});

$('#b9_nav').hover(function(){
	var temp=$(this).attr('src')
	if (temp.indexOf('_over')==-1) {
	temp=temp.replace('.gif', '_over.gif');
	$(this).attr('src', temp);
	}
	}, 
	function() {
	var temp=$(this).attr('src')
	if (temp.indexOf('_over')!=-1) {
	temp=temp.replace('_over.gif', '.gif');
	$(this).attr('src', temp);
	}
	});


});