/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  ecl_global.js
 * ##### VERSION: v1.0
 * ##### UPDATED: 22/07/08 (Eclipse Group)
/* ########################################################################### *

/* ########################################################################### *
/* ***** 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
/* ########################################################################### */

$(document).ready(ecl_init_globalFunctionality);
$(document).ready(ecl_init_globalPresentation);

function ecl_init_globalFunctionality()
{
	// Tooltips
	ecl_tooltip();
	
	// Popup window
	ecl_popup_window();
	
	// Modal window
	ecl_init_modalWindow();

	// Forms
	ecl_checkAllCheckboxes();	
	
	// Accessibility: Font Size Buttons
	ecl_accessibilityFontSize();	
	
	// Enable smooth anchor scrolling
	ecl_init_anchorSmoothScroll();
	
	// Support insert target="_blank" on class="externalLink" anchors
	ecl_init_externalLinkTarget();
}

function ecl_init_globalPresentation()
{
	// Homepage Random Images
	ecl_randomImage();	
	
	// Table alternate rows
	ecl_tableAltRows();

	// Internet Explorer specific tweaks
	ecl_ieTweaks();
	
	// PNG fix
	ecl_pngfix();
}



/* ########################################################################### *
/* ##### FUNCTIONALITY
/* ########################################################################### */



/* ##### FORMS */

// Check all checkboxes
// Note: This only supports 1 check all button.
// DESC: Mark all checkable checkboxes with class .ecl_checkableWithAll, mark the 'click to check all' checkbox with .ecl_checkAllCheckboxes
function ecl_checkAllCheckboxes()
{
	$('.ecl_checkAllCheckboxes').click(function(){
		var checkedStatus = this.checked;
		$('.ecl_checkableWithAll').each(function(){
			this.checked = checkedStatus;
		});
	});
}



/* ##### TOOLTIPS */

function ecl_tooltip()
{
	$(".fieldinfo").each(function(){
		$(this).tooltip({
			track: true,
			delay: 0,
			showURL: false,
			showBody: " - ",
			opacity: 1,
			fixPNG: true,
			extraClass: "pretty",
			top: -40,
			left: 10
		});
	});
}



/* ##### POPUP WINDOW */

function ecl_popup_window() 
{
	$('.popUp').click(function (e) {
		e.preventDefault();
		var targetHref = $(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 WINDOW */

// Generic modal window
function ecl_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>';
	
	$('.modalWindow').click(function (e) 
	{
		e.preventDefault();
		
		// create a modal dialog with the data
		$(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 () {
						$('#modalContainer #buttClose').focus();
					});
				});
			});
		},
		close: function (dialog) {
			dialog.content.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.remove(dialog);
					});
				});
			});
		}
	};
}

// Modal window
function ecl_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
				$(dialog.container).width(windowWidth);

				// Set the title of the window
				$(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();
				}
				$(dialog.container).find('#buttClose').focus();
				
			});
		},
		close: function (dialog) {
			$("#contentContainer").hide();
			
			dialog.content.hide();
			dialog.container.hide();
			dialog.overlay.fadeOut(500, function () 
				{
					$.modal.remove(dialog);
				});
		
		}
	};
	
	// create a modal dialog with the data
	$(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 ecl_init_ajaxModalWindow(htmlUrl)
{
	$('.ajaxModalWindow').click(function (e) 
	{
		e.preventDefault();
		
		// load the page content via ajax
		$.get(htmlUrl, function(data)
		{
			// create a modal dialog with the data
			$(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 () {
						$('#modalContainer #buttClose').focus();
					});
				});
			});
		},
		close: function (dialog) {
			dialog.content.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.remove(dialog);
					});
				});
			});
		}
	};
}


/* ##### ACCESSIBILITY */

// Font Size Buttons
function ecl_accessibilityFontSize()
{
	if ($(".fontSize").length > 0)
	{
		$(".fontSize").html('<a href="#" class="decrease" alt="Decrease font size" title="Decrease font size"><span class="decreaseIcon"></span></a><a href="#" class="increase" alt="Increase font size" title="Increase font size"><span class="increaseIcon"></span></a>');
		$(".fontSize .increase").click($.FontSizer.IncreaseSize);
		$(".fontSize .decrease").click($.FontSizer.DecreaseSize);
	
		var options = { min: 80, max: 130};
		$.FontSizer.Init(options);
	}
}



// Smooth scroll to in page anchors
function ecl_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 = $('a[href*=#]');
		for(var i = 0; i < nodes.length; i++) 
		{
			if ($(nodes[i]).attr("href") != "#")
			{
				$(nodes[i]).click(function() 
				{
					if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
					&& location.hostname == this.hostname) {
						var $target = $(this.hash);
						$target = $target.length && $target
						|| $('[name=' + this.hash.slice(1) +']');
						if ($target.length) {
						var targetOffset = $target.offset().top;
						$('html,body')
						.animate({scrollTop: targetOffset}, 1000);
						 return false;
						}
					}
				});
			}
		}
	}
}


// Target blank new windows
function ecl_init_externalLinkTarget()
{
	// Add attribute target="_blank" to all anchor elements with class="externalLink"
	$("a.externalLink").attr("target", "_blank");
}


/* ########################################################################### *
/* ##### PRESENTATION
/* ########################################################################### */


/* ##### PNG FIX */

function ecl_pngfix()
{
	$('img[src*=png]').parent().pngFix(); 
}



/* ##### HOMEPAGE RANDOM IMAGE */

function ecl_randomImage()
{
	/*
		Setup the path and number of images that you want to display below. 
		Add the integer (between and including MAX_NUM and MIN_NUM set below
		to the end of the graphic file, between the prefix and suffix path strings.
		An image will then be selected and inserted at random. This is random and
		doesn't currently support remembering history for the users browser (ie. cookies).
	*/
	var MAX_NUM = 2;
	var MIN_NUM = 1;
	var URL_PATH_PREFIX = 'media/images/img_randomPhoto_';
	var URL_PATH_SUFFIX = '.jpg';
	var IMG_WIDTH = "216";
	var IMG_HEIGHT = "209";
	var IMG_ALT = "";	
	
	if ($('#randomImage').length > 0)
	{		
		var HTML_STRING = '<img src="" width="' + IMG_WIDTH + '" height="' + IMG_HEIGHT + '" alt="' + IMG_ALT + '" />';
		var randomNumber = Math.floor(Math.random( ) * MAX_NUM - MIN_NUM + 1) + MIN_NUM;
		var randomImagePath = URL_PATH_PREFIX + randomNumber + URL_PATH_SUFFIX;
		
		$('#randomImage').html(HTML_STRING);
		$('#randomImage img').attr("src", randomImagePath);
	}
}



/* ##### TABLE ALTERNATE ROWS */

function ecl_tableAltRows()
{
	$('.highlightRows tr').mouseover(function(){$(this).addClass('over');}).mouseout(function() {$(this).removeClass('over');});
	$('.ecl_tableAltRows tr:odd').removeClass('alt');
	$('.ecl_tableAltRows tr:even').addClass('alt');
	$('.ecl_tableAltRows tr').removeClass('last');
	$('.ecl_tableAltRows tr:first-child').addClass('first');	
	$('.ecl_tableAltRows tr:last-child').addClass('last');
}



/* ##### IE6 TWEAKS */

// Enable functionality if the user's browser is IE
function ecl_ieTweaks()
{
	// If browser is IE6
	if ((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)==6)) 
	{
		ecl_input_padding();
		ecl_lists();
	}
	
	// If browser is IE7
	if ((jQuery.browser.msie)&&(parseInt(jQuery.browser.version)==7)) 
	{
		ecl_lists();
	}	
}

// Adds a class to checkbox and radio buttons if IE
function ecl_input_padding()
{
	$('input[type=checkbox],input[type=radio]').addClass('nopad');
}

// Adds .first and .last classes to simulate :first :last pseudo classes on list items.
function ecl_lists()
{
	$('ul li:first-child').addClass("first");
	$('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 = $(NAV_COLUMN_ELEMENT_ID);
	if (leftcol != null)
	{
		var outer = $(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 */
