(function ($) {

/**
 * Tiny cycling plugin
 */
$.fn.tinycycle = function (options) {
  $(this).each(function () {
    var target = $(this);
    // cycle context object
    var o = $.extend({
      // options
      controls: [],
      delay: 10,
      transition: 500,
      color: '#fff',
      // internal
      slides: target.children(),
      current: -1,
      interval: null,
      lock: false,
      init: function () {
        // append the fader element
        target.css('position', 'relative');
        o.fader = $('<div class="fader"></div>')
          .css({ position:'absolute', top:0, left:0, right:0, bottom:0, backgroundColor: o.color})
          .appendTo(target);
        
        if (o.controls.length) {
          o.controls.click(function () {
            o.goto(o.controls.index(this));
            o.resetInterval(o.delay);
          });
        }
        o.slides.hide();
        o.goto(0);
        o.resetInterval(o.delay);
      },
      goto: function (num) {
        var o = this;
        if (o.lock || o.current == num) {
          return;
        }
        var next = o.slides.eq(num);
        if (next.length) {
          o.lock = true;
          o.fader.fadeIn(o.transition, function () {
            // switch slides
            o.slides.eq(o.current).hide();
            next.show();
            // change controls
            o.controls.eq(o.current).removeClass('active');
            o.controls.eq(num).addClass('active');
            
            o.current = num;
            o.fader.fadeOut(o.transition, function () {
              o.lock = false;
            });
          });
        }
      },
      next: function () {
        var o = this;
        var index = o.current + 1;
        if (index >= o.slides.length) {
          index = 0;
        }
        o.goto(index);
      },
      resetInterval: function (delay) {
        if (o.interval !== null) {
          clearInterval(o.interval);
          o.interval = null;
        }
        if (delay > 0) {
          o.interval = setInterval(function () {
            o.next();
          }, delay * 1000);
        }
      }
    }, options);
    
    o.init();
  });
  return this;
}




$(document).ready(function() { 
	resizeImages('.rev-photo img', 266);
	 
 	//  featured reviews cycling
	  $('#featured:not(.theme-processed)').each(function () {
	    var $featured = $(this).addClass('theme-processed');
	    
	    var $reviews = $featured.children().filter('.reviews');
	    var $thumbs = $featured.children().filter('.thumbs');
	    $reviews.tinycycle({ controls: $thumbs.children() });
	  });
	  
	  $('#social:not(.theme-processed)').each(function () {
	    var $inputs = $(this).addClass('theme-processed').find('.email input.textfield');
	    $inputs.bind('focus blur keydown click init', function () {
	      var $input = $(this);
	      if ($input.val() != '') {
	        $input.addClass('has-text');
	      } else {
	        $input.removeClass('has-text');
	      }
	    }).trigger('init');
	  
	});

});

})(jQuery);


