Tooltip.Inherits(Shape);
function Tooltip(name){
	if(typeof(name) == "undefined"){
		name = "Tooltip";
	}
	
	this.Inherits(Shape, name);
	
	this.svgTooltip = null;
	this.vmlTooltip = null;
	this.svgDoc = null;
	
	this.strokeColor = "#000000";
	this.strokeSize = 2;
	
	this.titleBackgroundColor = "#000000";
	this.titleFontFace = "Verdana";
	this.titleFontSize = 11;
	this.titleFontColor = "#ffffff";
	this.titleBoxHeight = 25;
	
	this.bodyBackgroundColor = "#efefef";
	this.bodyFontFace = "Verdana, Tahoma";
	this.bodyFontSize = 10;
	this.bodyFontColor = "#454545";
	
	this.title = "";
	this.text = "";
	
	this.top = 100;
	this.left = 120;
	
	this.height = 70;
	this.width = 220;
	
	this.opacity = 0.5;
}

Tooltip.prototype.getWidth = function(text){
	if(getBrowser() == "Explorer") return (this.width + 10);
	return this.width;
}

Tooltip.prototype.getHeight = function(){
	if(getBrowser() == "Explorer") return this.height;
	return this.height;
}

Tooltip.prototype.getRoundX = function(){
	if(getBrowser() == "Explorer") return 0.15;
	return 10;
}

Tooltip.prototype.getRoundY = function(){
	if(getBrowser() == "Explorer") return 0.15;
	return 10;
}

Tooltip.prototype.create = function(svgDoc){
	switch(getBrowser()){
		case "Explorer":{
			this.vmlTooltip = this.createVML("tooltip");
			this.vmlTooltip.style.visibility = "hidden";
			break;
		}default:{
			this.svgDoc = svgDoc;
			this.svgTooltip = this.createSVG("tooltip");
			this.svgTooltip.style.visibility = "hidden";
			break;
		}
	}
	return this;
}

Tooltip.prototype.createSVG = function(id){
	this.width = this.getWidth();
	this.height = this.getHeight();
	
	var svgNS = 'http://www.w3.org/2000/svg'; 
	
	var tooltip = document.createElementNS(svgNS, "g");
	tooltip.setAttributeNS(null, "id", id);
	
	var tooltipGroup = document.createElementNS(svgNS, "g");
	tooltipGroup.setAttributeNS(null, "opacity", this.opacity);
	var clipPath = document.createElementNS(svgNS, "clipPath");
	clipPath.setAttributeNS(null, "id", "clipPath");
	
	var clipRect = document.createElementNS(svgNS, "rect");
	clipRect.setAttributeNS(null, "x", this.left);
	clipRect.setAttributeNS(null, "y", this.top);
	clipRect.setAttributeNS(null, "rx", this.getRoundX());
	clipRect.setAttributeNS(null, "ry", this.getRoundY());
	clipRect.setAttributeNS(null, "width", this.getWidth());
	clipRect.setAttributeNS(null, "height", parseInt(this.getHeight()));
	clipPath.appendChild(clipRect);
	
	tooltipGroup.appendChild(clipPath);
	
	var bodyGroup = document.createElementNS(svgNS, "g");
	var bodyRect = document.createElementNS(svgNS, "rect");
	bodyRect.setAttributeNS(null, "id", "bodyRect");
	bodyRect.setAttributeNS(null, "x", this.left);
	bodyRect.setAttributeNS(null, "y", this.top);
	bodyRect.setAttributeNS(null, "rx", this.getRoundX());
	bodyRect.setAttributeNS(null, "ry", this.getRoundY());
	bodyRect.setAttributeNS(null, "width", this.getWidth());
	bodyRect.setAttributeNS(null, "height", this.getHeight());
	bodyRect.setAttributeNS(null, "fill", this.bodyBackgroundColor);
	bodyRect.setAttributeNS(null, "stroke", this.strokeColor);
	bodyRect.setAttributeNS(null, "stroke-width", this.strokeSize);
	
	bodyGroup.appendChild(bodyRect);
	
	tooltipGroup.appendChild(bodyGroup);
	
	var titleGroup = document.createElementNS(svgNS, "g");
	titleGroup.setAttributeNS(null, "clip-path", "url(#clipPath)");
	
	var titleRect = document.createElementNS(svgNS, "rect");
	titleRect.setAttributeNS(null, "id", "titleRect");
	titleRect.setAttributeNS(null, "x", this.left);
	titleRect.setAttributeNS(null, "y", this.top);
	titleRect.setAttributeNS(null, "width", this.getWidth());
	titleRect.setAttributeNS(null, "height", this.titleBoxHeight);
	titleRect.setAttributeNS(null, "fill", this.titleBackgroundColor);

	titleGroup.appendChild(titleRect);
	tooltipGroup.appendChild(titleGroup);
	
	var titleText = document.createElementNS(svgNS, "text");
	titleText.setAttributeNS(null, "id", "titleText");
	titleText.setAttributeNS(null, "x", (this.left+ this.getRoundX()+ 2));
	titleText.setAttributeNS(null, "y", (this.top+ this.getRoundY() + 6));
	titleText.setAttributeNS(null, "fill", this.titleFontColor);
	titleText.setAttributeNS(null, "font-family", this.titleFontFace);
	titleText.setAttributeNS(null, "font-size", this.titleFontSize);
	titleText.setAttributeNS(null, "font-weight", "bold");
	titleText.appendChild(document.createTextNode(""));
	
	var bodyText = document.createElementNS(svgNS, "text");
	bodyText.setAttributeNS(null, "opacity", 1);
	bodyText.setAttributeNS(null, "id", "bodyText");
	bodyText.setAttributeNS(null, "x", (this.left+ this.getRoundX()+ 2));
	bodyText.setAttributeNS(null, "y", (this.top+ parseInt(titleRect.getAttribute("height")) + this.getRoundY() + 6));
	bodyText.setAttributeNS(null, "fill", this.bodyFontColor);
	bodyText.setAttributeNS(null, "font-family", this.bodyFontFace);
	bodyText.setAttributeNS(null, "font-size", this.bodyFontSize);
	bodyText.setAttributeNS(null, "font-weight", "bold");
	
	tooltip.appendChild(tooltipGroup);
	tooltip.appendChild(titleText);
	tooltip.appendChild(bodyText);
	return tooltip;
}

Tooltip.prototype.createVML = function(id){
	var tooltip = document.createElement("v:group");
	tooltip.setAttribute("id", id);
	tooltip.setAttribute("coordsize", this.getWidth() + ", " + this.getHeight());
	tooltip.style.position = "relative";
	tooltip.style.left = this.left;
	tooltip.style.top = this.top;
	tooltip.style.width = this.getWidth();
	tooltip.style.height = this.getHeight();
	
	var titleGroup = document.createElement("v:group");
	titleGroup.setAttribute("coordsize", this.getWidth() + ", " + this.titleBoxHeight);
	titleGroup.style.position = "relative";
	titleGroup.style.top = 0;
	titleGroup.style.left = 0;
	titleGroup.style.width = this.getWidth();
	titleGroup.style.height = this.titleBoxHeight;
	
	var titleRect = document.createElement("v:roundrect");
	titleRect.style.width = this.getWidth();
	titleRect.style.height = this.titleBoxHeight;
	titleRect.fillColor = this.titleBackgroundColor;
	titleRect.setAttribute("stroked", "false");
	titleRect.setAttribute("arcsize", this.getRoundX());
	
	var titleFill = document.createElement("v:fill");
	titleFill.setAttribute("opacity", this.opacity);
	titleRect.appendChild(titleFill);

	titleGroup.appendChild(titleRect);
	
	var bodyGroup = document.createElement("v:group");
	bodyGroup.setAttribute("id", "bodyGroup");
	bodyGroup.setAttribute("coordsize", this.getWidth() + ", " + this.getHeight());
	bodyGroup.style.position = "relative";
	bodyGroup.style.top = 0;
	bodyGroup.style.left = 0;
	bodyGroup.style.width = this.getWidth();
	bodyGroup.style.height = this.getHeight();
	
	var outerRect = document.createElement("v:roundrect");
	outerRect.setAttribute("id", "bodyRect");
	outerRect.style.width = this.getWidth();
	outerRect.style.height = this.getHeight();
	outerRect.fillColor = this.bodyBackgroundColor;
	outerRect.setAttribute("arcsize", this.getRoundX());
	
	var stroke = document.createElement("v:stroke");
	stroke.setAttribute("weight", this.strokeSize + "px");
	stroke.setAttribute("color", this.strokeColor);
	stroke.setAttribute("opacity", this.opacity);
	outerRect.appendChild(stroke);
	
	var fill = document.createElement("v:fill");
	fill.setAttribute("opacity", this.opacity);
	outerRect.appendChild(fill);
	
	bodyGroup.appendChild(outerRect);
	
	tooltip.appendChild(bodyGroup);
	tooltip.appendChild(titleGroup);
	
	var textRect = document.createElement("v:rect");
	textRect.setAttribute("stroked", false);
	textRect.style.position = "relative";
	textRect.style.width = this.getWidth() + "px";
	textRect.style.left =  (this.getRoundX() + 2) + "px";
	textRect.style.top = (this.getRoundY()) + "px";
	textRect.style.zIndex = 2;
	
	var textBox = document.createElement("v:textbox");
	textBox.setAttribute("id", "titleText");
	textBox.style.fontFamily = this.titleFontFace;
	textBox.style.fontWeight = "bold";
	textBox.style.fontSize = this.titleFontSize;
	textBox.style.color = this.titleFontColor;
	textRect.appendChild(textBox);
	
	tooltip.appendChild(textRect);
	
	var bodyRect = document.createElement("v:rect");
	bodyRect.setAttribute("stroked", false);
	bodyRect.style.position = "relative";
	bodyRect.style.width = this.getWidth() + "px";
	bodyRect.style.left =  (this.getRoundX() + 2) + "px";
	bodyRect.style.top = (parseInt(titleRect.style.height) + this.getRoundY()) + "px";
	bodyRect.style.zIndex = 2;
	
	var bodyTextBox = document.createElement("v:textbox");
	bodyTextBox.setAttribute("id", "bodyText");
	bodyTextBox.style.fontFamily = this.bodyFontFace;
	bodyTextBox.style.fontWeight = "bold";
	bodyTextBox.style.fontSize = this.bodyFontSize;
	bodyTextBox.style.color = this.bodyFontColor;
	bodyRect.appendChild(bodyTextBox);
	
	tooltip.appendChild(bodyRect);
	
	return tooltip;
}

Tooltip.prototype.setTitle = function(title){
	switch(getBrowser()){
		case "Explorer":{
			var titleText = this.vmlTooltip.getElementsByTagName("textbox");
			for(var i=0; i<titleText.length; i++){
				if(titleText[i].id == "titleText"){
					if(titleText[i].childNodes.length > 0) {
						titleText[i].replaceChild(document.createTextNode(title),titleText[i].childNodes[0]);
					}else{
						titleText[i].appendChild(document.createTextNode(title));
					}
				}
			}
			break;
		}default:{
			var svgNS = 'http://www.w3.org/2000/svg'; 
			if(this.svgDoc.getElementById("titleText").childNodes.length > 0){
				this.svgDoc.getElementById("titleText").replaceChild(this.svgDoc.createTextNode(title), this.svgDoc.getElementById("titleText").childNodes[0]);
			}else{
				this.svgDoc.getElementById("titleText").appendChild(this.svgDoc.createTextNode(title));
			}
			break;
		}
	}
	
}

Tooltip.prototype.setBodyText = function(arrText){
	if(typeof(arrText) != "object"){
		var temp = new Array();
		temp[0] = new Object();
		temp[0] = arrText;
		arrText = temp;
		
	}
	
	switch(getBrowser()){
		case "Explorer":{
			var bodyText = this.vmlTooltip.getElementsByTagName("textbox")[1];
			
			if(bodyText.childNodes.length == 0){
				for(var i=0; i<arrText.length; i++){
					bodyText.appendChild(document.createTextNode(arrText[i]));
					bodyText.appendChild(document.createElement("br"));
				}
			}else{
				while(bodyText.firstChild){
					bodyText.removeChild(bodyText.firstChild);
				}
				
				//alert(bodyText.childNodes.length);
				for(i=0; i<arrText.length; i++){
					bodyText.appendChild(document.createTextNode(arrText[i]));
					if((i+1) != arrText.length){
						bodyText.appendChild(document.createElement("br"));
					}
				}
			}
			break;
		}default:{
			var svgNS = 'http://www.w3.org/2000/svg'; 
			
			textNode = this.svgDoc.getElementById("bodyText");
			
			if(textNode.childNodes.length == 0){
				for(i=0; i<arrText.length; i++){
					var tspan = document.createElementNS(svgNS, "tspan");
					tspan.setAttributeNS(null, "x", (this.left+ this.getRoundX()+ 2));
					var dy = (i==0)? 0 : 1.5;
					tspan.setAttributeNS(null, "dy",  dy + "em");
					tspan.appendChild(this.svgDoc.createTextNode(arrText[i]));
					
					textNode.appendChild(tspan);
				}
			}else{
				while(textNode.firstChild){
					textNode.removeChild(textNode.firstChild);
				}
				
				for(var j=0; j<arrText.length; j++){
					tspan = document.createElementNS(svgNS, "tspan");
					tspan.setAttributeNS(null, "x", (this.left+ this.getRoundX()+ 2));
					dy = (j==0)? 0 : 1.5;
					tspan.setAttributeNS(null, "dy",  dy + "em");
					tspan.appendChild(this.svgDoc.createTextNode(arrText[j]));
					textNode.appendChild(tspan);
				}
			}
			break;
		}
	}

	this.resize();
}

Tooltip.prototype.show = function(){
	switch(getBrowser()){
		case "Explorer":{
			this.vmlTooltip.style.visibility = "visible";
			break;
		}default:{
			this.svgTooltip.style.visibility = "visible";
			break;
		}
	}
}

Tooltip.prototype.hide = function(){
	switch(getBrowser()){
		case "Explorer":{
			this.vmlTooltip.style.visibility = "hidden";
			break;
		}default:{
			this.svgTooltip.style.visibility = "hidden";
			break;
		}
	}
}

Tooltip.prototype.resize = function(){
	switch(getBrowser()){
		case "Explorer":{
			var bodyText = this.vmlTooltip.getElementsByTagName("textbox")[1];
			var newHeight = Math.ceil(bodyText.childNodes.length/2) * (this.bodyFontSize + 5) + (1.5 * this.titleBoxHeight) + (1.5 * this.getRoundY());
			
			var bodyGroup = this.vmlTooltip.getElementsByTagName("group")[0];
			
			var outerRect = document.createElement("v:roundrect");
			outerRect.setAttribute("id", "bodyRect");
			outerRect.style.width = this.getWidth();
			outerRect.style.height = newHeight + "px";
			outerRect.fillColor = this.bodyBackgroundColor;
			outerRect.setAttribute("arcsize", this.getRoundX());
			
			var stroke = document.createElement("v:stroke");
			stroke.setAttribute("weight", this.strokeSize + "px");
			stroke.setAttribute("color", this.strokeColor);
			stroke.setAttribute("opacity", this.opacity);
			outerRect.appendChild(stroke);
			
			var fill = document.createElement("v:fill");
			fill.setAttribute("opacity", this.opacity);
			outerRect.appendChild(fill);
			
			bodyGroup.replaceChild(outerRect, this.vmlTooltip.getElementsByTagName("roundrect")[0]);
			break;
		}default:{
			var titleRect = this.svgDoc.getElementById("titleRect");
			var bodyRect = this.svgDoc.getElementById("bodyRect");
			
			var titleBox = this.svgDoc.getElementById("titleText");
			var bodyBox = this.svgDoc.getElementById("bodyText");
			
			var newHeight = bodyBox.childNodes.length * (this.bodyFontSize + 5) + this.titleBoxHeight + (1.5 * this.getRoundY());
			bodyRect.setAttributeNS(null, "height", newHeight);
			break;
		}
	}
}

Tooltip.prototype.resetPosition = function(clientX, clientY){
	switch(getBrowser()){
		case "Explorer":{
			var mousePos = this.getVMLPosition(clientX, clientY);
			
			this.vmlTooltip.style.left = mousePos.x;
			this.vmlTooltip.style.top = mousePos.y;
			
			break;
		}default:{
			mousePos = this.getSVGPosition(clientX, clientY);
		
			this.svgTooltip.setAttributeNS(null, "transform", "translate(" + mousePos.x + "," + mousePos.y + ")");
			break;
		}
	}
}

Tooltip.prototype.getVMLPosition = function(clientX, clientY){
	var vmlDoc = document.getElementsByTagName("group")[0];
		
	var point = new Point(clientX, clientY);
	
	if(point.x < (parseInt(vmlDoc.style.width)/2)){
		point.x = point.x + (parseInt(vmlDoc.style.width)/2) - (this.getWidth());
	}else{
		point.x = point.x - (parseInt(vmlDoc.style.width)/2);
	}
	if(point.y < (parseInt(vmlDoc.style.height)/2)){
		point.y = point.y + this.getHeight() - parseInt(this.getHeight()/2);
	}else{
		point.y = point.y - (parseInt(vmlDoc.style.height)/2) + (this.getHeight());
	}
	
	return point;
}

Tooltip.prototype.getSVGPosition = function(clientX, clientY){
	var point = new Point(clientX, clientY);
	
	if(point.x < (parseInt(this.svgDoc.width)/2)){
		point.x = point.x - this.getWidth()/2;
	}else{
		point.x = point.x - (parseInt(this.svgDoc.width)/2) - this.getWidth()/2;
	}
	
	if(point.y < (parseInt(this.svgDoc.height)/2)){
		point.y = point.y - this.getHeight();
	}else{
		point.y = point.y - (parseInt(this.svgDoc.height)/2) - 20;
	}
	
	return point;
	
}

Tooltip.prototype.getUserCoordinate = function(clientX, clientY){
	var ctm = this.getTransformToElement(this.svgTooltip, this.svgDoc.rootElement);
	
	var iCtm = ctm.inverse();
	
	var trans = this.svgDoc.rootElement.currentTranslate;
	var scale = this.svgDoc.rootElement.currentScale;
	
	var p1 = this.svgDoc.rootElement.createSVGPoint();
	var p2;
	
	p1.x = (clientX - trans.x) / scale;
	p1.y = (clientY - trans.y) / scale;
	
	p2 = p1.matrixTransform(iCtm);
	
	return p1;
	
}

Tooltip.prototype.getTransformToElement = function(workElement, svgDocument){
	var ctm = workElement.getCTM();
	
	while((workElement = workElement.parentNode) != svgDocument){
		ctm = workElement.getCTM().multiply(ctm);
	}
	
	return ctm;
}