jQuery(document).ready(function($){

	/* Remove the 'no-js' class from the HTML element */
	$('html').removeClass('no-js');

	/* Style certain form fields with Uniform */
	$("select, input:checkbox, input:radio, input:file").uniform();

	/* Open external links in a new tab/window */
	// $('a.external').attr('target','_blank');
	
	/* Fancy Ampersands */
	$(":header:contains('&')").each(function(){
		$(this).html($(this).html().replace(/&amp;/, "<span class='ampersand'>&amp;</span>"))
	});
	
	$('#directory-form select').change(function(){
		office = $(this).val();
		page = document.domain;
		newPage = 'http://' + page + '/about/employee-directory/' + office;
		window.location = newPage;
	});
	
	$('.insurance-form').validate();

	/* Determine what code needs to run based on the body's ID attribute. */
	var pageId = $('body').attr('id');
	switch(pageId){

		// Homepage
		case "home":

			/* Initialize the cycle plugin for homepage featured content */
			$('#slides').after('<ol id="slide-nav">').cycle({ 
				activePagerClass: 'active',                // class name used for the active pager link 
				allowPagerClickBubble: true,               // allows or prevents click event on pager anchors from bubbling 
				autostop: true,                            // true to end slideshow after X transitions (where X == slide count)
				autostopCount: 22,                         // number of transitions (optionally used with autostop to define X) 
				fx: 'fade',                                // name of transition effect (or comma separated names, ex: fade,scrollUp,shuffle)
				pager: '#slide-nav',                       // selector for element to use as pager container 
				pagerAnchorBuilder: function(idx, slide) { // callback fn for building anchor links:  function(index, DOMelement)
					var clone = $(slide).clone(true);
					clone
						.find('img').remove().end()
						.attr('style','');
					return clone;
				},
				pagerEvent: 'mouseenter.cycle',            // name of event which drives the pager navigation 
				pauseOnPagerHover: true,                   // true to pause when hovering over pager link 
				speed: 'slow',                             // speed of the transition (any valid fx speed value)
				timeout: 8000                              // milliseconds between slide transitions (0 to disable auto advance) 
			});

		break;


		// Product landing pages
		case "product-landing":

			/* Toggle secondary carriers on click */
			var toggle = $('.carriers .toggle');
			var toggleable = $('.carriers .secondary-carriers');
			toggle.click(function(e){
				e.preventDefault();
				var self = $(this);
				self.toggleClass('active');
				toggleable.slideToggle();
			});

		break;
		
		
		// Client Services pages
		case "client-services":

			/* Toggle visibility of resource files when the user selects a category */
			var resourceList = $('ul.resource-list');
			var resourceSelect = $('select#resource-categories');
			resourceList.isotope({
				filter: '.latest'
			});
			resourceSelect.change(function(){
				var self = $(this);
				var selector = self.val();
				resourceList.isotope({
					filter: selector
				});
			});

		break;
		
		case "client-services-subpage":
			var formType = $('#form-type');
			var instructionsContainer = $('fieldset.type .instructions');
			var notifyField = $('fieldset.type .notify input#recipient_email');
			formType.change(function(){
				var self = $(this);
				var subject = self.val();
				var instructions = self.find('option[value="'+subject+'"]').data('description');
				instructionsContainer.text(instructions);
				var notifyValue = self.find('option[value="'+subject+'"]').data('notify');
				notifyField.val(notifyValue);
			});
			$('#client-services-form').validate();
		break;

		case "client-services-file-claim":
		case "client-services-pay-bill":
			$('#section-nav ul li:first').addClass('active');
		break;

		case "contact":
			$('#contact-form').validate();
		break;

		case "start-proposal":

			/* Fetch the appropriate form when the user selects it from the dropdown menu */
			var formSelector = $('#form-type');
			var formContainer = $('#quote-form');
			var instructionsContainer = $('#instructions');
			formSelector.change(function(){
				var self = $(this);
				var targetForm = self.val();
				var instructions = self.find('option[value="'+targetForm+'"]').data('description');
				if(targetForm){
					$.ajax({
						url: targetForm,
						cache: false,
						beforeSend: function(){
							formContainer.addClass('loading');
						},
						error: function(){
							$(formContainer).removeClass('loading');
							alert("We had trouble displaying the form you selected. Please try again.");
						},
						success: function(html){
							$(formContainer).removeClass('loading').html(html);
							instructionsContainer.text(instructions);
							$('p.instructions').show();
							$("select, input:checkbox, input:radio, input:file",formContainer).uniform();
							$(formContainer).find('form').validate();
						}
					});
				}
			})

		break;
		
		case "form-return":
			$('#section-nav li:first').addClass('active');
		
		break;


	}

});
