$(document).ready( function () 
{
  $('.thumbs a').click(function ()
  {
    $.get($(this).attr('href') + '&ajax=1', '', 
      function (data, textStatus)
      {
        if (data.result == -1) //user not logged in
        {
          jAlert("Please <a href='"+HOST_PATH+"/login.php'>login</a> "+
                 'or <a href="'+HOST_PATH+'/forums/register.php">join</a> CheapCheapCheap.com now!');
        }
        else if (data.result == 1) //action successful
        {
          $('#deal'+data.deal_id).addClass((data.score == -1) ? 'buried' : '');
          $('#deal'+data.deal_id+' .thumbs').html((data.score == -1) ? '<b>BURIED!</b>' : '<b>LIKED!</b>');
          if (data.score == 1)
          {
            var new_score = parseInt($('#deal'+data.deal_id+' .deal_score').html())+1;
            if (new_score == 1)
              var text = '1 Person Likes This';
            else
              var text = new_score + ' People Like This';
            $('#deal'+data.deal_id+' .deal_score').html(text);
          }
        }
        else //error
        {
          jAlert("Sorry, you have already liked or buried this deal.");
        }
      }, 'json');
    return false;
  });
  
  $('#toggle_expired').click(function()
  {
    if ($.trim($('#toggle_expired').html()) == 'Show Expired Deals')
    {
      $('.deal:has(h2:has(.expired))').slideDown('slow');
      $('#toggle_expired').html('Hide Expired Deals');
    }
    else //Hide expired deals
    {
      $('.deal:has(h2:has(.expired))').slideUp('slow');
      $('#toggle_expired').html('Show Expired Deals');
    }
    return false;
  });
  
  $('#toggle_desc').click(function()
  {
    if ($.trim($('#toggle_desc').html()) == 'Expand All Deals')
    {
      $('.deal_text').slideDown('slow');
      $('#toggle_desc').html('Collapse All Deals');
    }
    else //collapse
    {
      $('.deal_text').slideUp('slow');
      $('#toggle_desc').html('Expand All Deals');
    }
    return false;
  });
  
  $('#add_comment').click(function() 
  { 
    $('#comment_form').show(); 
    $('#add_comment').hide();
    return false;
  });
  
  $('#comment_form #close_button').click(function()
  {
    $('#comment_form').hide();
    $('#add_comment').show();
    $('#comment_form .error').hide();
    return false;
  });
  
  $('.comment_delete a').click(function()
  {
    var link = this;
    jConfirm('Are you sure you want to delete this?', 
           'Delete Confirmation', 
           function (r)
           {
             if (r)
             {
               var ids = $(link).attr('id');
               var divider = ids.search(/:/);
               var deal_id = ids.substring(0, divider);
               var post_id = ids.substring(divider+1);
               $.post(HOST_PATH + '/forums/editpost.php', { 'do': 'deletepost', 
                                                           p : post_id,
                                                           url : $('#deal'+deal_id + ' h2 a').attr('href'),
                                                           s : '',
                                                           deletepost : 'delete',
                                                           ajax : 1},
                    function (data, status)
                    {
                      $('#comment'+post_id).hide();
                    });
             }
           });
    return false;
  });
  
  $('#feedback_tab a').click(function()
  {
    if ($('#feedback_tab').hasClass('open')) {
        $('#feedback_tab')
        .animate({left:'-340px'}, 300)
        .removeClass('open');
    } else {
        $('#feedback_tab')
        .animate({left:'0'}, 300)
        .addClass('open');
    }
    return false;
  });
  $('#feedback_close').click(function()
  {
    $('#feedback_tab')
        .animate({left:'-340px'}, 300)
        .removeClass('open');
    return false;
  }); 

  $("input[name='q']").autocomplete(
    { 
      serviceUrl: HTTP_HOST + '/coupons/submit.php', 
      minChars: 2, 
      maxHeight: 400, 
      width: 405,
      params: { ajax : 1 }
    });
});
