﻿function bubble (){
    this.cache;
    this.box;
    this.x;
    this.y;
    this.offsetx;   // bubble offset
    this.offsety;   // bubble offset
    this.hostcontainer; // vars divven ska läggas till
    this.showHandle;
    this.closeHandle;
};

bubble.init = function (tag, id)
{
    bubble.offsetx = -34;
    bubble.offsety = 5;
    bubble.hostcontainer = "sidhuvud";
    
	bubble.showHandle = setTimeout("bubble.getdata('"+tag+"','"+id+"')", 300);
	document.onmousemove = bubble.updateMousePos;
};

/* Hämtar beskrivning från server */
bubble.getdata = function(tag, id)
{
	// kollar om arrayen är skapad
	if (bubble.cache == null)
		bubble.cache = new Array();
	
	// kollar om beskrivningen redan är hämtad från servern
	if (bubble.cache[id] != null)
	{
		bubble.create(bubble.cache[id]);	// isåfall tar vi den sparade
		return;
	}

	var url="default.aspx?id=" + id;
	
	var xmlHttp=null;
	if (window.XMLHttpRequest)
		xmlHttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		
	if (xmlHttp==null)
		return;
	
	xmlHttp.onreadystatechange=function (){
	    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	    {
	    	bubble.cache[id] = xmlHttp.responseText;
	    	if (bubble.box!=null)
		    	bubble.box.innerHTML = xmlHttp.responseText;
	    }
   	 }
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	xmlHttp.send(null);
	
	bubble.create("<div class=\"top\"></div><div class=\"content\"><div><strong>Laddar definition..</strong></div><div></div></div><div class=\"bottom\"><span></span></div>");
}
/* skapar element och infogar på sidan */
bubble.create = function(innerhtml)
{
		if (bubble.box!=null)
			return;

		var el = document.createElement("div");
		el.className = "bubble";
		el.innerHTML = innerhtml;
		
		el.style.left = (bubble.x + bubble.offsetx).toString() + "px";
		el.style.top = (bubble.y + bubble.offsety).toString() + "px";
			
		el.onmouseover = function() { bubble.show(); };
		el.onmouseout = function() { bubble.close(); };
		
		var sidhuvud = document.getElementById(bubble.hostcontainer);
		if (sidhuvud != null)
			sidhuvud.appendChild(el);
		else
			document.body.appendChild(el);

		bubble.box=el;
		return bubble.box;
};

/* onmouseover på beskrivningsrutan */
bubble.show = function()
{
	if (bubble.closeHandle!=null)
		clearTimeout(bubble.closeHandle);
};

/* onmouseout länk */
bubble.close = function (obj)
{
	if (bubble.showHandle!=null)
		clearTimeout(bubble.showHandle);
	bubble.closeHandle=setTimeout('bubble.kill()',200);
};

/* stänger beskrivningsrutan */
bubble.kill = function()
{
	if (bubble.box!=null)
	{
		document.onmousemove = null;
		var sidhuvud = document.getElementById(bubble.hostcontainer);
		if (sidhuvud != null)
			sidhuvud.removeChild(bubble.box);
		else
			document.body.removeChild(bubble.box);
	}
	bubble.box=null;
};
	
/* Muspositionen */
bubble.updateMousePos = function (e) 
{
	if (!e)	e = window.event;
	if (e.pageX || e.pageY) 	
	{
		bubble.x = e.pageX;
		bubble.y = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
		bubble.x = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		bubble.y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
};