// Limit textarea to 200 characters and output remaining

function limitChars(textclass, limit, outputclass)
{
 var text = $('.'+textclass).val(); 
 var textlength = text.length;
 if(textlength > limit){
		$('.'+outputclass+' span').html('0');
	 $('.'+textclass).val(text.substr(0,limit));
	 return false;
 } else {
	 $('.'+outputclass+' span').html((limit - textlength));
	 return true;
 }
}

$(document).ready(function(){
	// Clear default search value		
	$("input#searchBox").focus(function(){
		if($(this).val() == "search..."){
			$(this).attr("value",""); 
		}
	});
	
	// Replace default search value if nothing entered
	$("input#searchBox").blur(function(){
		if($(this).val() == ""){
			$(this).attr("value","search..."); 
		}
	});
	
	// Process other input boxes with default values
	$("input#CBname").focus(function(){
		if($(this).val() == "Name"){
			$(this).attr("value",""); 
		}
	});

	$("input#CBname").blur(function(){
		if($(this).val() == ""){
			$(this).attr("value","Name");
		}
	});
	
	$("input#CBemail").focus(function(){
		if($(this).val() == "Email"){
			$(this).attr("value",""); 
		}
	});

	$("input#CBemail").blur(function(){
		if($(this).val() == ""){
			$(this).attr("value","Email");
		}
	});
	
	$("input#CBphone").focus(function(){
		if($(this).val() == "Telephone"){
			$(this).attr("value",""); 
		}
	});

	$("input#CBphone").blur(function(){
		if($(this).val() == ""){
			$(this).attr("value","Telephone");
		}
	});
	
	// Get length of text in contact box
	if($('.limit').length > 0){
		var text = $('.limit').val(); 
		var textlength = text.length;
		$('.limitDisplay span').html((500 - textlength));
	}
	
	$('.limit').keyup(function(){
		limitChars('limit', 500, 'limitDisplay');
	})
	
	// Initialise smooth scrolling plugin for anchors
	$.smoothAnchors("fast");
	
	// Initialise in-page glossary tooltips
	$('span.glossary[title]').qtip({
      content: {
         text: false // Use each elements title attribute
      },
						position: {
							corner: {
										target: 'topMiddle',
										tooltip: 'bottomMiddle' 
							}
						},
      style: {
							name: 'light', // Give it some style
							tip: true, // Give it a speech bubble tip with automatic corner detection
							border: {
										color: '#898989'
							}
						}
   });
	
		// Show tracing form
		$("a.showForm").click(function(){
			$(".tracingText").fadeOut("normal",function(){
				$(".tracingForm").fadeIn("normal");
			});																														
		});
		
		// Show tracing form from URL
		var myFile = document.location.toString();
		if (myFile.match('#form')) {
			$(".tracingText").hide();
			$(".tracingForm").show();
		}

		// Cancel button controls
		$("input.cancel").click(function(){
			$(".tracingForm").fadeOut("normal",function(){
				$(".tracingText").fadeIn("normal");
				location.href = "#text";
			});
		});
});