
var currentid = 0;
function SwitchFocus(focusid)
{
	var foc_obj = document.getElementById(focusid);
	foc_obj.style.position = "absolute";
	foc_obj.style.left = 100; //(screen.width / 2) - (foc_obj.clientWidth / 2);
	foc_obj.style.top = 100; //(screen.height / 2) - (foc_obj.clientHeight / 2);
	//foc_obj.style.zindex = 5;
	foc_obj.focus();
}
function GetAppName(src)
{
	var app = src.substring(src.lastIndexOf('/')+1, src.length);
	return app;
}
function AddWindowRow()
{
	  var tbl = document.getElementById('bar');
	  var lastRow = tbl.rows.length;
	  // if there's no header row in the table, then iteration = lastRow + 1
	  //var iteration = lastRow + 1;
	  var row = tbl.insertRow(lastRow);

	  // left cell
	  var cellLeft = row.insertCell(0);
	  var link = document.createElement('a');
	  link.setAttribute('href', 'javascript:SwitchFocus(\'window'+ currentid + '\')');

	  var textNode = document.createTextNode('window'+currentid);
	  link.appendChild(textNode);
	  cellLeft.setAttribute('align','center');
	  cellLeft.appendChild(link);

}

function DeleteWindowRow(deleteid)
{
	var mytable     = document.getElementById('bar');
	//find the correct row to delete
	var i=1;
	for (i=1 ; i <= mytable.rows.length ; i++)
	{
		var myrow       = mytable.getElementsByTagName("tr")[i];
		var mycel       = myrow.getElementsByTagName("td")[0];

		// first item element of the childNodes list of mycel
		var myceltext=mycel.childNodes[0];

		if(myceltext.childNodes[0].data == deleteid)
		{
			myrow.parentNode.removeChild(myrow)
			i--;
		}
	}
}
function CreateWindow(src, w, h)
{
	//create object to attach in main cell
	var o = document.createElement('object'), p1 = document.createElement('param'), p2 = document.createElement('param'), p3 = document.createElement('param'), p4 = document.createElement('param'), e = document.createElement('embed');

	o.setAttribute('classid',	'CLSID:9DDFB297-9ED8-421d-B2AC-372A0F36E6C5');
	o.setAttribute('codebase',	'http://www.rebol.com/plugin/rebolb7.cab#Version=1,0,0,0');
	o.setAttribute('id',currentid);

	p1.setAttribute('bgcolor',	'#FFFFFF');
	p2.setAttribute('version',	'1.3.5');
	p3.setAttribute('args',	' ');
	p4.setAttribute('LaunchURL',src);

	e.setAttribute('LaunchURL',	src);
	e.setAttribute('width',	w);
	e.setAttribute('height',h);
	e.setAttribute('version','1.3.5');
	e.setAttribute('args',	' ');
	e.setAttribute('type',	'application/x-rebol-plugin-v1');
	e.setAttribute('bgcolor', 'transparent');

	o.appendChild(p1);
	o.appendChild(p2);
	o.appendChild(p3);
	o.appendChild(p4);
	o.appendChild(e);

	var maincell=CreateBorder(GetAppName(src), w, h);
	maincell.appendChild(o);

}

function CloseWindow(oldid)
{
	//oldid = 'window'+currentid;
	var t = document.getElementById(oldid);
	t.parentNode.removeChild(t);
	DeleteWindowRow(oldid);
	//currentid = currentid - 1;
}


function CreateBorder(title, w, h)
{
	currentid = currentid + 1;
	var tbl = document.createElement('table');
	tbl.setAttribute('id','window' + currentid);
	tbl.setAttribute('class','dragme');
	tbl.setAttribute('border','1');
	tbl.setAttribute('width',w);
	//title bar
	var row = tbl.insertRow(0);
	var topcell = row.insertCell(0);
	topcell.setAttribute('bgcolor','#CCCCCC');

	var win = currentid + ' - ' + title;
	var textNode = document.createTextNode(win);
	topcell.appendChild(textNode);

	var closecell = row.insertCell(1);
	closecell.setAttribute('bgcolor','#CCCCCC');
	closecell.setAttribute('width','10');
	closecell.setAttribute('align','center');
	closecell.setAttribute('valign','middle');

	var link = document.createElement('a');
	link.setAttribute('href', 'javascript:CloseWindow(\'window'+ currentid + '\')');
	link.setAttribute('style', 'text-decoration:none; color:#000000;');
	var closeNode = document.createTextNode('X');
	link.appendChild(closeNode);
	closecell.appendChild(link);
	
	
	//app window
	var row2 = tbl.insertRow(1);
	var maincell = row2.insertCell(0);
	maincell.setAttribute('class','defaultcursor');
	maincell.setAttribute('colspan','2');
	
	document.body.appendChild(tbl);
	AddWindowRow();
	
	return maincell;
}

function CreateAppWindow()
{
	var w = 160;
	var h = 200;

	//create object to attach in main cell
	//---------------------------------------------------
	var apptbl = document.createElement('table');
	apptbl.setAttribute('class','menu');
	apptbl.setAttribute('border','0');
	apptbl.setAttribute('width','100%');
	apptbl.setAttribute('height','100%');

	var approw0 = apptbl.insertRow(0);
	var approw0_cell0 = approw0.insertCell(0);
	approw0_cell0.appendChild(document.createTextNode('Name'));
	var approw0_cell1 = approw0.insertCell(1);
	var approw0_cell1_fld = document.createElement('input');
	approw0_cell1_fld.setAttribute('type','text');
	approw0_cell1_fld.setAttribute('OnClick','javascript:focus()');
	approw0_cell1_fld.setAttribute('id','newname');
	approw0_cell1_fld.setAttribute('size','10');
	approw0_cell1.appendChild(approw0_cell1_fld);


	var approw1 = apptbl.insertRow(1);
	var approw1_cell0 = approw1.insertCell(0);
	approw1_cell0.appendChild(document.createTextNode('URL'));
	var approw1_cell1 = approw1.insertCell(1);
	var approw1_cell1_fld = document.createElement('input');
	approw1_cell1_fld.setAttribute('type','text');
	approw1_cell1_fld.setAttribute('OnClick','javascript:focus()');
	approw1_cell1_fld.setAttribute('id','newscript');
	approw1_cell1_fld.setAttribute('size','10');
	approw1_cell1.appendChild(approw1_cell1_fld);

	var approw2 = apptbl.insertRow(2);
	var approw2_cell0 = approw2.insertCell(0);
	approw2_cell0.appendChild(document.createTextNode('Width'));
	var approw2_cell1 = approw2.insertCell(1);
	var approw2_cell1_fld = document.createElement('input');
	approw2_cell1_fld.setAttribute('type','text');
	approw2_cell1_fld.setAttribute('OnClick','javascript:focus()');
	approw2_cell1_fld.setAttribute('id','newwidth');
	approw2_cell1_fld.setAttribute('size','10');
	approw2_cell1.appendChild(approw2_cell1_fld);


	var approw3 = apptbl.insertRow(3);
	var approw3_cell0 = approw3.insertCell(0);
	approw3_cell0.appendChild(document.createTextNode('Height'));
	var approw3_cell1 = approw3.insertCell(1);
	var approw3_cell1_fld = document.createElement('input');
	approw3_cell1_fld.setAttribute('type','text');
	approw3_cell1_fld.setAttribute('OnClick','javascript:focus()');
	approw3_cell1_fld.setAttribute('id','newheight');
	approw3_cell1_fld.setAttribute('size','10');
	approw3_cell1.appendChild(approw3_cell1_fld);


	var approw4 = apptbl.insertRow(4);
	var approw4_cell0 = approw4.insertCell(0);
	approw4_cell0.appendChild(document.createTextNode(' '));
	var approw4_cell1 = approw4.insertCell(1);
	var approw4_cell1_fld = document.createElement('input');
	approw4_cell1_fld.setAttribute('type','button');
	approw4_cell1_fld.setAttribute('value','Add');
	approw4_cell1_fld.setAttribute('OnClick','javascript:AddApp(\'newname\', \'newscript\', \'newwidth\', \'newheight\')');
	approw4_cell1.appendChild(approw4_cell1_fld);

	//-----------------------------------------------

	var maincell=CreateBorder("Add Script", w, h);
	maincell.appendChild(apptbl);

}