/***
 * Creates new tooltip box
 */
function tooltip(boxId, boxWidth, boxHeight) {
	document.writeln('<div id="box' + boxId + '" name="box' + boxId + '"></div>');
		
	// Set box
	this.divbox	= GetLayer('box' + boxId);
	if(this.divbox == null) {
		alert('Box "'+boxId+'" not found!');
		return;
	}

	if(isEmpty(boxWidth)) {
		boxWidth = '';
	}
	if(isEmpty(boxHeight)) {
		boxHeight = '';
	}
	
	this.width	= boxWidth;
	this.height	= boxHeight;

	this.init();
};

// Set style
tooltip.prototype.init = function () {
	var current = this;

	var elems = document.getElementsByTagName("a");
	for(var i=0;i<elems.length;i++) {
	    if(elems[i].getAttribute("tooltip") && elems[i].getAttribute("tooltip") != '') {
			elems[i].onmouseover	= function(e) { current._showTooltip(e); };
			elems[i].onmouseout		= function(e) { current._hideTooltip(e); };
			elems[i].onmousemove	= function(e) { current._moveTooltip(e); };
		}
    }
};

// Set style
tooltip.prototype.setTooltipStyles = function (styles) {
	styles = styles.replace(/: /g, ":");

	var arrStyles = styles.split(";");
	for(var s=0;s<arrStyles.length;s++) {
		var style = arrStyles[s];
		var arrStyle = style.split(":");
		if(arrStyle[0] && arrStyle[1] && arrStyle[0] != '' && arrStyle[1] != '') {
			arrStyle[0] = arrStyle[0].replace(/-q/g, "Q");
			arrStyle[0] = arrStyle[0].replace(/-w/g, "W");
			arrStyle[0] = arrStyle[0].replace(/-e/g, "E");
			arrStyle[0] = arrStyle[0].replace(/-r/g, "R");
			arrStyle[0] = arrStyle[0].replace(/-t/g, "T");
			arrStyle[0] = arrStyle[0].replace(/-z/g, "Z");
			arrStyle[0] = arrStyle[0].replace(/-u/g, "U");
			arrStyle[0] = arrStyle[0].replace(/-i/g, "I");
			arrStyle[0] = arrStyle[0].replace(/-o/g, "O");
			arrStyle[0] = arrStyle[0].replace(/-p/g, "P");
			arrStyle[0] = arrStyle[0].replace(/-a/g, "A");
			arrStyle[0] = arrStyle[0].replace(/-s/g, "S");
			arrStyle[0] = arrStyle[0].replace(/-d/g, "D");
			arrStyle[0] = arrStyle[0].replace(/-f/g, "F");
			arrStyle[0] = arrStyle[0].replace(/-g/g, "G");
			arrStyle[0] = arrStyle[0].replace(/-h/g, "H");
			arrStyle[0] = arrStyle[0].replace(/-j/g, "J");
			arrStyle[0] = arrStyle[0].replace(/-k/g, "K");
			arrStyle[0] = arrStyle[0].replace(/-l/g, "L");
			arrStyle[0] = arrStyle[0].replace(/-y/g, "Y");
			arrStyle[0] = arrStyle[0].replace(/-x/g, "X");
			arrStyle[0] = arrStyle[0].replace(/-c/g, "C");
			arrStyle[0] = arrStyle[0].replace(/-v/g, "V");
			arrStyle[0] = arrStyle[0].replace(/-b/g, "B");
			arrStyle[0] = arrStyle[0].replace(/-n/g, "N");
			arrStyle[0] = arrStyle[0].replace(/-m/g, "M");
			SetStyle(this.divbox, arrStyle[0], arrStyle[1]);
		}
	}
};		

// Show tooltip
tooltip.prototype._showTooltip = function (e) {
	if(!e) e = window.event;

	var currentElement = e.target || e.srcElement || null;
	if(SwitchLayer && currentElement.getAttribute("tooltip") && currentElement.getAttribute("tooltip") != '') {

		if(this.width != '')
			SetStyle(this.divbox, 'width', parseInt(this.width) + 'px');
		if(this.height != '')
			SetStyle(this.divbox, 'height', parseInt(this.height) + 'px');
		
		SetContent(this.divbox, currentElement.getAttribute("tooltip"));
		SwitchLayer(this.divbox, 1);

		this._moveTooltip(e);
	}
}

// Hide tooltip
tooltip.prototype._hideTooltip = function (e) {
	if(!e) e = window.event;

	if(SwitchLayer) SwitchLayer(this.divbox, 0);
}

// Move tooltip
tooltip.prototype._moveTooltip = function (e) {
	if(!e) e = window.event;

	var posx=0, posy=0;
	if(e.pageX || e.pageY) {
	    posx = e.pageX;
	    posy = e.pageY;
    } else if(e.clientX || e.clientY) {
   	    posx = e.clientX;
       	posy = e.clientY;
    }
    
    posy += 18;
    posx += 7;

	var body = document.getElementsByTagName("body")[0];
	if(body) {
		var height = parseInt(GetHeight(this.divbox));
    	var width = parseInt(GetWidth(this.divbox));
	
		var bodyWidth = parseInt(GetWidth(body));
		var bodyHeight = parseInt(GetHeight(body));
		
		if(bodyHeight <= (posy + height)) {
			posy -= (height + 20);
		}
		if(bodyWidth <= (posx + width)) {
			posx -= (width + 4);
		}
	}
    
    if(SetStyle) {
	    SetStyle(this.divbox, 'top', posy + "px");
	    SetStyle(this.divbox, 'left', posx + "px");
	}
}
