/* Table of Contents
========================================================
1.  Main Menu animation
2.  contact form functions
  2.1  hideForm
  2.2 clearDefaultAccessValue
3.  jQuery onLoad
  3.1  Nivo Slider Init
  3.2  Main Menu Animation Init
  3.2  jQuery Validate and Ajax submit
========================================================
*/

/*
========================================================
1. Main Menu animation
========================================================
*/
function mainmenu(){
$(" nav ul a").removeAttr("title");
$(" nav ul ul ").css({display: "none"}); // Opera Fix
$(" nav ul li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}

/*
========================================================
2. hideForm
========================================================
*/
function hideForm(){
	var shootQ = $("#shootQ");
	shootQ.fadeOut();
}

function clearDefaultAccessValue(){
	var accessValue = $("#accessValue");
	if(accessValue === "type the code to the left"){
		accessValue.attr("value","");
	}
}

/*
========================================================
3. jQuery onLoad
========================================================
*/

$(function(){
  /* ` 3.1 Nivo Slider Init
  -----------------------------------------------------*/
	$('.sliderFull').nivoSlider({
		effect: "fade",
		animspeed: 500,
		pauseTime: 5000,
		controlNav:false,
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:true, //Use image rel for thumbs
		captionOpacity:1, //Universal caption opacity
		afterChange: function(){
			
		}
	});
  
  /* ` 3.2 Main Menu Animation Init
  -----------------------------------------------------*/
  	mainmenu();

  /* ` 3.3 jQuery Validation and Ajax submit
  -----------------------------------------------------*/
	// show a simple loading indicator
	var loader = $('<div id="loader"><img src="/wp-content/themes/sterling/images/loading.gif" alt="loading..." height="16" width="16" /></div>')
		.css({position: "relative", top: "1em", left: "25em"})
		.appendTo("body")
		.hide();
	$().ajaxStart(function() {
		loader.show();
	}).ajaxStop(function() {
		loader.hide();
	}).ajaxError(function(a, b, e) {
		throw e;
	});
	
	var v = $("#shootQ").validate({
		rules: {
			first_name: {
				required: true,
				maxlength: 129
			},
			phone_number: {
				required: true,
				phoneUS: true
			},
			email_addr: {
				required: true,
				email: true
			},
			event_type: {
				required: true
			},
			access: {
				required: true
			}
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				target: "#result",
				beforeSubmit: clearDefaultAccessValue,
				success: hideForm
			});
		}
	});
	
	$("#accessValue").focus(function(){
		if($(this).attr("value") === "type the code to the left"){
			$(this).attr("value","");
		}
	});
	$("#accessValue").blur(function(){
		if($(this).attr("value") === ""){
			$(this).attr("value","type the code to the left");
		}
	});
	
	$('#event_date').datepicker({
		numberOfMonths: 3,
		showButtonPanel: true,
		constrainInput: false
	});

	
	//code to hide "other" event_type
	var otherEvent = $("#event_type_other");
	if($("#event_type").val() == "other"){
		otherEvent.removeClass('hide');
	} else {
		otherEvent.addClass('hide');
	}
	$("#event_type").change(function(){
		if($(this).val() == "other"){
			otherEvent.removeClass('hide');
		} else {
			otherEvent.addClass('hide');
		}
	});

});