var progressbar_interval =  0;


$(document).ready( function () 
{
  $('a.printlink').click(function()
  {
    var offer_id = $(this).attr('data');
    my_window = window.open(root_sub + '/merchant/preview.php?p=1&id='+offer_id,'print_popup','width=1000,height=800,scrollbars=1,resizable=1');
    return false;
  });
  
  $('a.subscribetaglink').click(function()
  {
    var link = $(this).attr('href');
    $.alerts.select_email('How often do you want updates from stores with this tag?',
                          '',
                          'Subscription Setting',
                          function (r)
                          {
                            if (r != null)
                            {
                              $.get(link+'&ajax=1&deliver='+r);
                              jAlert("This tag has been <b>added</b> to your <a href='"+root_sub+
                                      "/user/subscriptions.php'>Subscriptions</a>.",
                                     'Subscription Added');
                            }
                          });
    return false;
  });
  
  $('a.subscribetaglink_login').click(function()
  {
    jAlert("Please <a href='"+root_sub+"/user/register.php'>register</a> or <a href='"+root_sub+"/login.php'>login</a> to subscribe to this tag.");
    return false;
  });
  
  $('#mall_plaza_filter select').change( function ()
  {
    var plaza_id = $('#mall_plaza_filter select').val();
    $.get(root_sub + '/system/ajax.php?action=plaza_store&p=' + plaza_id, '', function (data, status)
    {
      $('#store_directory').html(data);
    }, 'html');
    $('#store_directory').html('<div style="padding: 50px 0;text-align:center">' +
                               '<img src="'+root_sub+'/img/ajax-loader.gif"/>'+
                               '</div>');
  });
  
  $('a.dodlink').click(function()
  {
    $.get($(this).attr('href')+'&ajax=1');
    jAlert("You have successfully subscribed to the <br/>Coupon of the Day newsletter.<br/>" +
           "<br/><a href='"+root_sub+"/user/newsletter_settings.php'>Change Subscription</a>",
           'Subscribed');
    return false;
  });
  
  $('a.dodlink_login').click(function()
  {
    jAlert("Please <a href='"+root_sub+"/user/register.php'>register</a> or <a href='"+root_sub+"/login.php'>login</a> first before subscribing.");
    return false;
  });
  
  
  //countdown used for deal of the day
  if (typeof(coupon_of_day) != "undefined")
  {
    $('.countdown').countdown({until: $.countdown.UTCDate(-8, coupon_of_day.year, coupon_of_day.month-1, coupon_of_day.day, coupon_of_day.hour,coupon_of_day.min,coupon_of_day.sec,0), 
                                              serverSync: serverTime, 
                                              timezone: -8,
                                              compact: true,
                                              layout : coupon_of_day_text + '<b>{hnn}{sep}{mnn}{sep}{snn}</b>',
                                              expiryUrl: root_sub});
  }
  if ($('#progressbar').length != 0)
  {
    $('#progressbar').progressbar({value:progressbar_percentage});
    progressbar_interval = setInterval('updateProgressbar();', 60000);
  }
  
  $('#coupon_of_day_previous a').click (function ()
  {
    $('.previous_coupon_of_day').animate({marginLeft:'0'}, 300);
    return false;
  });
  
  $('#coupon_of_day_current a').click (function ()
  {
    $('.previous_coupon_of_day').animate({marginLeft:'-650px'}, 300);
    return false;
  });
  
  $('#coupon_of_day_print a').hover( function ()
  {
    $(this).find('img').attr('src', root_sub + '/img/coupon_of_day/printGo_hover.png');
  }, function ()
  {
    $(this).find('img').attr('src', root_sub + '/img/coupon_of_day/printGo_inactive.png');
  });
  
  $("#coupon_of_day_print a").click( function (event)
  {
    if ($(this).parent().attr('class') == 'fade')
    {
      jAlert('Sorry. Coupon is not available for print yet.');
    }
    else
    {
      var offer_id = $(this).attr('data');
      jConfirm('Do you want to subscribe to the specials from this store?', 'Subscribe to specials for this store', 
               function(result)
               {
                if (result) //show subscribe popup
                {
                  $('#coupon_of_day_right a.subscribe_link').trigger('click', [offer_id]);
                  $('#coupon_of_day_right a.subscribe_link_login').trigger('click', [offer_id]);
                }
                else
                  my_window = window.open(root_sub + '/merchant/preview.php?p=1&id='+offer_id,'print_popup','width=1000,height=800,scrollbars=1,resizable=1');
                return false;
               });
    }
    event.preventDefault();
    return false;
  });
  
  //keyboard shortcut preventions
  if ($('#coupon_of_day_left').length > 0)
  {
    $(document).bind('keydown', 'Ctrl+p', function (event)
    {
      jAlert('Please use the <b>"Print & Go"</b> button or <b>"Print"</b> links to get a valid coupon.');
      event.preventDefault(); 
      return false; 
    });
    $(document).bind('keydown', 'Ctrl+s', function (event)
    {
      jAlert('Please use the <b>"Print & Go"</b> button or <b>"Print"</b> links to get a valid coupon.');
      event.preventDefault(); 
      return false; 
    });

    $(document).bind("contextmenu",function(event)
    {
      jAlert('Please use the <b>"Print & Go"</b> button or <b>"Print"</b> links to get a valid coupon.');
      event.preventDefault(); 
      return false;
    });
  }
});

function updateProgressbar()
{
  var percentage = null;
  var left = null;

  $.ajax({url: root_sub + '/system/ajax.php?action=claimed', 
          async: false,
          dataType : 'json',
          success : function (result)
          {
            percentage = result.percentage;
            if (percentage == '100')
              clearInterval ( progressbar_interval );
          
            if (percentage != null)
            {
              $('#progressbar').progressbar('option', 'value', percentage);
              $('#progressbar_info').text(percentage + '% claimed');
            }
          },
          error : function (result, text)
          {
            //alert('error' + text);
          }});
}

  function serverTime() { 
    var time = null; 
    $.ajax({url: root_sub+'/system/ajax.php?action=server_time', 
        async: false, dataType: 'text', 
        success: function(text) { 
            time = new Date(text); 
        }, error: function(http, message, exc) { 
            time = new Date(); 
    }}); 
    return time; 
}

