(function($){

  var counter = 0;

  $.fn.extend({

    delay: function(time,fn) {
      this.each(function(){
        setTimeout(fn,time);
      });
      return this;
    },

    // Download a PDF from a select box selection (homepage)
    menu_downloader: function(){
      $(this).change(function(){
        var selected = $("option:selected", this).val();
        if(selected != 0){
          location.href = selected;
          // window.open(selected, '_target');
        }
      });
    },

    // BHG-Rewards card animation
    giftcard_animate: function (){
      
      if(counter > 1) return;

      var url;
      if(counter == 0) {
        url = 'http://store.bluegrasshospitality.com/ProductDetails.asp?ProductCode=GC';
      } else {
        url = 'http://www.mybhgrewards.com/';
      }

      counter = counter + 1;

      var self = this;

      $(this).find('.contents1, .contents2').toggleClass('hidden');

      function slide_back(){
        self.animate({
          marginLeft: '-270px'
        }, 500, function() { self.giftcard_animate(); } );
      }

      $(this)
        .css('cursor','pointer')
        .click(function(){
          window.open(url, "_target");
          return false;
        })
        .animate({ marginLeft: '0px' }, 500)
        .delay(10000, slide_back);

    },

    // Photo gallery navigation
    gallery_nav: function(){

      function loading(){
        $('#gallery').fadeOut();
        $('#gallery-viewport').fadeOut();
        $('#gallery-home').fadeOut();
        $('#loading').show();
      }

      function display_gallery(html){
        $('#gallery').html(html);
        $('#loading').fadeOut();
        $('#gallery-viewport').fadeIn();

        $('#prev, #next')
          .css('opacity', 0)
          .click(function(){ return false; })
          .hover(
            function(){
              $(this).animate({
                opacity: 0.5
              });
            },
            function(){
              $(this).animate({
                opacity: 0
              });
            }
          );

        // Cycle gallery
        $('#gallery').cycle({
          prev: '#prev',
          next: '#next'
        });

        return false;
      }

      $(this).find('a').click(function(){
        $('#prev, #next').unbind();
        loading();
        $.get($(this).attr('href'), {}, display_gallery, 'html');
        return false;
      });

    }

  });

})(jQuery);
