
$(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 = { 
		target:				'#ajx-capture-resp',	// target element(s) to be updated with server response 
		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
	
	$('.panel:first').show(); // show the first one
	
	$("#t a").click( function () {
		var to_show = $(this).attr('href');
		$('.panel').hide();
		$('div#'+to_show).show();
		$("#indexCopy 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();
	$(options.target).animate({"height":"show","opacity":"show"}, "slow");
	$(options.target).addClass("process");
	$(options.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]) { 
				$(options.target).removeClass("process");
				$(options.target).addClass("error");
				$(options.target).html("<span>Please enter an email address.</span>");
				return false; 
			} else if (!emailReg.test(emailValue[0])) { 
				$(options.target).removeClass("process");
				$(options.target).addClass("error");
				$(options.target).html("<span>Please enter a valid email address.</span>");
				return false; 
			}
				
		$("#capture").fadeTo("slow", .33);
				
}// close showcapturerequest


function showCaptureResponse(responseText, statusText) { 
	//$("#contest-capture").replaceWith("<div id='ajx-capture-answer' class='alert'>" + responseText + "</div>");
	
	var response = responseText;
				

	$("#ajx-capture-resp span").fadeOut("slow");
		$("#ajx-capture-resp span").empty();
		// this tests against the response type in upload_controller.php
		if(response == "We got it! Thanks!") {
			$("#ajx-capture-resp span").removeClass("error").addClass("success").append(response).fadeIn("slow");
			$("#capture").resetForm();
			$("#capture").fadeTo("slow", 1,
				function callback() {
					$("#ajx-capture-resp").animate({"opacity":1}, 1200).animate({"height":"toggle","opacity":"toggle"}, "slow");
				}//close callback
			);//close fade
		} else {
				$("#capture").fadeTo("slow", 1);
				$("#ajx-capture-resp").removeClass("process").addClass("error");
				/*$(options.target).html("<span>" + response + "</span>");*/
		}// close else


}


/*
 * 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);
