////////////////////////////////////////////////////
//
// MASTER.JS
//
// This file sets up 'ready' and 'onload'
// events for each document/window of the site. 
// It also declares any functions used by 
// these two events.
//
// Created 22/02/2009 by Chris Jones
//
////////////////////////////////////////////////////
// AMENDMENT 0.1
//
// Amendment Details ...
//
// Amended dd/mm/yyyy by ...
//
////////////////////////////////////////////////////
// ==============
// Initialisation
// ==============

// Ensure no conflict between libraries
var jQ = jQuery.noConflict();

// Get the name of the current page
var currPage = justTheFilename(self.location.pathname);
//var encDiv = enclosingDivFromFilename(currPage);


// ======
// Events
// ======

/***************************************************
 * Set-up the 'ready' event (used by jQuery)
 ***************************************************/
jQ(document).ready(function(){

	// Display the addthis button and attach draggable 
	// events to the pop-up window
	jQ('#addthis_button').css('display', 'block');
	jQ('#at16p').live("mouseover",function(){
		jQ('#at16p').draggable({
			handle:'#at16pt',
			scroll:false
		});
	});
	jQ('#at16p').live("mouseout",function(){
		jQ('#at16p').draggable('destroy');
	});

	// Setup click events on the leftbar menu
	jQ('#menubar > ul > li > a').click(function(){
		var priLink = jQ(this).attr('href');
		var children = null;
		if(children = jQ(this).parent().children('ul').children('li').length){
			if(children == 1){ return true; }
			var matchFound = false;
			jQ(this).parent().children('ul').children('li').each(function(){
				if(jQ(this).children('a').attr('href') == priLink) {
					jQ('#menubar > ul > li > ul:not(.activeprimary)').not(jQ(this).parent()).hide(500);
					jQ(this).parent().slideToggle(500);
					matchFound = true;
					return false;
				}
			});
			return !matchFound;
		}
	});
	
	// Run the content specific JavaScript
	if (typeof window.runContentJavascriptReady == "function"){
		runContentJavascriptReady();
	}

});

/***************************************************
 * Set up an 'onload' event for the window
 ***************************************************/
window.onload = loadPage;

function loadPage(){
	// Indicate that the page has loaded
	pageLoaded = 1;
	
	// Call the Google Analytics code
	if (typeof window.callAnalytics == "function"){
		callAnalytics();
	}
	
	// Setup a resize event on the browser window
	jQ(window).resize(function(){
		resizeFlash();
		resizeImageReflection();
	});

/*	
	// Setup text resize event on the browser window
	TextResizeDetector.TARGET_ELEMENT_ID = 'wrapper';			// id of element to check for and insert control
	TextResizeDetector.USER_INIT_FUNC = setEventForFontResize;	// function to call once TextResizeDetector has init'd
	// The text resize buttons
	setupTextResizer();
*/

	// Run the content specific JavaScript
	if (typeof window.runContentJavascriptOnLoad == "function"){
		runContentJavascriptOnLoad();
	}

}


// =========
// FUNCTIONS
// =========

/***************************************************
 * This function sets up a listener event for when
 * default font size is changed.
 ***************************************************/
function setEventForFontResize(){
    var iBase = TextResizeDetector.addEventListener(onFontResize,null );
}

/***************************************************
 * This function is called when the default font
 * size changes.
 ***************************************************/
function onFontResize( e, args ){
	// Resize the accordion and content area
	// Run the content specific JavaScript
	if (typeof window.browserFontResize == "function"){
		browserFontResize();
	}
}

/***************************************************
 * This function is called when the default font
 * size is changed via the browser.
 ***************************************************/
function browserFontResize(){
//	var cookie_name = "FILLMEIN-FontSize";
//	var currentFontSize = jQ("#fontresizedetection").height();
//	var currentFontSizeNum = parseFloat(currentFontSize, 10);
//alert('BFR - ' + currentFontSizeNum);
//	jQ.cookie(cookie_name, currentFontSizeNum);
}

/***************************************************
 * JQuery Text resize + Cookie recall
 * by Jonny Kamaly
 * based on script written by Faisal & Homar
 ***************************************************/
function setupTextResizer(){
	// show text resizing links
	jQ(".adjustFontSize").show();
	var cookie_name = "FILLMEIN-FontSize";
	var originalFontSize = parseFloat(jQ("body").css("font-size"), 10);
	// if exists load saved value, otherwise store it
	if(jQ.cookie(cookie_name)) {
		var getSize = jQ.cookie(cookie_name);
		jQ("body").css({fontSize : getSize + (getSize.indexOf("px")!=-1 ? "" : "px")}); // IE fix for double "pxpx" error
	} else {
		jQ.cookie(cookie_name, originalFontSize);
	}
	// reset link
	jQ(".FontSizeReset").bind("click", function() {
		jQ("body").css({fontSize : originalFontSize+"px"});
//		jQ("body").animate({fontSize : originalFontSize+"px"});
		jQ.cookie(cookie_name, originalFontSize);
	});
	// text “+” link
	jQ(".FontSizeInc").bind("click", function() {
		var currentFontSize = jQ("body").css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum+2;
		if (newFontSize <= 20) {
		  jQ("body").css({fontSize : newFontSize+"px"});
//		  jQ("body").animate({fontSize : newFontSize+"px"});
		  jQ.cookie(cookie_name, newFontSize);
		}
		return false;
	});
	// text “-” link
	jQ(".FontSizeDec").bind("click", function() {
		var currentFontSize = jQ("body").css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum-2;
		if (newFontSize >= 6) {
		  jQ("body").css({fontSize : newFontSize+"px"});
//		  jQ("body").animate({fontSize : newFontSize+"px"});
		  jQ.cookie(cookie_name, newFontSize);
		}
		return false;
	});
}


/***************************************************
 * This function ...
 ***************************************************/
function justTheFilename(thisFile) {
	if (thisFile.indexOf("/") > -1) {
		thisFile = thisFile.substring(thisFile.lastIndexOf("/")+1);
	}

	if (thisFile.indexOf("?") == 0) {
		thisFile = thisFile.substring(1);
	}
	if (thisFile.indexOf(".") > -1) {
		thisFile = thisFile.substring(0,thisFile.lastIndexOf("."));
	}

	return thisFile;
}


/***************************************************
 * This function ...
 ***************************************************/
function enclosingDivFromFilename(thisFile) {
	
	thisFile = justTheFilename(thisFile);

	if (thisFile.indexOf("_") > -1) {
		thisFile = thisFile.substring(0,thisFile.lastIndexOf("_"));
	}

	return thisFile;
}

/***************************************************
 * This function returns numeric portion of a 
 * string.
 ***************************************************/
function str2num(size) {
	if ((isNaN(parseFloat(size,10))) || (size == undefined)) {
		return 0;
	} else {
		return parseFloat(size,10);
	}
}

