/**
 * Loader file for Willison Construction
 *
 * This is the main loader file for willison construction, it loads the
 * plugins needed for the page and sets them up.
 *
 * @author Chris Mytton - Cutpastecreate
 * @copyright Cutpastecreate.com 2009
 * @package WillisonConstruction
 * @subpackage Javascript
 *
 */
// define our loader
function Loader ()
{
	// load the scripts we need
	$.getScript(Config.cdn + 'scripts/cycle/jquery.cycle.lite.min.js', this.initCycle);
	
	
	if (/contact-us/.test(window.location.pathname))
	{
		$.getScript(Config.cdn + 'scripts/validate/jquery.validate.min.js');
	
		// replace the submit button with the link that we will javascriptify
		$("div#submit input[type='submit']").replaceWith('<a href="#" class="button submit"><span>Submit</span></a>');
	
		$("a.button.submit").click(this.submitMessage);
	}
};

// define the initialisation functions
Loader.prototype.initCycle = function ()
{
	$("div.portfolio-scroller").cycle({
		timeout:       	8000,
		speed:			3000
	});
};

Loader.prototype.submitMessage = function (e)
{
	e.preventDefault();
	
	$("form#contactform").validate().form();
	
	if 	($("form#contactform").valid())
	{
		$("a.button.submit").text("Sending...");
		
		$.post($("form#contactform").attr('action'), $("form#contactform").serialize(), function(data) {
			
			
			if (data.status == 200)
			{
				$("form#contactform")[0].reset();
				$("a.button.submit").text("Sent");
				$("div.status p").text(data.message);
				
				setTimeout(function() {
					$("div.status p").fadeOut("fast");
					$("a.button.submit").text("Submit");
				}, 2000);
			}
			else
			{
				$("a.button.submit").text("Submit");
				$("div.status p").addClass("error").text(data.message);
			}
		}, 'json');
	}
	
};

$(function () {
	var loader = new Loader();
});
