if(!se)         var se              = {};
if(!se.hb)          se.hb           = {};
if(!se.hb.blr)      se.hb.blr       = {};
if(!se.hb.blr.bada) se.hb.blr.bada  = {};

se.hb.blr.bada.filters = {
  file_link : function () {
    return this.href.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/);
  },
  local_link : function () {
    //return true;
    return (this.hostname == location.host);
  },
  // Dependent on se.hb.blr.bada.i18n !
  unused_locale : function () {
    return ( ( $(this).attr("lang") ) && ( $(this).attr("lang") != se.hb.blr.bada.i18n.LOCALE ) );
  }
};

se.hb.blr.bada.statistics = {
  log_file_download : function (event) {
    // path must start with "/", pathname doesn't in all browsers
    var path = this.pathname.replace( /^([^\/].*)/, "/$1");
    //alert("Downloading\n" + path);
    if (typeof(urchinTracker) == "function") urchinTracker(path);
    //return false;
  }
};

se.hb.blr.bada.utils = {
  unwrap : function () {
    var element = $(this);
    element.replaceWith( element.html() );
  },
  load_and_replace : function () {
    var element = $(this);
    var wrapper = $('<div>').attr('id', this.id).attr('class', element.attr('class'));
    var callback = element.hasClass('wrapped') ? function () {} : se.hb.blr.bada.utils.unwrap;
    element.replaceWith(wrapper.load(this.href, '', callback));
  } 
};

se.hb.blr.bada.i18n = {
  setLocale : function (locale) {
    se.hb.blr.bada.i18n.LOCALE = locale;
  },
  fix_collections_and_communities : function () {
    var element = $(this);
    var pattern = /(.* )\/ ([^(:]*)/;
    var substitution = (se.hb.blr.bada.i18n.LOCALE == "sv") ? "$1" : "$2";
    element.text(element.text().replace(pattern, substitution));
  }
}

$(document).ready(function(){
  var BADA = se.hb.blr.bada;
  //BADA.i18n.setLocale("sv");
  
  if (BADA.i18n.LOCALE) {
  // Remove text repeated in multiple languages
    $(".dual-language").filter(BADA.filters.unused_locale).remove();
    
    // Fix localized collection names
    $("h1, .communityLink a, .locationBar a, td.standard a, .collectionListItem a, select#tlocation option").each(BADA.i18n.fix_collections_and_communities);
  };
  
  // Log file downloads with Google Analytics
  $("a[href]").filter(BADA.filters.file_link).filter(BADA.filters.local_link).click(BADA.statistics.log_file_download);
  
  // Autoload links with class dynamic into the page
  $("a[href].dynamic").filter(BADA.filters.local_link).hide().each(BADA.utils.load_and_replace).fadeIn("slow");
  
  // Replace 
  if ($("#rss2html").length) {
    var rss2html_url = "/cgi-bin/dspace/rss2html.pl?rss_url=http://bada.hb.se/feed/rss_2.0/2320/1511";
    if (BADA.i18n.LOCALE) { rss2html_url = rss2html_url + "&lang=" + BADA.i18n.LOCALE; }
    $("#rss2html").hide().load(rss2html_url).fadeIn("slow");
  }
});
