var cenWgUI = {
	init: function () {
		cenWgUI.dataTable.init();
	},
	dataTable: {
		init: function () {
			if (cenWgUI.dataTable.init.initialized)
				return;

			// Load table information from database.
			var wgList = cenWgUtil.getWgList();

			var rows = [];

			for (var i = 0; i < wgList.length; i++) {
				if (wgList[i].isVisible()) {
					rows.push({
						title: wgList[i].getTitle(),
						wg: wgList[i]
					});
				}
			}

			// Initialize the data table.
			$('#wgListTable').dataTable({
				bJQueryUI: true,
				sScrollY: "300px",
				bPaginate: false,
		    	bScrollCollapse: true,
		    	bFilter: false,
		    	bSort: false,
		    	bInfo: false,
		    	bProcessing: true,
		    	aaData: rows,
				fnRowCallback: function (nRow, aData, index) {
					$(nRow).attr('ident', aData.wg.getIdentifier());

					if (aData.wg.isStartupConfig()) {
						$(nRow).addClass('row_selected');
					}
				},
				aoColumns: [
					{ "mData": "title" }
				]
			});

			cenWgUI.dataTable.makeSelectable();
			cenWgUI.dataTable.hideToolbar();
			cenWgUI.dataTable.resizeScrollBody();

			cenWgUI.dataTable.init.initialized = true;
		},
		addRow: function (wg) {
			if (!wg)
				throw "[cenWgUi.dataTable.init]: Argument of (wg) is required.";

			$('#wgListTable').dataTable().fnAddData({
				title: wg.getTitle(),
				wg: wg
			});

			$('.dataTables_scrollBody').css('height', '278px');
		},
		makeSelectable: function () {
			if (cenWgUI.dataTable.makeSelectable.initialized)
				return;

			// Add callback to rows for selecting window configs. 
			$('#wgListTable tbody tr').live('click', function () {
				$('#wgListTable tbody tr.row_selected').removeClass('row_selected');

				$(this).addClass('row_selected');
			});

			cenWgUI.dataTable.makeSelectable.initialized = true;
		},
		getSelected: function () {
			return $('#wgListTable tbody tr.row_selected');
		},
		getSelectedIdent: function () {
			return cenWgUI.dataTable.getSelected().attr('ident');
		},
		renameSelected: function (text) {
			$('#wgListTable tbody tr.row_selected td').html(text);
		},
		removeSelected: function () {
			var index = $('#wgListTable tbody tr.row_selected').prevAll().length;

			$('#wgListTable').dataTable().fnDeleteRow(index);

			$('.dataTables_scrollBody').css('height', '278px');
		},
		clearSelected: function () {
			cenWgUI.dataTable.getSelected().removeClass('row_selected');
		},
		hideToolbar: function () {
			$('#wgListTable_wrapper .fg-toolbar').hide();
		},
		resizeScrollBody: function () {
			$('.dataTables_scrollBody').css('height', '278px');
		}
	},
	loadWindowGroupList: function () {
		var table = $("#windowGroupSettingList");

		$(table).empty();

		var wg = cenWgUtil.createWg().loadConfig();

		var windows = wg.getWindows();

		for (var i = 0; i < windows.length; i++) {
			var w = windows[i];

			var tr = $("<tr></tr>");

			$(tr).append("<td>Window #{0}</td>".format(i + 1));
			$(tr).append("<td>Position: {0}x{1}</td>".format(w.getWinPosX(), w.getWinPosY()));
			$(tr).append("<td>Size: {0}x{1}</td>".format(w.getWinWidth(), w.getWinHeight()));

			$(table).append(tr);
		}
	},
	dupSelConfig: function () {
		var ident = cenWgUI.dataTable.getSelectedIdent();

		var wg = cenWgUtil.createWg();

		wg.loadConfig(ident);

		var title = 'Copy of ' + wg.getTitle();

		$('#modalRename input[id="modalRenameInput"]').val(title);

		$('#modalRename').dialog({
			buttons: {
				"Cancel": function () {
					$(this).dialog("close");
				},
				"Confirm": function () {
					var title = $('#modalRename input[id="modalRenameInput"]').val();

					wg.forkConfig();
					wg.setTitle(title);
					wg.saveConfig();

					cenWgUI.dataTable.clearSelected();
					cenWgUI.dataTable.addRow(wg);

					$(this).dialog("close");
				}
			}
		});

		$('#modalRename').dialog("open");
	},
	delSelConfig: function () {
		var confirmMessage = 'Are you sure you want to delete this window group?';

		var div = document.createElement('div');

		div.appendChild(document.createTextNode(confirmMessage));

		$(div).dialog({
			title: "Confirm Action",
			modal: true,
			buttons: {
				"No": function () {
					$(this).dialog("close");
				},
				"Yes": function () {
					var ident = cenWgUI.dataTable.getSelectedIdent();

					var success = cenWgUtil.deleteWgFile(ident);

					if (success) {
						cenWgUI.dataTable.removeSelected();
					}

					$(this).dialog("close");
				}
			}
		});
	},
	renameSelConfig: function () {
		var ident = cenWgUI.dataTable.getSelectedIdent();

		var wg = cenWgUtil.createWg().loadConfig(ident);

		var title = wg.getTitle();
		
		$('#modalRename input[id="modalRenameInput"]').val(title);

		$('#modalRename').dialog({
			buttons: {
				"Cancel": function () {
					$(this).dialog("close");
				},
				"Rename": function () {
					var newTitle = $('#modalRename input[id="modalRenameInput"]').val();

					wg.setTitle(newTitle);
					wg.saveConfig();

					cenWgUI.dataTable.renameSelected(newTitle);

					$(this).dialog("close");
				}
			}
		});

		$('#modalRename').dialog("open");
	},
	loadSelConfig: function () {
		var ident = cenWgUI.dataTable.getSelectedIdent();

		cenWgUtil.initializeWindows(ident);
	}
};
