/* 	 
	Author:  John Bertola
	Date:  January 15, 2008  
*/

var current_row = 0; //Keeps track of the current row of thumbnails
var title_photo = true; //True if the title_photo is being displayed, falsed otherwise
var SUFFIX_LENGTH = 2; //Can be set to 2 to support filenames with suffixes 01-99 or 3 to support 001-999
var IMG_PATH = "images/image"; //The relative path of the images folder, include filename prefix (i.e., filenames "image01", prefix is "image")
var IMG_EXT = ".jpg"; //File type of images (i.e. .jpg, .gif)

/* BEGIN thumbnail functions */

//Returns an array of <div> elements containing the thumbnails, each element representing one "row"
function get_rows(){
	var container = document.getElementById("photo_cont");
	var rows = container.getElementsByTagName("div");
	return rows;
}

//Displays a given row of thumbnails, with 0 referring to the first row
function show_row(num){
	var rows = get_rows();
	hide_all();
	rows[num].style.visibility = "visible";
}

//Hides all rows of thumbnails
function hide_all(){
	var rows = get_rows();
	for (i=0; i < rows.length; i++)
		rows[i].style.visibility = "hidden";
}

//Displays the next row of thumbnails
function down(){
	var size = get_rows().length;
	if (current_row < (size-1))
	{
		current_row = current_row + 1;
		update();
	}
}		

//Displays the previous row of thumbnails
function up(){
	if (current_row > 0)
	{
		current_row = current_row - 1;
		update();
	}
}

//Updates the status (enabled/disabled) of the up/down control arrows
function set_arrows(){
	var up = document.getElementById("up");
	var down = document.getElementById("down");
	if (current_row == 0)
		up.style.backgroundImage = "url(../resources/up_disabled.gif)";
	else
		up.style.backgroundImage = "url(../resources/up_enabled.gif)";
	if (current_row == (get_rows().length - 1))
		down.style.backgroundImage = "url(../resources/down_disabled.gif)";
	else
		down.style.backgroundImage = "url(../resources/down_enabled.gif)";
}

//Returns a particular thumbnail 
function get_photo(num){
	var container = document.getElementById("photo_cont");
	var photos = container.getElementsByTagName("a");
	return photos[num];
}

//Displays the number of the current row of thumbnails that is visible
function print_row(){
	var para = document.getElementById("row");
	var row_num = current_row + 1;
	var max_row = get_rows().length;
	var text = "Group " + (current_row + 1) + " of " + max_row;
	para.innerHTML = text;
}

//Causes all thumbnails to be reset to normal apperance
function unselect_all(){
	for (i=0; i < count_photos(); i++)
		get_photo(i).style.border = "1px solid #999999";
}

//Changes the display of a thumbnail to indicated it is selected, 0 referring to the first photo
function select_thumb(num)
{
	get_photo(num).style.borderBottom = "3px solid #00467F";
}

/* END thumbnail functions */

/* BEGIN photo functions */				

//Returns the total number of photos in the album
function count_photos(){
	var container = document.getElementById("photo_cont");
	var photos = container.getElementsByTagName("a");
	return photos.length;
}

//Causes the next photo to be displayed 
function next_photo(){
	if (current_photo() < count_photos())
	{
		swap(current_photo()+1);
		current_row = parseInt((current_photo() - 1) / 12);
		update();
	}
}

//Causes the previous photo to be displayed
function prev_photo(){
	if (current_photo() > 1)
	{
		swap(current_photo()-1);
		current_row = parseInt((current_photo() - 1) / 12);	
		update();
	}
}

//Returns the number of the current photo being displayed, 1 referring to the first photo (0 refers to the title photo)
function current_photo(){
	if (title_photo)
		return 0;
		
	var src = document.getElementById("frame").src;
	src = src.substr(src.length - (SUFFIX_LENGTH + 4));
	return parseInt(src, 10);
}

//Updates the status (enabled/disabled) of the next/previous control arrows
function set_side_arrows(){
	var left = document.getElementById("left");
	var right = document.getElementById("right");
	if (current_photo() <= 1)
		left.style.backgroundImage = "url(../resources/left_disabled.gif)";
	else
		left.style.backgroundImage = "url(../resources/left_enabled.gif)";
	if (current_photo() == count_photos())
		right.style.backgroundImage = "url(../resources/right_disabled.gif)";
	else
		right.style.backgroundImage = "url(../resources/right_enabled.gif)";
}

/* END photo functions */

/* BEGIN general functions */

//Displays the picture passed in by the parameter, 1 referring to the first picture
function swap(num){
	if (title_photo)
		title_photo = false;
		
	document.getElementById("frame").src = IMG_PATH + num_to_suffix(num) + IMG_EXT;
	unselect_all();
	select_thumb(current_photo()-1);
	set_side_arrows();
}

//Converts a number to a suffix, adding zeroes where necessary
function num_to_suffix(num)
{
	if (SUFFIX_LENGTH == 3)
	{
		if (num < 10) return ("00" + num);
		if (num < 100) return ("0" + num);
		return num;
	}
	if (SUFFIX_LENGTH == 2)
	{
		if (num < 10) return ("0" + num);
		return num;
	}
}

//Updates necessary features (arrows, row number, row display)
function update(){
	show_row(current_row);
	set_arrows();
	print_row();
	set_side_arrows();
}

/* END general functions */
