// General JQuery Functions v1.3
// Andrew Pettican (June 2008)
//
// Changelog:
// v1.3 - 15/06/2009 - More preload images added
// v1.2 - 22/10/2008 - Added simple image preload function (the old preloadCssImages() was too slow)
// v1.1 - 17/10/2008 - Added Bookmark and Print icon functionality
// v1.0 - 26/06/2008 - Initial build
//////////////////////////////////////////////////////////////////

// ---------------------------------------------------------------
// Parameters
// ---------------------------------------------------------------

// An array of images to preload
var jf_preload_img_srcs = new Array();

// ---------------------------------------------------------------
// Parameters End
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// Global Vars, Do not change anything below this line!
// ---------------------------------------------------------------

var jf_images = null;	// An array of preloaded images

// ---------------------------------------------------------------
// Global Vars End, Do not change anything above this line!
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// JS Events
// ---------------------------------------------------------------

jQuery(document).ready(function()
{
	// Preload images
	jf_images = preload_images(jf_preload_img_srcs);
	
	// Bookmark this page
	$("#icn_bookmark").click(function() {
		bookmark_page();
		return false;
	});
	
	// Print this page
	$("#icn_print").click(function() {
		print_page();
	});
});

// ---------------------------------------------------------------
// JS Events End
// ---------------------------------------------------------------



// ---------------------------------------------------------------
// Functions
// ---------------------------------------------------------------

/**
 * Preloads an array of image
 * img_srcs	- An array of image source locations
 * Returns an array of preloaded images
 */
function preload_images(img_srcs) {
	var result = null;
	
	if (typeof(img_srcs) != "undefined" && img_srcs.length > 0) {
		result = new Array();
		for(i=0; i<img_srcs.length; i++) {
			result[i] = new Image();
			result[i].src = img_srcs[i];
		}
	}
	return result;
}


/**
 * Calculates the width/height of an object.
 * node			- The node to measure the width/height
 * horizontal	- Flag indicating if we should measure width or height (true = width, false = height).
 * padding		- Flag indicating if we should include any padding in the width/height
 * margin		- Flag indicating if we should include any margins in the width/height
 * borders		- Flag indicating if we should include any borders in the width/height
 * 
 * Note: By default this function will calculate the width, including all padding/margins/borders.
 */
function calc_full_width_height(node, horizontal, padding, margin, borders) {
	// horizontal defaults to true.
	if (typeof(horizontal) == "undefined") { horizontal = true; }
	
	// padding/margin/borders default to true
	if (typeof(padding) == "undefined") { padding = true; }
	if (typeof(margin) == "undefined") { margin = true; }
	if (typeof(borders) == "undefined") { borders = true; }
	
	// Are we measuring vertically (default) or horizontally
	var dimensionA = "top";
	var dimensionB = "bottom";
	if(horizontal) {
		dimensionA = "left";
		dimensionB = "right";
	}
	
	var result = 0;
	if(node !== null) {
		var dimensionA_padding = (padding) ? parseInt($(node).css("padding-" + dimensionA), 10) : 0;
		var dimensionB_padding = (padding) ? parseInt($(node).css("padding-" + dimensionB), 10) : 0;
		var dimensionA_margin = (margin) ? parseInt($(node).css("margin-" + dimensionA), 10) : 0;
		var dimensionB_margin = (margin) ? parseInt($(node).css("margin-" + dimensionB), 10) : 0;
		var dimensionA_border = (borders) ? parseInt($(node).css("border-" + dimensionA + "-width"), 10) : 0;
		var dimensionB_border = (borders) ? parseInt($(node).css("border-" + dimensionB + "-width"), 10) : 0;
		var objsize = (horizontal) ? $(node).width() : $(node).height();
		
		// Add it all up
		if(!isNaN(dimensionA_padding)) { result += dimensionA_padding; }
		if(!isNaN(dimensionB_padding)) { result += dimensionB_padding; }
		if(!isNaN(dimensionA_margin)) { result += dimensionA_margin; }
		if(!isNaN(dimensionB_margin)) { result += dimensionB_margin; }
		if(!isNaN(dimensionA_border)) { result += dimensionA_border; }
		if(!isNaN(dimensionB_border)) { result += dimensionB_border; }
		if(!isNaN(objsize)) { result += objsize; }
	}
	
	return result;
}


/**
 * Determines if the specified subnav should remain active based on the current mouse position
 * mouseX	- Current mouse x-coordinate
 * mouseY	- Current mouse y-coordinate
 * id		- The node to test against
 */
function is_mouseover(mouseX, mouseY, node, padding, margin, borders) {
	// padding/margin/borders default to true
	if (typeof(padding) == "undefined") { padding = true; }
	if (typeof(margin) == "undefined") { margin = true; }
	if (typeof(borders) == "undefined") { borders = true; }
	
	var answer = false;
	
	if(node !== null) {
		// Calculate the boundaries that the mouse must stay within.
		var nodeTopCoord = $(node).offset().top;
		var nodeLeftCoord = $(node).offset().left;
		var nodeBottomCoord = nodeTopCoord + calc_full_width_height(node, false, padding, margin, borders);
		var nodeRightCoord = nodeLeftCoord + calc_full_width_height(node, true, padding, margin, borders);
		
		// Is the mouse within these bounds?
		answer = (mouseY >= nodeTopCoord && mouseX >= nodeLeftCoord && mouseY <= nodeBottomCoord && mouseX <= nodeRightCoord);
	}
	
	return answer;
}


/**
 * Bookmarks the current page
 * NB: this function supports IE only
 */
function bookmark_page() {
	// Get the current location and title
	var url = document.location.href;
	var title = document.title;
	
	// Build a backup message
	var backup_message = "Please press CTRL + D to bookmark this page";
	if(mactest) {
		backup_message = "Please press CMD + D to bookmark this page";
	}
	
	// Ensure we have a valid http url
	if((url.substr(0, 4)).toLowerCase() == "http") 
	{
		if(ie) {
			// ie
			window.external.AddFavorite(url, title);
		}
		else {
			// other browser
			alert(backup_message);
		}
	}
	return false;
}


/**
 * Opens the print dialog for the current page
 */
function print_page() {
	window.print();
}