/* 
	macforbes.com
	
	11/09/2009
	Patrick Carne (Inlight Media)
*/

$(document).ready(function() {			
	// apply_pngfix();
	google_map();
	setup_forms(['#subscribe-form','.search-form']);
	setup_subscription_form();
	init_contact_form();
});

function setup_subscription_form() {
	$("a#subscribe-link").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 230,
		'frameHeight': 270
	}); 
}

function apply_pngfix() {
	$("#page").pngFix();	
}

/** 
 * Sets up the google map link on the contact page to open in the fancy box.
 */ 
function google_map() {
	$("#map-link").fancybox({
		 zoomSpeedIn: 500,
		 zoomSpeedOut: 500,
		 overlayShow: true,
		 overlayOpacity: .75,
		 frameWidth: 640,
		 frameHeight: 480,
		 hideOnContentClick: false,
		 callbackOnShow: function() {},
		 callbackOnClose: function() {}
	}); 
}

function setup_forms(forms) {
	// Hide values on click
	for(i=0;i<=forms.length-1;i++) {
		$(forms[i]+' .text').focus(function(){
			this.value = '';
		});
	}
}



/* 
	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 = 10000;								// 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];
			var quote_text = $('.quote .quote-text', slide);	
				
			// 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];
			
			if (quote_text.length) {
				// Fade out text, when complete begin transition to next slide
				$(quote_text).fadeOut(text_transition_speed, function() {
					transition_slide(slide, next_slide, text_transition_speed, slide_transition_speed);		
				});	
			} else { 
				transition_slide(slide, next_slide, text_transition_speed, slide_transition_speed);	
			}
			
		},slide_view_time);
	}
}

function transition_slide(slide, next_slide, text_transition_speed, slide_transition_speed) {
	var next_quote_text = $('.quote .quote-text', next_slide);
	
	// Fade out current slide
	$(slide).fadeOut(slide_transition_speed);
	
	// Fade the slide back in, when complete fade in text
	$(next_slide).fadeIn(slide_transition_speed, function() {
		if (next_quote_text.length) {
			$(next_quote_text).fadeIn(text_transition_speed);
		}
	});	
}



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").fadeOut(500, function() {	
						// Display the thankyou message.
						$("#thankyouMessage").fadeIn(500);											
					});	
				}
				else
				{
					alert("There was an error sending your message. \r\nPerhaps you could try giving us a call on +61 3 9818 8099 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
			},
			message: "required"
		},
		messages: {
			name: "Please enter your name.",			
			email: {
				required: "Please enter your email.",
				email: "Please enter a valid email."
			},
			message: "Please enter your message."
		}
	});
}
 









