$(document).ready(function(){  
  $(".data_table th.arrows").live("click",function(){
    $(".data_table tbody").html('<tr><td colspan="8"><img src="/images/ajax-loader.gif" alt="loading" /></td></td>');
    orderby = "desc";
    var sortby = $(this).attr("class").replace(/deals_h_[1-9] arrows /,"");
    if($(this).find("div").attr("class").indexOf("up")!=-1)
    {
        orderby="asc";
        $(this).find("div").attr("class",$(this).find("div").attr("class").replace(/up/,"down"));  
    }
    else
    {
      $(this).find("div").attr("class",$(this).find("div").attr("class").replace(/down/,"up"));  
    }
    
    if(typeof filterCookie == "function")
    {
      filterCookie("sort="+sortby+'&order='+orderby+'&');
    }

     index = $(this).index()+1;
    str="";
    queryString=$.getQueryString().replace(/[&]*sort\=[a-z]*/,"");
    queryString=$.getQueryString().replace(/[&]*order\=[desc|asc]*/,"");
    queryString+="&order="+orderby+"&sort="+sortby;
   params="ajax="+ajax+'&offset=0&'+queryString;
 reloadDataTable(params,true);
    
    return false;
    });
  
  });
var reloadingDataTable=false;
function reloadDataTable(params,sort)
{
  if(reloadingDataTable) return;
  params+='&url='+window.location;
  reloadingDataTable=true;
    $.ajax({
                        url: "/ajax",
                        dataType: 'html',
                        data: params,
                        success: function(html)
                        {
                          $(".data_table").parent().html(html);
                            },
                            complete: function(){
                              if(typeof index !== 'undefined')
                          {
                            $(".data_table thead tr th div[class^=pointer]").remove();
                            $(".data_table thead tr th > div").attr("class",function(){ return $(this).attr("class").replace(/ deals_column_highlight/,'')});
                            if(orderby=="desc")
                            {
                            $(".data_table thead tr th:nth-child("+index+") > div").attr("class",function(){ return $(this).attr("class").replace(/down/,'up')+" deals_column_highlight"}).append('<div class="pointer"></div>');
                            }
                            else
                            {
                              $(".data_table thead tr th:nth-child("+index+") > div").attr("class",function(){ return $(this).attr("class").replace(/up/,'down')+" deals_column_highlight"}).append('<div class="pointer_up"></div>');
                            }
                            
                          }
                          if(typeof loadCookieData =='function')
                          {
                            loadCookieData();
                          }
                              reloadingDataTable=false;
                              },
                            error: function error(XMLHttpRequest, textStatus, errorThrown){
                              $(".data_table tbody").html(textStatus+" "+errorThrown);
                              reloadingDataTable=false;
                              }
                         });
}

