var assignPopup = function(item){
    var target = item.attr("href");
    var id = target.replace(/#/,'');
    if(!id){ return; }
    var selector = 'h4 a'+target;
    var tmp = $(selector);
    if(tmp.size() > 0){
      var content = $('<div></div>');
      var heading = $('<h4></h4>').text(tmp.parent().text());
      content.append(heading);
      content.append(tmp.parent().next().clone(true));

      //var close = $('<a></a>').text('Close');
      //close.attr('href','#');
      //close.addClass('closeLink');
      //close.click( hideToolTips);
      //content.append(close);

      var callout = $('<div></div>');
      callout.addClass('callout');

      var top = $('<div></div>');
      top.addClass('top');

      content.addClass('mid');

      var bottom = $('<div></div>');
      bottom.addClass('bottom');

      var box = $('<div></div>');
      box.css('class','tooltip');

      callout.attr('id',id+'_callout');
      callout.hide();

      box.append(top);
      box.append(content);
      box.append(bottom);

      box.hover( function(){}, hideToolTips);

      box.addClass('popup_address');
      box.hide();
      box.attr('id',id+'_address');
      if($('#'+id+'_address').size() == 0){
          $('body').prepend(box);
          $('body').prepend(callout);
      }
    }
};

var hideToolTips = function(){
      $('.popup_address, .callout').each(function(){
        if($(this).css('display') != 'none'){
          $(this).fadeOut("normal");
        }
      });
      return false;
};

var hideOtherCallouts = function(id){
      $('.callout').each(function(){
        if($(this).css("display") != 'none' && ("#" + $(this).attr("id")) != id){
          $(this).hide();
        }
      });
};

var hideOtherAddresses = function(id){
      $('.popup_address').each(function(){
        if($(this).css('display') != 'none' && ("#" + $(this).attr('id')) != id){
          $(this).hide();
        }
      });
};


var showToolTip = function(event, item) {
      var target = item.attr("href");
      var selector = target+'_address';
      var callout = target+'_callout';
      $(callout).css('left', event.pageX - 30);
      $(callout).css('top', event.pageY + 10);
      $(selector).css('left', event.pageX - 30);
      $(selector).css('top', event.pageY + 32);
      $(callout).fadeIn("normal", function(){ hideOtherCallouts(callout); });
      $(selector).fadeIn("normal", function(){ hideOtherAddresses(selector); });
};

$(document).ready( function(){
  $('area[@shape=rect]').each( function(i) { assignPopup($(this)); } );
  $('area[@shape=rect]').hover( function(event){ showToolTip(event, $(this)); }, function(){ } );

  $('area[@shape=circle]').each( function(i) { assignPopup($(this)); } );
  $('area[@shape=circle]').hover( function(event){ showToolTip(event, $(this)); }, function(){ } );

  $('area[@shape=RECT]').each( function(i) { assignPopup($(this)); } );
  $('area[@shape=RECT]').hover( function(event){ showToolTip(event, $(this)); }, function(){ } );

  $('area[@shape=CIRCLE]').each( function(i) { assignPopup($(this)); } );
  $('area[@shape=CIRCLE]').hover( function(event){ showToolTip(event, $(this)); }, function(){ } );
});
