// This file contains scripts for controlling navigation and positioning of images on the gallery page

// global variables
var pic_info_array = new Array();   // contains image information (path, width, height, description)
var pic_array = new Array();   // holds the image objects
var pic_update = new Array(); // contains info about whether image source has been updated for pic_array
var pic_index=0;
var max_pic_count = 25;
var pic_text= "";
var i=0; 
var div_width=607;
var div_height=474;
var err_img = new Image(300,100);


//-----------------------------------------------------------
// Image Control Functions
//
// calculate the offset of an image in the div for horizontal centering
function center(index){
		return (div_width - pic_info_array[index].width)/2;
}

// calculate the offset of an image in the div for vertical centering
function top(index){
		return (div_height - pic_info_array[index].height)/2;
}

// update the gallery pic when thumb is clicked
function updatePic(){
	
var source = "";

	i=pic_index;

	if(pic_index == 0){ return};

	$('img#gallery_pic')
	.hide()
	.css("width", pic_info_array[pic_index].width)
	.css("height", pic_info_array[pic_index].height)
	.css("margin-left", center(pic_index))
	.css("margin-top", top(pic_index))
	.attr('src',pic_array[pic_index].src)
	.fadeIn("slow");
}

// update the gallery text for the indexed image
function updatePicText(){

var desc_src = "";

	//if(pic_index >= max_pic_count) return;
	
	desc_src = pic_info_array[pic_index].desc;

	$('#tframe').attr('src',desc_src);
}



//------------------------------------------------------

// initialization and event functions
$(document).ready(function(){
	
	// load the info array (used to create images)
	loadPics();	
	
	// load the image to display if there is a load error
	//err_img.src="images/Image_Load_Error.jpg";
	
	// create the first image, which is just a blank image)
	pic_array[0] = new Image(pic_info_array[0].width, pic_info_array[0].height);
	pic_array[0].src = pic_info_array[0].path;
	pic_array[0].alt = pic_info_array[0].desc;
	
	// load the rest of the gallery images
	for(i=1; i<max_pic_count; i++)
	{
		if(pic_update[i] == 0)
		{	
			pic_array[i] = new Image(pic_info_array[i].width, pic_info_array[i].height);
			pic_array[i].src = pic_info_array[i].path;
			pic_array[i].alt = pic_info_array[i].desc;	
			pic_update[i] = 1;
		}	
	}
	
	
	// event handler for showing next pic (">")	
	$("a#a2").click(function(){
		
		if(pic_index<max_pic_count-1)
		{
			pic_index = pic_index + 1;
			if(pic_index == 1)
			{
				$("div#images-right-div").hide();
				$("div#gallery-div").show();
			}
			updatePic();
			updatePicText();
		}
		
	 } // end click function
	); // end click
	
	
	// event handler for showing prev pic ("<")	
	$("a#a1").click(function(){
						
		if(pic_index == 1)
		{
			$("div#images-right-div").show();
			$("div#gallery-div").hide();
			pic_index = 0;
			updatePicText();
		}
								  
		if(pic_index>0)
		{
			pic_index=pic_index-1;
			updatePic();
			updatePicText();
		} 		
	 } // end click function
	); // end click
	
	}); // end ready function






