/*
	Complete Styling
	04/06/2009
	Tony Milne
*/

/** 
 * Used to log to the console.
 */
jQuery.fn.log = function (msg) 
{
	try
	{	
		if (console)	
			console.log("%s: %o", msg, this);
	}
	catch(e) {}
	
	return this;
}

$(document).ready(function() {
	init_contact_form();
	init_gift_form();
	fading_content();
	fading_content2();
});

function fading_content() {	
	if ($("div.services").length > 0) {
		
		$("div#menu.services a").click(function() {
			// Cater for active state.
			$("div#menu.services a").removeClass("active");
			$(this).addClass("active");
			
			// Services has intro content... therefor there is one additional content to menu item.
			// Adjustments to i are made for this reason.
			
			var i = 1 + $("div#menu.services a").index(this);
			
			var already_visible = $("div.main:eq("+i+")").hasClass("visible");
			if (!already_visible) {
				// Fade out div.main.visible
				$("div.main.visible").removeClass("visible").fadeOut("normal", function() {
					// Fade in div.main:eq(i)	
					$("div.main:eq("+i+")").addClass("visible").fadeIn("fast");
				});				
			}
			
			return false;
		});
		
		// Add class visible to first, and hide all others.
		$("div.main:eq(0)").addClass("visible");
		$("div.main:gt(0)").hide();
	}
}

function fading_content2() {	
	if ($("div.products").length > 0) {
		
		$("div#menu.products a").click(function() {
			// Cater for active state.
			$("div#menu.products a").removeClass("active");
			$(this).addClass("active");
			
			// Products has intro content... therefor there is one additional content to menu item.
			// Adjustments to i are made for this reason.
			
			var i = 1 + $("div#menu.products a").index(this);
			
			var already_visible = $("div.main:eq("+i+")").hasClass("visible");
			if (!already_visible) {
				// Fade out div.main.visible
				$("div.main.visible").removeClass("visible").fadeOut("normal", function() {
					// Fade in div.main:eq(i)	
					$("div.main:eq("+i+")").addClass("visible").fadeIn("fast");
				});				
			}
			
			return false;
		});
		
		// Add class visible to first, and hide all others.
		$("div.main:eq(0)").addClass("visible");
		$("div.main:gt(0)").hide();
	}
}

function init_contact_form() {
	// Set _isAjaxRequest variable to avoid sending the page contents back after form submission.
	$("#ajax").val("true");	
	$("#thecontactform").validate({						
		submitHandler: function(form) {						
			$("#thecontactform").ajaxSubmit(function(result) {								
				if (result == "true")
				{
					// Remove the form components.
					$("#contactform").log("fading out thecontactform").fadeOut(500, function() {	
						// Display the thankyou message.
						$("#thankyouMessage").fadeIn(500);											
					});	
				}
				else
				{
					alert("There was an error sending your message. \r\n Perhaps you could try emailing us or try again later.");
				}				
			});
		},		
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 mandatory field (it has been highlighted).'
					: 'You missed ' + errors + ' mandatory fields (they have been highlighted).';
				
				$("#thecontactform div.error span").html(message);
				$("#thecontactform div.error").show();
			} else {
				$("#thecontactform div.error").hide();
			}
		},
		rules: {
			name: "required",			
			email: {
				required: true,
				email: true
			},
			comment: "required"
		},
		messages: {
			name: "",			
			email: {
				required: "",
				email: ""
			},
			comment: ""
		}
	});
}

function init_gift_form() {
	// Set _isAjaxRequest variable to avoid sending the page contents back after form submission.
	$("#ajax").val("true");	
	$("#thegiftform").validate({						
		submitHandler: function(form) {						
			$("#thegiftform").ajaxSubmit(function(result) {								
				if (result == "true")
				{
					// Remove the form components.
					$("#giftform").log("fading out thecontactform").fadeOut(500, function() {	
						// Display the thankyou message.
						$("#thankyouMessage").fadeIn(500);											
					});	
				}
				else
				{
					alert("There was an error sending your request. \r\n Perhaps you could try emailing us or try again later.");
				}				
			});
		},		
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 mandatory field (it has been highlighted).'
					: 'You missed ' + errors + ' mandatory fields (they have been highlighted).';
				
				$("#thegiftform div.error span").html(message);
				$("#thegiftform div.error").show();
			} else {
				$("#thegiftform div.error").hide();
			}
		},
		rules: {
			name: "required",			
			value: "required",	
			contact: "required",
			message: "required"
		},
		messages: {
			name: "",		
			value: "",
			contact: "",
			message: ""
		}
	});
}

/* 
	Hero Slider 

	Transitions between divs that contain an image, a quote box with quote text within it.

*/
$(window).bind('load', function () {
	setup_hero_slide();
});

function setup_hero_slide() {
	var container = $('#home-hero');

	if(container.length > 0) {
		var slides = $('#home-hero .hero-slide'); 	// the div slides to fade
		var slide_view_time = 6000;								// how long the slide appears
		var text_transition_speed = 1000; 					// the fade out/in time of the text
		var slide_transition_speed = 1000; 					// the fade out/in time of the slides
		var i = 0;
		
		setInterval(function() {
			var slide = slides[i];

			// Increment to next slide (reseting to zero if at end of array)
			i++;
			if (i + 1 > slides.length) i = 0;
			
			var next_slide = slides[i];

			$(slide).fadeOut(text_transition_speed, function() {
				$(next_slide).fadeIn(slide_transition_speed);
			});
		},slide_view_time);
	}
}
