Event.observe(window, "load", create_tooltips, false);
function create_tooltips() {
    if ($$(".hc")) {
        $$(".hc").each( function(element) {
          new Effect.Tooltip(element.id, '');
        });
    }
    if ($$(".hc")) {
        $$(".tb").each( function(element) {
          new Effect.Tooltip(element.id, '');
        });
    }
    if ($$(".cd")) {
        $$(".cd").each( function(element) {
          new Effect.Tooltip(element.id, '');
        });
    }
    if ($$(".autorbild")) {
        $$(".autorbild").each( function(element) {
          new Effect.Tooltip(element.id, '');
        });
    }
    if ($$(".bl")) {
        $$(".bl").each( function(element) {
          new Effect.Tooltip(element.id, '');
        });
    }
    if ($$(".blcd")) {
        $$(".blcd").each( function(element) {
          new Effect.Tooltip(element.id, '');
        });
    }
    if ($$(".katalog")) {
        $$(".katalog").each( function(element) {
          new Effect.Tooltip(element.id, '');
        });
    }
}

function nw(url, breite, hoehe) {
    var w = breite ? breite : 900;
    var h = hoehe ? hoehe : 697;
    var f = window.open(url, "neues_fenster", "width=" + w + ",height=" + h + ",top=20,left=90,resizable=yes,status=no,scrollbars=no,location=no,toolbar=no,menubar=no");
    f.focus();
}

// Effect.Tooltip
// First Version by Nick Stakenburg
// Extended Version by Roger Klein

Effect.Tooltip = Class.create();
  Object.extend(Object.extend(Effect.Tooltip.prototype, Effect.Base.prototype), {
  initialize: function(element, content) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      cwId: "cw" + element,
      content: content,
      alttxt: this.element.alt,
      title: false,
      classNameWrapper: 'tooltip_wrapper',
      classNameWrapperArrowRight: 'tooltip_wrapper_r_arrow',
      classNameContentWrapper: 'tooltip_content_wrapper',
      classNameContentWrapperAlignLeft: 'tooltip_content_wrapper_left',
      offset: {'x':11, 'y':6}
    }, arguments[2] || {});
    this.start(options);
  },
  setup: function() {
    // add observers
    this.element.observe('mousemove', this.showTip.bind(this));
    this.element.observe('mouseout', this.hideTip.bind(this));
  },
  buildTip: function() {
    // generate Content
    if (this.options.cwId.indexOf('autor', 0) > -1)
    {
      this.options.title = '<p>' + this.options.alttxt + "</p>";
    }
    else if (this.options.content == '') {
       content_splitter = this.options.alttxt.split("  |  ");
       this.options.title = content_splitter[0] != "" ? content_splitter[0] + "<br />" : "";
       if (content_splitter[1]) {
        this.options.title += "<i>" + content_splitter[1] + "</i>";
       }
       if (content_splitter[2]) {
        this.options.content =  content_splitter[2].replace(/ \| /g, "<br />");
       }
    }

    // create a wrapper
    this.wrapper = document.createElement('div');
    this.wrapper.className = this.options.classNameWrapper;
    Element.setStyle(this.wrapper, {
      position: 'absolute',
      display: 'none'
    });

    // add content_wrapper
    var content_wrapper = document.createElement('div');
    content_wrapper.className = this.options.classNameContentWrapper;
    content_wrapper.id = this.options.cwId;

    // add the title
    if(this.options.title) {
      var title = document.createElement('div');
      title.className = 'tooltip_title';
      Element.update(title, this.options.title);
      content_wrapper.appendChild(title);
    }

    if (this.options.content != '') {
        // create the actual tooltip
        this.tip = document.createElement('div');
        this.tip.className = 'tooltip_content';
        Element.update(this.tip, this.options.content);
        content_wrapper.appendChild(this.tip);
    }

    this.wrapper.appendChild(content_wrapper);

    // add wrapper to the body
    document.body.appendChild(this.wrapper);
  },
  showTip: function(event){
    if (!this.wrapper) this.buildTip();
    this.positionTip(event);
    this.wrapper.show();
    this.element.alt = "";
  },
  hideTip: function(){
    if (this.wrapper) {
        this.wrapper.hide();
        this.element.alt = this.options.alttxt;
    }
  },
  positionTip: function(event){
    var offsets = {'x': this.options.offset['x'],'y': this.options.offset['y']};
    var mouse = {'x': Event.pointerX(event), 'y': Event.pointerY(event)};
    var page = {'x':this.viewportSize()['x'], 'y':this.viewportSize()['y']};
    var tip = {'x': mouse['x'] + this.options.offset['x'] + this.wrapper.getWidth(),
    'y' : mouse['y'] + this.options.offset['y'] + this.wrapper.getHeight()};

    // inverse x or y to keep tooltip within viewport
    if(tip['x'] > page['x']) {
      offsets['x'] = 0 - (this.wrapper.getWidth() + this.options.offset['x']);
      if (!this.wrapper.hasClassName(this.options.classNameWrapperArrowRight)) {
        this.wrapper.className = this.options.classNameWrapperArrowRight;
      }
      if ($(this.options.cwId) && !$(this.options.cwId).hasClassName(this.options.classNameContentWrapperAlignLeft)) {
          $(this.options.cwId).className = this.options.classNameContentWrapperAlignLeft;
      }
    }
    else {
      if (!this.wrapper.hasClassName(this.options.classNameWrapper)) {
        this.wrapper.className = this.options.classNameWrapper;
      }
      if ($(this.options.cwId) && !$(this.options.cwId).hasClassName(this.options.classNameContentWrapper)) {
          $(this.options.cwId).className = this.options.classNameContentWrapper;
      }
    }
    // vertical swapping
    //if(tip['y']>page['y']) { offsets['y'] = 0-(this.wrapper.getHeight() + this.options.offset['y']); }

    this.wrapper.setStyle({
      left: mouse['x'] + offsets['x'] + 'px',
      top: mouse['y'] + offsets['y'] + 'px'
    });
  },
  viewportSize : function(){
    var x = self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
    var y = self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
    return {'x': x, 'y': y};
  }
});

