﻿TTBoxGen = Class.create({
	initialize: function(boxid,content,styleNames,styleContents) {
		this.BoxId = boxid;
		this.createBox();
		this.setStyles(styleNames,styleContents);
		this.setContent(content);
	},
	createBox: function() {
		if ( !document.getElementById(this.BoxId) ) {
			this.element = document.createElement('div'); 
			this.element.id = this.BoxId;
			this.element.className = 'toolTippBox';
			with (this.element.style) {
				position = 'absolute';
				display = 'none';
			}
			this.element.innerHTML = '';
			document.body.appendChild(this.element); 
		} else {
			this.element = document.getElementById(this.BoxId);
		}
	},
	posBox: function(posx,posy) {
		with (this.element.style) {
			top = posy;
			left = posx;
		}
	},
	setStyles: function(styleNames,styleContents) {
		this.styleNames = styleNames;
		this.styleContents = styleContents;
	},
	setContent: function(content) {
		this.content = content;
	},
	addContent: function(content) {
		this.content += content;
	},
	show: function(dockElement,dockPos) {
		for ( var i = 0; i < this.styleNames.length; i++ ) {
			switch ( this.styleNames[i] ) {
				case 'position': this.element.style.position = this.styleContents[i]; break;
				case 'top': this.element.style.top = this.styleContents[i]; break;
				case 'right': this.element.style.right = this.styleContents[i]; break;
				case 'left': this.element.style.left = this.styleContents[i]; break;
				case 'bottom': this.element.style.bottom = this.styleContents[i]; break;
				case 'backgroundColor': this.element.style.backgroundColor = this.styleContents[i]; break;
				case 'border': this.element.style.border = this.styleContents[i]; break;
				case 'width': this.element.style.width = this.styleContents[i]; break;
			}
		}
		this.element.innerHTML = '<div class="toolTippBoxHeader"><a href="#" onclick="closeTTBox(\''+this.BoxId+'\');">schließen</a></div>' + this.content;
		this.dockOnElement(dockElement,dockPos);
		this.element.style.display = 'block';
	},
	close: function() {
		document.getElementById(this.BoxId).style.display = 'none';
	},
	visible: function() {
		document.getElementById(this.BoxId).style.display = 'block';
	},
	dockOnElement: function(elmt,elpos) {
		var pos = getPosition(elmt);
		this.element.style.top = pos.y+'px';
		switch (elpos) {
			case 'top-left': var width = this.element.style.width.replace(/px/g," ");  this.element.style.left = (pos.x-width)+'px'; break;
			default: this.element.style.left = pos.x+'px';
		}
	}
});

function closeTTBox(boxid) {
	document.getElementById(boxid).style.display = 'none';
}

function getPosition(element) {
	var elem=document.getElementById(element),tagname="",x=0,y=0;
	while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined")) {
		y+=elem.offsetTop;
		x+=elem.offsetLeft;
		tagname=elem.tagName.toUpperCase(); 
		if (tagname=="BODY") {
			elem=0;
		}
		if (typeof(elem)=="object") {
			if (typeof(elem.offsetParent)=="object") {
				elem=elem.offsetParent;
			}
		}
	}
	position=new Object();
	position.x=x;
	position.y=y;
	return position;
}

