$(document).ready(function() {

	// Animate our homepage features. Handles the sliding and arrow rotation.
	$(function() {
		$('#features li').hover(function(){
			$(this).find('div').animate({top:'38px'},{queue:false,duration:150});
			$(this).find('.featureArrow').rotate({animateTo:-180});
		}, function(){
			$(this).find('div').animate({top:'108px'},{queue:false,duration:250});
			$(this).find('.featureArrow').rotate({animateTo:-360});
		});
	});
	
	// Homepage Image Fade
	$(function(){
		slideTotal = $('#home-image>div').length;
		count=1;
		setInterval(function(){
			$('#home-image div:nth-child('+count+')').fadeOut(2000);
			count = count+1;
			if(count>slideTotal) {
				count = 1;
				$('#home-image div:nth-child('+count+')').fadeIn(2000);
			}else{
				$('#home-image div:nth-child('+count+')').fadeIn(2000);
			}
		}, 6000)
	});
	
	
	// This maintains the hover state on the main nav links while interacting with the drop down lists.
	$(".drop-div").hover(function () {
        $(this).parent('li').addClass("sub-select");
    }, function () {
       	$(this).parent('li').removeClass("sub-select");
    });


	// This fades the images on hover on the Used Boats Listing Page. Fadddddeees.
	$('#boat-listing li').each(function() {
		$(this).hover(function() {
			$(this).find('img').stop().animate({ opacity: 0.8 }, 100);
			},
		function() {
			$(this).find('img').stop().animate({ opacity: 1.0 }, 100);
		});
	});
	
	
	// Make the whole listing boxes clickable on the Used Boats Listing Page and the features on the homepage. 
	// Uses jsClickable plugin.
	$("#boat-listing li.boat, #features li").clickable();
	
	
	// Makes our scroll to top button scroll nice and smooth.
	$('a.top').click(function(event){
		event.preventDefault();
		$('html, body').animate({scrollTop:0}, 'fast');
	});


	// finds all lists within our content area and wraps the text inside them in a span so we can change the bullet colour
	// except the boat-specs lists and the team list as they have different styles.
	$(".copy li").not('.copy ul.boat-specs li, .copy ul#team li, #boat-types li').wrapInner('<span />');
	
	
	// adds hooks to the first and last list items
	$("ul li:first-child").addClass("first-child");
	$("ul li:last-child").addClass("last-child");
	$("ol li:first-child").addClass("first-child");
	$("ol li:last-child").addClass("last-child");
	
	
	// This checks our used-boat search on submit to see if any fields have a value of Any.
	// If they do we then disable the field to ensure that it doesn't get passed through to
	// the SuperSearch url query string. 
	$('#boat-search').bind('submit', function() {
		$(this).find('select option').each(function() {
			var value = $(this).val();
			if (value == "any") {
				$(this).attr("disabled", "disabled");
			}
		});
	});
	
	
	// Open the youtube videos on the boat page in FancyBox.
	$('a.video').click(function() {
	    $.fancybox({
	        'type' : 'iframe',
	        // hide the related video suggestions and autoplay the video
	        'href' : this.href.replace(new RegExp('watch\\?v=', 'i'), 'embed/') + '?rel=0&autoplay=1',
	        'overlayShow' : true,
	        'centerOnScroll' : true,
	        'speedIn' : 100,
	        'speedOut' : 50,
			'width' : 640,
			'height' : 390
	    });
	    return false;
	});
	
	
	// Moves the pagination links that Structure outputs to the correct div.
	$("ul.pagination").appendTo("div#bottom-actions");
	$("p.pagination").appendTo("div#bottom-actions");
	
	
	// Adding child id to our second level ul's that are in the sub navigation. Needed
	// for our css styles to target.
	$("#sub-nav ul ul").attr('id', 'child');
	
	
	// Our boat-id field on the contact page should only appear if the user selects the Boat Enquiry option.
	$('#topic').change(function() {
	    $('.specific-boat').toggle($(this).val() == "Boat Enquiry");
	}).change();
	
	
	// Below runs the gallerys on our Boat Detail Pages.
	$('#gallery').galleria({transition: 'fade'});

	
	// Reset our form values when user clicks the reset link.
	$("#reset").click(function(){
		event.preventDefault();
		$(':text', '#boat-search').val('');  
		$(':input, option:selected', '#boat-search').removeAttr('checked').removeAttr('selected');
		$('select', '#boat-search').val('any');
	});
	
	
	// Form validation
	$("#contact").validate({
	 invalidHandler: function(form) {
			$("#contact input[type=submit]").after("<label class='error all'>Please complete all of the above highlighted fields.</label>");	
		}
	});
	
	$("#ask-question").validate();
	
	
	// Arrows on SubNav
	$("#child").parent().addClass("hasChild").find("a").wrapInner("<span class='arrow' />");
	$(".parent-here.sub-level-0 a").not(".parent-here.sub-level-0.hasChild a, #main .parent-here.sub-level-0 a").wrapInner("<span class='arrow' />");
	
	
	// PDF Links
	$("a.pdf").click(function(){
		$("body").append('<div id="pdf-downloading"><h3>Thanks for waiting...</h3><p>Your PDF will begin to download shortly.</p></div>');
		$("#pdf-downloading").fadeIn();
		setTimeout(function(){
			$("#pdf-downloading").fadeOut();
		},6000);
	});
	
	
	// Clear the default value on our Newsletter name field.
	$("form #name").focus(function(){
		var nameValue = $(this).val();
		if (nameValue == "enter your name"){
			$(this).attr('value', '');
		}
	});
	
	
	// Put back the default value if they leave Newsletter name field empty.
	$("form #name").blur(function(){
		var nameValue = $(this).val();
		if (nameValue == ""){
			$(this).attr('value', 'enter your name');
		}
	});
	
	
	// Clear the default value on our Newsletter email field.
	$("form #email").focus(function(){
		var nameValue = $(this).val();
		if (nameValue == "enter your email"){
			$(this).attr('value', '');
		}
	});
	
	
	// Put back the default value if they leave Newsletter email field empty.
	$("form #email").blur(function(){
		var nameValue = $(this).val();
		if (nameValue == ""){
			$(this).attr('value', 'enter your email');
		}
	});
		
});
