var fh_promo, fh_promo_autoplay;

jQuery(function($){

	$('a.track_button').click(function(){
		try {
			pageTracker._trackEvent('Button Click', $(this).text() + ' - ' + this.href );
			setTimeout('document.location = "' + this.href + '"', 100);
			return false;
		}catch(err){}
	});


	fh_promo = new FeedHenryPromo('.feedhenry-promo');

	// Why doesn't $() work here?
	if ( jQuery('#carousel').length ) {
		jQuery('#carousel').jcarousel({
			scroll : 1,
			initCallback : carousel_initCallback,
			itemVisibleInCallback : carousel_itemVisibleInCallback,
			buttonNextHTML : null,
			buttonPrevHTML : null,
			itemFallbackDimension : 300
		});
	}

	$('.cta-box form input[type="text"]').each(function(){
		$(this).attr('data-placeholder',$(this).attr('value'));
	}).focus(function(){
		if ( $(this).val() == $(this).attr('data-placeholder') )
			$(this).val('');
	}).blur(function(){
		if ( $(this).val() == '' )
			$(this).val( $(this).attr('data-placeholder') );
	});

});

function carousel_itemVisibleInCallback( carousel, item, index, state ) {
	jQuery('.carousel-counter').removeClass('current');
	jQuery('#carousel-counter-'+index).addClass('current');
}

function carousel_initCallback( carousel ) {

	jQuery('.carousel-counter').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).find('span').text()));
        return false;
    });

	jQuery('.button-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('.button-prev').bind('click', function() {
        carousel.prev();
        return false;
    });

};


function FeedHenryPromo(selector){
  
  this.transition_length = 850;
  this.autoplay_interval = 5000;
  
  this.container = $(selector);
  this.icons = this.container.find('ul.promo-icons li.promo-icon');
  this.images = this.container.find('ul.promo-images li');
  
  this.current_icon = this.icons.first(); //the first item is the prev button, second item is the first icon
  this.current_image = this.images.first();
  
  var self = this;
  this.icons.find('a').click(function(e){self.clicked_icon(e, this)});
  
  this.next_button = this.container.find('ul.promo-icons li.next')
  this.prev_button = this.container.find('ul.promo-icons li.prev')
  this.next_button.click(function(e){self.next()});
  this.prev_button.click(function(e){self.prev()});
  
  this.current_icon.find('a').click();
  this.autoplay = setInterval(function(){self.next()},this.autoplay_interval);
}

FeedHenryPromo.prototype = {
  clicked_icon: function (e, clicked){
    e.preventDefault();
    e.stopPropagation();
    var a = $(clicked);
    var href = a.attr('href');
    clearInterval(this.autoplay);
    this.select(a, href);
  },
  
  next: function(){
    var icon = this.current_icon.next('.promo-icon');
    if( icon.length == 0 ) icon = this.icons.first();
    var a = icon.find('a')
    this.select(a, a.attr('href'));
  },
  
  prev: function(){
    var icon = this.current_icon.prev('.promo-icon');
    if( icon.length == 0 ) icon = this.icons.last();
    var a = icon.find('a')
    this.select(a, a.attr('href'));
  },
  
  select: function(a, href){
    if( this.current_icon.find('a').attr('href') != href){
      this.current_image.removeClass('selected');
      this.current_icon.removeClass('selected');
    
      this.current_icon = a.parent('li')
      this.current_icon.addClass('selected');
    
      this.current_image.fadeOut(this.transition_length);
      this.current_image = this.images.filter(href);
      this.current_image.fadeIn(this.transition_length);
    }
  }
}

