/**
 * Ensure that #feature-bar always reaches the footer
 * @return void
 */
function featureBarHeight(){
  $('#feature-bar').height(($('#wrapper').height()-parseInt($('#feature-bar').css('padding-bottom'), 10)) + 48 + 'px');
  return;
}

/**
 * Check that an object property is both defined and non-null
 * @param prop
 * @return bool
 */
function propDefined(prop){
  return ( typeof(prop) !== 'undefined' && prop !== null ? true : false );
}

/**
 * Get an object containing the data attributes
 * @param obj a - A jQuery object w/ data attributes
 * @return bool
 */
function getOffer(a){
  return offer = {
    participant: ( propDefined(a.data('participant')) ? a.data('participant') : a.text() ),
    offer: ( propDefined(a.data('offer')) ? a.data('offer') : false ),
    description: ( propDefined(a.data('offer'))? a.data('description') : false ),
    address: ( propDefined(a.data('address')) ? a.data('address') : false ),
    city: ( propDefined(a.data('city')) ? a.data('city') : false ),
    neighborhood: ( propDefined(a.data('neighborhood')) ? a.data('neighborhood') : false ),
    phone: ( propDefined(a.data('phone')) ? a.data('phone') : false ),
    url: ( propDefined(a.data('url')) ? a.data('url') : false ),
    hours: ( propDefined(a.data('hours')) ? a.data('hours') : false ),
    img: ( propDefined(a.data('image')) ? a.data('image') : false )
  }
}

/**
 * Load an offer into #offer-detail
 * @param obj a - A jQuery object w/ data attributes
 * @param bool i - Initial load?
 * @return bool
 */
function loadOffer(a,i){
  var offer = getOffer(a);
  var h = $('#offer-detail .inner').height();
  if( typeof(i) !== 'boolean' ){
    i = false;
  }

  $('#offer-detail .inner').fadeTo(100, 0.0, function(){
    $.each(offer, function(k,v){
      if( v && typeof(v) === 'string' && k !== 'img' && k !== 'url' ){
        $('#o-' + k).html(v).show();
      } else {
        $('#o-' + k).hide();
      }
    });
    if( offer.img ){
      $('#o-img').attr({
        src: offer.img,
        alt: offer.participant
      }).show();
    } else {
      $('#o-img').hide();
    }
    if( offer.url ){
      $('#o-url').attr('href', offer.url).show();
    } else {
      $('#o-url').hide();
    }
    if( offer.hours ){
      $('#o-hours').prepend('Hours<br>');
    }

    if( i ){
      featureBarHeight();
    }

    // Animate height changes to make them smoother
    $('#offer-detail, #feature-bar').animate({
      height: '+=' + ( i ? 0 : ($('#offer-detail .inner').height()-h) + 'px' )
    }, 200, function(){
      $('#offer-detail .inner').fadeTo(100, 1.0);
    });
  });
}

jQuery(document).ready(function($){
  if( $('body').id !== 'home' ){
    $('#content h1').hide(0, function(){
      $('#offer-detail').show(0, function(){
        featureBarHeight();
      });
    });
    $('.offer-list').addClass('active');
  }

  // Load the first offer on page load
  if( $('#offer-detail').length > 0 ){
    var firstOffer = $('.offer-list a.offer:first');
    firstOffer.addClass('current');
    loadOffer(firstOffer, true);
  }

  // Handle clicks on .offer-list items
  $('.offer-list a').click(function(){
    $('.offer-list .current').removeClass('current');
    $(this).addClass('current');
    loadOffer($(this), false);
    return false;
  });

  // Open a[rel="external"] in a new window
  $('a[rel="external"]').click(function(){
    window.open($(this).attr('href'));
    return false;
  });

  // Help browsers that don't support a:hover selector in CSS (IE)
  $('#feature-bar').find('a').hover(function(){
    $(this).addClass('hover');
  }, function(){
    $(this).removeClass('hover');
  });
});
