$(function(){
// --------------------- Variables  -------------------------
    var boatList = $('div.v-container');


    // --------------------- Nivo Slider Options -------------------------
    $(window).load(function() {
        $('#slider').nivoSlider({
            pauseTime:6000
        })
    });

    // --------------------- Slider Warning -------------------------
    $('#slider-javascript-warning').hide();


    // Main Navigation
    // Functionality
    $("#main-nav ul").find('li:nth-child(n+5)').addClass('left-nav');
    $("#main-nav ul li:has(ul)").hover(function(){
        $(this).find('ul').hide();
        $(this).find('ul:first').slideDown('fast');
        }, function() {
            $(this).find('ul:first').slideUp(50);
            }
    );
    $("#main-nav ul li:has(ul) > a").css({'background': 'url(http://nowrapowerboats.com.au/wp-content/themes/NowraPowerboatsV2/images/dropdown-arrows.jpg) no-repeat center 85%'});
    $("#main-nav ul ul li > a").css({'background': 'none'});
    // --------------------- END of NAVIGATION -------------------------


    //--------- Used Boats NGG carousel AJAX ----------
    boatList.each(function(){
        //Variables
        var largePath = $(this).find('a').attr("href");
        var imgAlt = $(this).find('img').attr("alt");
        $(this).find("li.ngg-thumbnail-list:first").addClass('selected');
        $(this).find("li.ngg-thumbnail-list").live('click',function(){
            largePath = $(this).find('a').attr("href");
            imgAlt = $(this).find('img').attr("alt");
		if ($(this).is(".selected")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			//The Main Image change
                $(this).parents(".ngg-galleryoverview").find('.pic img').attr({ src: largePath, alt: imgAlt });

		}
		$(this).parents(".ngg-galleryoverview").find("li.ngg-thumbnail-list").removeClass('selected'); //Remove class of 'active' on all lists
		$(this).addClass('selected');  //add class of 'active' on this list only
		return false; //Prevent page reload

	});
    });
    $('div.v-container:last').addClass('last');

    /*$("li.ngg-thumbnail-list").live('click',function(){
		//Variables
		var imgAlt = $(this).find('img').attr("alt");
		var largePath = $(this).find('a').attr("href");

		if ($(this).is(".selected")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			//The Main Image change
                $(this).parents(".ngg-galleryoverview").find('.pic img').attr({ src: largePath, alt: imgAlt });

		}
		$("li.ngg-thumbnail-list").removeClass('selected'); //Remove class of 'active' on all lists
		$(this).addClass('selected');  //add class of 'active' on this list only
		return false; //Prevent page reload

	}); */

    // --------------------- Event Accordions -------------------------
    $('#accordion dd:first').addClass('default');
    $('#accordion .default').css({'display': 'block'});
    $('#accordion dt').click(function(){
        if ($(this).next().css('display') == 'block'){
          // Do nothing
        } else {
          $('#accordion dd').slideUp();
          $(this).next().slideDown();
          return false;
          }
    });


     /// --------------------- Link Hover Effects -------------------------
    $('div.nav-blocks ul li, #used-boats-sidebar h3').hover(function(e){
            $el = $(this);
        $el.find('a').hoverFlow(e.type, {'left': '20px'});
    }, function(e){
            $el = $(this);
        $el.find('a').hoverFlow(e.type, {'left': '0'});
    });


    /* Search Sidebar */
    $('#sidebar #s').focus(function(){
        $('#search label em.text-flourish').fadeOut(100);
        $('#sidebar #searchsubmit').fadeIn('fast');
    });
    $('#sidebar #s').blur(function(){
      $('#search label em.text-flourish').fadeIn('fast');
      $('#sidebar #searchsubmit').fadeOut(100);
    });

    // --------------------- Sticky Footer -------------------------
      $(window).bind("load", function() {

             var footerHeight = 0,
                 footerTop = 0,
                 $footer = $("#footer");

             positionFooter();

             function positionFooter() {

                      footerHeight = $footer.height();
                      footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";

                     if ( ($(document.body).height()+footerHeight) < $(window).height()) {
                         $footer.css({
                              position: "absolute"
                         }).animate({
                              top: footerTop
                         })
                     } else {
                         $footer.css({
                              position: "static"
                         })
                     }

             }

             $(window)
                     .scroll(positionFooter)
                     .resize(positionFooter)

      });

});
