window.tooltips = []

/**
 * Creating tooltip objects
 */
TooltipFactory = Class.create()
TooltipFactory.prototype = {
  initialize: function(selector, options) {
    this.selector = selector
    this.elements = []
    this.tooltips = []
    this.options = options||{}
  },

  activate: function(elements, options) {
    this.elements = $$(this.selector)
    Object.extend(this.options, options||{})
    for (var i=0; i<this.elements.length; i++){
      var tooltip = new Tooltip(this.elements[i], this)
      if (tooltip) {
        this.tooltips.push(tooltip)
      }
    }
  },

  activateOnLoad: function(options) {
    Event.observe(window, "load", this.activate.bind(this))
  }
}

/**
 * Class Tooltip (prototype)
 */
Tooltip = Class.create()
Tooltip.prototype = {
  /**
   * Initialize
   */
  initialize: function(marker, options) {
    this.marker = marker
    if (options instanceof TooltipFactory) {
      this.factory = options
      options = options.options
    }
    this.setOptions(options)
    if (this.options.getPopUp) {
      this.popUp = this.options.getPopUp.bind(this)(event)
    } else if (!this.popUp && this.options.popUp) {
      this.popUp = $(this.options.popUp)
    } else if (this.marker.href && this.marker.href.indexOf("#") != -1) {
      this.popUp = $(this.marker.href.split("#").pop())
    }
    if (!this.popUp && this.marker.id) this.popUp = $(this.marker.id + "Description")
    if (!this.popUp && this.marker.title) {
      this.popUp = document.createElement("div")
      document.body.appendChild(this.popUp)
      this.popUp.className = this.options.popUpClassName
      this.popUp.innerHTML = this.marker.title
      this.marker.removeAttribute("title")
    }
    if (!this.popUp) return

    window.tooltips.push(this)
    this.runningEffect = null

    /**
     * Events handler
     */
    Event.observe(this.marker, 'click', this.open.bind(this))
    if (this.options.onInitialize) this.options.onInitialize.bind(this)()
  },

  setOptions: function(options) {
    this.options = {
      queue: {
        position: "end",
        scope: "tooltip" + window.tooltips.length,
        limit: 1
      }
    };
    Object.extend(this.options, Tooltips.options)
    Object.extend(this.options, options || {})
    if (this.setInitialPosition) this.setInitialPosition = this.options.setInitialPosition.bind(this)
    if (this.getPosition) this.getPosition = this.options.getPosition.bind(this)
  },

  open: function(event) {
    sParentId = this.popUp.getAttribute('id')

    if (this.popUp.style.display == '') {
      // Close tooltip after second click
      HideTooltip(sParentId)
      return false;
    } else {
      // Hide all tooltips
      aTooltips = $$('.static_hotspot_tooltip')
      aTooltips.each(HideAllTooltips)
    }

    /**
     * Added close button
     */
    sIdCloseBtnName = 'id_close_btn_' + sParentId
    if (! Object.isElement($(sIdCloseBtnName))) {
      /*Add close image*/
      CloseBtn = document.createElement('div')
      CloseBtn.className = 'hotspot_close_btn'
      CloseBtn.setAttribute('id', sIdCloseBtnName)
      $(CloseBtn).update('<img title="close" style="border-style: none ! important; margin: 3px 3px 0px 0px; cursor: pointer;" onclick=HideTooltip("' + this.popUp.getAttribute('id') + '"); alt="close" src="fileadmin/img/schliessen.png" />')
      this.popUp.insertBefore(CloseBtn, this.popUp.firstChild)
      this.popUp.className = 'static_hotspot_tooltip';
      document.body.insertBefore(this.popUp, document.body.firstChild)
    }

    /**
     * Position calculation
     */
    var iMarkerX = Position.cumulativeOffset(this.marker)[0]
    var iMarkerY = Position.cumulativeOffset(this.marker)[1]
    var iMarkerWidth = this.marker.offsetWidth;
    var iMarkerHeight = this.marker.offsetHeight;

    ToolTipDem = Element.getDimensions(this.popUp)

    /* Default position */
    var iTooltTipX = (iMarkerX - ToolTipDem.width) + (iMarkerWidth / 4)
    var iTooltTipY = (iMarkerY + iMarkerHeight)
    if (iTooltTipX < 0) { iTooltTipX = 10 }
    this.popUp.style.left = iTooltTipX + "px"
    this.popUp.style.top = iTooltTipY + "px"

    $(sParentId).appear({
      duration: 0.5
    })
  },

  toggle: function(event) {
    if (Element.visible(this.popUp)) {
      this.open(event)
    }
  }
};

/**
 * Tooltips objects
 */
Tooltips = new TooltipFactory(".marker", {
  offsetLeft: 10,
  offsetTop: 10,
  effect: "appear",
  duration: 0.4,

  onInitialize: function() {
    // Get config for tooltip
    sConf = $('hotspot_config').innerHTML;
    aConf = sConf.evalJSON();

    if (aConf.width != '') this.popUp.style.width = aConf.width + "px"
    if (aConf.height != '') this.popUp.style.height = aConf.height + "px"

    Element.hide(this.popUp)
  }
});

/**
 * Hide All Tooltips
 */
function HideAllTooltips(element, index) {
  Id = element.getAttribute('id')
  if (sParentId != Id) {
    element.fade({
      duration: 0.5
    });
  }

  return false;
}

/**
 * Hide Tooltip
 */
function HideTooltip(sIdName) {
  $(sIdName).fade({
    duration: 0.5
  })
  return false;
}

/**
 * Activate tooltip functionality
 */
Tooltips.activateOnLoad()
