init_clicks = function() {
  if (analytics_enabled) {
    $('.print a, .coupon_actions a.print').click(function(){
      analytics_event(analytics_code, 'Coupon', 'Print coupon', window.location.pathname);
      window.location = $(this).attr('href');
    });

    $('.vote_line a').click(function(){
      analytics_event(analytics_code, 'Coupon', 'Vote', $(this).attr("href"));
    });
  }

  $('#become_a_member_link').click(function(e) {
    show_member_form();
    hide_suggestions_form();

    e.preventDefault();
  });

  $('#become_a_member_link_expanded').click(function(e) {
    hide_member_form();

    e.preventDefault();
  });

  $('#suggest_a_coupon_link').click(function(e) {
    show_suggestions_form();
    hide_member_form();

    e.preventDefault();
  });

  $('#suggest_a_coupon_link_expanded').click(function(e) {
    hide_suggestions_form();

    e.preventDefault();
  });

  $('.vote').click(function(e) {
    $.get($(this).attr('href'), $.proxy(function(data) {
      $(this).parent().next().html(data.votes).effect("highlight", {}, 2500);
      $('#flash').remove();
      $(data.message).prependTo("#body_content_left");
      $('#flash').show();
      $(document).oneTime(2500, "hide_flash", function(){
        $('#flash').remove();
      });
      $('#flash #flash_close').click(function(e) {
        $(this).parent().remove();
        e.preventDefault();
      });
    }, $(this)));
    e.preventDefault();
  });
}

init_flash_messages = function(){
  $('#flash').fadeIn(550);
  $('#flash_close').click(function(e) {
     $('#flash').fadeOut({duration: 330});
     e.preventDefault();
  });
  $('#flash.flash_message').prependTo('#records');
}

analytics_event = function(analytics_code, category, action, label) {
  if (typeof(_gat) == 'object') {
    var pageTracker = _gat._getTracker(analytics_code);
    pageTracker._initData();
    pageTracker._trackEvent(category, action, label);
  }
}
possible_analytics_event = function(analytics_enabled, analytics_code, category, action, label) {
  if (typeof(analytics_enabled != "undefined") && analytics_enabled) {
    analytics_event(analytics_code, category, action, label);
  }
}

show_suggestions_form = function() {
  $('#suggest_a_coupon_link').hide();
  $('#suggest_a_coupon_link_expanded').show();

  $('#suggest_a_coupon_form').slideDown({easing: "swing"});
  $('#become_a_member_form').slideUp({easing: "swing"});
  $('#suggest_a_coupon').addClass('expanded').removeClass('not_expanded');
}

hide_suggestions_form = function() {
  $('#suggest_a_coupon_link_expanded').hide();
  $('#suggest_a_coupon_link').show();

  $('#suggest_a_coupon_form').slideUp({
    easing: "swing"
    , complete: function(){
      $('#suggest_a_coupon').addClass('not_expanded').removeClass('expanded');
    }
  });
}

fix_background = function() {
  $(document.body).css('background-position', ($(document.body).get(0).clientWidth > 1200 ? 'center' : '0 0'));
}

$(document).ready(function() {
  $.placeholder.shim();
	init_flash_messages();
	init_delete_confirmations();

  $('#become_a_member, .content_page #body_content_left, #body_content_left .content_page, #twitter, #suggest_a_coupon, .inquiries #body_content_right #inquiry_content, .coupon_chooser, #todays_coupons_filter, #todays_coupons_filter > ul').corner();
  $('#todays_coupons_subfilter li a').corner("bottom");
  $('#todays_coupons_area_filter').corner("top");

  init_clicks();

  $('.sfa_verified, .suggestion_field label a').each(function(index, element){
    $(element).hover(function(e) {
      (tooltip = $("<div class='tooltip'></div>")).html($(this).attr('tooltip'));
      tooltip.appendTo($("#page")).css({
          'left': ((left = $(this).offset().left - (tooltip.outerWidth() / 2) + ($(this).outerWidth() / 2)) >= 0 ? left : 0)
        , 'top': $(this).offset().top - tooltip.outerHeight() - 6
      }).corner().show();
    }, function(e) {
      $('.tooltip').remove();
    });

    if ($(element).attr('tooltip') == null) {
      $(element).attr({'tooltip': $(element).attr('title'), 'title': ''});
    }
    // wipe clean the title on any children too.
    $(element).children('img').attr('title', '')
  });

  fix_background();
  $(document.onresize ? document : window).resize(fix_background);
});

init_delete_confirmations = function() {
  $('a.confirm-delete').click(function(e) {
    if (confirm("Are you sure you want to " + (t=($(this).attr('title') || $(this).attr('tooltip')))[0].toLowerCase() + t.substring(1) + "?"))
    {
      $("<form method='POST' action='" + $(this).attr('href') + "'></form>")
        .append("<input type='hidden' name='_method' value='delete' />")
        .append("<input type='hidden' name='authenticity_token' value='" + $('#admin_authenticity_token').val() + "'/>")
        .appendTo('body').submit();
    }
    e.preventDefault();
  });
}