sections = [];
lastTopicID = 0;

function selectTopics(){
	window.open('Popups/SelectTopics.php', 'Select Topics', 'width=400,height=350,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
}
function CreateSections(){
	var sect = document.getElementsByClassName('section');
	var i = 0;
	sect.each(function(sect) {
		var sectionID = sect.id;
		sections[i] = sectionID;
		var ID = sectionID.substring(6);
		ID = parseInt(ID);
		if (ID > lastTopicID){
			lastTopicID = ID;
		}
		i = i+1;
	});
}

	function createNewSection(name) {
		var name = $F('sectionName');
		if (name != '') {
			CreateSections();
			var newDiv = Builder.node('div', {id: 'group_' + (lastTopicID + 1), className: 'section', style: 'display:none;' }, [
				Builder.node('p', {className: 'handle'}, [
					Builder.node('table', [
						Builder.node('tr', [Builder.node('td', name)]),
						Builder.node('tr', [Builder.node('td', {className:'bottomtable'}, [Builder.node('a', {href:'admin.php?action=menus'},'You will not be able to modify until you Refresh this page')])])
					])
				]), 
				Builder.node('ul', {id:'TopicSections_' + (lastTopicID +1)})
			]);
			lastTopicID = lastTopicID + 1;
			sections.push(newDiv.id);
			$('row_2').appendChild(newDiv);
			Effect.Appear(newDiv.id);
			destroyLineItemSortables();
			createLineItemSortables();
			createGroupSortable();
			
			window.open('Popups/SaveMenuItem.php?name=' + name + '&id=' + lastTopicID + '&row=2', 'Save', 'width=800,height=375,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');

		}
	}

	function createLineItemSortables() {
		Sortable.create('row_1',{tag:'div', dropOnEmpty: true, containment: ['row_1', 'row_2']});
		Sortable.create('row_2',{tag:'div', dropOnEmpty: true, containment: ['row_1', 'row_2']});
	}

	function destroyLineItemSortables() {
		for(var i = 0; i < sections.length; i++) {
			Sortable.destroy(sections[i]);
		}
	}

	function createGroupSortable() {
		Sortable.create('page',{tag:'div',only:'section',handle:'handle'});
	}

	/*
	Debug Functions for checking the group and item order
	*/
	function getGroupOrder() {
		var sections = document.getElementsByClassName('section');
		var alerttext = '';
		sections.each(function(section) {
			var sectionID = section.id;
			var order = Sortable.serialize(sectionID);
			alerttext += sectionID + ': ' + Sortable.sequence(section) + '\n';
		});
		alert(alerttext);
		return false;
	}

function saveMenuLayout(){
	var row1=Sortable.serialize('row_1');
	var row2=Sortable.serialize('row_2');
	
	var sect = document.getElementsByClassName('section');
	var i = 0;
	sectionOrder = "";
	sect.each(function(sect) {
		var sectionID = sect.id;
		var ids = sectionID.substring(6);
		sectionOrder += Sortable.serialize('TopicSections_' + ids);
		sectionOrder += ":";
		i = i+1;
	});
	row1 = row1.replace(/&/g, ":");
	row2 = row2.replace(/&/g, ":");
	sectionOrder = sectionOrder.replace(/&/g, ":");
	
	row1 = row1.replace(/row_1\[\]=/g, "");
	row2 = row2.replace(/row_2\[\]=/g, "");
	sectionOrder = sectionOrder.replace(/\[\]/g, "");

	window.open('Popups/SaveMenuLayout.php?row1=' + row1 + '&row2=' + row2 + '&topicsections=' + sectionOrder, 'Save', 'width=300,height=100,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
}	