/*
********** INITIALISE ON DOCUMENT READY *
-----------------------------------------
*/
$(document).ready( function() { //When the document is ready

	/* Add class to body (can be used to trigger styles when js is enabled) **********/
	$("body").addClass("js-on");
	
	/* Apply first-child fix (:first-child not supported in ie6) **********/
	$('#nav-footer li:first-child').addClass('first-child');
	$('.feature-news ul li:first-child').addClass('first-child');
	
	/* Insert span into table caption (caption styling not supported in ie6 & ie7) **********/
	$('caption').wrapInner('<span></span>');
	
	/* Open external links in a new window **********/
	$('a[rel="external"],a[href*=.pdf],a[href*=.docs],a[class="ext"]')
	.attr({
		target: "_blank",
		title: "Opens in a new window"
	})
	.addClass('ext');
	
	/* Show & hide text input values **********/
	$('input[type="text"], textarea').focus(function() {
		value=$(this).val();
		$(this).attr("value","");
	});
	$('input[type="text"], textarea').blur(function() {
		if($(this).val()=="") {
			$(this).val(value);
		}
	});
	
	/* Make whole panel clickable **********/
	$(".panel, .feature-news li").click(function(){ 
		//window.location=$(this).find("a").attr("href");
		if($(this).find("a").attr("rel")=="external" || $(this).find("a").attr("target")=="_blank")
		{
			window.open($(this).find("a").attr("href")); //Open external links in a new window
		}
		else
		{
			window.location=$(this).find("a").attr("href");
		}
		return false;
	});
	$(".panel, .feature-news li").hover( //Change hover states
		function () {
			$(this).css('cursor','pointer');
			$(this).children().css('text-decoration','underline');
		},
		function () {
			$(this).children().css('text-decoration','none');
		}
	);
	
	/* Zoom image on spotlight panel **********/
	$('.spotlight-panels .panel img').wrap("<span></span>"); //Contain the image
	$('.spotlight-panels .panel').mouseenter(function(e) { //Apply zoom effect on hover
		$(this).children('span').children('img').animate({ height: '110', left: '0', top: '0', width: '237'}, 100);
	}).mouseleave(function(e) {
		$(this).children('span').children('img').animate({ height: '100', left: '-20', top: '-20', width: '216'}, 100);
	});
	
	/* Equal heights **********/
	$('.landing .spotlight-panels').equalHeights();
	$('.landing .feature-panel').equalHeights();
	
	/* Easy List Splitter **********/
	$('.sitemap').easyListSplitter({ 
		colNumber: 4, // Insert here the number of columns you want. Consider that the plugin will create the number of cols requested only if there's enough items in the list.
		direction: 'vertical'
	});
	
	/* Letter builder tool **********/
	//Accordion
	$(".lbt-trigger-section fieldset").hide();
	$(".lbt-trigger-section h2").click( function(){ //When heading is clicked show/hide fieldset
		if($(this).next('fieldset').is(":hidden")){
			$(this).addClass('selected');
			$(this).next('fieldset').slideDown('fast');
		}else{
			$(this).removeClass('selected');
			$(this).next('fieldset').slideUp('fast');
		}
	})
	//Build letter
	$('.lbt-copy').addClass("dormant"); //Hide the copy icon until something is checked
	$('.lbt-copy-section').hide();
	$('.lbt-trigger-section fieldset div input').attr("checked", false); //Reset checkboxes
	$('.lbt-trigger-section fieldset div input').click(function() { //When checkbox is triggered show/hide content
		var triggerID = $(this).attr("id");
		var contentID = "#" + $(this).attr("id") + "-content";
		if ($(this).attr("checked")) {
			$(contentID).show();
			$('.lbt-copy').removeClass("dormant"); //Show the copy icon
		}else{
			$(contentID).hide();
		}
	});
	
	/* Registration form (United States extra options) **********/
	$('#form_reg_form #options_for_usa').hide(); //Hide options
	$('#form_reg_form #fld_location').change( function(){
		var location = $(this).val(); //On select box change
		if(location == "US"){
			$('#form_reg_form #options_for_usa').show();
		} else {
			$('#form_reg_form #options_for_usa').hide();	
		}
	});
	$('#form_reg_form #fld_location').change(); //Keeps options open if page is refreshed with United States selected
	
	/* Smooth scrolling for in-page links **********/
	$('#content').localScroll();
	
}); //End When the document is ready
