$(document).ready( function () 
{
  $('#container #print_button').click(print_button);
  $('#container #clear_all_btn').click(function ()
  {
    $.get(root_sub + '/system/ajax.php', { action : 'ClearAll'});
    $.each($('#container .clip'), function ()
    {
      var offer_id = $(this).attr('data');
      $('#offer'+offer_id).remove();
      $('#clipping'+offer_id).remove();
    });
    $('#clippings_count').text(0);
  });
  //removing a coupon
  $('#container a.clip').click(function()
  {
    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));
    $('#offer'+offer_id).remove();
    $('#clipping'+offer_id).remove();
    return false;
  });
  $('#container #save_button').click( function ()
  {
    jPrompt('Enter a name for this coupon book: ', '', 'Name for Coupon Book', function(user_label) 
    {
      if( user_label )
      {
        $.post(root_sub + '/system/ajax.php?action=save_couponbook', {label : user_label}, function (result, status)
        {
          if (status == 'success' && result.coupon_book_id != null)
            jAlert('Your coupon book has been successfully saved.<br/><br/><a href="'+root_sub+'/user/coupon_books.php">View My Coupon Books</a>');
          else
            jAlert('Sorry. Your coupon book could not be saved at this time.<br/>Please try again.');
        }, 'json');
      }
    });
  });
  $('#container #save_button_login').click( function ()
  {
    jAlert("Please <a href='"+root_sub+"/register.php'>register</a> or <a href='"+root_sub+"/login.php'>login</a> before saving.");
    return false;
  });
 
  //keyboard shortcut preventions
  $(document).bind('keydown', 'Ctrl+p', function (event)
  {
    jAlert('Please use the "Print Coupon" button');
    event.preventDefault(); 
    return false; 
  });
  $(document).bind('keydown', 'Ctrl+s', function (event)
  {
    jAlert('Please use the "Print Coupon" button');
    event.preventDefault(); 
    return false; 
  });
  
});

function print_button()
{
  my_window = window.open(root_sub + '/user/clippings.php?p=1','print_popup','width=1000,height=800,scrollbars=1,resizable=1');
  return false;
}
