var funblockcontainer = false;
var funblockcurrent = false;
function scanFunblocks()
{
	var areas = document.getElementsByTagName("textarea");
	for (x in areas)
	{
		if (areas[x].tagName == "TEXTAREA")
		{
			if(areas[x].getAttribute('funblocks'))
			{
				areas[x].onfocus = connectFunblocks;
				areas[x].disconnectFunblock = function () { disconnectFunblocks() };
			}
		}
	}
}

function connectFunblocks()
{
	if (!funblockcontainer)
	{
        funblockcontainer = document.createElement("div");
        funblockcontainer.id = "funblockcontainer";
        funblockcontainer.style.border = "1px solid black";
        funblockcontainer.style.marginLeft = "5px";
        funblockcontainer.style.padding = "5px";
        //funblockcontainer.style.position = "absolute";
        funblockcontainer.style.backgroundColor = "white";
        document.body.appendChild(funblockcontainer);
        Element.hide("funblockcontainer");
		new Ajax.Updater("funblockcontainer", "/php/getfunblocks.php");
	}
	dim = Element.getDimensions(this);
	funblockcontainer.style.width = dim.width - 11;
	this.parentNode.appendChild(funblockcontainer);
	
	/*
	dim = Element.getDimensions(this);
	pos = findPos(this);
	funblockcontainer.style.left = pos.left;
	funblockcontainer.style.top = pos.top + dim.height;
	
	*/
	Element.show("funblockcontainer");
	funblockcurrent = this;
	return false;
}

function disconnectFunblocks()
{
	Element.hide("funblockcontainer");
}

function findPos(target){
	x=0; y=0; var el,temp
	el = $(target);
	if(el.offsetParent){
	  temp = el
	  while(temp.offsetParent){ //Looping parent elements to get the offset of them as well
	    temp=temp.offsetParent; 
	    x+=temp.offsetLeft
	    y+=temp.offsetTop;
	  }
	}
	x+=el.offsetLeft
	y+=el.offsetTop
  	return {left: x, top: y}
}

function storeCaret (textEl) 
{
	if (textEl.createTextRange)
	{
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

function insertAtCaret (textEl, text) 
{
	var objEl = textEl;
	if (objEl.createTextRange && objEl.caretPos)
	{
		var caretPos = objEl.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	}
	else
	{
		objEl.value += text + " ";
	}
}


function insertFunblock(myText, textEl)
{
	if (!textEl) textEl = funblockcurrent;
	textEl.focus();
	storeCaret(textEl);
	insertAtCaret(textEl,myText);
	textEl.focus();
}

function toonKaart(netnummer) {
	popup = open("/php/zoek_netnummer.php?netnummer=" + netnummer, "kaart", width=373, height=420);
}

function winOpen(titel, plaatje, wijdte, hoogte){
    var win = window.open("", "win", "width="+wijdte+",height="+hoogte+",left=10,top=10");
    var doc = win.document;
    doc.open("text/html", "replace");
    doc.write("<HTML><HEAD>\n");
    doc.write("<TITLE>"+titel+"</TITLE>\n");
    doc.write("</HEAD><BODY leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 onblur='self.close();'>\n");
    doc.write("<a href='javascript:self.close();'><img src="+plaatje+" border=0></a>\n");
    doc.write("</BODY></HTML>\n");
    doc.close();
    win.focus();
}

