$(document).ready(function(){
  //Restore sortmodes:
  $('#sort').val($.cookie('sortmode'));
  $('#order').val($.cookie('ordermode'));
  
  //Re-set product count:
  $('#crCounter').html($('#productcount').val());
});

function goToPage(id, url) {
  window.location = url + $("#" + id + " option:selected").val() + ".aspx";
}

function ChangeSortMode() 
{
  /*Resort, get result by ajax*/
  var sXHTMLSource='';
  var iCpage = 0;
  var sPageParam = '';
  var sSortMode = $('#sort').val();
  var sOrderMode = $('#order').val();
  
  //Store initial values:
  $.cookie('sortmode', sSortMode.toString(), {path: '/'});
  $.cookie('ordermode', sOrderMode.toString(), {path: '/'});
  
  iCpage = $('#currentpage').val();
  
  //Detemine page param:
  if(iCpage != 1)
  {
    sPageParam = '/p' + iCpage;
  }
  
  //Determine source address:
  sXHTMLSource = $('#xhtmlsource').val();
  sXHTMLSource = sXHTMLSource.replace('&', '_');
  sXHTMLSource = sXHTMLSource + sPageParam + '.aspx?forajax=true&page=' + iCpage + '&sort=' + sSortMode + '&order=' + sOrderMode;
  
  //Disables sorting:
  $('#sort').attr({disabled: 'true'});
  $('#order').attr({disabled: 'true'});

  //Do ajax request:  
  $.ajax({
    url: sXHTMLSource,
    cache: false,
    success: function(data){
      refreshProducts(data);
    }
  });
}

function refreshProducts(data)
{
  var iCpage = $('#currentpage').val();
  var sXHTMLSource = "";
  
  //Determine XHTML source:
  sXHTMLSource = $('#xhtmlsource').val();
  sXHTMLSource = sXHTMLSource.replace('&', '_');

  //Set products:
  $('#content_locked').html(data);
  
  //Set current page:
  $('.pages > *').removeAttr('class');
  $($('.pages > *')[(iCpage - 1)]).attr({'class': 'currentpage'});
  
  //Fix footer when running on IE:
  if(document.all)
  {
    FixFooter();
  }
  
  //Call this function to restore the filters: (Whole page will be refreshed)
  filters(sXHTMLSource + '.aspx');
}