
// Mehrdimensionales Array
// erste Ebene mapid
// zweite Ebene ['imgs'] Array mit den Bilder.src
var maps = new Array();
var mapids = new Array();

var map_img_minwidth = 100;
var map_img_checks = 2;

var map_lupe_p = "/js/b_lupe_p.gif";
var map_lupe_m = "/js/b_lupe_m.gif";
var map_lupe_size_x = 20;
var map_lupe_size_y = 20;


function map_set_img_minwidth(mw) {
  map_img_minwidth = mw;
}

function map_set_img_checks(ic) {
  map_img_checks = ic;
}

function map_add_img(mapid, bildurl) {
  if (!maps[mapid]) {
    maps[mapid] = new Array();
    maps[mapid]['imgs'] = new Array();
    maps[mapid]['imgs_check_interval_handle'] = new Array();
    maps[mapid]['imgs_check_interval_count'] = new Array();
    maps[mapid]['imgs_check_interval_running'] = new Array();
    maps[mapid]['imgs_valid'] = new Array();
    maps[mapid]['current'] = 0;
  }
  var img_number = maps[mapid]['imgs'].length;
  maps[mapid]['imgs'][img_number] = new Image;
  maps[mapid]['imgs'][img_number].src = bildurl;
  maps[mapid]['imgs_check_interval_handle'][img_number] = 0;
  maps[mapid]['imgs_check_interval_count'][img_number] = 0;
  maps[mapid]['imgs_check_interval_running'][img_number] = true;
  maps[mapid]['imgs_valid'][img_number] = false;
}

function map_count_imgs(mapid) {
  var count = 0;
  var i = 0;
  for (i = 0; i < maps[mapid]['imgs'].length; i++) {
    if (maps[mapid]['imgs_valid'][i]) {
      count++;
    }
  }
  return count;
}


function map_getCoordinates(elementId) {
  var coordinates = new Array(4);
  var element = document.getElementById(elementId);
  coordinates['x1'] = 0;
  coordinates['y1'] = 0;
  while (element) {
    coordinates['x1'] += element.offsetLeft;
    coordinates['y1'] += element.offsetTop;
    element = element.offsetParent;
  }
  coordinates['x2'] = coordinates['x1'] + document.getElementById(elementId).offsetWidth;
  coordinates['y2'] = coordinates['y1'] + document.getElementById(elementId).offsetHeight;
  return coordinates;
}

function map_overlay_repos() {
  var i = 0;
  for (i = 0; i < mapids.length; i++) {
    mapid = mapids[i];
    if (map_count_imgs(mapid) > 1) {
      var coor = map_getCoordinates('map_' + mapid);
      with (document.getElementById('map_overlay_' + mapid)) {
        with (style) {
          top = coor['y1'] + 'px';
          left = (coor['x2'] - map_lupe_size_x) + 'px';
        }
      }
    }
  }
}

function map_show_overlay(mapid, imgid) {
  var coor = map_getCoordinates('map_' + mapid);
  if (map_count_imgs(mapid) > 1) {  // overlay wrid nur angezeigt, wenn es mehr als 1 Bild gibr angezeigt, wenn es mehr als 1 Bild gibtt
    if (maps[mapid]['imgs'][imgid]) {
      with (document.getElementById('map_overlay_' + mapid)) {
        with (style) {
          visibility = 'visible';
          top = coor['y1'] + 'px';
          left = (coor['x2'] - map_lupe_size_x) + 'px';
        }
        if (maps[mapid]['imgs'][imgid].src.indexOf('zoom') > 0) {
          src = map_lupe_m;
        } else {
          src = map_lupe_p;
        }
      }
    }
  }
}

function map_next(mapid) {
  if (maps[mapid]['current'] < (maps[mapid]['imgs'].length - 1)) {
    maps[mapid]['current']++;
  } else {
    maps[mapid]['current'] = 0;
  }
  var c = maps[mapid]['current'];
  if (maps[mapid]['imgs_valid'][c]) {
    document.getElementById('map_' + mapid).src = maps[mapid]['imgs'][c].src;
  } else {
    map_next(mapid);
  }
  map_show_overlay(mapid, c);
}

function map_click_handler(mapid) {
  map_next(mapid);
}

function map_check_img(mapid, imgid) {
  var src = maps[mapid]['imgs'][imgid].src.substr(maps[mapid]['imgs'][imgid].src.lastIndexOf('/') + 1, maps[mapid]['imgs'][imgid].src.length);
  maps[mapid]['imgs_check_interval_count'][imgid]++;
  if (maps[mapid]['imgs_check_interval_count'][imgid] > map_img_checks) { // Maximale Anzahl Checks ueberschritten -> Bild löschen
    window.clearInterval(maps[mapid]['imgs_check_interval_handle'][imgid]);
    maps[mapid]['imgs_check_interval_running'][imgid] = false;
  } else {
    if (maps[mapid]['imgs'][imgid].complete) { // Bild geladen
      window.clearInterval(maps[mapid]['imgs_check_interval_handle'][imgid]);
      maps[mapid]['imgs_check_interval_running'][imgid] = false;
      if (maps[mapid]['imgs'][imgid].width < map_img_minwidth) { // Bild nicht korreckt geladen
      } else {
        maps[mapid]['imgs_valid'][imgid] = true;
      }
    }
  }

  // zaehle laufende intervalle der map
  var interval_count = 0;
  var i = 0;
  for (i = 0; i < maps[mapid]['imgs'].length; i++) {
    if (maps[mapid]['imgs_check_interval_running'][i]) {
      interval_count++;
    }
  }

  // kein interval mehr da
  var valid_count = 0;
  if (interval_count == 0) {
    valid_count = map_count_imgs(mapid);
    if (valid_count == 0) {
      document.getElementById('map_' + mapid).style.display = 'none';
    }
  }
}

function map_check_imgs() {
  var mapid;
  var m;
  var imgid;
  for (m = 0; m < mapids.length; m++) {
    mapid = mapids[m];
    for (imgid = 0; imgid < maps[mapid]['imgs'].length; imgid++) {
      maps[mapid]['imgs_check_interval_handle'][imgid] = window.setInterval("map_check_img(" + mapid + ", " + imgid + ")", 500);
    }
  }
}

function map_find_first_valid_img(mapid) {
  var i = 0;
  for (i = 0; i < maps[mapid]['imgs'].length; i++) {
    if (maps[mapid]['imgs_valid'][i]) {
      return i;
    }
  }
  return -1;
}

function map_show_first(mapid, timeout) {
  var i = 0;
  if (map_count_imgs(mapid) > 0) {
    i = map_find_first_valid_img(mapid);
    if (i >= 0) {
      document.getElementById('map_' + mapid).src = maps[mapid]['imgs'][i].src;
      map_show_overlay(mapid, i);
    } else {
      alert("fehler " + mapid);
    }
  } else {
    window.setTimeout("map_show_first(" + mapid + ", " + timeout * 2 + ")", timeout * 2);
  }
}

function map_overlay_repos_timeout() {
  map_overlay_repos();
  window.setTimeout('map_overlay_repos_timeout()', 100);
}

function map_init() {
  var i = 0;

  map_check_imgs();

  for (i = 0; i < mapids.length; i++) {
    window.setTimeout("map_show_first(" + mapids[i] + ", 20)", 10);
  }
}

function map_start() {
  
  window.setTimeout("map_init()", 1000);
  
  var theEventHandler = document.createAttribute('onmouseover');
  theEventHandler.nodeValue = 'map_overlay_repos()';
  document.getElementsByTagName('body')[0].setAttributeNode(theEventHandler);

  var theEventHandler = document.createAttribute('onmouseout');
  theEventHandler.nodeValue = 'map_overlay_repos()';
  document.getElementsByTagName('body')[0].setAttributeNode(theEventHandler);

  // installiere einen Timeout, der die overlays neu positioniert
  window.setTimeout('map_overlay_repos_timeout()', 100); 
}

function map_new(mapid) {
  mapids.push(mapid);
  with (document) {
    write('<img onclick="map_click_handler(' + mapid + ')" style="position:absolute; visibility:hidden;" id="map_overlay_' + mapid + '" src="/bilder/spaces/space8.gif">');
    write('<img onclick="map_click_handler(' + mapid + ')" src="/bilder/spaces/space8.gif" id="map_' + mapid + '">');
  }
}

