
var box_xpos;
var box_ypos;

var listAddBox;

var closeLock = false; //used to prevent closing box just after creating it

document.onclick = clickedSomewhere;

var myObject = new Object();
myObject.eventHandler = function(event) { 
	if(listAddBox) clickedSomewhere(Event.pointerX(event), Event.pointerY(event));
} 

document.onclick = myObject.eventHandler.bindAsEventListener(myObject);


function clickedSomewhere(mousex, mousey)
{
	var elem = listAddBox;
	var width = listAddBox.clientWidth;
	var height = listAddBox.clientHeight;
	
	var pos = findPos(listAddBox);
	var xpos = pos[0];
	var ypos = pos[1];
	
	if(mousex>xpos && mousex<(xpos+width) && mousey>ypos && mousey<(ypos+height))
	{
		//alert('box!');
	}
	else
	{
		closeBoxes();
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		
		return [curleft,curtop];
	}
}


function displayListSelection(obj)
{
	// remove already existing box if there is one
	if(listAddBox) closeBoxes();
	
	var pos = findPos(obj);
	var xpos = pos[0];
	var ypos = pos[1];
	
	listAddBox = document.createElement("div");
	listAddBox.setAttribute("style", "position:absolute; top:"+ (ypos) +"px; left:"+(xpos+20)+"px; background-color:#b79e9d; width:140px; border:1px solid #524043");
	listAddBox.innerHTML = 'Add to existing list:<br/><select id="addToListSelect" name="existingList"><option>asd</option></select><br/>Add to new list:<br/><input type="text" id="addToNewListInput" name="newList"/><br/><input type="submit" value="Add" id="addToListButton" onClick="submitAddWord()"/>';
	document.getElementById("main").appendChild(listAddBox);
	
	closeLock = true;
}




// -------- BOX FUNCTIONS -------------

function closeBoxes()
{
	var parent;
	
	if(closeLock)
	{
		closeLock = false;
		return;
	}
	
	closeLock--;
	parent = listAddBox.parentNode;
	parent.removeChild(listAddBox);
	listAddBox = undefined;
}



// NOTE: GET RID OF THESE AND SWITCH TO Prototype INSTEAD!
// -------- MOUSE CURSOR POSITON FUNCTIONS ------
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}

function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}



// ------- FORM SUBMISSION FUNCTIONS -------
function submitAddWord()
{
	var existingListValue = $('existingList');
	var newListValue = $('newList');
	new Ajax.Request('/action/addword.php',
	{
		method:'post',
		parameters: {existingList: 'asd', newList: 'qwe'},
		onSuccess: function(transport){
		var response = transport.responseText || "no response text";
		alert("Success! \n\n" + response);
	},
	onFailure: function(){ alert('Something went wrong...') }
	});
}