
// Create closed namespace for jQuery code.
(function($) {


	$makeNumeric = function()
	{
		var $numericInputs = $(".imp-number");
		if( $numericInputs.length ) {
			$numericInputs.numeric();
		}
	}


	// ------------------------------------------------------
	// JSON update function
	$doJSONSuccess = function(json){
		for(var $i=0; $i<json.length; $i++) {
			$theTarget = $("#" + json[$i].id);
			$theMethod = json[$i].method;
			if($theMethod == 'replaceWith')
			{
				theTarget.replaceWith(json[$i].html);
			}
			else
			{
				$theTarget.html(json[$i].html);
			}
		}
		return false;
	};


	// ------------------------------------------------------
	// Print page function
	$printpage = function() {
		window.print();
		return;
	};


	$(document).ready(function() {


		// ------------------------------------------------------
		// Attach the AJAX to the trigger figures calculator
		$("#trigcalc-form button").live("click",
			function() {

				var $self = $(this);
				var $theForm = $self.parents("form");
				var $theURL = $("#tfc_ajax_url").val();
				var $theData = $theForm.serialize();

				//alert($theData)

				$.ajax({
					type: "GET",
					timeout: 5000, // May need to adjust time allowance here
					url: $theURL,
					data: $theData,
					dataType: "json",
					success: function(json) {
						$doJSONSuccess(json);
					},
					error: function() {
						// Do something on error
					},
					complete: function() {
						// Do something on complete
						$.scrollTo("#tfr", 600);
					}
				});

				return false;
			}
		);


		// ------------------------------------------------------
		// Attach the numeric only behaviour to the inputs
		$makeNumeric()


		// ------------------------------------------------------
		// Add the print button
		$(".actions").append(" <span>|</span> <a href=\"javascript:$printpage();\">Print this page</a>");


		// ------------------------------------------------------
		// Add the site search to the page
		$('#searchform').each(function () {
      		$(this).replaceWith("\
			<form id=\"searchform\" action=\"" + websiteURL + "search_results/\" method=\"get\">\
			<input type=\"hidden\" name=\"cx\" value=\"016557508732839049450:_n5hia8ikiw\" />\
      		<input type=\"hidden\" name=\"cof\" value=\"FORID:11\" />\
      		<input type=\"hidden\" name=\"ie\" value=\"UTF-8\" />\
			<fieldset>\
			<legend>Search the site:</legend>\
			<label for=\"q\">Please enter a search term:</label>\
			<input id=\"q\" type=\"text\" value=\"\" name=\"q\" value=\"Search this site\" />\
			<button id=\"sa\" name=\"sa\" type=\"submit\" value=\"search\">Search</button>\
			</fieldset>\
      		</form>\
      		<script type=\"text/javascript\" src=\"http://www.google.com/coop/cse/brand?form=sitesearch&lang=en\"></script>\
      		");
		 });


		$('.flash-message').delay(5000, function(){$('.flash-message').fadeOut()})

	})
})(jQuery);


jQuery.fn.delay = function(time,func){
	return this.each(function(){
		setTimeout(func,time);
	});
};
