$(document).ready(function(){ 
	
	
	// home menu drop down
	$("#home-drop-nav").hide();
	
	// open and close the menu
	var timer;
	$("#home-btn").hover(function(){
		clearTimeout(timer);
		$('#home-drop-nav').slideDown("slow");
	},function(){
		timer = setTimeout (function(){ $('#home-drop-nav').slideUp("slow");}, 1000);
	});
	
	$('#home-drop-nav').hover(function(){
		clearTimeout(timer);
		$('#home-drop-nav').slideDown("slow");
	},function(){
		timer = setTimeout (function(){ $('#home-drop-nav').slideUp("slow");}, 1000);
	});
	
	// contact directory nav
	$("#alpha-list li a").click(function(){
		
		// set var for letter
		var selected_letter_is = $(this).attr("class");
		
		// remove classes
		$("address").removeClass("highlight");
		$("#alpha-list li a").removeClass("act");
		
		// add class to select proper nav and highlight it
		$("#alpha-list li a."+selected_letter_is).addClass("act");
		
		// scroll div to find correct card
		$("#contact-list").scrollTo("address#"+selected_letter_is, 700);
		
		// highlight card
		$("address."+selected_letter_is).addClass("highlight");
		
		// prevent page from reloading
		return false;
	});

	// ===============================================================================
	// = GS Calendar
	// Author: Jonathan Kelly
	// ===============================================================================

	// Handle the info button click
	$("#main-content.calendar-items .event-days a.open-close").click(function() {

		if($(this).hasClass("shown")) {

			$(this)
				.removeClass("shown")
				.attr("title", "Learn More");
			$day = $(this).parents(".day");
			$day
				.find(".details").hide()
			.end()
				.find(".long-intro").hide()
			.end()
				.find(".short-intro").show();

		} else {

			$(this)
				.addClass("shown")
				.attr("title", "Hide Details");
			$day = $(this)
				.parents(".day");
			$day
				.find(".details").show()
			.end()
				.find(".long-intro").show()
			.end()
				.find(".short-intro").hide();
		}

		return false;

	});
	
	// Stimulate a click on the info button if we are viewing a single entry
	if(document.location.pathname.match(/entry-details/)) {
		$(".day .open-close:first").click();
	}

	// Stimulate a click on the info button if they click the title
	$("#main-content.calendar-items .event-days .intro a.title").click(function() { $(this).parents(".day").find("a.open-close").click(); return false; });
	
	// careers table show/hide
	$(".info td div").hide();
	
	$("a.more-info-link").toggle(function(){
		$(".info."+$(this).attr('id')+" td div").slideDown(300);
		$(".info."+$(this).attr('id')+" td").addClass("bord");
	},function(){
		$(".info."+$(this).attr('id')+" td div").slideUp(300);
		$(".info."+$(this).attr('id')+" td").removeClass("bord");
	});
	
	// clear out text field
	$('#keywords, #quick-docs-search-input, #quick-docs-search-inpage-input').focus(function() {
	  if ($(this).val() == $(this).attr('title')) {
	    $(this).val('');
	  }
	})
	.blur(function() {
	  if ($(this).val() == '') {
	    $(this).val($(this).attr('title'));
	  }
	});
	
	// quick resources search
	$('#quick-docs-search').submit(function(){
		$("#quick-resources-load").addClass("loading");
		$("#quick-resources-load li").fadeOut('fast');
			var act  = $(this).attr('action');
			var vars = $(this).serialize();
			$.ajax({
				type: "POST",
				url: act,
				data: vars,
				cache: false,
				dataType: "html",
				success: function(searchResults){
					setTimeout(function(){
						$("#quick-resources-load").removeClass("loading").html(searchResults);
						$("#quick-resources-load li").hide().fadeIn();
						}, 6000);
					}					
				});
			return false;
		});
		
	// quick resources in page search
	$('#quick-docs-search-inpage').submit(function(){
		$("#doc-search-result").addClass("loading");
		$("#doc-search-result li, #doc-search-result p").fadeOut('fast');
			var act  = $('#quick-docs-search-inpage').attr('action');
			var vars = $('#quick-docs-search-inpage').serialize();
			$.ajax({
				type: "POST",
				url: act,
				data: vars,
				cache: false,
				dataType: "html",
				success: function(searchResults){
					setTimeout(function(){
						$("#doc-search-result").removeClass("loading").html(searchResults);
						$("#doc-search-result li, #doc-search-result p").hide().fadeIn();
						}, 6000);
					}					
				});
			return false;
		});

	// Contact Form Validation
	$('#contact-form').RSV({
		displayType: "display-html",
		errorFieldClass: "f-error",
		errorTargetElementId: "contact-error",
		rules: [
		"required,name,Please provide your <b>name</b>.",
		"required,email,Please provide your <b>e-mail address</b>.",
		"valid_email,email,Please enter a valid <b>e-mail address</b>.",
		"required,street1,Please provide your <b>street address</b>.",
		"required,city,Please provide your <b>city</b>.",
		"required,state,Please provide your <b>state</b>.",
		"if:state=TN,required,county,Please provide your <b>county</b>.",
		"required,zipcode,Please provide your <b>zip code</b>.",
		"required,phone1,Please provide your <b>phone number</b>.",
		"required,interest,Please provide your <b>interest</b>.",
		"if:interest=1,required,school,Please provide the <b>school</b>.",
		"if:interest=1,required,school_grade,Please provide the <b>grade in school</b>.",
		"required,referred_from,Please tell us <b>how you found out about us</b>.",
		"if:referred_from=other,required,referred_from_other,Please specify <b>how you found out about us</b>.",
		"required,comments,Please provide your <b>comments</b>.",
		"required,age_check,Please verify that you are <b>18 years of age or older</b>."
		]
	});
	
	// Contact Form Function
	$(function(){
		$("#state").change(function(){
			// or use this 'cos its more jQueryfied:
			if($(this).val() == 'TN')	$('#county-field').show();
			else					$('#county-field').hide();
		});
	});
	
	// Employment Form Validation
	$('#employment-form').RSV({
		displayType: "display-html",
		errorFieldClass: "f-error",
		errorTargetElementId: "contact-error",
		rules: [
		"required,name,Please provide your <b>name</b>.",
		"required,email,Please provide your <b>e-mail address</b>.",
		"valid_email,email,Please enter a valid <b>e-mail address</b>.",
		"required,street1,Please provide your <b>street address</b>.",
		"required,city,Please provide your <b>city</b>.",
		"required,state,Please provide your <b>state</b>.",
		"required,zipcode,Please provide your <b>zip code</b>.",
		"required,phone1,Please provide your <b>phone number</b>.",
		"required,position,Please provide a <b>desired position</b>.",
		"required,position_type,Please choose a <b>position type</b>.",
		"required,date_available,Please provide a <b>start date</b>.",
		"required,salary_desired,Please provide a <b>salary</b>.",
		"required,referral_type,Please choose a <b>referral</b>.",
		"required,referral_name,Please provide a <b>referral name</b>.",
		"required,willing_travel,Please choose a <b>travel</b> option.",
		"required,willing_relocate,Please choose a <b>relocation</b> option.",
		"required,relatives_employed,Please choose a <b>relative</b> option.",
		"required,you_employed,Please choose an <b>employment</b> option.",
		"required,prev_applied,Please choose a <b>application</b> option.",
		"required,file1,Please choose a <b>resume</b> to upload."
		]
	});
	
	// contact form instruction fields
	$(".instruct").hide();
	
	$(".form-general :input").click(function(){
		$(".instruct").fadeOut();
		var input_is = $(this).attr("id");
		$("#"+input_is+"-instruct").fadeIn();
	});

	// featured fade
	//$('#feature-cycle').cycle('fade');
	$("#feature-cycle").cycle({ 
    	fx:    'fade', 
    	speed:  500,
		containerResize: 0,
		next:   '#next', 
		prev:   '#prev',
		autostop: 1,
		autostopCount: 1
 	});

	$("#banner-cycle").cycle({  
		fx: 'scrollHorz',
		timeout: 10000,
		next:   '#next-banner', 
		prev:   '#prev-banner'
 	});
	
	// Load the SWF in the body if it is needed.
	if ($('#gs-swf-object').length != 0) {
		var flashvars = {wmode: 'transparent'};
		var params = {wmode: 'transparent'};
		var attributes = {wmode: 'transparent'};
		var src = $('#swfID').attr('href');
		swfobject.embedSWF(src, "gs-swf-object", "590", "435", "9.0.0","/content/swfobject/expressInstall.swf", flashvars, params, attributes);
	}
	
	
	// ===============================================================================
	// = GS Training Pages (Related Videos)                                          =
	// Author: Jesse Bunch
	// ===============================================================================
	
	// User clicked a related video
	$("#wrap #sidebar div.training ul li a").click(function() {
		
		// Get the target video's SWF URL & name
		var strNewVidURL = $(this).find("span.vid-url").html();
		var strNewVidName = $(this).find("span.vid-name").html();
		
		// Make sure both were found, if not, proceed to non-javascript loading method
		if (strNewVidURL == undefined || strNewVidName == undefined) {
			alert("There was an error loading your video.");
			return true;
		}
		
		// Switch out the SWF
		var flashvars = {wmode: 'transparent'};
		var params = {wmode: 'transparent'};
		var attributes = {wmode: 'transparent'};
		swfobject.embedSWF(strNewVidURL, "gs-swf-object", "590", "435", "9.0.0","/content/swfobject/expressInstall.swf", flashvars, params, attributes);
		
		// Switch out the video name
		$("div#gs-swf-pad h2 span:first").html(strNewVidName);
		
		// Scroll the window back into position--gracefully
		//$.scrollTo("div#intro", 750, {offset: {left: 0, top:-50}});
		
		// Prevent link from happening
		return false;
		
	});

});

function onAfter(curr) {
	// var id = curr.id;
	// console.log($('#'+id+' a:first').attr('href'));
	// $('#'+id+' a').attr('href', href);
}

// add any other non-jquery js here, including swf object

function toggleAgeFields(sel) {
	if(sel) {
		var s = sel.options[sel.selectedIndex].value;
	} else {
		var obj = document.getElementById('age');
		var s = obj.selectedIndex;
	}
	
	if(s == 1) {
		$('#minor-fields').removeClass('hide');
		$('#adult-fields').addClass('hide');
	} else if(s == 2) {
		$('#minor-fields').addClass('hide');
		$('#adult-fields').removeClass('hide');
	} else {
		$('#minor-fields').addClass('hide');
		$('#adult-fields').addClass('hide');
	}
}

function toggleInterestFields(sel) {
	if(sel) {
		var s = sel.options[sel.selectedIndex].value;
	} else {
		var obj = document.getElementById('interest');
		var s = obj.selectedIndex;
	}
	
	if(s == 1) {
		$('#minor-fields').removeClass('hide');
		$('#adult-fields').addClass('hide');
	} else if(s == 2) {
		$('#minor-fields').addClass('hide');
		$('#adult-fields').removeClass('hide');
	} else {
		$('#minor-fields').addClass('hide');
		$('#adult-fields').addClass('hide');
	}
}


function toggleReferredFields(sel) {
	if(sel) {
		var s = sel.options[sel.selectedIndex].value;
	} else {
		var obj = document.getElementById('referred_from');
		var s = obj.selectedIndex;
	}
	
	if(s == "other") {
		$('#referred_from-fields').removeClass('hide');
	} else {
		$('#referred_from-fields').addClass('hide');
	}
}

