﻿$(document).ready(function() {

	// Set Up the Page Tools that require JavaScript to work
	$('div#pagetools li#print').show();
	$('div#pagetools li#print a').attr('href', window.location)
	$('div#pagetools li#emailAFriend').show();
	$('div#pagetools li#emailAFriend a').attr('href', window.location)

	$('div.emailAFriendThankyou').appendTo('div#footer');
	$('div.emailAFriendForm').appendTo('div#footer');
	$('div.emailFormContainer').remove();

	$('div#pagetools li#print a').click(function() {
		window.print();
		return false;
	});

	// Navigation
	$('div#navigation a.nav').mouseover(function() {
		var idBit = $(this).attr('id');
		var currentId = 'nav2-' + idBit.slice(idBit.lastIndexOf('_') + 1);

		$('div[id|=nav2]').each(function() {
			if ($(this).attr('id') == currentId) {
				$(this).show();
			}
		});
	});

	$('div.navigation li.nav3').mouseleave(function() { $('div[id|=nav2]').hide(); });

	// Dropdown Menu in Navigation
	$('div#navigation [id^=menu] li ul').hide();

	$('div#navigation [id^=menu] li').hover(
    	function() {
    		$(this).children('ul').show();
    		$(this).css('background-color', '#201c3e');
    	}, function() {
    		$(this).children('ul').hide();
    		$(this).css('background-color', '#6f6b95');
    	});

	$('div#navigation li.nav3').hover(
        function() {
        	$(this).css('background-color', '#6f6b95');
        }, function() {
        	$(this).css('background-color', '#201c3e');
        });



	// Adds confirm dialogue to email links
	$('.site-container a[href^=mailto:]').click(function(e) {
		e.preventDefault();
		thisHref = $(this).attr('href');
		var confirmDiv = $('#disclaimer');

		if ($(confirmDiv).is(":hidden")) {
			$(confirmDiv).center().fadeIn();
		}

		$('#disclaimer #acceptButton').live('click', function() {
			window.location = thisHref;
			$(confirmDiv).hide();
			return false;
		});

		$('#disclaimer #cancelButton').live('click', function() {
			$(confirmDiv).fadeOut(300, function() {
				return false;
			});
		});
	});

	$('#disclaimer-contact').click(function(e) {
		e.preventDefault();
		$('#disclaimer-contactus').center().fadeIn();
		$('#disclaimer-contactus #acceptButton-contactus').click(function() {
			$('#disclaimer-contactus').fadeOut();
		});
	});

	// Email a friend box
	$('#emailAFriendFormButton').mousedown(function() { $('div.emailAFriendForm').center().show(); });
	$('#emailAFriendFormClose').click(function() { $('div.emailAFriendForm').fadeOut(); });

});

// Open links in new window
$(function() {
	$('a.new-window').click(function() {
		window.open(this.href);
		return false;
	});
});

// Centre-positions an element
$.fn.center = function() {
	this.css("position", "absolute");
	//this.css("top", ($(window).height() - this.outerHeight()) / 2 + $(window).scrollTop() + this.position().top + "px");
	this.css("top", this.position().top + 200 + "px");
	this.css("left", ($(window).width() - this.outerWidth()) / 2 + $(window).scrollLeft() - 152 + "px");
	return this;
}



// Fade out for email a friend thank you
function fadeOutEmailAFriend() { $('div.emailAFriendThankyou').center().show().delay(1600).fadeOut(400); }

// Display for email a friend error
function emailAFriendError() { $('div.emailAFriendForm').center().show(); }

// Checks image has loaded
$.fn.imageLoad = function(fn) {
	this.load(fn);
	this.each(function() {
		if ($(this).get(0).complete && $(this).get(0).naturalWidth !== 0) {
			$(this).trigger('load');
		}
	});
}

