var clip = null;
$(document).ready( function () 
{
  //Thumbs up and down - Did this coupon work for you?
  $('.thumbs a').click(function ()
  {
    $.get(HOST_PATH + '/coupons/'+ $(this).attr('href') + '&ajax=1', '', 
      function (data, textStatus)
      {
        if (data.score == -1)
          $('#coupon'+data.coupon_id).slideUp('slow');
        else
          $('#coupon'+data.coupon_id+' .thumbs').html('<b>WORKED!</b>');
      }, 'json');
    return false;
  });
  
  //Copy coupon code
  ZeroClipboard.setMoviePath( HOST_PATH + '/js/ZeroClipboard.swf' );
  clip = new ZeroClipboard.Client();
  clip.setHandCursor( true )
  clip.addEventListener( 'mouseOver', function(client) {
      $(client.domElement).parent().append($("<span class='coupon_help'>Click to copy code and open website</span>"));
      return false;
    } );
  clip.addEventListener( 'mouseOut', function(client) { 
    $(client.domElement).parent().find('.coupon_help').remove();
  } );
  
  clip.addEventListener( 'complete', function(client, text) {
    $(client.domElement).css('color', '#5D82BA');
    $(client.domElement).css('text-decoration', 'line-through');
    window.open($(client.domElement).attr('href'));
    clip.receiveEvent('mouseOut');
  } );
  $('.coupon_code a').mouseover(function()
  {
    var link = $(this);
    $(this).removeAttr('title');
    clip.setText($.trim(link.text()));
    if (clip.div) 
      clip.reposition(this);
    else 
      clip.glue(this);
  });
  
  
  //View comments link
  $('.coupon_comments_link a').click( function ()
  {
    var parent = $(this).parents('.coupon');
    var id = parent.attr('id');
    id = id.replace(/coupon/, '');
    
    //if no comments div
    var comments = parent.find('.coupon_comments_container');
    var comment_count = parseInt($(this).text().replace(/Comments \(/, '').replace(/\)/, ''));
    if ((comments.find('.coupon_comment').size() == 0) && (comment_count != 0))
    {
      //use ajax to get list of comments
      $.get($(this).attr('href')+'&ajax=1', '', function (data, status)
      {      
        //append comments to coupon
        comments.append(data);
        comments.slideDown('slow');
      }, 'html');
    }
    else //else show comment div
    {
      comments.slideDown('slow');
    }
    
    
    return false;
  });
  
  //Add comments link - show comment box
  $('.add_comment_link').click( function ()
  {
    var id = $(this).parents('.coupon').attr('id');
    id = id.replace(/coupon/, '');
    $(".comment_add input[name='coupon_id']").val(id);
    $(".comment_add form").attr('action', $(this).attr('href'));
    $('.comment_add').fadeIn('slow');
    $('.comment_add_bg').fadeIn('slow');
    return false;
  });
  
  //close add comment box
  $('.comment_add .close').click( function ()
  {
    $('.comment_add').fadeOut('slow');
    $('.comment_add_bg').fadeOut('slow');
    $('.comment_add .error').hide();
    return false;
  });
  
  $('.coupon_comments_container .close').click( function ()
  {
    $(this).parents('.coupon_comments_container').slideUp('slow');
    return false;
  });
  
  //Subscribe to Store Email Alerts
  $('a.subscribe_link').click(function()
  {
    var link = $(this).attr('href');
    $.alerts.select_email('How often do you want updates about this store?',
                          '',
                          'Subscription Setting',
                          function (r)
                          {
                            if (r != null)
                            {
                              $.get(link+'&ajax=1&deliver='+r);
                              jAlert("This store has been <b>added</b> to your <a href='"+HTTP_HOST+
                                      "/coupons/subscriptions.php'>Subscriptions</a>.",
                                     'Subscription Added');
                            }
                          });
    return false;
  });
  $('a.subscribe_link_login').click(function()
  {
    jAlert("Please <a href='"+HTTP_HOST+"/forums/register.php'>register</a> or <a href='"+HTTP_HOST+"/login.php'>login</a> to subscribe to this store.");
    return false;
  });
  
  //Favorite Store
  $('a.favorite_link').click(function()
  {
    $.get($(this).attr('href')+'&ajax=1');
    jAlert("This store has been <b>added</b> to your <a href='"+HTTP_HOST+
            "/coupons/favorites.php'>Favorite Stores</a>.",
           'Favorite Added');
    return false;
  });
  $('a.favorite_link_login').click(function()
  {
    jAlert("Please <a href='"+HTTP_HOST+"/forums/register.php'>register</a> or <a href='"+HTTP_HOST+"/login.php'>login</a> to favorite this store.");
    return false;
  });
  
  //SMS store
  $('a.sms_link').click(function()
  {
    $.get($(this).attr('href')+'&ajax=1');
    jAlert("This store has been <b>added</b> to your <a href='"+HTTP_HOST+
            "/coupons/textalerts.php'>SMS Text Alerts</a>." +
            "<Br/><br/><div class='small'>**Note: Please make sure you entered a valid phone number<br/> in <a href='"+HTTP_HOST+
            "/coupons/textalerts.php'>SMS Settings</a>, so you can receive these alerts.</div>", 
           'SMS Text Alert Added');
    return false;
  });
  $('a.sms_link_login').click(function()
  {
    jAlert("Please <a href='"+HTTP_HOST+"/forums/register.php'>register</a> or <a href='"+HTTP_HOST+"/login.php'>login</a> to subscribe to SMS text "+ 
           "alerts for this store.");
    return false;
  });
});
