/*------------------------------------------------------------
*  Function:  newImage
*  
*  Description:
*  accepts a path to an image file. creates a new image object
*  and stuffs the src. used by the rollover framework
*  
*  Parameters:
*  arg		string	path to image file
*  
*  Return:
*  rslt		image object
*------------------------------------------------------------*/
function newImage(arg) {
	rslt = new Image();
	rslt.src = arg;
	return rslt;
}


/*------------------------------------------------------------
*  Function:  getObject
*  
*  Description:
*  accepts an object reference or a string, returns an object reference
*  
*  Parameters:
*  which	string or object	ID of HTML dom entity, or reference to an object
*  
*  Return:
*  obj		object				reference to named or passed DOM object
*------------------------------------------------------------*/
function getObject(which) {
	var obj;
	if (typeof(which) == "object") {
		obj = which;
	} else if (typeof(which) == "string") {
		obj = document.getElementById(which);
	} else {
		obj = null;
	}
	
	return obj;
}


/*=========================================================================================
	gallery thumbnail scrolling
==========================================================================================*/


// gallery variables
var galleryHorizId = "photoThumbsTable";			// id of gallery html object
var viewerHorizId = "photoThumbsViewer";			// id of div enclosing gallery html object
var galleryLeft = 0;						// Gallery offset

var galleryVertId = "photoThumbsVertTable";			// id of gallery html object
var viewerVertId = "photoThumbsVertViewer";			// id of div enclosing gallery html object
var galleryUp = 0;

var scrollDir = "none";						// Scroll flag.	 Either left, right, or none
var galleryInterval;						// Timeout interval Id
var galleryScrollIncrement = 5;				// gallery scrolling increment


// css IDs of arrow html objects
var photoArrowLeftId = "photoArrowLeft";
var photoArrowRightId = "photoArrowRight";
var photoArrowUpId = "photoArrowUp";
var photoArrowDownId = "photoArrowDown";




/*------------------------------------------------------------
*  Function:  scrollForward
*  
*  Description:
*  Starts scroll to the right
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
function scrollForward() {
	scrollDir = "right";	// Set flag
	scrollGallery();		// Now scroll
}



/*------------------------------------------------------------
*  Function:  scrollBack
*  
*  Description:
*  Starts scroll to the left
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
function scrollBack() {
	scrollDir = "left";		// Set flag
	scrollGallery();		// Now scroll
}

/*------------------------------------------------------------
*  Function:  scrollDown
*  
*  Description:
*  Starts scroll down
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
function scrollDown() {
	scrollDir = "down";		// Set flag
	scrollGallery();		// Now scroll
}

/*------------------------------------------------------------
*  Function:  scrollUp
*  
*  Description:
*  Starts scroll up
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
function scrollUp() {
	scrollDir = "up";		// Set flag
	scrollGallery();		// Now scroll
}


/*------------------------------------------------------------
*  Function:  scrollNone
*  
*  Description:
*  Stops scrolling
*  
*  Parameters:
*  none
*  
*  Return:
*  false
*------------------------------------------------------------*/
function scrollNone() {
	scrollDir = "none"; // Reset flag
	return false;
}



/*------------------------------------------------------------
*  Function:  scrollGallery
*  
*  Description:
*  does the actual work of scrolling
*  calls itself until user mouses up or gallery reaches left or right edge
*  relies on the scrollDir variable set in the above functions
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
function scrollGallery() {


	switch (scrollDir) {
		// Scroll right
		case "right":
			var gallery = getObject(galleryHorizId);
			var galleryWidth = gallery.offsetWidth;
			var viewerWidth = getObject(viewerHorizId).offsetWidth;
			//alert(galleryLeft);
			//alert(galleryWidth);
			//alert(viewerWidth);
			// Only scroll right if right edge is not aligned with right side
			if (galleryLeft > (viewerWidth - galleryWidth)) {
				galleryLeft -= galleryScrollIncrement;
				
				// check bounds
				if (galleryLeft < (viewerWidth - galleryWidth)) galleryLeft = (viewerWidth - galleryWidth);
				gallery.style.left = galleryLeft + "px";

				// Wait to see if flag is reset. Otherwise, keep scrolling
				galleryInterval = setTimeout("scrollGallery()", 10);
			} else {

				// Gallery is flush right.	Stop scrolling
				clearTimeout(galleryInterval);
			}
			break;

		// Scroll left
		case "left":
			var gallery = getObject(galleryHorizId);
			var galleryWidth = gallery.offsetWidth;
			var viewerWidth = getObject(viewerHorizId).offsetWidth;
			// Only scroll left if left edge is not aligned 0
			if (galleryLeft < 0) {
				galleryLeft += galleryScrollIncrement;
				
				// check bounds
				if (galleryLeft > 0) galleryLeft = 0;
				gallery.style.left = galleryLeft + "px";

				// Wait to see if flag is reset. Otherwise, keep scrolling
				galleryInterval = setTimeout("scrollGallery()", 10);
			} else {

				// Gallery is flush left.  Stop scrolling
				clearTimeout(galleryInterval);
			}
			break;
			
		// Scroll up
		case "up":
			var gallery = getObject(galleryVertId);
			var galleryHeight = gallery.offsetHeight;
			var viewerHeight = getObject(viewerVertId).offsetHeight;
			// Only scroll up if top edge is not aligned 0
			if (galleryUp < 0) {
				galleryUp += galleryScrollIncrement;
				
				// check bounds
				if (galleryUp > 0) galleryUp = 0;
				gallery.style.top = galleryUp + "px";
				//alert(galleryUp);

				// Wait to see if flag is reset. Otherwise, keep scrolling
				galleryInterval = setTimeout("scrollGallery()", 10);
			} else {

				// Gallery is flush left.  Stop scrolling
				clearTimeout(galleryInterval);
			}
			break;
			
		// Scroll down
		case "down":
			var gallery = getObject(galleryVertId);
			var galleryHeight = gallery.offsetHeight;
			var viewerHeight = getObject(viewerVertId).offsetHeight;

			// Only scroll down if bottom edge is not aligned with bottom side
			if (galleryUp > (viewerHeight - galleryHeight)) {
				galleryUp -= galleryScrollIncrement;
				//alert(galleryUp);
				//alert(galleryHeight);
				//alert(viewerHeight);
				
				// check bounds
				if (galleryUp < (viewerHeight - galleryHeight)) galleryUp = (viewerHeight - galleryHeight);
				gallery.style.top = galleryUp + "px";
				//alert(galleryUp);

				// Wait to see if flag is reset. Otherwise, keep scrolling
				galleryInterval = setTimeout("scrollGallery()", 10);
			} else {

				// Gallery is flush right.	Stop scrolling
				clearTimeout(galleryInterval);
			}
			break;
			
		// Flag not set.  Stop scrolling
		default:
			clearTimeout(galleryInterval);
	}
	
	checkArrows();
}



/*------------------------------------------------------------
*  Function:  checkArrows
*  
*  Description:
*  checks the state of the gallery scroller and enables or disables the arrows accordingly
*  this function is called by the gallery scroller
*  but it should also be called on startup to set the initial arrow state
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
function checkArrows() {
	
	switch (scrollDir) {
		case "down":
		case "up":
		//alert(scrollDir);
			var gallery = getObject(galleryVertId);
			var arrowUp = getObject(photoArrowUpId);
			var arrowDown = getObject(photoArrowDownId);
			if (gallery && arrowUp && arrowDown) {
				var galleryHeight = gallery.offsetHeight;
				var viewerHeight = getObject(viewerVertId).offsetHeight;

				// if gallery is taller than viewer window, check the arrows
				if (galleryHeight > viewerHeight) {
					//if top edge is hidden, enable up arrow
					arrowUp.className = (galleryUp < 0 ) ? "enabled" : "";
					arrowUp.title = (galleryUp < 0 ) ? "Scroll up" : "";

					// if bottom edge is hidden, enable down arrow
					arrowDown.className = (galleryUp > (viewerHeight - galleryHeight)) ? "enabled" : "";
					arrowDown.title = (galleryUp > (viewerHeight - galleryHeight)) ? "Scroll down" : "";

				// gallery isn't wide enough to scroll - disable both arrows
				} else {
					arrowUp.className = "";
					arrowDown.className = "";
				}
			}
			break;
		case "left":
		case "right":
		//alert(scrollDir);
			var gallery = getObject(galleryHorizId);
			var arrowLeft = getObject(photoArrowLeftId);
			var arrowRight = getObject(photoArrowRightId);
			if (gallery && arrowLeft && arrowRight) {
				var galleryWidth = gallery.offsetWidth;
				var viewerWidth = getObject(viewerHorizId).offsetWidth;

				// if gallery is wider than viewer window, check the arrows
				if (galleryWidth > viewerWidth) {
					//if left edge is hidden, enable left arrow
					arrowLeft.className = (galleryLeft < 0 ) ? "enabled" : "";
					arrowLeft.title = (galleryLeft < 0 ) ? "Scroll left" : "";

					// if right edge is hidden, enable right arrow
					arrowRight.className = (galleryLeft > (viewerWidth - galleryWidth)) ? "enabled" : "";
					arrowRight.title = (galleryLeft > (viewerWidth - galleryWidth)) ? "Scroll right" : "";

				// gallery isn't wide enough to scroll - disable both arrows
				} else {
					arrowLeft.className = "";
					arrowRight.className = "";
				}
			}
			break;
	}
}

/*=========================================================================================
	end gallery thumbnail scrolling
==========================================================================================*/



/*=========================================================================================
	gallery image swapping
==========================================================================================*/


var galleryPhotos = new Array();		// array used to store gallery photo image data and captions

var photoPaneID = "photoPane";			// html ID of div where large image tag will be written
var photoCaptionID = "photoCaption";	// html ID of div where image caption will be written

String.prototype.substitute = function(was, becomes) {
	return this.split(was).join(becomes);
}

/*------------------------------------------------------------
*  Function:  media_widget_show_current_zoomed_out
*  
*  Description:
*  shows fully zoomed out large version of the current thumbnail
*  also generates an accompanying image map for flashpix zooming.
*------------------------------------------------------------*/
function media_widget_show_current_zoomed_out() 
{
	
	
	var thePhotoDiv = getObject(photoPaneID);
	if(!thePhotoDiv){alert("Error: Could not find photo div to zoom out.");return;}

	current_img = galleryPhotos[currentThumbIndex].img;

	if(!current_img.complete)
	{
		setTimeout("media_widget_show_current_zoomed_out()",500);
		return;
	}

	
	path = current_img.src.toString().split('/');
	codes = path[(path.length)-1].toString().split("_")
	flashpix_path = "/image_library/" + codes[0] + "/" + codes[0] + codes[1] + ".fpx";
	
	if(current_img.width > current_img.height)
		this_image_specs = new Array("0,0,150,120:0.0000,0.0000,0.5000,0.5000", "0,120,150,200:0.0000,0.2500,0.5000,0.5000", "0,200,150,320:0.0000,0.5000,0.5000,0.5000", "150,0,250,120:0.2500,0.0000,0.5000,0.5000", "150,120,250,200:0.2500,0.2500,0.5000,0.5000", "150,200,250,320:0.2500,0.5000,0.5000,0.5000", "250,0,400,120:0.5000,0.0000,0.5000,0.5000", "250,120,400,200:0.5000,0.2500,0.5000,0.5000", "250,200,400,320:0.5000,0.5000,0.5000,0.5000");
	else
		this_image_specs = new Array("0,0,120,150:0.000000,0.000000,0.500000,0.500000","0,150,120,250:0.000000,0.250000,0.500000,0.500000","0,250,120,400:0.000000,0.500000,0.500000,0.500000","120,0,200,150:0.250000,0.000000,0.500000,0.500000","120,150,200,250:0.250000,0.250000,0.500000,0.500000","120,250,200,400:0.250000,0.500000,0.500000,0.500000","200,0,320,150:0.500000,0.000000,0.500000,0.500000","200,150,320,250:0.500000,0.250000,0.500000,0.500000","200,250,320,400:0.500000,0.500000,0.500000,0.500000");
	
	
	hover_text = galleryPhotos[currentThumbIndex].hover_text;
	zoomed_out_source = '<img src="' + galleryPhotos[currentThumbIndex].img.src + '" alt="'+hover_text+'" title="'+hover_text+'" border="0" hspace="0" usemap="#map_' + currentThumbIndex +  '"/>'
	zoomed_out_source += '<map name="map_' + currentThumbIndex + '">';
	
	for(i=0; i<this_image_specs.length; i++)
	{
		specs = this_image_specs[i].toString().split(':');
		if(hover_text.indexOf("'")>-1)
			hover_text = hover_text.substitute("'","`");
		zoomed_out_source += "<area shape=\"rect\" coords=\"" +specs[0] + "\"  alt=\"" + hover_text + "\"  title=\"" + hover_text + "\"  href=\"javascript:media_widget_load_href('/apps/flashpix/zoom.weml','picture=" + flashpix_path + "&zoomLevel=1&rgn=" + specs[1] + "&wid=" + current_img.width + "&hei=" + current_img.height + "&hover_text=" + escape(hover_text) + "')\">"
	}
	zoomed_out_source += "</map>"
	
	thePhotoDiv.innerHTML = zoomed_out_source;
	thePhotoDiv.blur();
}


/*------------------------------------------------------------
*  Function:  media_widget_load_href
*  
*  Description:
*  Does an ajax-style reload of the photoPane div, to hold the zoomed image plus it's image map
*------------------------------------------------------------*/
function media_widget_load_href(new_href, pars)
{
	var ajaxUpdater = new Ajax.Updater
	(
		photoPaneID,
		new_href,
		{
			method:		'get',
			parameters:  	pars
		}
	);
}

function failed(request)
{
	alert("failed");
}
/*------------------------------------------------------------
*  Function:  newGalleryPhoto
*  
*  Description:
*  creates an entry in the galleryPhotos array for each gallery photo
*  this function is called in the onload handler of each gallery thumb img tag
*  
*  Parameters:
*  which	integer		numeric index of the photo
*  src		string		path to large version of photo
*  caption	string		photo caption
*  
*  Return:
*  none
*------------------------------------------------------------*/
function newGalleryPhoto(which,src,caption, hover_text) {
	galleryPhotos[which] = new Object();
	galleryPhotos[which].img = newImage(src);
	galleryPhotos[which].caption = caption;
	galleryPhotos[which].hover_text = hover_text;
}



/*------------------------------------------------------------
*  Function:  showGalleryPhoto
*  
*  Description:
*  displays the large version of a gallery image
*  called when user clicks on a thumbnail
*  showGalleryPhoto(0) should also be called on startup to load the initial photo
*  
*  Parameters:
*  which	integer		numeric index of the photo
*  
*  Return:
*  none
*------------------------------------------------------------*/
function showGalleryPhoto(which) {
	if (galleryPhotos[which]) {
		var thePhotoDiv = getObject(photoPaneID);
		var theCaptionDiv = getObject(photoCaptionID);
		var theTag = '<img src="' + galleryPhotos[which].img.src + '" alt="'+galleryPhotos[which].caption+'" title="'+galleryPhotos[which].caption+'" border="0" />';
		if (thePhotoDiv) thePhotoDiv.innerHTML = theTag;
		if (theCaptionDiv) theCaptionDiv.innerHTML = galleryPhotos[which].caption;
		
		// turn off last thumb
		if (galleryPhotos.currentThumb) {
			galleryPhotos.currentThumb.className = "";
			galleryPhotos.currentThumb.parentNode.className = "";
		}
		
		// turn on current thumb
		galleryPhotos.currentThumb = getObject('id_' + which);
		galleryPhotos.currentThumb.className = "currentThumb";
		galleryPhotos.currentThumb.parentNode.className = "currentThumb";
		currentThumbIndex=which;
		media_widget_show_current_zoomed_out();
	}
}



/*=========================================================================================
	end gallery image swapping
==========================================================================================*/
