/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  ecl_global.js
 * ##### VERSION: v1.0 (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_forms_checkAllCheckboxes();	

	// Forms: Select on click
	//ecl_init_forms_selectOnClick();
	
	// 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();
	
	// Buttons
	//ecl_forms_buttons();
}



/* ########################################################################### *
/* ##### FUNCTIONALITY
/* ########################################################################### */


/* ##### FORMS */

// Check all checkboxes
// Note: This only supports 1 check all button.
// DESC: Mark all checkable checkboxes with class .ecl_forms_checkableWithAll, mark the 'click to check all' checkbox with .ecl_forms_checkAllCheckboxes
function ecl_forms_checkAllCheckboxes()
{
	$('.ecl_forms_checkAllCheckboxes').click(function()
	{
		var checkedStatus = this.checked;
		$('.ecl_forms_checkableWithAll').each(function()
		{
			this.checked = checkedStatus;
		});
	});
}



// Select contents of input on click
function ecl_init_forms_selectOnClick()
{
	if ($(".ecl_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
		$(".ecl_forms_selectOnClick").each(function() 
			{
				$(this).val($(this).attr("title"));
			});
		
		// Handle this element's focus event
		$(".ecl_forms_selectOnClick").focus(function()
		{
			// Get the value from the elements title
			var searchTextOff = $(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 ($(this).val() == searchTextOff)
			{
				$(this).val(SEARCH_TEXT_ON);
			}
		});
		
		// Handle this element's blur event
		$(".ecl_forms_selectOnClick").blur(function()
		{
			// Get the value from the elements title
			var searchTextOff = $(this).attr("title");
			
			// If the value of the element is empty set it to the default value (taken from the element title)
			if ($(this).val() == "")
			{
				$(this).val(searchTextOff); 
			}
		});	
	}
}



/* Form buttons (including rounded buttons with background image) */

function ecl_forms_buttons()
{
	// If browser is not IE6 (or less)
	if ($.browser.msie == false || ($.browser.msie && $.browser.version > 6)) 
	{
		// Get the buttons that should be effected
		var nodes = $("button.button,a.button");
		for (var i = 0; i < nodes.length; i++)
		{
			// If this is a primary action button
			if ($(nodes[i]).hasClass("primary"))
			{
				$(nodes[i]).addClass("button_curve_primary");
				$(nodes[i]).after("<div class='buttonEnd_primary'></div>");
				$(nodes[i]).hover(buttonHoverOver_primary, buttonHoverOut_primary);
			} else {
				$(nodes[i]).addClass("button_curve"); 
				$(nodes[i]).after("<div class='buttonEnd'></div>");         
				$(nodes[i]).hover(buttonHoverOver, buttonHoverOut);
			}
		}
	}
}
 
function buttonHoverOver()
{
	$(this).addClass("button_hover");
	$(this).next("div").addClass("buttonEnd_hover");
}
 
function buttonHoverOut()
{
	$(this).removeClass("button_hover");
	$(this).next("div").removeClass("buttonEnd_hover");
}
 
function buttonHoverOver_primary()
{
	$(this).addClass("button_primary_hover");
	$(this).next("div").addClass("buttonEnd_primary_hover");
}
 
function buttonHoverOut_primary()
{
	$(this).removeClass("button_primary_hover");
	$(this).next("div").removeClass("buttonEnd_primary_hover");
}



/* ##### TOOLTIPS */

function ecl_tooltip()
{
	$('.fieldinfo').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fixPNG: true,
		//showBody: " - ",
		extraClass: "pretty",
		top: -24,
		left: 6
	});
}



/* ##### 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="message" style="display:none"></div><div id="contentContainer"><div class="loading"></div></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="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>');
		$(".fontSize .increase").click($.FontSizer.IncreaseSize);
		$(".fontSize .decrease").click($.FontSizer.DecreaseSize);
	
		var options = { min: 80, max: 130};
		$.FontSizer.Init(options);
	}
}



/* ##### PAGE ANCHORS SMOOTH SCROLL */

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 */
