(function($) {
  var SwissGrills = function() {
    this.bannerTimer = null;
    this.bannerTimeout = 7000;
  }

  SwissGrills.prototype.startBannerTimer = function() {
    this.bannerTimer = setInterval(function() {
      // select next banner item
      if($('#ticker').find('.active').next().length > 0) {
        $('#ticker').find('.active').next().find('a').click();
      } else {
        $('#ticker').find('a').eq(0).click();
      }
    }, this.bannerTimeout);
  };

  SwissGrills.prototype.banner = function() {
    var self = this;

    self.startBannerTimer();

    $('#bannerScroll').find('div').eq(0).addClass('active');

    $('#ticker').delegate('a', 'click', function() {
      clearInterval(self.bannerTimer);

      $('#ticker').find('li').removeClass('active').filter($(this).parent()).addClass('active');

      var index = $('#ticker').find('li').index($(this).parent());

      $('#bannerScroll').find('.active').fadeOut(function() {
        $('#bannerScroll').find('.bannerContainer').removeClass('active').eq(index).fadeIn().addClass('active');
      });

      self.startBannerTimer();

      return false;
    });
  };

  SwissGrills.prototype.productMenu = function() {
    $('#mainNav').find('.products').hover(function() {
      if($('#navRoll').is(':hidden')) {
        $('#navRoll').stop(true, true).slideDown('fast');
      } else {
        $('#navRoll').stop(true, true).slideDown(0);
      }
    }, function() {
      $('#navRoll').stop(true, true).slideUp('fast');
    });

    $('#navRoll').hover(function() {
      $('#navRoll').stop(true, true).slideDown(0);
    }, function() {
      $('#navRoll').stop(true, true).slideUp('fast');
    });
  };

  SwissGrills.prototype.hideLabels = function() {
    $('label').each(function(i, label) {
      var id = $(label).attr('for');
      if(id != "warranty_model" && id != "warranty_date_of") {
        $(label).hide();
      }
    });
  };

  SwissGrills.prototype.lightbox = function() {
    if($("a[rel^='prettyPhoto']").length > 0) {
      $("a[rel^='prettyPhoto']").prettyPhoto();
    }
  };

  SwissGrills.prototype.share = function() {
    if($('#share').length > 0) {
      $('#share').find('a').each(function(i, share) {
        var background = $(this).css('background-position').split(" ");
        $(this).css({backgroundPosition:background[0]+" 29px"});
      });

      $('#share').find('a').hover(function() {
        var background = $(this).css('background-position').split(" ");
        $(this).css({backgroundPosition:background[0]+" 0px"});
      }, function() {
        var background = $(this).css('background-position').split(" ");
        $(this).css({backgroundPosition:background[0]+" 29px"});
      });
    }
  };

  SwissGrills.prototype.extendCycle = function() {
    $.fn.cycle.commonReset = function(curr,next,opts,w,h,rev) {
      $(opts.elements).not(curr).hide();
      opts.cssBefore.opacity = 1;
      opts.cssBefore.display = 'block';
      if (w !== false && next.cycleW > 0)
        opts.cssBefore.width = next.cycleW;
      if (h !== false && next.cycleH > 0)
        opts.cssBefore.height = next.cycleH;
      opts.cssAfter = opts.cssAfter || {};
      opts.cssAfter.display = 'none';
      $(curr).css('zIndex',opts.slideCount + (rev === true ? 1 : 0));
      $(curr).css('left', opts.offsetLeft);
      $(next).css('zIndex',opts.slideCount + (rev === true ? 0 : 1));
    }

    $.fn.cycle.transitions.carousel = function($cont, $slides, opts) {
      $cont.css('overflow','hidden').width();
      opts.before.push(function(curr, next, opts, fwd) {
        if (opts.rev)
          fwd = !fwd;
        $.fn.cycle.commonReset(curr,next,opts);
        opts.cssBefore.left = fwd ? (next.cycleW-1) : (1-next.cycleW);
        opts.animOut.left = fwd ? -curr.cycleW : curr.cycleW;
      });
      opts.cssFirst = { left: 0 + opts.offsetLeft };
      opts.cssBefore= { top: 0 };
      opts.animIn   = { left: 0 + opts.offsetLeft };
      opts.animOut  = { top: 0 };
    };
  };

  SwissGrills.prototype.initialize = function() {
    if($('#bannerScroll').find('.bannerContainer').length > 1) {
      this.banner();
    }

    this.productMenu();

    this.hideLabels();

    this.lightbox();

    this.share();

    if(typeof($.fn.cycle) != "undefined") {
      this.extendCycle();

      $('.productContainer, .featuredProducts > .container').each(function() {
        var _self = $(this);
        _self.cycle({
          timeout: 0,
          fx: 'carousel',
          slideExpr: 'ul',
          easing: 'easeInOutQuint',
          fit: true,
          next: _self.find('.arrow.forward'),
          prev: _self.find('.arrow.back'),
          containerResize: false,
          offsetLeft: 20,
          cssAfter: {
            left: '20px'
          }
        });
      });
    }
  };

  $(document).ready(function() {
    sg = new SwissGrills();
    sg.initialize();
  });
})(window.jQuery);
if(!('placeholder'in $('<input>').get(0))){$("input[placeholder],textarea[placeholder]").each(function(){if($(this).val()===''){$(this).val($(this).attr("placeholder")).addClass('_placeholder')}$(this).bind('focus.placeholder',function(){if($(this).val()===$(this).attr("placeholder")){$(this).val('').removeClass('_placeholder')}}).bind('blur.placeholder',function(){if($(this).val()===''){$(this).val($(this).attr("placeholder")).addClass('_placeholder')}})})}

