/* Bind specific touch gestures to scrollables. */
var api 
$.fn.handleSwipes = function() {
  return this.each(function() {
    if ($(this).data) {
     api = $(this).data("scrollable");
      $(this).swipe({
        allowPageScroll:'vertical',
        swipeLeft:  function() { api.next(); },
        swipeRight: function() { api.prev(); }
      });
    }
  });
};

/* Custom JS */
jQuery(function ($) {
// window.setInterval("alert("+api+")",5000);

  var current = 0;
  
  if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
    
    // Add enhanced animations for iOS devices.
    var enhancer = document.createElement( 'script' );
    enhancer.type = 'text/javascript';
    enhancer.src = 'common/js/libs/jquery.animate-enhanced.min.js';
    $("head").append( enhancer );
    
    // Makes for a cleaner transition...
    $('section#upper.feature .item').each(function() {
      $(this).css({
        '-webkit-backface-visibility':'hidden',
        '-moz-backface-visibility':'hidden'
      });
    });

    var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
    if (viewportmeta) {
      viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
      document.body.addEventListener('gesturestart', function() {
        viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
      }, false);
    }
  }

  // Sets the slide caption.
  var setCaption = function (title) {
    $('#carousel-caption').html(title);
  }

  // Content Carousels
  $(".carousel").scrollable({
    size: 1,
    speed: 800,
    touch: false, /* Does not work as advertised; okthxbai! */
    easing: 'easeInBack',
    prev: 'ul.arrow-pager a.prev',
    next: 'ul.arrow-pager a.next',
	circular: true,
    onBeforeSeek: function(e, index) {
      var items = this.getItems();
      var item  = items.eq(index);
      var title = item.find('img').attr('alt');
      // Set the current caption/title.
      setCaption(title);
      items.removeClass('current from-left from-right');
      if (current < index) {
        item.addClass('current from-right');
      } else {
        item.addClass('current from-left');
      }
      current = index;
    }
  }).navigator({
    naviItem: 'li',
    navi: 'ul.arrow-pager li.pager-dots ul'
  }).autoscroll({interval: 8000}).handleSwipes();

  // Set up HTML5 autoclear for inputs.
  if(!Modernizr.input.placeholder){
  
    $('[placeholder]').focus(function() {
      var input = $(this);
      if (input.val() == input.attr('placeholder')) {
        input.val('');
        input.removeClass('placeholder');
      }
    }).blur(function() {
      var input = $(this);
      if (input.val() == '' || input.val() == input.attr('placeholder')) {
        input.addClass('placeholder');
        input.val(input.attr('placeholder'));
      }
    }).blur();
    $('[placeholder]').parents('form').submit(function() {
      $(this).find('[placeholder]').each(function() {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
          input.val('');
        }
      })
    });
  }

  // Set the caption/title for the first slide.
  if ($('.carousel').length > 0) {
    var title = $('.carousel .items > .item:first-child').find('img').attr('alt');
    setCaption(title);
  }

});
