/*=========================================================================================
	four seasons javascript library


	2005-09-16
	this library contains javascript functions
	in the following groupings:
	
		misc utility functions 
		cookie library 
		font resize library 
		rollover library 
		gallery thumbnail scrolling 
		email this page					// BUGFIX ID 7
		startup and shutdown calls 		// BUGFIX ID 13
		share this page				// Social Bookmarking

==========================================================================================*/

/*=========================================================================================
	misc utility functions
==========================================================================================*/

/*------------------------------------------------------------
*  Function:  parseQuery
*  
*  Description:
*  allows us to set javascript variables by passing them in as
*  values in the query string. by default it looks for the
*  query string after the occurrence of '?' in URL of the
*  current document
*  
*  Parameters:
*  loc		string	url to search for query string
*  key		string	string which delimits query string from rest of URL
*  
*  Return:
*  none
*------------------------------------------------------------*/
function parseQuery(loc,key) {
	if (loc == null) loc = document.location.href;
	if (key == null) key = '?';
	if (loc.indexOf(key) > -1) {
		query = loc.substring(loc.indexOf(key)+key.length,loc.length);
		args = query.split('\&');
		for (i=0; i<args.length; i++) {
			chunk = args[i];
			eval(chunk.split('=')[0] + " = '" + chunk.split('=')[1] + "'");
		}
	}
}



/*------------------------------------------------------------
*  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:  resizeWindow
*  
*  Description:
*  resizes a window to a given width and height
*  by default, will only resize if the window is smaller than the prescribed width and/or height
*  if forceResize is passed in as true, it will force the window to the desired size
*  
*  Parameters:
*  w				integer	desired width
*  h				integer	desired height
*  forceResize		boolean	if true, forces large window to resize down to desired size
*  
*  Return:
*  none
*------------------------------------------------------------*/
function resizeWindow(w,h,forceResize) {
	if (window.innerWidth && window.innerHeight) {
		var wdiff = window.outerWidth - window.innerWidth;
		var hdiff = window.outerHeight - window.innerHeight;
		
		var windowIsSmaller = false;
		if (window.innerWidth < w) {
			endw = w + wdiff;
			windowIsSmaller = true;
		} else {
			endw = window.outerWidth;
		}
		if (window.innerHeight < h) {
			endh = h + hdiff;
			windowIsSmaller = true;
		} else {
			endh = window.outerHeight;
		}
		if (forceResize) {
			endw = w + wdiff;
			endh = h + hdiff;
		}
		
		if (windowIsSmaller || forceResize) self.resizeTo(endw,endh);
	} else {
		// can't do it the nice way
		// add padding for scrollbar in IE6
		self.resizeTo(w + 30,h);
	}
}



/*------------------------------------------------------------
*  Function:  dw
*  
*  Description:
*  shorthand for document.write
*  
*  Parameters:
*  msg		string	text to be written
*  
*  Return:
*  none
*------------------------------------------------------------*/
function dw(msg) {
	document.write(msg);
}



/*------------------------------------------------------------
*  Function:  randomNum
*  
*  Description:
*  returns a random number in the specified range of numbers, inclusive
*  
*  Parameters:
*  start	integer	first number in desired range
*  end		integer	last number in desired range
*  
*  Return:
*  n		integer
*------------------------------------------------------------*/
function randomNum(start,end) {
	var range = end - start + 1;
	var n = Math.floor(Math.random() * (range)) + start;
	return(n);
}




/*------------------------------------------------------------
*  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;
}


/*------------------------------------------------------------
*  Function:  show
*  
*  Description:
*  shows an object, if not nested in a hidden object
*  
*  Parameters:
*  which	string/object	ID of object to be shown, or reference to the object itself
*  
*  Return:
*  none
*------------------------------------------------------------*/
function show(which) {
	var obj = getObject(which);
	if (obj) obj.style.visibility = "inherit";
}


/*------------------------------------------------------------
*  Function:  jsQSGet
*  
*  Description:
*  
*  Parameters:
*  key on menu - key consists of PropertyID_LHN Id
*  
*  Return:
*  true or false  - true Cookie denotes menu should remain open
*------------------------------------------------------------*/
function jsQSGet(qs,field) {

	if((!qs) || (!field))
		return '';

	var field_found = qs.indexOf('&' + field + '=');
	if(field_found>-1)
	{
		qs = qs.substring(field_found + field.length + 2);
		var value = qs.split("&");
		
		return qs = unescape(value[0]);
	}

	return '';
}

/*------------------------------------------------------------
*  Function:  jsQSSet
*  
*  Description:
*  
*  Parameters:
*  key on menu - key consists of PropertyID_LHN Id
*  
*  Return:
*  true or false  - true Cookie denotes menu should remain open
*------------------------------------------------------------*/
function jsQSSet(qs,field,value) {
	
	if(!field)
		return qs;

	var new_qs = '';

	var field_found = qs.indexOf('&' + field + '=');
	if(field_found>-1)
	{
		new_qs = qs.substring(0,field_found);
		qs = qs.substring(field_found + field.length + 2);
		var seperator_found = qs.indexOf('&');
		if(seperator_found>-1)
			new_qs = new_qs + qs.substring(seperator_found);
	}
	else
	{
		new_qs = qs;
	}

	if(value)
		new_qs = new_qs + '&' + field + '=' + escape(value);

	return new_qs;
}







/*------------------------------------------------------------
*  Function:  compareById
*  
*  Description:
*  Comparison function for alphabetic comparison of DOM
*  elements by ID.
*  
*  Parameters:
*  elementA		element		First element to compare
*  elementB		element		Second element to compare
*  
*  Return:
*  integer		-1 (b is greater), 0 (a == b), 1 (a is greater)
*------------------------------------------------------------*/
function compareById(elementA, elementB) {
	if (elementA.id > elementB.id) {
		return 1;
	} else if (elementA.id == elementB.id) {
		return 0;
	} else {
		return -1;
	}
}




/*------------------------------------------------------------
*  Function:  disableButton
*  
*  Description:
*  Disables a button element.  For use with font controls.
*  
*  Parameters:
*  button	object	The button to disable
*  
*  Return:
*  none
*------------------------------------------------------------*/
function disableButton(button) {
	imgSwap(button.id,'disabled');			// Display disabled image
	rollovers[button.id].frozen = true;		// Turn off rollovers
	button.className = FROZEN_CLASS;			// Set disabled class
	button.title = "";						// Remove tooltip
}



/*------------------------------------------------------------
*  Function:  enableButton
*  
*  Description:
*  Enables a button element.  For use with font controls.
*  
*  Parameters:
*  button	object	The button to enable
*  titleStr	string	The tooltip text of the button
*  
*  Return:
*  none
*------------------------------------------------------------*/
function enableButton(button, titleStr) {
	rollovers[button.id].frozen = false;	// Turn on rollovers
	imgOff(button.id);						// Display off image
	button.className = UNFROZEN_CLASS;		// Set enabled class
	button.title = titleStr;				// Set tooltip
}

/*=========================================================================================
	end font resize library
==========================================================================================*/
/* end BUGFIX ID 13 */



/*=========================================================================================
	rollover library
==========================================================================================*/

// array used to store rollover states and image data
var rollovers = new Array();


/*------------------------------------------------------------
*  Function:  imgOn, imgOver, imgOff
*  
*  Description:
*  shorthand functions for calling imgSwap
*  
*  Parameters:
*  which	string 	name of image to swap
*  
*  Return:
*  none
*------------------------------------------------------------*/
function imgOn(which) {
	imgSwap(which,'on');
}

function imgOver(which) {
	imgSwap(which,'over');
}

function imgOff(which) {
	imgSwap(which,'off');
}



/*------------------------------------------------------------
*  Function:  imgSwap
*  
*  Description:
*  stub function for rollovers
*  expects an image name and state
*  checks for existence of named image, and defined state
*  also checks to see if image has been disabled -- this is used to freeze an image in a given state
*  
*  Parameters:
*  which	string 	name of image to swap
*  state	string	name of state to swap to
*  
*  Return:
*  none
*------------------------------------------------------------*/
function imgSwap(which,state) {
	if (which && rollovers[which]) {
		if (document.images[which] && eval('rollovers[which].states.' + state)) {
			if (! rollovers[which].frozen) {
				document.images[which].src = eval('rollovers[which].states.' + state).src;
			}
		}
	}
}


/*------------------------------------------------------------
*  Function:  newRollover
*  
*  Description:
*  this function populates the rollovers array with the image
*  objects used for the various states of a rollover
*  
*  Parameters:
*  name		string	name of image object
*  offsrc		string	path to image file for off (normal) state
*  oversrc		string	path to image file for over state
*  onsrc		string	path to image file for on (selected) state
*  disabledsrc	string	path to image file for disabled state
*  
*  Return:
*  none
*------------------------------------------------------------*/
function newRollover(name,offsrc,oversrc,onsrc,disabledsrc) {
	rollovers[name] = new Object();
	rollovers[name].states = new Object();
	rollovers[name].states.off = (offsrc) ? newImage(offsrc) : null;
	rollovers[name].states.over = (oversrc) ? newImage(oversrc) : null;
	rollovers[name].states.on = (onsrc) ? newImage(onsrc) : null;
	rollovers[name].states.disabled = (disabledsrc) ? newImage(disabledsrc) : null;
	// the following variable is used to prevent an image from being changed on rollover
	// used for disabling images that are in a disabled or selected state and shouldn't change
	rollovers[name].frozen = false;
}


/*=========================================================================================
	end rollover library
==========================================================================================*/



/*=========================================================================================
	gallery thumbnail scrolling
==========================================================================================*/


// gallery variables
var galleryId = "photoThumbsTable";			// id of gallery html object
var viewerId = "photoThumbsViewer";			// id of div enclosing gallery html object
var galleryLeft = 0;						// Gallery offset
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";




/*------------------------------------------------------------
*  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:  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() {
	var gallery = getObject(galleryId);
	var galleryWidth = gallery.offsetWidth;
	var viewerWidth = getObject(viewerId).offsetWidth;
	
	switch (scrollDir) {
		// Scroll right
		case "right":

			// 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":
			// 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;

		// 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() {
	var gallery = getObject(galleryId);
	var arrowLeft = getObject(photoArrowLeftId);
	var arrowRight = getObject(photoArrowRightId);
	
	if (gallery && arrowLeft && arrowRight) {
		var galleryWidth = gallery.offsetWidth;
		var viewerWidth = getObject(viewerId).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 = "";
		}
	}
}

/*=========================================================================================
	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";
	flashpix_path =   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;
}


/*------------------------------------------------------------
*  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
*
*  20071130|Jacquie|set method to post and unescape(@pars) for non latin based languages AR, JP, and ZH  tm 12921
*------------------------------------------------------------*/
function media_widget_load_href(new_href, pars)
{
	var ajaxUpdater = new Ajax.Updater
	(
		photoPaneID,
		new_href,
		{
			method:		'post',
			parameters:  	unescape(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();
	}
	return false;
}



/*=========================================================================================
	end gallery image swapping
==========================================================================================*/



/*------------------------------------------------------------
*  Function:  add_onload
*  
*  Description:
*  Event handler for window.onload.
*  Stores the tooltips for font control buttons in global vars.
*  Retrieves the active stylesheet and enables and disables
*  font control buttons accordingly.
*  
*  Parameters:
*  none
*  
*  Return:
*  none
*------------------------------------------------------------*/
//////////////////////////////////// functions for adding body on load ///////////////////////
var add_onload_func_count;
var add_onload;

if(!add_onload_func_count)
	add_onload_func_count=0

/////////////////////////////////////////////////////////////////////////////////////////////////
if(!add_onload)
{
	add_onload = function (func)
	{
		if(add_onload_func_count==0)
		{
			if(window.onload)
			{
				eval("clone" + add_onload_func_count + " = window.onload;")
				add_onload_func_count++
			}
		}

		eval("clone" + add_onload_func_count + " = func;")
		add_onload_func_count++;

		window.onload=run_all_onload;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////

function run_all_onload()
{
	for(add_onload_current_function=0;add_onload_current_function<add_onload_func_count;add_onload_current_function++)
		eval("clone" + add_onload_current_function + "()")
}

function no_op() {
}

