//GLOBALS
var magnet; 			// currently selected magnet
var offsetx; 			// distance from magnet's left edge to cursor's point
var offsety; 			// distance from magnet's top edge to cursor's point
var z = 2; 				// z-index counter, keeps active magnet on top.
var tronwidth; 			// width of the fridge, for bounds-checking.
var tronheight; 		// height of the fridge, for bounds-checking.
var bSetup = false;
var savefield;
var dzwidth = 400;
var dzheight = 90;

function dragmagnet(e, tron)
{
	if (magnet == null)
		return;

	if (bSetup == false)
		Setup(tron);

	var posx = 0;
	var posy = 0;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}

	if ((posx - offsetx - 1) >= 0 && // left-bounds checking
		(posy - offsety - 1) >= 0 &&  // top-bounds checking
		(posx - offsetx + magnet.offsetWidth + 3) <= tronwidth && // right-bounds checking
		(posy-offsety + magnet.offsetHeight + 3) <= tronheight    // bottom-bounds checking
       )
	{
		magnet.style.left = posx - offsetx;
		magnet.style.top = posy - offsety;
	}

	// fade if in dz
	if (parseFloat(magnet.style.top) > tronheight - dzheight &&
		parseFloat(magnet.style.left) + magnet.offsetWidth < dzwidth)
	{
		magnet.style.borderLeft = "1px solid #CCC";
		magnet.style.borderTop = "1px solid #CCC";
		magnet.style.borderBottom = "2px solid #CCC";
		magnet.style.borderRight = "2px solid #CCC";
		magnet.style.color = '#999';
	}
	else
	{
		magnet.style.borderLeft = "1px solid black";
		magnet.style.borderTop = "1px solid black";
		magnet.style.borderBottom = "2px solid black";
		magnet.style.borderRight = "2px solid black";
		magnet.style.color = 'blue';
	}
}

function Setup(tron)
{
	bSetup = true;
	tronwidth = tron.offsetWidth;
	tronheight = tron.offsetHeight;
}

function selectmagnet(e, thing)
{
	if (magnet==thing)
		return;

	magnet=thing;

	magnet.style.zIndex = z++;
	magnet.style.color = 'blue';

	var posx = 0;
	var posy = 0;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
	offsetx = posx - parseFloat(magnet.style.left);
	offsety = posy - parseFloat(magnet.style.top);
}

function deselectmagnet()
{
	if (magnet != null)
	{
		if (magnet.style.color == 'blue')
			magnet.style.color = 'black';
		else
			magnet.style.color = '#BBB';
		magnet=null;
		storePoemData();
	}
}

function storePoemData()
{
	var magnetList = getElementsByStyleClass("magnet");
	var cgidata = "";
	var m;

	for (var i = 0; i < magnetList.length; i++)
	{
		if (magnetList[i].className == "magnet")
		{
			m = magnetList[i];

			// check not in discardzone
			if (parseFloat(m.style.top) > tronheight - dzheight &&
				parseFloat(m.style.left) + m.offsetWidth < dzwidth)
				continue;

			cgidata += m.innerHTML + ","
					+ parseFloat(m.style.left) + ","
					+ parseFloat(m.style.top) + "\n";
		}
	}

	if (savefield == null)
		savefield = document.getElementById('save');

	if (savefield != null)
	{
		savefield.value = cgidata;
		document.forms[0].savebutton.disabled = false;
		document.forms[0].author.disabled = false;
		document.forms[0].title.disabled = false;
	}
}

function getElementsByStyleClass(className)
{
  var all = document.all ? document.all : document.getElementsByTagName('*');

  var elements = new Array();

  for (var e = 0; e < all.length; e++)
    if (all[e].className == className)
      elements[elements.length] = all[e];

  return elements;
}

function upsizetron()
{
	var magnetron = document.getElementById('magnetron');
	magnetron.style.height = magnetron.offsetHeight + 100;
	if (document.getElementById('h') != null)
	{
		document.getElementById('h').value = magnetron.offsetHeight
}
	Setup(magnetron);
}

//prevent text selection
function disabletext(e){return false;}
function reEnable(){return true;}
if (document.getElementById('magnetron') != null)
{
	document.getElementById('magnetron').onselectstart=new Function ("return false");

	if (window.sidebar)
	{
		document.getElementById('magnetron').onmousedown=disabletext;
		document.getElementById('magnetron').onclick=reEnable;
	}
}