$(document).ready(function() {
  
  $(".obf").each(function(){  
    var self = $(this);
    var email = this.tagName.toLowerCase() == 'a' ? self.attr('href') : self.html();
    email = email.replace(/[a-zA-Z]/g, function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
    if(this.tagName.toLowerCase() == 'a'){
      if(self.html() == self.attr('href')){
        self.html(email);
      }
      self.attr('href','mailto:'+email);
    }else{
      self.html(email)
    }
  });
  
  
  // if($.browser.msie){
  //   $('#nav').children().hover(function(e){
  //     $(this).addClass('hover');
  //   }, function(e){
  //     $(this).removeClass('hover');
  //   })
  //   
  //  $('.rounded, #left-nav li').append('<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>');
  // }

  $('input[type="text"][title]').focus(function(e){
    $(this).filter('.empty').val('').removeClass('empty');
  }).blur(function(e){
    $(this).filter(function(){return this.value == ''}).val($(this).attr('title')).addClass('empty');
  }).blur();
 


  if (window.filters_map != undefined) {
    var $showing = $('#showing');
    var $filters = $('.filter');
    var $filtered = $('.filtered');
    
    $showing.data('default_title', $showing.text());
    $filters.each(function(e){
      var $this = $(this);
      $this.attr('title', $this.next().text());
    })
    
    if ($filters.length && $filtered.length){
  
      var filter_func = function(e){ 
        var checked = $filters.filter('input:checked')
        window.location.hash = checked.length ? 'filters:'+checked.map(function(){return this.name}).toArray().join(',') : '';
        $filtered.each(function(){
          /*
          var to_show = true;
          for(var i=0; i<checked.length; i++){
            if($.inArray(checked[i].name, window.filters_map[this.id]) < 0 ){
              to_show = false;
              break;
            }
          }
          */
          
          var to_show = checked.length ? false : true;
          for(var i=0; i<checked.length; i++){
            if($.inArray(checked[i].name, window.filters_map[this.id]) >= 0 ){
              to_show = true;
              break;
            }
          }
          
          if (to_show) {
            $(this).show()
          }else{
            $(this).hide()
          }
          
          if(checked.length){
            $showing.text(checked.map(function(){ return this.title }).get().join(', '));
          }else{
            $showing.text($showing.data('default_title'))
          }

        })
      }
      
      $filters.change(filter_func)
      
      $('.filters-reset').click(function(e){
        e.preventDefault();
        $filters.attr('checked', false);
        filter_func();
      })
      
      
      if (window.location.hash.indexOf('#filters:') == 0) {
        $.each(window.location.hash.replace('#filters:','').split(','), function(i, id){  
          $('#'+id).attr('checked','checked')
        })
        filter_func();
      };
      
    };
  };
  
});

