jq(window).load(function(){
  if(jQuery('body').hasClass('template-atct_map_view')){
    // fix anchors
    var doc_href = document.location.href;
    var param_index = doc_href.lastIndexOf('#')
    jQuery('#map-view-container area').each(function(){
      var area = jQuery(this);
      var href = area.attr('href');
      if (param_index != -1)
        area.attr('href', doc_href.substring(0,param_index)+href);
      else 
        area.attr('href', doc_href+href);
    });
    jQuery('#map-view-container a').each(function(){
      var a = jQuery(this);
      var href = a.attr('href');
      if (href.indexOf('#') == 0) {
        if (param_index != -1)
          a.attr('href', doc_href.substring(0,param_index)+href);
        else 
          a.attr('href', doc_href+href);
      }
    });
    jQuery("#map-container AREA").mouseover(function(){
      var regionMap = '.'+jQuery(this).attr('id')+'-map';
      var regionList = '.'+jQuery(this).attr('id')+'-list';
      jQuery(regionMap).css('display', 'inline');
    }).mouseout(function(){
      var regionMap = '.'+jQuery(this).attr('id')+'-map';
      if (! jQuery(regionMap).hasClass('selected')) {
        jQuery(regionMap).css('display', 'none');
      }
    });
    jQuery("#map-container AREA").click(function(){
      // close all bubbles
      jQuery('.bubble').removeClass('selected').css('display', 'none');
      jQuery('#map-container img.region').removeClass('selected').css('display', 'none');
      var regionMap = '.'+jQuery(this).attr('id')+'-map';
      var regionBubble = '#'+jQuery(this).attr('id')+'-bubble';
      var regionTabs = '#'+jQuery(this).attr('id')+'-tabs';
      jQuery(regionMap).addClass('selected').css('display', 'inline');
      jQuery(regionBubble).addClass('selected').css('display', 'block');
      // jQuery(".scrollpane").jScrollPane({showArrows:true, scrollbarWidth: 7, arrowSize: 5});
      jQuery(regionBubble+' .scrollpane').each(function(){
        var scrollpane = jQuery(this);
        if(! scrollpane.hasClass('is_scrollpane')) {
          scrollpane.jScrollPane({showArrows:true, scrollbarWidth: 7, arrowSize: 5});
          scrollpane.addClass("is_scrollpane");
        }
      });
      if(! jQuery(regionTabs).hasClass('tabs')){
        jQuery(regionTabs).tabs();
        jQuery(regionTabs).addClass("tabs");
      }
    });
    jQuery(".bubble a.bubble_close").click(function(){
      var regionMap = '.'+jQuery(this).attr('id')+'map';
      var regionBubble = '#'+jQuery(this).attr('id')+'bubble';
      jQuery(regionMap).removeClass('selected').css('display', 'none');
      jQuery(regionBubble).removeClass('selected').css('display', 'none');
    });
    // init scroll and tabs on currently open bubble
    jQuery('.bubble.selected .scrollpane').each(function(){
      var scrollpane = jQuery(this);
      if(! scrollpane.hasClass('is_scrollpane')) {
        scrollpane.jScrollPane({showArrows:true, scrollbarWidth: 7, arrowSize: 5});
        scrollpane.addClass("is_scrollpane");
      }
    });
    var current_tab = jQuery('.bubble.selected .bubble-tabs');
    if(current_tab && !current_tab.hasClass('tabs')){
      current_tab.tabs();
      current_tab.addClass("tabs");
    }
    
  }
});
