var CurrentGallery = 0;

// Constructor
function photo(image, caption, menu, location) {
		this.image = image;
		this.caption = caption;
		this.menu = menu;
    this.location = location; // URL Path
    this.LoadPhoto = LoadPhoto;
}

// Initializes the starting page
function SetUpPage(){
    for (i = 0; i < GalleryName.length; i++) { // add new items
			  document.main.Make.options[i] = new Option(GalleryName[i]);
		} 		 
 		document.main.Make.options[0].selected = true; // select first item 
    fillMenu(document.main.Choice, 0)
    document.write("<a href= javascript:popUp(\'" + gallery0[0].location + gallery0[0].image+ "\') id=\"LinkName\"><img border=\"0\" src=\"" + gallery0[0].location + gallery0[0].image +  "\" id=\"PhotoName\" height=\"300\"></a>");
		document.write("<div id=PhotoCaption class=\"body_text\">");
    document.write(gallery0[0].caption);
}

// Responds to the form option selections
function formHandler(SelectedIndex){
		tempArray2 = Allgallery[CurrentGallery];
		var newPhoto = tempArray2[SelectedIndex].location + tempArray2[SelectedIndex].image;
		var newLink = "javascript:popUp(\'" + newPhoto + "\')";
		tempArray2[SelectedIndex].LoadPhoto("PhotoName", newPhoto, "PhotoCaption", tempArray2[SelectedIndex].caption, "LinkName", newLink);
}

// Loads a given photo and caption (picture and caption) to given locations (pictureLocation and captionLocation) 
function LoadPhoto(pictureLocation, picture, captionLocation, caption, linkLocation, mylink){
  document.getElementById(pictureLocation).src = picture;
  document.getElementById(linkLocation).href = mylink;
  document.getElementById(captionLocation).innerHTML=caption;
}

// Fills the second menu with it's choices
function fillMenu(selectCtrl, GalleryIndex){
		var i;
		tempArray = Allgallery[GalleryIndex];
		CurrentGallery = GalleryIndex;
		
    selectCtrl.options.length = 0; // empty existing items 
		for (i = 0; i < tempArray.length; i++) { // add new items
			 	selectCtrl.options[i] = new Option(tempArray[i].menu);
		} 		 
		selectCtrl.options[0].selected = true; // select first item 
}

// Opens a popup window with the desired data
function popUp(URL) {
		day = new Date();
		id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=600,height=400,left = 212,top = 84');");
}
