// JavaScript Document
// ---------- Image handler  ------------
function GalleryHandler(podID, imagesString, baseFilePath, size){
	var currentImage = 0;
	var previousImage = 0;
	var images = imagesString.split(",");
	//this.imageDiv = document.getElementById(this.imageID);
	
	// grabs the order of the imageIDs for this page, which is used for previous and next buttons
	function init(){
		currentImage = 0;
		
		// make sure the images are numbers not strings
		for(var i = 0; i < images.length; i++){
			images[i] = parseInt(images[i]);
		}
		
		// build the next previous buttons if the div exists
		if($("#nextPrevious" + podID)){
			var nextPrevious = document.getElementById('nextPrevious' + podID); //$("#nextPrevious" + podID);
			// store the contents of the list item
			var list = document.createElement('ul');
			var l1 = document.createElement('li');
			l1.className = "prev";
			var l2 = document.createElement('li');
			l2.className = "next";
			var a1 = document.createElement('a');
			var a2 = document.createElement('a');
			a1.innerHTML = "<img src='/includes/modules/images/images/prev-on.png' alt='Prev' />";
			a2.innerHTML = "<img src='/includes/modules/images/images/next-on.png' alt='Next' />";

			a1.href = 'javascript:void(0);';
			a1.onclick = previous_image;
			a2.href = 'javascript:void(0);';
			a2.onclick = next_image;

			list.appendChild(l1);
			l1.appendChild(a1);
			list.appendChild(l2);
			l2.appendChild(a2);
			nextPrevious.appendChild(list);
		}
				
		// add the links to the thumbnails of the exist
		if($("#galleryList" + podID)){
			var kids = $("#galleryList" + podID + ' > li');

			for(i = 0; i < kids.length; i++){
				if(kids[i].nodeName == "LI"){
					var linkItems = kids[i].childNodes;
					for(var j = 0; j < linkItems.length; j++){
						if(linkItems[j].nodeName == "A"){
							// set the link
							var link = linkItems[j];
							//alert("linkItems[j].id = " + linkItems[j].id);
							linkItems[j].onclick = show_gallery_image;
						}
					}
				}			
			}
		}	
		
		show_gallery_image2(images[0]);
	}
	
	// change the color of the current next previous button
	function change_next_previous(id){
		// reset all the buttons
		/*
		for(i = 0; i < this.ar_images.length; i++){
			document.getElementById("npa" + this.ar_images[i]).className = "";	
		}
		//alert("changing to current style");
		// change the current button
		document.getElementById("npa" + id).className = "current";
		*/
		set_current(id);
	}
		
	// deciphers the next image, and displays it
	function next_image(){
		//alert("currentImage=" + currentImage);
		var nImage = currentImage + 1;
		//alert("next_image" + nImage);
		nImage = (nImage < images.length ? nImage : 0); 
		currentImage = nImage;
		show_gallery_image2(images[nImage]);
	}
	
	// decipher the previous image and shows it
	function previous_image(){
		//alert("currentImage" + currentImage);
		var nImage = currentImage - 1;
		
		nImage = (nImage >= 0 ? nImage : images.length - 1);
		//alert("next_image" + nImage);
		currentImage = nImage;
		show_gallery_image2(images[nImage]);
	}
	
	function set_current(id){
		for(i = 0; i < images.length; i++){
			if(images[i] == id){
				currentImage = i;
				break;
			}
		}
	}	
	
	function show_gallery_image(e){		
		//setTimeout("gallery" + podID + ".show_gallery_image2(" + imageID + ");", 100);
		var parts = this.id.split("-");
		//alert("parts[1] - " + parts[1]);
		show_gallery_image2(parts[1]);
	}
	
	function show_gallery_image2(imageID){
		// get the information for the image that includes the file and artist info
		var theImage = $("#galleryThumb" + imageID);
		var theFile = theImage.attr("filename");

		var description = theImage.attr("description");
	
		var imageString = "<img src='" + baseFilePath + "/" + size + "/" + theFile + "' />";

		// populate current image with the large image
		var img = new Image();
		img.onload = function(){ $("#currentGalleryImage" + podID).html(imageString); }
		img.src = baseFilePath + "/" + size +"/" + theFile;

		// format the caption, then print it out
		var caption = "";
		if(description != "" && description != null){ caption += "<p>" + description + "</p>"; }
		if($("#caption" + podID)){
			$("#caption" + podID).html(caption);
		}
		
		// make sure the current image is visible
		$("#currentGalleryImage" + podID).css('display', 'block');
		change_next_previous(imageID);
	}
	
	init();
}
