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, 'mouseover', this.open.bind(this))
    Event.observe(this.marker, 'mouseout', this.close.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) {
    if (this.options.onOpen) this.options.onOpen.bind(this)(event);
  },

  close: function(event) {
    if (this.options.onClose) this.options.onClose.bind(this)(event);
  },

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

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

  onInitialize: function() {
    def_content = $('hotspot_content').innerHTML
  },

  /**
   * Show tooltip
   */
  onOpen: function(event) {
    Element.hide('hotspot_content')
    Element.hide('fix_content')

    $('hotspot_content').setStyle({
      borderColor: '#ddd',
      borderStyle: 'solid',
      borderWidth: '1px 2px 3px 1px',
      padding: '5px'
    });

    if (Prototype.Browser.IE) {
      $('fix_content').setStyle({
        left: -5000 + "px",
        top: -5000 + "px"
      })
    }

    $('hotspot_content').update(this.popUp.innerHTML)
    Effect.Appear('hotspot_content')
  },

  onClose:function(event) {
   $('hotspot_content').setStyle({
      display: 'none',
      border: 'none',
      padding: '0'
    });

    if (Prototype.Browser.IE) {
      $('hotspot_content').update('')

      iHeight = $('fix_content').getHeight()
      iLeft = $('pos_flag').offsetLeft
      iTop = $('pos_flag').offsetTop

      $('fix_content').setStyle({
        left: iLeft + "px",
        top: iTop + "px"
      })

      Effect.Appear('fix_content')
    } else {
      $('hotspot_content').update(def_content)
      Effect.Appear('hotspot_content')
    }
  }
});

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