var showedDetails = false;
var showedImages = false;
var showedTooltip = false;
var activeLocation = false;
var activeCategory = false;
var locationImage = false;
var lastZIndex = 4;

function ilm_locationAction(event, caller, locationId, cmd, callerSrc, callerActFile, callerWidth, callerHeight, normalSrc, normalWidth, normalHeight) {
  if (!activeLocation) {
    activeLocation = (activeLocationUid) ? document.getElementById('location'+activeLocationUid) : false;
  }
  var isie = (navigator.userAgent.indexOf('MSIE 6') != -1) ? true : false;
  //isie = false;
  if (callerSrc == '') callerSrc = caller.src;
  if (callerWidth == '') callerWidth = caller.width;
  if (callerHeight == '') callerHeight = caller.height;
  var regL = /(\d+)px/;
  var regArrL = caller.style.left.match(regL);
  var regT = /(\d+)px/;
  var regArrT = caller.style.top.match(regT);
  var callerLeft = regArrL[1];
  var callerTop = regArrT[1];
  var selector = document.getElementById('map-categories-selector');
  var input = document.getElementById('map-categories-input');
  switch (cmd) {
    case 'over': {
      if (isie) {
        selectorWidth = selector.clientWidth;
        selector.style.display = 'none';
        for (i = 0; i < selector.length; i++) {
          if (selector.options[i].selected == true) {
            input.value = selector.options[i].text;
          }
        }
        input.style.display = 'block';
        input.style.width = selectorWidth + 'px';
      }

      if (caller != activeLocation) {
        caller.src = callerSrc;
        var div = callerWidth / 2 - caller.width / 2;
        caller.style.left = callerLeft - div + 'px';
        caller.style.top = callerTop - div + 'px';

        caller.width = callerWidth;
        caller.height = callerHeight;
      }
      if (activeLocation) {
        activeLocation.style.zIndex = 3;
      }
      caller.style.zIndex = 10000001;
      tooltipAction('show', locationId, caller);
    break;
    }

    case 'click': {
      if (activeLocation && caller != activeLocation) {
        var ALregL = /(\d+)px/;
        var ALregArrL = activeLocation.style.left.match(ALregL);
        var ALregT = /(\d+)px/;
        var ALregArrT = activeLocation.style.top.match(ALregT);
        var ALLeft = ALregArrL[1];
        var ALTop = ALregArrT[1];
        activeLocation.src = normalSrc;
        activeLocation.width = normalWidth;
        activeLocation.height = normalHeight;
        activeLocation.style.zIndex = 3;
        //alert(caller.width);
        //alert(normalWidth);
        var div = caller.width - normalWidth;
        activeLocation.style.left = ALLeft - 0 + div/2 + 'px';
        activeLocation.style.top = ALTop - 0 + div/2 + 'px';
      }
      activeLocation = caller;
      lastZIndex += 1;
      activeLocation.style.zIndex = lastZIndex;
      revealDetails(locationId);
      revealImages(locationId);
    break;
    }

    case 'out': {
      if (isie) {
        selector.style.display = 'inline';
        input.style.display = 'none';
      }

      caller.src = callerActFile;
      if (caller != activeLocation) {
        caller.style.zIndex = 3;
        caller.src = callerSrc;
        var div = callerWidth / 2 - caller.width / 2;
        caller.style.left = callerLeft - div + 'px';
        caller.style.top = callerTop - div + 'px';
        caller.width = callerWidth;
        caller.height = callerHeight;
      }
      if (activeLocation) {
        activeLocation.style.zIndex = lastZIndex;
      }

      tooltipAction('hide', locationId, caller);
    break;
    }
  }
}

function revealDetails(locationId) {
  var details = document.getElementById('details-'+locationId);
  if (!showedDetails) showedDetails=document.getElementById('details-'+activeLocationUid);
  if (showedDetails) showedDetails.style.display = 'none';
  details.style.display = 'block';
  showedDetails = details;
}

function revealImages(locationId) {
  var images = document.getElementById('images-'+locationId);
  if (!showedImages) showedImages=document.getElementById('images-'+activeLocationUid);
  if (showedImages) showedImages.style.display = 'none';
  if (images) {
    images.style.display = 'block';
    showedImages = images;
  }
}

function tooltipAction(cmd, locationId, locationPoint) {
  switch (cmd) {
    case "show": {
      var tooltip = document.getElementById('tooltip-'+locationId);
      var regL = /(\d+)px/;
      var regArrL = locationPoint.style.left.match(regL);
      var regT = /(\d+)px/;
      var regArrT = locationPoint.style.top.match(regT);
      var locationPointLeft = regArrL[1];
      var locationPointTop = regArrT[1];
      tooltip.style.display = 'block';
      tooltip.style.left = locationPointLeft - 2 + 'px';
      if (navigator.userAgent.indexOf('IE') != -1) {
        tooltip.style.top = locationPointTop - 125 + 'px';
      } else {
        tooltip.style.top = locationPointTop - 125 + 'px';
      }
      showedTooltip = tooltip;
    break;
    }
    case "hide": {
      if (showedTooltip) {
        showedTooltip.style.display = 'none';
      }
    break;
    }
  }
}

function changeCategory(event, newCategory) {
  activeCategory = newCategory;
  var locationsContainer = document.getElementById('locations-map-map');
  var children = locationsContainer.childNodes
  var len = children.length;
  //Hiding all locations, details and image
  for (c=0; c<len; c++) {
    if(children[c].className == 'location') {
      children[c].style.visibility = 'hidden';
    }
  }

  //Reveal all locations if category not selected
  if (activeCategory == '') {
    for (c=0; c<len; c++) {
      if(children[c].className == 'location') {
        children[c].style.visibility = 'visible';
      }
    }
  }

  //Revealing locations that belongs to selected category
  if (locationsCategoriesArray[activeCategory]) {
    categoriesLocationsLen = locationsCategoriesArray[activeCategory].length;
    if (categoriesLocationsLen != 0) {
      for (c=0; c<categoriesLocationsLen; c++) {
        var locationId = 'location'+locationsCategoriesArray[activeCategory][c];
        var location = document.getElementById(locationId);
        location.style.visibility = 'visible';
      }
    }
  }
  //Hiding details and image if active location disappears after category switching
  if (showedDetails && activeLocation.style.visibility == 'hidden') showedDetails.style.display = 'none';
  if (showedImages && activeLocation.style.visibility == 'hidden') showedImages.style.display = 'none';
  //if (activeLocation.style.visibility == 'hidden' && locationImage) activeLocation.src = locationImage;
}

