$(document).ready(function() {


	// Set everything up - hide and show things	
	
 	$("#formcollapse:not(.haserrors,.expanded)").hide();
 	$('input[type="text"]').addClass("idle");
 	$('textarea').addClass("idle");
 	$("#newslink").addClass("grey");
	$("#searchinput").addClass("grey");
	$("#eventsbox").hide();
	
	// video listing - expand/collapse
 	$(".newssummaryplayvideo.id1:not(.expanded)").hide();
	$('.playvideo.id1').click(function(){
		$('.newssummaryplayvideo.id1').slideToggle();
		$('.newssummaryplayvideo.id2').hide();
		$('.newssummaryplayvideo.id3').hide();
		return false;
	});
	$(".newssummaryplayvideo.id2:not(.expanded)").hide();
	$('.playvideo.id2').click(function(){
		$('.newssummaryplayvideo.id2').slideToggle();
		$('.newssummaryplayvideo.id1').hide();
		$('.newssummaryplayvideo.id3').hide();
		return false;
	});
	$(".newssummaryplayvideo.id3:not(.expanded)").hide();
	$('.playvideo.id3').click(function(){
		$('.newssummaryplayvideo.id3').slideToggle();
		$('.newssummaryplayvideo.id1').hide();
		$('.newssummaryplayvideo.id2').hide();
		return false;
	});
	

	// Newsletters listing - text at the end of each item
	
	$(".newsletters #maincontent li:not(.newsletters #maincontent li:first) a").append(" <span>[view]</span>");
	$(".newsletters #maincontent li:first a").append(" <span>[new]</span>");
	
	$("#eventslink").click(function(){
		$("#newslink").removeClass("grey")
		$("#eventslink").addClass("grey")
		$(".currentboxcontent").hide();
		$("#eventsbox").show();
		return false;
	});
	$("#newslink").click(function(){
		$("#eventslink").removeClass("grey")
		$("#newslink").addClass("grey")
		$(".currentboxcontent").hide();
		$("#newsbox").show();
		return false;
	});
	$("#searchinput").focus(function(){
		$("#searchinput").removeClass("grey");
		$("#searchinput").addClass("white");
		return false;
	});
	$("#searchinput").blur(function(){
		$("#searchinput").removeClass("white");
		$("#searchinput").addClass("grey");
		return false;
	});
	
	
	// Toggling the comment form on news posts
	
	$('#postacomment').click(function(){
		$('#formcollapse').slideToggle();
		return false;
	});
	
	
	// Highlighting fields on forms when they have focus
	
	$('input[type="text"]:not(.search-input)').focus(function(){
		$(this).animate({
			borderTopColor:"#666",
			borderRightColor:"#666",
			borderBottomColor:"#666",
			borderLeftColor:"#666"},'fast');
     });  
     $('input[type="text"]:not(.search-input)').blur(function(){
     $(this).animate({
	     	borderTopColor:"#ccc",
			borderRightColor:"#ccc",
			borderBottomColor:"#ccc",
			borderLeftColor:"#ccc"},'fast');
     });
	$('textarea').focus(function() {  
		$(this).animate({
			borderTopColor:"#666",
			borderRightColor:"#666",
			borderBottomColor:"#666",
			borderLeftColor:"#666"},'fast');
     });  
     $('textarea').blur(function() {  
		$(this).animate({
			borderTopColor:"#ccc",
			borderRightColor:"#ccc",
			borderBottomColor:"#ccc",
			borderLeftColor:"#ccc"},'fast');
     });

	$("#latestnews .newsarticle").hover(
	  function () {
		$(this).css("cursor", "pointer");
		$(this).animate({"background-color": "#eee"}, "slow");
	  },
	  function () {
		$(this).css("cursor", "auto");
		$(this).animate({"background-color": "#fff"}, "slow");
	  }
	);

	$(".solutions ul#tabs li a").hover(
	  function () {
		$(this).animate({"background-color": "#7E0F18"}, "slow");
	  },
	  function () {
		$(this).animate({"background-color": "#B01522"}, "slow");
	  }
	);

	$("#latestnews .newsarticle").click(function(){
		var linkURL = $(this).find(".articlelink").attr("href");
		window.location = linkURL;
		return false;
		
	});
	
	/* Book promo - Feb 2011 */
	$("#top_expander").hide();
	$("#top_expander").delay(800).slideDown("slow");
	$("div#top_expander_control div.inner p.open").delay(800).hide();
	$("div#top_expander_control div.inner p.close").delay(800).show();
	
	// expander controls
	$("div#top_expander_control div.inner p.close").hover(
	  function () {
		$(this).css("cursor", "pointer");
		$(this).animate({"color": "#fff"}, "slow");
	  },
	  function () {
		$(this).css("cursor", "auto");
		$(this).animate({"color": "#ccc"}, "slow");
	  }
	);
	
	$("div#top_expander_control div.inner p.close").click(function(){
		$("#top_expander").slideUp("slow");
		$("div#top_expander_control div.inner p.close").hide();
		$("div#top_expander_control div.inner p.open").show();
	});
	
	$("div#top_expander_control div.inner p.open").click(function(){
		$("#top_expander").slideDown("slow");
		$("div#top_expander_control div.inner p.close").show();
		$("div#top_expander_control div.inner p.open").hide();
	});
	
	// preload images first (can run before page is fully loaded)
	//$.preloadImages("images/expander/btn_amazon_uk_on.gif", "images/expander/btn_amazon_us_off.gif");
	$("div#top_expander img.rollover").hover(
		function() {
			$(this).attr("src",$(this).attr("src").replace("_off","_on"));
		}, function() {
			$(this).attr("src",$(this).attr("src").replace("_on","_off"));
		}
	);

});
