$(document).ready(function() {
	
	//search bar focus
	$('#searchQuery').focus(function() {
		if ($('#searchQuery').val() == 'quick search') {
			$('#searchQuery').val('');	
		}
	});
	
	$('#searchQuery').blur(function() {
		if ($('#searchQuery').val() == '') {
			$('#searchQuery').val('quick search');	
		}
	});
	
	//search bar focus
	$('#searchQuery1').focus(function() {
		if ($('#searchQuery1').val() == 'quick search') {
			$('#searchQuery1').val('');	
		}
	});
	
	$('#searchQuery1').blur(function() {
		if ($('#searchQuery1').val() == '') {
			$('#searchQuery1').val('quick search');	
		}
	});
	
	
	
	//adv search
	$("#frm_startdate").datepicker(
		{
			yearRange : '1900:2099',
			dateFormat: 'dd/mm/yy',
			changeMonth: true,
			changeYear: true
		}
	);
	
	$("#frm_enddate").datepicker(
		{
			yearRange : '1900:2099',
			dateFormat: 'dd/mm/yy',
			changeMonth: true,
			changeYear: true
		}
	);
	
	
	//testimonial scroller
	var tScrollSpeed = 300;
	var tScrollInterval = 6000;
	var tItemCount = $('#testimonialBoxContent .item').size();
	var tCurrentItem = 1;
	var tTimer = setInterval(tSlideNext, tScrollInterval );
	
	$('#testimonialBoxContent .item:first-child').clone().appendTo('#testimonialBoxContent');
	$('#testimonialBoxContent').mouseover(function() { clearInterval(tTimer) });
	$('#testimonialBoxContent').mouseout(function() { tTimer = setInterval(tSlideNext, tScrollInterval ) });
	
	function tSlideNext() {
	
		$('#testimonialBoxContent').animate({ left: '-' + tCurrentItem*177 + 'px' }, tScrollSpeed, function() {
				
			tCurrentItem ++;
			if (tCurrentItem > tItemCount) {
				$('#testimonialBoxContent').css('left','0');
				tCurrentItem = 1;
				
			}
			
		});
		
	}
	
	/**
	 * User has ticked or unticked the super user mode checkbox
	 */
	$('#superUserCk').click(function(){
		
		// News and in focus items are a special case. We need to get the news_id get parameter and pass it on
		// when the form is submitted (it will be lost if we don't do this).
		var news_id = getQuerystring('news_id');
		var infocus_id = getQuerystring('infocus_id');
		
		if (news_id.length)
		{
			var news_id = $('<input>').attr('type', 'hidden').attr('name', 'news_id').val(news_id);
			$('#superUserModeForm').append(news_id);
		}
		else if (infocus_id.length)
		{
			var infocus_id = $('<input>').attr('type', 'hidden').attr('name', 'infocus_id').val(infocus_id);
			$('#superUserModeForm').append(infocus_id);
		}
						
		$('#superUserModeForm').submit();
	});
	
	// Get a querystring value.
	// see http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx
	function getQuerystring(key, default_)
	{
		if (default_ == null) 
		{
			default_="";
		}
		
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		
		var qs = regex.exec(window.location.href);
		
		if(qs == null)
		{
			return default_;
		}    
		else
		{
			 return qs[1];		
		}   
	}
	
});



////trial
//function trialPopup() {
//
//	var settings = {href:'trial.htm', title: '', iframe: true, innerWidth: 272, innerHeight: 578 };	
//	$.colorbox(settings);
//	
//}


//cta tabs
function ctaSelect(tab) {

	$('.ctaContent').hide();
	$('#cta'+tab+'Content').show();
	$('#ctaTabs div').removeClass('current');
	$('#cta'+tab).addClass('current');

}


//e-bulletin
function showBulletin() {
	$('#bulletinButton').slideUp('slow');
	$('#bulletinContent').slideDown('slow');
	$('#bulletinContentStep1').animate({"left": "0px"}, "slow");
	$('#bulletinContentStep2').animate({"left": "242px"}, "slow");
}


//function cancelBulletin() {
//	$('#bulletinButton').slideDown('slow');
//	$('#bulletinContent').slideUp('slow');
//}

function nextBulletin() {
	$('#bulletinContentStep1').animate({"left": "-242px"}, "slow");
	$('#bulletinContentStep2').animate({"left": "0px"}, "slow");
}

function prevBulletin() {
	$('#bulletinContentStep1').animate({"left": "0px"}, "slow");
	$('#bulletinContentStep2').animate({"left": "242px"}, "slow");
}
	
	
	
//advanced search
var advSearchPos = 'closed';
function openAdvSearch() {
	if (advSearchPos == 'closed') {
		$('#advSearch').animate({"top": "52px"}, "slow", function(){
			advSearchPos = 'open';	
		});		
	}
	
	if (advSearchPos == 'open') {
		$('#advSearch').animate({"top": "-300px"}, "slow", function(){
			advSearchPos = 'closed';
		});	
	}
}


