$(document).ready(function() {
	
	// Home page slider
	$('#home-slider-panes').cycle({ 
		fx:     'fade', 
	    speed:  'fast',
		timeout: 6000,
		pagerEvent: 'mouseover', 
		pauseOnPagerHover: true,
		pager: '#home-slider-tabs',
		pagerAnchorBuilder: function(idx, slide) {
			return $('#home-slider-tabs li:eq('+idx+')');
		}		
	});
	$('#home-slider-panes').hover(function(){
		$('#home-slider-panes').cycle('pause');
	},
	function(){
		$('#home-slider-panes').cycle('resume');
	});
		
	
	// Add a class of "even" to the news index/details list of categories
	// For styling purposes
	$("#news-categories :nth-child(even)").addClass("even");
	

	//  Commands for the Image Galleries
	$(".entry-gallery.cycle").each(function(index) {
		var $self = $(this);
		var $sliderNav = $("<ul />").appendTo($self);
		$self.cycle({
			fx:   'fade',
			slideExpr: "> div",
			timeout: 0,
			pager: $sliderNav,
			pagerAnchorBuilder: function(idx, slide) { 
				thm_src = $("object", slide).attr('data-thm') || $("img", slide).attr("src");
				return '<li><a href="#"><img src="' + thm_src + '" /></a></li>'; 
			}
		});
	});
	$('.entry-gallery.cycle').hover(function(){
		$('.entry-gallery.cycle').cycle('pause');
	},
	function(){
		$('.entry-gallery.cycle').cycle('resume');
	});


	// Commands for the Google Maps
	// Located on the Parks/Accommodations page
	$(".park-directions").each(function(index) {

		// global - we only need one of these
		directionsService = new google.maps.DirectionsService();

		// local - they apply to the current context which is this loop
		var $self = $(this);
		var park_lat = $self.attr('data-lat');
		var park_lng = $self.attr('data-lng');
		var park_lat_lng = new google.maps.LatLng(park_lat, park_lng);
		var opts = {
			zoom: 8,
			center: park_lat_lng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false		
			
		};

		$("ul.menu.tabs a", this).bind('click', function(event) {

			var $city = $(this);
			var $map_container = $(this.hash).show();

			if($city.data('map-rendered') == undefined)
			{
				var directionsDisplay = new google.maps.DirectionsRenderer();

				var city_lat = $city.attr('data-lat');
				var city_lng = $city.attr('data-lng');
				var city_lat_lng = new google.maps.LatLng(city_lat, city_lng);

				var map = new google.maps.Map($map_container[0], opts);
				directionsDisplay.setMap(map);

				var request = {
					origin: city_lat_lng, 
					destination: park_lat_lng,
					travelMode: google.maps.DirectionsTravelMode.DRIVING
					
					
				};

				directionsService.route(request, function(response, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionsDisplay.setDirections(response);
					}
					
				});

				$city.data('map-rendered', true);
			}
			return false;
		});
		
	
	});

	$('.menu.tabs').each(function(index) {
		// Create an array for the element selectors that are to be targeted
		// They are the divs that show and hide
		var target_selectors = [];
		
		// Loop over the triggers in the tab menu
		// The triggers are the <a /> elements in the menu
		var $triggers = $('a', $(this)).each(function(index) {
			// get their href attribute which is the target id
			// eg: href="#target1" -> <div id="target1" />
			// Push the selector into the array
			target_selectors.push(this.hash);
		})
		// when we click the link...
		// or the click event is fired
		.click(function() {
			// remove the active class from all the triggers
			$triggers.parent().removeClass("active");
			// add the active class to the selected trigger
			$(this).parent().addClass("active");

			$targets.hide();
			// If the anchor href hash is #show_all -> a href="#show_all"  
			if((/show_all$/).test(this.hash))
			{
				// fade in all the targets
				$targets.fadeIn();
			}
			// else we're tageting something sepcific
			else
			{
				// fade in the target
				$targets.filter(this.hash).fadeIn();
			}
			
			return false;
		});
		// Join the selectors: "#target1, #target2, #target3"
		// create a jquery object from the selectors
		// hide the selected elements (the targets)
		// return the jQuery collection of the targets
		var $targets = $(target_selectors.join(", ")).hide();
		// click the first trigger
		// which is the first anchor
		// this fires the click event above
		$triggers.eq(0).trigger('click');
	});	


	// Call the validation JS for the Contact form
	$("#form-contact").each(function(index) {
		$(this).validate({
 			errorLabelContainer: "#form-contact .errormsg",
			errorElement: "span", 
			wrapper: "li"
		});
	});
	// Call the validation JS for the Mailing List
	$("#form-mailinglist").each(function(index) {
		$(this).validate({
			errorClass: "inlineError",
			errorLabelContainer: "#form-mailinglist .errormsg",
			errorElement: "span", 
			wrapper: "li"
		});
	});
	


	   $.fn.clearDefault = function(){
	       return this.each(function(){
	           var default_value = $(this).val();
	           $(this).focus(function(){
	               if ($(this).val() == default_value) $(this).val("");
	           });
	           $(this).blur(function(){
	               if ($(this).val() == "") $(this).val(default_value);
	           });
	       });
	   };



	   /*    CLEAR INPUT FIELDS ONFOCUS
	       -------------------------------------------------------------*/

	       $("input.clearinput").clearDefault();

});
