function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}




jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("ticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
					stripWidth += jQuery(this, i).width();
				});
				$strip.width(stripWidth);			
				var defTiming = stripWidth/settings.travelocity;
				var totalTravel = stripWidth+containerWidth;								
				function scrollnews(spazio, tempo){
					$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(
					function(){
						jQuery(this).stop();
					},
					function(){
						var offset = jQuery(this).offset();
						var residualSpace = offset.left + stripWidth;
						var residualTime = residualSpace/settings.travelocity;
						scrollnews(residualSpace, residualTime);
					}
				);			
		});	
};

$(document).ready(function () {

	if($('#ticker ul#ticker01 li').length > 0)
	{
		$("#ticker ul#ticker01").liScroll({travelocity: 0.02});
	}
	
	MM_preloadImages('./images/site/go_on.gif');
	
	$('#search #searchentry').focus(function() {
		if(this.value == 'Search')
		{
			this.value = '';
		}
	});
	
	$('#search #searchentry').blur(function() {
		if(this.value == '')
		{
			this.value = 'Search';
		}
	});
		
	$('#bookacallback #cb_name').focus(function() {
		if(this.value == 'Your Name')
		{
			this.value = '';
		}
	});
	
	$('#bookacallback #cb_name').blur(function() {
		if(this.value == '')
		{
			this.value = 'Your Name';
		}
	});
	
	$('#bookacallback #cb_phone').focus(function() {
		if(this.value == 'Your Phone Number')
		{
			this.value = '';
		}
	});
	
	$('#bookacallback #cb_phone').blur(function() {
		if(this.value == '')
		{
			this.value = 'Your Phone Number';
		}
	});
	
	$('#bookacallback #cb_email').focus(function() {
		if(this.value == 'Your Email Address')
		{
			this.value = '';
		}
	});
	
	$('#bookacallback #cb_email').blur(function() {
		if(this.value == '')
		{
			this.value = 'Your Email Address';
		}
	});
	
	$('#callback_form #go-callback').click(function() {
		$('#callback_form').submit();
	});
	
	$('#callback_form').submit(function() {
		name = $('#callback_form #cb_name');
		phone = $('#callback_form #cb_phone');
		if(name.val() == '' || name.val() == 'Your Name')
		{
			alert('You must enter your name to book a callback');
			name.focus();
		}
		else if(phone.val() == '' || phone.val() == 'Your Phone Number')
		{
			alert('You must enter your phone number to book a callback');
			phone.focus();
		}
		else
		{
			return true;
		}
		
		return false;
	});
	
	$('#newsletter_form #go-register').click(function() {
		$('#newsletter_form').submit();
	});
	
	$('#newsletter_form').submit(function() {
		email = $('#newsletter_form #cb_email');
		if(email.val() == '' || email.val() == 'Your Email Address')
		{
			alert('You must enter an email address to register for the newsletter');
			email.focus();
		}
		else
		{
			return true;
		}
		
		return false;
	});
	
	$('#search').submit(function() {
		search = $('#search #searchentry');
		if(search.val() == '' || search.val() == 'Search')
		{
			alert('You must enter a keyword to search using');
			search.focus();
		}
		else
		{
			return true;
		}
		
		return false;
	});
	
	$('.date').datepicker({dateFormat: 'dd/mm/yy'});
});
