
$(document).ready(function()
{
  //clip links
  set_clip_link();
  
  // Couponbook tab
  $('#couponbook_tab a').click(function()
  {
    if ($('#couponbook_tab').hasClass('open')) { //close tab
    
      close_couponbook_tab();
        
    } else { //open table
        
        $.get(root_sub + '/system/ajax.php', { action : 'couponbook'}, display_couponbook, 'html');
        close_feedback_tab();
        $('#couponbook_tab')
        .animate({left:'-40px'}, 'slow')
        .addClass('open');
        
    }
    return false;
  });
  $('#couponbook_close').click(function()
  {
    close_couponbook_tab();
    
    return false;
  }); 
  $('#couponbook_tab #print_button_tab').click(function ()
  {
    my_window = window.open(root_sub + '/user/clippings.php?p=1','print_popup','width=1000,height=800,scrollbars=1,resizable=1');
    return false;
  });
  $('#couponbook_tab #clear_all_btn').click(function ()
  {
    $.get(root_sub + '/system/ajax.php', { action : 'ClearAll'});
    $('#couponbook_listing').html('There are no coupons in your coupon book.');
    $('#clippings_count').text(0);
    
    //update counts for the listings
    $.each($('#couponbook_tab a.clip'), reset_listing);
    $.each($('#left_container a.clip'), reset_listing);
    $.each($('.left_container a.clip'), reset_listing);
    $.each($('#merchant_extra_info a.clip'), reset_listing);
    if ( ($('#left_container').length == 0) && 
         ($('.left_container').length == 0) &&
         ($('#merchant_extra_info').length == 0))
    {
      $.each($('#container .clip'), function ()
      {
        var offer_id = $(this).attr('data');
        $('#offer'+offer_id).remove();
      });
    }
    set_clip_link();
  });
  

  // Feedback Tab
  $('#feedback_tab a').click(function()
  {
    if ($('#feedback_tab').hasClass('open')) { //close
        close_feedback_tab();
    } else { //open 
        $('#feedback_tab')
        .animate({left:'-40px'}, 'slow')
        .addClass('open');
        close_couponbook_tab()
    }
    return false;
  });
  $('#feedback_close').click(function()
  {
    close_feedback_tab();
    return false;
  }); 
});



//HELPER FUNCTIONS
function reset_listing()
{
  var offer_id = $(this).attr('data');
  $(this).parent().html('<a href="#" class="clip" data="'+offer_id+'" title="Add To Coupon Book">' +
            '<img src="'+root_sub+'/img/cut.png" style="position:relative;top:3px" alt="Add"/> '+
            'Add to Coupon Book'+
          ' </a>');
}



function close_feedback_tab()
{
  $('#feedback_tab')
      .animate({left:'-367px'}, 300)
      .removeClass('open');
}

function close_couponbook_tab()
{
  $('#couponbook_tab')
        .animate({left:'-367px'}, 300, '', 
          function () 
          {
            $('#couponbook_listing').html('<img src="'+root_sub+'/img/ajax-loader.gif" class="loading" alt="loading"/>');
          })
        .removeClass('open');
}

function display_couponbook(data, status)
{
  $('#couponbook_listing').html(data);
  $('#couponbook_tab a.clip_remove').click(function() //Remove coupon
  {
    var offer_id = $(this).attr('data');
    $.get(root_sub + '/system/ajax.php', { action : 'Added', id : offer_id });
    var count = parseInt($('#clippings_count').text());
    $('#clippings_count').text((count+-1));
    $('#clipping'+offer_id).remove();
    
    if ( ($('#left_container').length == 0) && 
         ($('.left_container').length == 0) &&
         ($('#merchant_extra_info').length == 0))
      $('#offer'+offer_id).remove();
    
    //update listing if present
    var previous_count = parseInt($('#offer'+offer_id+' .clipping_count').text());
    var new_count = previous_count - 1;

    if (new_count > 0)
      $('#offer'+offer_id+' .clipping_count').text(new_count);
    else
      $('#offer'+offer_id+' .clipping_count').parent().html('<a href="#" class="clip" data="'+offer_id+'" title="Add To Coupon Book">' +
                '<img src="'+root_sub+'/img/cut.png" style="position:relative;top:3px" alt="Add"/> '+
                'Add to Coupon Book'+
              ' </a>');
    set_clip_link();
    return false;
  });
}

function set_clip_link()
{
  $('#couponbook_tab a.clip').click(clip);
  $('#left_container a.clip').click(clip);
  $('.left_container a.clip').click(clip);
  $('#merchant_extra_info a.clip').click(clip);
}

function update_couponbook(data, status)
{
  if ($('#couponbook_tab').hasClass('open'))
  {
    $.get(root_sub + '/system/ajax.php', { action : 'couponbook'}, display_couponbook, 'html');
  }
}

function clip()
{
     $.get(root_sub + '/system/ajax.php', { action : 'Add to Coupon Book', id : $(this).attr('data') }, update_couponbook);
    var count = parseInt($('#clippings_count').text());
    //var inc = ($.trim($(this).text()) == 'Add to Coupon Book') ? 1 : -1;
    var inc = 1;
    $('#clippings_count').text((count+inc));
    //$(this).html(($.trim($(this).text()) == 'Add to Coupon Book') ? 'Added' : '<img alt="Add" style="position: relative; top: 3px;" src="'+root_sub+'/img/cut.png"/> Add to Coupon Book'); 
    if ($.trim($(this).text()) == 'Add to Coupon Book')
    {
      $(this).before('(<span class="clipping_count">1</span>Added) ');
    }
    else
    {
      var clipping_count = parseInt($(this).parent().find('.clipping_count').text());
      clipping_count++;
      $(this).parent().find('.clipping_count').text(clipping_count);
    }
    $(this).html('<img src="'+root_sub+'/img/cut.png" style="position:relative;top:3px" alt="Add"/> Add more'); 
    return false;
}
