var current_hash = false;

function loadPage() {
    $('.loadIcon').show();
    current_hash = window.location.hash;
    query = current_hash.substr(1).replace(/\s/g,'%20');
    link = '/' + pagelink + '/get/?' + query;
    $('#materialsBlock').load(link, function() {
        $('.loadIcon').hide();
        window.scroll(0,0);
    });
}

$(function(){

setInterval(function() {
    if (window.location.hash != current_hash) {
        loadPage();
    }
}, 100);

if (window.location.hash) {
   current_hash = window.location.hash;
   hash = window.location.hash.substr(1).replace(/\s/g,'%20');
   $('#materialsBlock').load('/' + pagelink + '/get/?' + hash, function() {
     $('div.fromhashLoaderArea').hide();
     $('#materialsBlock').show();
   });
};

$('.pagination a').live('click', function() {
     hash = this.search.substr(1,this.search.length);
     window.location.hash = hash;
     return false;
});

$('a.srchType').click(function(e) {
      id = '#'+$(this).attr('id')+'Area';
      $('div.searchBlock').hide();
      $(id).show();
      $('a.srchType').removeClass('selected');
      $(this).addClass('selected');
      return false;
});

$('#searchForm').submit(function() {
   hash = $(this).serialize();
   window.location.hash = hash;
   return false;
});

$('#stylesForm').submit(function() {
   hash = unescape($(this).serialize());
   if (hash.length > 60) {
        $.gritter.add({title: 'Поиск слишком расплывчатый', text: 'Пожалуйста, укажите не более 5 стилей', image: '/img/icons/fail.gif'});
   } else {
        window.location.hash = hash;
   }
   return false;
});

$('a.sort').click(function(){
    type = $(this).attr('href').replace('#', '');
  if (window.location.hash) amp = '&'; else amp = '';
  window.location.hash = window.location.hash + amp + 'sortby=' + type;
  return false;
});

});
