/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  global.js
/* ########################################################################### *

/* ########################################################################### *
/* ***** INDEX *************************************************************** *
/* ########################################################################### *
/* ##### INITIALISATION
/* #####
/* ##### FUNCTIONALITY:
/* ##### Check all checkboxes
/* ##### Tooltip
/* ##### Popup window
/* ##### Modal window
/* ##### Smooth anchor scroll
/* #####
/* ##### PRESENTATION
/* ##### Table alternate rows
/* ##### IE6 tweaks
/* ##### PNG fix
/* ##### Iframe resize
/* ########################################################################### */


/* ########################################################################### *
/* ##### INITIALISATION
/* ########################################################################### */

$j(document).ready(hi_init_globalFunctionality);
$j(document).ready(hi_init_globalPresentation);
$j(document).ready(hi_init_globalCustom);

function hi_init_globalCustom()
{
	/* Setup captions on banner images */
	/*if ($j('#ja-banner .heroBannerAlign').length > 0)
	{
		var bannerImage = $j('#ja-banner .heroBannerAlign img');
		var captionText = $j(bannerImage).attr("alt");
		var captionHtml = '<div class="heroBannerCaption">' + captionText + '</div>';
		$j(bannerImage).after(captionHtml);
	}
	*/
	
	// Add .editMode to body
	if ($j(".contentpaneopen_edit").length > 0)
	{
		$j("body").addClass("editMode");	
	}	
	// Add .editMode_heroBannerAlign to body
	if ($j(".contentpaneopen_edit").length > 0 && $j(".heroBannerAlign").length > 0)
	{
		$j("body").addClass("editMode_heroBannerAlign");
	}
	
	// Add .hasSystemMessage_heroBannerAlign to body if one is on the page.
	if ($j("#system-message").length > 0 && $j(".heroBannerAlign").length > 0)
	{
		$j("body").addClass("hasSystemMessage_heroBannerAlign");
	}
}

function hi_init_globalFunctionality()
{
	// Tooltips
	// hi_tooltip();
	
	// Popup window
	// hi_popup_window();
	
	// Modal window
	// hi_init_modalWindow();

	// Forms
	// hi_forms_checkAllCheckboxes();	

	// Forms: Select on click
	hi_init_forms_selectOnClick();
	
	// Accessibility: Font Size Buttons
	// hi_accessibilityFontSize();	
	
	// Enable smooth anchor scrolling
	hi_init_anchorSmoothScroll();
	
	// Support insert target="_blank" on class="externalLink" anchors
	hi_init_externalLinkTarget();
}

function hi_init_globalPresentation()
{
	
	// Table alternate rows
	hi_tableAltRows();

	// Internet Explorer specific tweaks
	hi_ieTweaks();
	
	// PNG fix
	// hi_pngfix();
	
	// Buttons
	// hi_forms_buttons();
}



/* ########################################################################### *
/* ##### FUNCTIONALITY
/* ########################################################################### */


/* ##### FORMS */

// Check all checkboxes
// Note: This only supports 1 check all button.
// DESC: Mark all checkable checkboxes with class .hi_forms_checkableWithAll, mark the 'click to check all' checkbox with .hi_forms_checkAllCheckboxes
function hi_forms_checkAllCheckboxes()
{
	$j('.hi_forms_checkAllCheckboxes').click(function()
	{
		var checkedStatus = this.checked;
		$j('.hi_forms_checkableWithAll').each(function()
		{
			this.checked = checkedStatus;
		});
	});
}


// Select contents of input on click
function hi_init_forms_selectOnClick()
{
	if ($j(".hi_forms_selectOnClick").length > 0)
	{
		// When the element has focus, set its value to the following
		var SEARCH_TEXT_ON = "";	
		
		// Setup the value of the element to it's title attribute
		$j(".hi_forms_selectOnClick").each(function() 
			{
				$j(this).val($j(this).attr("title"));
			});
		
		// Handle this element's focus event
		$j(".hi_forms_selectOnClick").focus(function()
		{
			// Get the value from the elements title
			var searchTextOff = $j(this).attr("title");
			
			// If the current value of the element is the same as the title attribute then set it to the ON value.
			if ($j(this).val() == searchTextOff)
			{
				$j(this).val(SEARCH_TEXT_ON);
			}
		});
		
		// Handle this element's blur event
		$j(".hi_forms_selectOnClick").blur(function()
		{
			// Get the value from the elements title
			var searchTextOff = $j(this).attr("title");
			
			// If the value of the element is empty set it to the default value (taken from the element title)
			if ($j(this).val() == "")
			{
				$j(this).val(searchTextOff); 
			}
		});	
	}
}


/* Form buttons (including rounded buttons with background image) */

function hi_forms_buttons()
{
	// If browser is not IE6 (or less)
	if ($j.browser.msie == false || ($j.browser.msie && $j.browser.version > 6)) 
	{
		// Get the buttons that should be effected
		var nodes = $j("button.button,a.button");
		for (var i = 0; i < nodes.length; i++)
		{
			// If this is a primary action button
			if ($j(nodes[i]).hasClass("primary"))
			{
				$j(nodes[i]).addClass("button_curve_primary");
				$j(nodes[i]).after("<div class='buttonEnd_primary'></div>");
				$j(nodes[i]).hover(buttonHoverOver_primary, buttonHoverOut_primary);
			} else {
				$j(nodes[i]).addClass("button_curve"); 
				$j(nodes[i]).after("<div class='buttonEnd'></div>");         
				$j(nodes[i]).hover(buttonHoverOver, buttonHoverOut);
			}
		}
	}
}
 
function buttonHoverOver()
{
	$j(this).addClass("button_hover");
	$j(this).next("div").addClass("buttonEnd_hover");
}
 
function buttonHoverOut()
{
	$j(this).removeClass("button_hover");
	$j(this).next("div").removeClass("buttonEnd_hover");
}
 
function buttonHoverOver_primary()
{
	$j(this).addClass("button_primary_hover");
	$j(this).next("div").addClass("buttonEnd_primary_hover");
}
 
function buttonHoverOut_primary()
{
	$j(this).removeClass("button_primary_hover");
	$j(this).next("div").removeClass("buttonEnd_primary_hover");
}



/* ########################################################################### *
/* ##### TOOLTIPS
/* ########################################################################### */

function hi_tooltip()
{
	$j('.fieldinfo').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fixPNG: true,
		//showBody: " - ",
		extraClass: "pretty",
		top: -24,
		left: 6
	});
}


/* ########################################################################### *
/* ##### TABS
/* ########################################################################### */

function hi_init_tabs()
{
	if ($j('.hi_tabs > ul').length > 0) 
	{
		// Initialise the tabs
		$j('.hi_tabs > ul').tabs();
		
		// Check if there is an active tab, and select it
		$j('.hi_tabs > div').each(function() 
			{
				if ($j(this).hasClass("active"))
				{
					$j('.hi_tabs > ul').tabs('select', '#' + $j(this).attr("id"));
				}
			});
	}
}


/* ########################################################################### *
/* ##### POPUP WINDOW
/* ########################################################################### */

function hi_popup_window() 
{
	$j('.popUp').click(function (e) {
		e.preventDefault();
		var targetHref = $j(this).attr('href');
		window.open(targetHref,"_blank","height=640,width=700,toolbar=no,menubar=no,location=no,scrollbars=yes,status=no");
		// Return false to prevent the link click navigation occuring.
		return false;
	});
}



/* ########################################################################### *
/* ##### MODAL WINDOWS
/* ########################################################################### */

// Generic modal window
function hi_init_modalWindow()
{
	var modalHtml = '<div style="display:none"><a href="#" title="Close" class="modalCloseX modalClose"><span class="icon"></span></a><div class="content form-container"><h1 class="title"></h1><div class="loading" style="display:none"></div><div class="message" style="display:none"></div></div></div>';
	
	$j('.modalWindow').click(function (e) 
	{
		e.preventDefault();
		
		// create a modal dialog with the data
		$j(modalHtml).modal({
			close: true,
			overlay: 70,
			overlayId: 'modalOverlay',
			containerId: 'modalContainer',
			iframeId: 'modalIframe',
			onOpen: modal.open,
			onClose: modal.close
		});

	});
	
	var modal = {
		message: null,
		open: function (dialog) {
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.content.fadeIn(200, function () {
						$j('#modalContainer #buttClose').focus();
					});
				});
			});
		},
		close: function (dialog) {
			dialog.content.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$j.modal.remove(dialog);
					});
				});
			});
		}
	};
}

// Modal window
function hi_launchModalWindow(title, windowWidth, callBack)
{
	var modalHtml = '<div style="display:none"><a href="#" title="Close" class="modalCloseX modalClose"><span class="icon"></span></a><div class="content form-container"><h1 class="title"></h1><div class="loading" style="display:none"></div><div class="message" style="display:none"></div><div id="contentContainer"><p><strong>Sorry, you require the <a href="http://www.adobe.com/go/getflash">Adobe Flash</a> player to view this content.</strong></p></div></div></div>';

	var modal = {
		message: null,
		open: function (dialog) {
			dialog.overlay.fadeIn(500, function () {
				
				// Set the width of the window
				$j(dialog.container).width(windowWidth);

				// Set the title of the window
				$j(dialog.container).find('h1.title').html(title);

				dialog.container.show();
				dialog.content.show();																	 
			
				// Execute the callBack function
				if (typeof callBack == "function")
				{
					// execute callBack function
					callBack();
				}
				$j(dialog.container).find('#buttClose').focus();
				
			});
		},
		close: function (dialog) {
			$j("#contentContainer").hide();
			
			dialog.content.hide();
			dialog.container.hide();
			dialog.overlay.fadeOut(500, function () 
				{
					$j.modal.remove(dialog);
				});
		
		}
	};
	
	// create a modal dialog with the data
	$j(modalHtml).modal({
		close: true,
		overlay: 70,
		overlayId: 'modalOverlay',
		containerId: 'modalContainer',
		iframeId: 'modalIframe',
		onOpen: modal.open,
		onClose: modal.close
	});

}

// Modal windows that get HTML page for content via Ajax
function hi_init_ajaxModalWindow(htmlUrl)
{
	$j('.ajaxModalWindow').click(function (e) 
	{
		e.preventDefault();
		
		// load the page content via ajax
		$j.get(htmlUrl, function(data)
		{
			// create a modal dialog with the data
			$j(data).modal({
				close: true,
				overlay: 70,
				overlayId: 'modalOverlay',
				containerId: 'modalContainer',
				iframeId: 'modalIframe',
				onOpen: modal.open,
				onClose: modal.close
			});
		});
	});
	
	var modal = {
		message: null,
		open: function (dialog) {
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.content.fadeIn(200, function () {
						$j('#modalContainer #buttClose').focus();
					});
				});
			});
		},
		close: function (dialog) {
			dialog.content.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$j.modal.remove(dialog);
					});
				});
			});
		}
	};
}


/* ##### ACCESSIBILITY */

// Font Size Buttons
function hi_accessibilityFontSize()
{
	if ($j(".fontSize").length > 0)
	{
		$j(".fontSize").html('<a href="#" class="increase" alt="Increase font size" title="Increase font size"><span class="increaseIcon"></span></a><a href="#" class="decrease" alt="Decrease font size" title="Decrease font size"><span class="decreaseIcon"></span></a>');
		$j(".fontSize .increase").click($j.FontSizer.IncreaseSize);
		$j(".fontSize .decrease").click($j.FontSizer.DecreaseSize);
	
		var options = { min: 80, max: 130};
		$j.FontSizer.Init(options);
	}
}



/* ##### PAGE ANCHORS SMOOTH SCROLL */

function hi_init_anchorSmoothScroll()
{
	// If this is IE6, do not smooth scroll (doesn't play nicely)
	if (!((jQuery.browser.msie) && (parseInt(jQuery.browser.version) == 6)))
	{
		var nodes = $j('a[href*=#]');
		for(var i = 0; i < nodes.length; i++) 
		{
			if ($j(nodes[i]).attr("href") != "#")
			{
				$j(nodes[i]).click(function() 
				{
					if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
					&& location.hostname == this.hostname) {
						var $jtarget = $j(this.hash);
						$jtarget = $jtarget.length && $jtarget
						|| $j('[name=' + this.hash.slice(1) +']');
						if ($jtarget.length) {
						var targetOffset = $jtarget.offset().top;
						$j('html,body')
						.animate({scrollTop: targetOffset}, 1000);
						 return false;
						}
					}
				});
			}
		}
	}
}


// Target blank new windows
function hi_init_externalLinkTarget()
{
	// Add attribute target="_blank" to all anchor elements with class="externalLink"
	$j("a.externalLink").attr("target", "_blank");
}

/* ########################################################################### *
/* ##### JQUERY & OTHER UTILITIES
/* ########################################################################### */

// Supply a query string and parse it into a key value pairs

jQuery.query = function(s) 
{
		var r = {};
		if (s) {
				var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
				q = q.replace(/\&$j/, ''); // remove the trailing &
				jQuery.each(q.split('&'), function() {
						var splitted = this.split('=');
						var key = splitted[0];
						var val = splitted[1];
						// convert numbers
						if (/^[0-9.]+$j/.test(val)) val = parseFloat(val);
						// convert booleans
						if (val == 'true') val = true;
						if (val == 'false') val = false;
						// ignore empty values
						if (typeof val == 'number' || typeof val == 'boolean' || val.length > 0) r[key] = val;
				});
		}
		return r;
};

/* ########################################################################### *
/* ##### PRESENTATION
/* ########################################################################### */


/* ##### PNG FIX */

function hi_pngfix()
{
	$j('img[src*=png]').parent().pngFix(); 
}



/* ##### TABLE ALTERNATE ROWS */

function hi_tableAltRows()
{
	$j('.highlightRows tr').mouseover(function(){$j(this).addClass('over');}).mouseout(function() {$j(this).removeClass('over');});
	$j('.hi_tableAltRows tr:odd').removeClass('alt');
	$j('.hi_tableAltRows tr:even').addClass('alt');
	$j('.hi_tableAltRows tr').removeClass('last');
	$j('.hi_tableAltRows tr:first-child').addClass('first');	
	$j('.hi_tableAltRows tr:last-child').addClass('last');
}



/* ##### IE6 TWEAKS */

// Enable functionality if the user's browser is IE
function hi_ieTweaks()
{
	// If browser is IE6
	if ((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)==6)) 
	{
		hi_input_padding();
		hi_lists();
	}
	
	// If browser is IE7
	if ((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)==7)) 
	{
		hi_lists();
	}	
}

// Adds a class to checkbox and radio buttons if IE
function hi_input_padding()
{
	$j('input[type=checkbox],input[type=radio]').addClass('nopad');
}

// Adds .first and .last classes to simulate :first :last pseudo classes on list items.
function hi_lists()
{
	$j('ul li:first-child').addClass("first");
	$j('ul li:last-child').addClass("last");
}




/* ##### IFRAME RESIZE */
/* 
	 NOTE: The iframeLoaded() function is called on the iframe onLoad event.
*/

function iframeLoaded()
{
	var applicationIframe = this.parent.document.getElementById("applicationIframe");
	var siteletIframe = this.parent.document.getElementById("siteletIframe");

	if (applicationIframe != null)
	{
		resizeIframe(applicationIframe);
	}
	
	if (siteletIframe != null)
	{
		resizeIframe(siteletIframe);
	}

	var IFRAME_WRAPPER_ELEMENT_ID = "contentWrapper";
	var NAV_COLUMN_ELEMENT_ID = "rightColumn";
	
	// Sets the iframe wrapper element to have a height greater than the nav column.
	var leftcol = $j(NAV_COLUMN_ELEMENT_ID);
	if (leftcol != null)
	{
		var outer = $j(IFRAME_WRAPPER_ELEMENT_ID);
		if (outer != null && leftcol.offsetHeight > outer.offsetHeight)
		{
			outer.style.height = leftcol.offsetHeight + 50 + "px";
		}
	}
	
	return true;
};

function resizeIframe(iframeRef)
{
	iframeRef.style.height = "auto";
	iframeRef.style.overflow = "hidden";
	pageDimensions = getPageDimensions(iframeRef.contentWindow);
	iframeRef.style.height = pageDimensions[1] + "px";
	return true;
};

function getPageDimensions(windowRef)
{
	var body = windowRef.document.getElementsByTagName("body")[0];
	var bodyOffsetWidth = 0;
	var bodyOffsetHeight = 0;
	var bodyScrollWidth = 0;
	var bodyScrollHeight = 0;
	var pageDimensions = [0, 0];

	if (typeof windowRef.document.documentElement != "undefined" && typeof document.documentElement.scrollWidth != "undefined")
	{
		pageDimensions[0] = windowRef.document.documentElement.scrollWidth;
		pageDimensions[1] = windowRef.document.documentElement.scrollHeight;
	}

	bodyOffsetWidth = body.offsetWidth;
	bodyOffsetHeight = body.offsetHeight;
	bodyScrollWidth = body.scrollWidth;
	bodyScrollHeight = body.scrollHeight;

	if (bodyOffsetWidth > pageDimensions[0])
	{
		pageDimensions[0] = bodyOffsetWidth;
	}

	if (bodyOffsetHeight > pageDimensions[1])
	{
		pageDimensions[1] = bodyOffsetHeight;
	}

	if (bodyScrollWidth > pageDimensions[0])
	{
		pageDimensions[0] = bodyScrollWidth;
	}

	if (bodyScrollHeight > pageDimensions[1])
	{
		pageDimensions[1] = bodyScrollHeight;
	}

	return pageDimensions;
}





/* FIN */
