
$(document).ready(function(){
	
	$("#bg").resizeme('900'); // resize bg viewport. Function at end of this file
	$(window).bind("resize", function(){ $("#bg").resizeme('900'); }); // bind resize event to resize function
	

// Video Contest collection ------------------------------------------------------------------
	var options = { 
		beforeSubmit:	showCaptureRequest,		// pre-submit callback 
    success:			showCaptureResponse,	// post-submit callback
		type:					'post'								// 'get' or 'post', override for form's 'method' attribute
	}; 
	
	// bind form using 'ajaxForm' 
	$("#capture").ajaxForm(options); 


	// tab behaviour
	if( $('.panel').length ){
		$('.panel').hide(); // show the first one
		$('.panel:first').show(); // show the first one
		
		$("#t a").click( function () {
			var to_show = $(this).attr('href');
			$('.panel').hide();
			$('div#'+to_show).show();
			$("#t li.active").removeClass("active");
			$(this).parent('li').addClass('active');
			return false;
		});
	}
	// input hints
	//$("input:text").hint();
	
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7) ) { // target IE6
		$('#email').val($('#email').attr('title')).focus(function() {
			// See if the input field is the default
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		})
		.blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		});
	}
	
	//submit hover
	$("input#submit").hover(
		function() {
			$(this).css("background-position","0px -30px");
			//alert($(this).css('background-image'));
		}, //close over
		function() {
			$(this).css("background-position","0px 0px");
		} //close out
	); //close hover
	
}); // close outer function


// form validation functions

function showCaptureRequest(formData, jqForm, options) { 
	//$("#capture").find("label").remove();
	var target = $("#ajx-capture-resp");
	
	$(target).animate({"height":"show","opacity":"show"}, "slow");
	$(target).addClass("process");
	$(target).html("<span>Just a moment...We're processing.</span>");
	
	//alert("showCaptureRequest has fired!");
	
	var emailValue = $("input[name=email]").fieldValue();
	var emailReg   = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	if (!emailValue[0]) { 
		 $(target).removeClass("process");
		 $(target).addClass("error");
		 $(target).html("<span>Please enter an email address.</span>");
		 return false; 
	 } else if (!emailReg.test(emailValue[0])) { 
		 $(target).removeClass("process");
		 $(target).addClass("error");
		 $(target).html("<span>Please enter a valid email address.</span>");
		 return false; 
	 }
				
		$("#capture").fadeTo("slow", .33);
				
}// close showcapturerequest


// Perform post-submission tasks
function showCaptureResponse(responseText, statusText)  {
	
	var wrapper = $("#ajx-capture-resp");
	var message = $("span", wrapper);
	var the_form = $("#capture");
	
	$(wrapper).removeClass("process");
	
	if (statusText == 'success') {
		var responseObject = jQuery.parseJSON(responseText);
		var the_status = responseObject.status;
		var the_result = responseObject.result;

		if (the_status == 'error') {
				$(wrapper).addClass('error');
				$(message).html(the_result);
				$(message).show();
		} else if (the_status == 'success') {
				$(the_form).slideUp("fast");
				$(wrapper).removeClass('error').addClass('success');
				$(message).text(the_result).show();
		} // close inner
	} else { // the AJAX call didn't work
			$(wrapper).addClass('error');
			var errorMessage = 'Whoops! something went wrong!';
			$(message).html(errorMessage).show();
	} // close else
} // close function


/*
 * This is the resizing function bound to #carousel and #bg
 * it gets the browser width, multiplies it by $diff which
 * is the percentage difference between the width and height
 * of carousel/bg imgs to calculate the correct height of
 * #carousel/#bg.
 * change the $diff value if you change img aspect ratios.
*/
(function($) {
$.fn.resizeme = function(target) {
	return this.each( function () {
		var h = $(document).height();
			$(this).height(h);
	});

}
})(jQuery);

