var ObjectCreator = new function()
{
    this.finishTable = function(type,name,standards,inp_table)
    {
		var temp_standards = standards;
		temp_standards.widgetId = name;
		var sorteyTable = dojo.widget.createWidget("SortableTable", temp_standards, inp_table);
		sorteyTable.tableType = type;
		sorteyTable.domNode.dojoObject = sorteyTable;
		return(sorteyTable);
    }

    this.setBackPointer = function(index,str)
    {
    	WM.A_windows[index].backPointer = str;
    }

    this.addSearchWindow = function(name,type,features)
    {
		var tempFeatures = features;
		tempFeatures.title = name;
		tempFeatures.searchIdentifier=type;
		tempFeatures.dbType=type;
		tempFeatures.windowType='search';
		var temp = container.appendChild(document.createElement('div'));
		var newWindow = dojo.widget.createWidget("FloatingPaneSearch", tempFeatures , temp);
		newWindow.finalize();
		newWindow.titleBarIcon.title = name;
		new dojo.dnd.HtmlDragSource(newWindow.titleBarIcon,"titlebaricons");
		dojo.event.connect(newWindow.domNode,"onmousedown",
			function(evt) 
			{
				dojo.event.topic.publish("/WindowClicks",evt);
			}
		);

		// Add window to the list and the appropriate index (or create index if need be).
		var index = WM.registerWindow(newWindow);
		newWindow.index = index;
		newWindow.type = type;

		newWindow.domNode.panePtr = newWindow;
		newWindow.containerNode.panePtr = newWindow;
		newWindow.windowType = type;

		WM.resizeWindow(index,200,200);
		var coordArray = WM.findInitialPlacement(175,20);
		WM.placeWindow(index,coordArray[0],coordArray[1]);
		// dojo.html.setOpacity(this.byIndex(index).domNode,0.1);
		WM.activateWindow(index);
		return index;
    }

    this.addRsltWindow = function(name,type,features)
    {
		var tempFeatures = features;
		tempFeatures.title = name;
		tempFeatures.windowType = 'rslt';
		tempFeatures.dbType = type;

		var temp = container.appendChild(document.createElement('div'));

		var newWindow = dojo.widget.createWidget("FloatingPaneTable", tempFeatures , temp);
		newWindow.titleBarIcon.title = name;

		new dojo.dnd.HtmlDragSource(newWindow.titleBarIcon,"titlebaricons");
		dojo.event.connect(newWindow.domNode,"onmousedown",
			function(evt) {
				dojo.event.topic.publish("/WindowClicks",evt);
			}
		);
	
		// Add window to the list and the appropriate index (or create index if need be).
		var index = WM.registerWindow(newWindow);
		newWindow.index = index;

		newWindow.domNode.panePtr = newWindow;
		newWindow.containerNode.panePtr = newWindow;

		WM.resizeWindow(index,200,200);
		var coordArray = WM.findInitialPlacement(175,20);
		WM.placeWindow(index,coordArray[0],coordArray[1]);
		// dojo.html.setOpacity(this.byIndex(index).domNode,0.1);
		WM.activateWindow(index);
		return index;
    }

    this.addWindow = function(name,type,features)
    {
		var tempFeatures = features;
		tempFeatures.title = name;
		var a = name.split("_");
		tempFeatures.windowType=a[0];
		tempFeatures.dbType=type;
		var temp = container.appendChild(document.createElement('div'));
		var newWindow = dojo.widget.createWidget("FloatingPane", tempFeatures , temp);
		newWindow.titleBarIcon.title = name;
		new dojo.dnd.HtmlDragSource(newWindow.titleBarIcon,"titlebaricons");
		dojo.event.connect(newWindow.domNode,"onmousedown",
			function(evt) {
				dojo.event.topic.publish("/WindowClicks",evt);
			}
		);

		// Add window to the list and the appropriate index (or create index if need be).
		var index= WM.registerWindow(newWindow);
		newWindow.index = index;
		newWindow.type = type;

		newWindow.domNode.panePtr = newWindow;
		newWindow.containerNode.panePtr = newWindow;
		// newWindow.windowType = type;

		WM.resizeWindow(index,200,200);
		var coordArray = WM.findInitialPlacement(175,20);
		WM.placeWindow(index,coordArray[0],coordArray[1]);
		// dojo.html.setOpacity(this.byIndex(index).domNode,0.1);
		WM.activateWindow(index);

		return index;
    }

    this.createDataTable = function(data,container)
    {
		var temp = container.appendChild(document.createElement('div'));
		var tablePtr = dojo.widget.createWidget('DataTable',{},temp);
		for(var i=0;i<data.length;i++)
		{
		    row = data[i];
		    if(row[0] == 'text')
		    { tablePtr.addTextRow(row[1],row[2]); }
		    else if(row[0] == 'clickable')
		    { tablePtr.addClickableRow(row[1],row[2]); }
		    else if(row[0] == 'link')
		    { tablePtr.addLinkRow(row[1],row[2],row[3]); }
		}
		return tablePtr;
    }

    this.createTable = function(attachPoint,in_array)
    {
		var temp = attachPoint.appendChild(document.createElement('table'));
		dojo.lang.isArray(attachPoint.tables) ? attachPoint.tables.push(temp) : attachPoint.tables = [temp];
		temp.cellPadding='0';
		temp.cellSpacing='0';
		var thead = temp.appendChild(document.createElement('thead'));
		var thr = thead.appendChild(document.createElement('tr'));
		var th = thr.appendChild(document.createElement('th'));
		th.setAttribute("field","Id");
		th.setAttribute("dataType","Number");
		th.innerHTML=in_array[0][0];

		for(var j=1;j<in_array[0].length;j++)
		{
		    var th = thr.appendChild(document.createElement('th'));
		    th.dataType='String';
		    th.innerHTML=in_array[0][j];	
		}

		var tbody = temp.appendChild(document.createElement('tbody'));
		for(var i = 1; i < in_array.length;i++)
		{
		    var row = tbody.appendChild(document.createElement('tr'));
		    var row_array = in_array[i];
		    if(row_array){
		    for(var j=0;j < row_array.length;j++)
		    {
			var data = row.appendChild(document.createElement('td'));
			data.innerHTML = row_array[j];
		    }}
		}
		return temp;
    }

    this.createTablePush = function(attachPoint,in_array,type,name)
    {
		var temp = attachPoint.appendChild(document.createElement('table'));
		dojo.lang.isArray(attachPoint.tables) ? attachPoint.tables.push(temp) : attachPoint.tables = [temp];
		temp.cellPadding='0';
		temp.cellSpacing='0';

		var thead = temp.appendChild(document.createElement('thead'));
		var thr = thead.appendChild(document.createElement('tr'));
		var th = thr.appendChild(document.createElement('th'));
		th.setAttribute("field","Id");
		th.setAttribute("dataType","Number");
		th.innerHTML=in_array[0][0];

		for(var j=1;j<in_array[0].length;j++)
		{
		    var th = thr.appendChild(document.createElement('th'));
		    th.dataType='String';
		    th.innerHTML=in_array[0][j];	
		}

		var tbody = temp.appendChild(document.createElement('tbody'));
		var sorteytable = this.finishTable(type,name,tablestandards,temp);

		for(var i = 1; i < in_array.length;i++)
		{
		    var row = document.createElement('tr');
		    var o={row:row}
		    var row_array = in_array[i];
		    row.setAttribute("value",row_array[0]);
			if(row_array)
			{
				for(var j=0;j < row_array.length;j++)
				{
					var data = row.appendChild(document.createElement('td'));
					data.innerHTML = row_array[j];
					var field = in_array[0][j];
					var value = row_array[j];
					o[field] = value;
					sorteytable.pushRow(o);
		    	}
		    }
		}
		return sorteytable;
    }
}
