var initFontSizeChanger = function () {
	var baseFontSize = '62.75';
	if (!$.cookie("dffAccessibilityBaseFontSize")) {
		$.cookie("dffAccessibilityBaseFontSize", baseFontSize);
	}
	var baseFontSizeCurrent = $.cookie("dffAccessibilityBaseFontSize");
	if (baseFontSizeCurrent != baseFontSize) {
		$('body').css({ fontSize: baseFontSizeCurrent+'%' });
	}
	
	// Display the buttons
	$('.accessibilityControls').css({ display: 'inline' });
	
	// Initialise the buttons
	$('.accessibilityControls a').each(function () {
		$(this).click(function() {
			switch(this.className) {
				case 'decrease':
					baseFontSizeCurrent = Number(baseFontSizeCurrent) - 5;
					if (baseFontSizeCurrent < 40) {
						baseFontSizeCurrent = 40;
					}
					break;
				case 'increase':
					baseFontSizeCurrent = Number(baseFontSizeCurrent) + 5;
					if (baseFontSizeCurrent > 80) {
						baseFontSizeCurrent = 80;
					}
					break;
				case 'normal':
				default:
					baseFontSizeCurrent = baseFontSize;
					break;
			}
			$.cookie("dffAccessibilityBaseFontSize", baseFontSizeCurrent);
			$('body').css({ fontSize: baseFontSizeCurrent+'%' });
		});
	});
}

var initFlash = function () {
	var validTags = new Array('img');//'div', (Not sure how to get the src / href from div)
	for (var i = 0; i < validTags.length; i++) {
		$(validTags[i] + '.dynamicFlash').each(function () {
			if (!this.id || this.id == '') {
				this.id = 'dynamicFlash_' + i;
			}
			var src = this.src.replace(/\.(gif|jpg|png|jpeg)$/i, '.swf');
			
			// Check if parent is an anchor
			var bannerLink = '';
			var elements = $(this).parents();
			for (var j in elements) {
				if (typeof elements[j] != 'function') {
					if (elements[j].tagName == 'A') {
						bannerLink = elements[j].href;
						break;
					}
				}
			}
			
			// embedSWF: function (swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn)
			swfobject.embedSWF(src, this.id, this.width, this.height, "9.0.0", null, { bannerLink: bannerLink }, {}, {});
		});
	}
}

var initListTreeExpanding = function () {
	// Quick check to see if component is used
	if ($('dl.listTreeExpanding').length < 1) {
		return false;
	}
	
	$('dl.listTreeExpanding dd').hide();
	$('dl.listTreeExpanding dt').attr('class', 'closed');
	$('dl.listTreeExpanding dt').click(function () {
		if ($(this).next().is('dd')) {
			$(this).next().toggle('fast', function () {
				if ($(this).css('display') == 'none') {
					$(this).prev().attr('class', 'closed');
				} else {
					$(this).prev().attr('class', 'opened');
				}
			});
			return false;
		}
		return true;
	});
}

var initContentPromoBoxTabs = function () {
	// Quick check to see if component is used
	if ($('div.contentPromoBox.withTabs').length < 1) {
		return false;
	}
	
	// List of all the anchor elements
	var contentPromoBoxTabs = new Array();
	
	// A copy of the list of all the anchor elements
	var contentPromoBoxTabsCopy = new Array();
	
	// Load all the links
	$('div.contentPromoBoxTabs a.tab').each(function () {
		var hash = this.hash.match(/^\#(.+)/);
		if (hash) {
			contentPromoBoxTabs.push(hash[1]);
			contentPromoBoxTabsCopy.push(hash[1]);
			$(this).click(function () {
				// Show the tab and hide others
				$('div.contentPromoBoxTabs a.tab').attr('class', 'tab');
				for (var i = 0; i < contentPromoBoxTabs.length; i++) {
					if (contentPromoBoxTabs[i] == hash[1]) {
						$('#' + contentPromoBoxTabs[i]).show();
						$(this).attr('class', 'tab active');
					} else {
						$('#' + contentPromoBoxTabs[i]).hide();
					}
				}
				
				if (window.event) { window.event.cancelBubble = true; }
				return false;
			});
		}
	});
	
	// Attach the detected tab links to the tabs in the order it appears in the html
	$('div.contentPromoBox.withTabs').each(function () {
		$(this).attr('id', contentPromoBoxTabsCopy.shift());
	});
	
	// Hide all the tabs except first one
	for (var i = 1; i < contentPromoBoxTabs.length; i++) {
		$('#' + contentPromoBoxTabs[i]).hide();
	}
}

var initCloseButton = function () {
	// Quick check to see if component is used
	if ($('a.closeButton').length < 1) {
		return false;
	}
	
	$('a.closeButton').css({ display: 'inline' });
	$('a.closeButton').click(function () {
		window.close();
	});
}

var initInputDefaultText = function () {
	// Quick check to see if component is used
	if ($('input[type="text"].defaultText').length < 1) {
		return false;
	}
	
	$('input[type="text"].defaultText').each(function () {
		if ($(this).attr('class').indexOf('defaultTextIdle') == -1) {
			$(this).attr('class', $(this).attr('class') + ' defaultTextIdle');
		}
		
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	$('input[type="text"].defaultText').focus(function () {
		$(this).attr('class', $(this).attr('class').replace(' defaultTextIdle', ''));
		
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('input[type="text"].defaultText').blur(function () {
		if ($(this).attr('class').indexOf('defaultTextIdle') == -1) {
			$(this).attr('class', $(this).attr('class') + ' defaultTextIdle');
		}
		
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
}

var initPrint = function () {
	if (window.print) {
		$('p.print').show();
		$('p.print a').click(function () {
			window.print();
			if (window.event) { window.event.cancelBubble = true; }
			return false;
		});
	}
}

var initIe6Fixes = function () {
	// Fix caching issue in ie6
	document.execCommand("BackgroundImageCache", false, true);
}


/**
* for IE 6 remove the main nav and add it again at the end of the document
* which forces IE to redraw the nav and drop-down menu's after everything else
* which solves the many issues in making sure the drop-downs always appear on
* top of everything els
* 
*/
var initIe6FixHorizontalNavigation = function (){
	//alert($().jquery);
	if ( $.browser.msie ){
		//MICROSOFT IE
		var majorversion = parseInt($.browser.version );
		if(majorversion==6){
		
			// select main things we need
			var nav = $(".navigationHorizontal");
			var pag = $("#page");
			
			// record the position of the navigation element
			var pos = nav.position();
			
			// clone the navigation block
			var nav2 = nav.clone();
			
			// set the position to the same as it was before
			nav2.css({'position':'absolute','top':pos.top+'px','left':pos.left+'px','width':'960px'});
			
			// add the clone to the end of the page tag
			nav2.appendTo(pag);
			
			// hide the original nav
			nav.hide();

		}
	}
}

// Run startup scripts
$(document).ready(function () {
	initIe6FixHorizontalNavigation();

	initFontSizeChanger();
	initFlash();
	initListTreeExpanding();
	initContentPromoBoxTabs();
	initCloseButton();
	initInputDefaultText();
	initPrint();
	
	if ($.browser.msie) {
		initIe6Fixes();
	}

	
});