// JavaScript Document
var int_Capacity;
var int_Current;
var str_path;
var array_description;
var bool_vertical;

function button_down (int_direction) {
	if( int_direction == 0 ) { //back
		document.getElementById("backbutton").src = "images/adventures/back_down.gif";
	}
	else {
		document.getElementById("forwardbutton").src = "images/adventures/forward_down.gif";
	}
}

function button_up (int_direction) {
	if( int_direction == 0 ) { //back
		document.getElementById("backbutton").src = "images/adventures/back.gif";
	}
	else {
		document.getElementById("forwardbutton").src = "images/adventures/forward.gif";
	}
}

function album (int_x, array, path, vertical) { //int_Capacity will be assigned by int_x
	int_Current = 1;
	int_Capacity = int_x;
	array_description = array;
	str_path = path;
	bool_vertical = vertical;

	if( bool_vertical && reverse_dimensions[0] == 1 ) { //If first picture is vertical, it will always be assigned to index 0; //512 x 384
		document.write("<p align='center'><img id='thepicture' src=" + str_path + int_Current + ".jpg width='480' height='640' /></p>");
	}
	else {
		document.write("<p align='center'><img id='thepicture' src=" + str_path + int_Current + ".jpg width='640' height='480' /></p>");
	}
	document.write("<form class='form_album'>");
	document.write("<img id='backbutton' src='images/adventures/back.gif' height='20' width='20' onmousedown='button_down(0); return false;' onmouseup='button_up(0); return false;' onclick='goback(); return false;'/>");
	document.write("<select id='optionlist' onChange='changeImage(); return false;'>");

	for(var int_x = 1; int_x <= int_Capacity; int_x++) {
		document.write("<option value='" + int_x + ".jpg'>Slide " + int_x + "</option>");
	}

	document.write("</select>");
	document.write("<img id='forwardbutton' src='images/adventures/forward.gif' height='20' width='20' onmousedown='button_down(1); return false;' onmouseup='button_up(1); return false;' onclick='next(); return false;'>");
	document.write("</form>");
	document.write("<p id='slide_description'>" + array_description[0] + "</p>");

	var list = document.getElementById("optionlist");
	list.options[0].selected = true;
}

function changeImage() {
	var list = document.getElementById("optionlist");
	document.getElementById("thepicture").src = str_path + list.options[list.selectedIndex].value;

	var regexp = /[0-9]+/;
	var mymatch = regexp.exec(list.options[list.selectedIndex].value);
	var str_index = mymatch[0];
	int_Current = parseInt(str_index);

	change_dimensions();
	document.getElementById("slide_description").innerHTML = array_description[int_Current - 1];
}


function goback () {
	if (int_Current == 1) {
		//Do Nothing
	}
	else {
		int_Current--;
		document.getElementById("thepicture").src = str_path + int_Current + ".jpg";
		document.getElementById("slide_description").innerHTML = array_description[int_Current - 1];
		var list = document.getElementById("optionlist");
		list.options[int_Current - 1].selected = true;

		change_dimensions();
    }
}

function next () {
	if (int_Current == int_Capacity) {
		//Do Nothing
	}
	else {
		int_Current++;
		document.getElementById("thepicture").src = str_path + int_Current + ".jpg";
		document.getElementById("slide_description").innerHTML = array_description[int_Current - 1];
		var list = document.getElementById("optionlist");
		list.options[int_Current - 1].selected = true;

		change_dimensions();
	}
}

function change_dimensions() {
    //Check if current picture is vertical
	if (bool_vertical) {
		var bool_match = false;
		for( var int_x = 0; int_x < reverse_dimensions.length; int_x++) {
			if(int_Current == reverse_dimensions[int_x]) {//Our current picture is a vertical picture
				bool_match = true;
				break;
			}
		}

		var pic_object = document.getElementById("thepicture");
		var int_width = pic_object.getAttribute("width");
		var int_height = pic_object.getAttribute("height");
		
		if (int_width < int_height && bool_match) { 
			//Do nothing.  Previous picture was vertical and we have a current vertical picture.  Therefore, we don't need to change dimensions
		}
		else if (int_width < int_height && bool_match == 0) {
		    //Previous picture was vertical and the current picture is horizontal.  Need to change dimensions
			var temp = int_width;
			int_width = int_height;
			int_height = temp;

			pic_object.setAttribute("width", int_width);
    		pic_object.setAttribute("height", int_height);
		}
		else if (int_width > int_height && bool_match) {
			//Prevoius picture was horizontal and our current picture is vertical.  Need to change dimensions	
			var temp = int_width;
			int_width = int_height;
			int_height = temp;

			pic_object.setAttribute("width", int_width);
			pic_object.setAttribute("height", int_height);
		}
		
	}
}


