
function serializeList(sortableListID){
	if(Sortable.sortables[sortableListID])	{
		//alert("sortableListID: "+sortableListID);
		var listString=Sortable.serialize(sortableListID,{tag:'li'}); //, name:"itemIdentifier"
		return listString;
	}else {
		Sortable.create(sortableListID,{tag:'li', constraint:false, overlap:'horizontal'} );
		var listString=Sortable.serialize(sortableListID,{tag:'li'}); //, name:"itemIdentifier"
		//alert("<"+listString);
		Sortable.destroy($(sortableListID));
		return listString;

	}
	//

	//
	//alert ("listString:"+listString);


}

function makeListSortable(id){
	//alert ("makesortable: "+id);
	Sortable.create(id,{tag:'li', constraint:false, overlap:'vertical', hoverclass: 'draggedItem'} );
	$(id).className="sortableList";
	//alert(Sortable.serialize(id) );
}
function makeListSortableHorizontal(id){
	//alert ("makesortable: "+id);
	Sortable.create(id,{tag:'li', constraint:false, overlap:'horizontal'} );
	//alert(Sortable.serialize(id) );
}

function makeListEditable(id){
	//alert(id);
	containmentIDs= new Array('inventoryList',id);
	Sortable.create(id,{constraint:false, overlap:'horizontal', ghosting: true, dropOnEmpty: true, onUpdate:redrawList, hoverclass: 'draggedItem', containment: containmentIDs});

	//alert ("Droppable");
}

function redrawList(element){
	Sortable.destroy(element);

	makeListEditable(element.readAttribute('id'));
	//alert('redraw: '+element.readAttribute('id'));
	//element.update(element.innerHTML +"<li>hui</li>");

}



function addToList(element, listID){
	//stele fest ob element bereits in der liste enthalten ist, anhand der id
	//alle listen elemente kriegen
	var listenElemente= $$('#'+listID +' li');

	listenElemente.each(function(item){
			alert("!"+item.readAttribute('id'));
			//new Draggable(item,{revert:true});
		});
	alert ("dr0pped: " +element.readAttribute('id')+"on listID: " +listID);
	$(listID).innerHTML = $(listID).innerHTML +"<li class='draggableItem' id='"+element.readAttribute('id')+"'>"+element.innerHTML+"</li>";
	//alert(serializeList(listID));
	containmentIDs= new Array('inventoryFileList');
	Sortable.create(listID,{tag:'li', constraint:false, overlap:'horizontal', ghosting: true, dropOnEmpty: true, hoverclass: 'draggedItem',containment: containmentIDs });
	//alert ( $(listID).readAttribute('id') );
}

function removeFromList(elementID , listID){
	//alert ("von liste "+listID+" entfernen:"+ elementID);
	/*
	var myListItems = $$('#'+listID+' #'+elementID);
	myListItems.each(function(item){
		//alert(item);
		item.remove();
	});
	*/
	//alert(elementID);
	removeRelationID=$(elementID).getAttribute('relationID');
	if(removeRelationID!=""){
		//alert("a "+removeRelationID );
		itemClass=$(listID).getAttribute('itemClass');
		//alert("b "+itemClass );
		projectID=$(listID).getAttribute('projectID');
		relationKeyword=$(listID).getAttribute('relationKeyword');


		//alert("b "+itemClass );
		//alert("c "+projectID );
		//alert("d "+relationKeyword );
		//per ajax das relationsobject aus der tabelle entfernen und inhalte neue laden
		showLoadingBackground($(listID));
		new Ajax.Updater($(listID+"_div"), '?Action=updateProjectItems&projectID='+projectID+'&itemClass='+itemClass+'&relationKeyword='+relationKeyword+'&removeRelation='+removeRelationID, {
					asynchronous:true
		});
		return true;

	}else{
		$(elementID).replace("");
	}

}



function saveList(listID){
	//alert(serializeList(listID));
	inventoryVisible=true;
	showHideInventory();
$(listID+"_saveOption").hide();
$(listID+"_trash").hide();
$(listID+"_editOption").show();
	//alert(Sortable.sequence($(listID)));
	projectID=$(listID).getAttribute('projectID');
	itemClass=$(listID).getAttribute('itemClass');
	relationKeyword=$(listID).getAttribute('relationKeyword');
	itemListString=serializeList(listID);

	if(itemListString==""){
		itemListString=listID + "[]=";
		alert(itemListString);
	}
	//ajax aufrufen Action:getProjectItemsAction
	showLoadingBackground($(listID+"_div"));
	new Ajax.Updater($(listID+"_div"), '?Action=updateProjectItems&projectID='+projectID+'&itemClass='+itemClass+'&relationKeyword='+relationKeyword+'&'+itemListString, {
				asynchronous:true
	});
	return true;

}


function sortableChanges(bla){

	alert(bla);
}
//alert ("see_able_Lists.js included");


