
jQuery(function($)
{
  var $feature, $gallery;
  
  $(document).ready(function()
  {
    // Handle Feature
    if ($('#feature-navigation-bullets').length > 0)
    {
      create_featured();
    }
    
    // Handle Galleries
    if ($('.gallery').length > 0)
    {
      create_gallery();      
    }
    
    // Add Parent Menu Class
    $('ul.sub-menu, ul.tofino-expeditions').prev('a').addClass('parent');
    
    // Add seperator to form sections
    $('.gsection_title').each(function()
    {
      var title = $(this);
      
      title.html('<span>' + title.html() + '</span>')
    });   
    
    // Dynamic Updates for Book Trip Form
    if ($('.book-trip-name').length > 0)
    {
      $('.book-trip-name select, .book-trip-travelers select').change(function()
      {
        var price = $('.book-trip-name select:first').val().split('$');
        $('.book-trip-total-deposit input:first').val(parseInt(price[1])* $('.book-trip-travelers select:first').val());
      });
      
      var price = $('.book-trip-name select:first').val().split('$');
      $('.book-trip-total-deposit input:first').attr('disabled', 'disabled').val(parseInt(price[1]) * $('.book-trip-travelers select:first').val());
    
      $($('.book-trip-name').parents('form')[0]).submit(function()
      {
        $('.book-trip-total-deposit input:first').removeAttr('disabled');
      });
    }

  });

  function create_gallery()
  {
    // Update Gallery Formatting
    $('.gallery-caption').remove();
    $('.gallery a').unwrap().unwrap().unwrap();
    $('.gallery').wrap('<div id="gallery" />');
    
    $('#gallery').append(
      $('<img src="/wp-content/themes/tofino/images/zoom-icon.png" class="zoom-icon" />')
        .click(function()
        {
          var active = $(this).prev().children(':visible');
          Shadowbox.open({
            link: $(active)
            ,content: $(active).attr('href')
            ,player: 'img'
          });
        })
    );
  
    $gallery = $('.gallery').cycle({
      speed: 800 
      ,timeout: 5000
      ,timeoutFn: function(currSlideElement, nextSlideElement, options, forwardFlag)
      {
        $('#gallery-navigation-bullets a').each(function(i)
        {
          if (i == options.currSlide)
          {
            $(this).addClass('current');
          }
          else
          {
            $(this).removeClass('current').blur();
          }
        });

        return options.timeout;
      }
    });
    
    $gallery.children().each(function(i)
    {
      $('<a href="#">&bull;</a>')
        .addClass(i == 0 ? 'current' : '')
        .appendTo($('#gallery-navigation-bullets'))
        .click(function( )
        {
          $gallery.cycle($('#gallery-navigation-bullets a').index(this)); 

          return false;
        });
    });
  }
  
  function create_featured()
  {
    // Homepage Feature
    $feature = $('#feature').cycle({
      speed: 800 
      ,timeout: 5000
      ,timeoutFn: function(currSlideElement, nextSlideElement, options, forwardFlag)
      {
        $('#feature-navigation-bullets a').each(function(i)
        {
          if (i == options.currSlide)
          {
            $(this).addClass('current');
          }
          else
          {
            $(this).removeClass('current').blur();
          }
        });

        show_hide('#feature-title div', options.currSlide);
        show_hide('#feature-links a.feature-more-description-link', options.currSlide);
        show_hide('#feature-descriptions p', options.currSlide);
        
        function show_hide(list, current)
        {
          $(list).each(function(i)
          {
            if (i == current)
            {
              $(this).css('display', 'block');
            }
            else
            {
              $(this).css('display', 'none');
            }
          });
        }
        
        return options.timeout;
      }
    });
    
    $feature.children().each(function(i)
    {
      $('<a href="#">&bull;</a>')
        .addClass(i == 0 ? 'current' : '')
		
        .appendTo($('#feature-navigation-bullets'))
        .click(function()
        {
          $feature.cycle($('#feature-navigation-bullets a').index(this)); 
        
          return false;
        });
    });
  }

});

