//
// createSortables() takes an array of all our
// lists and creates sortables for them.
//
function createSortables() {
	Sortable.create("itinerary",{tag:'li',overlap:'verticle',onChange:updateItineraryOrder});
}


//
// updateItineraryOrder() updates the stack order of 
// a user's itinerary after a change has been made.
//
function updateItineraryOrder(obj) {

	var options = {
					method : 'post',
					parameters : Sortable.serialize("itinerary")+'&listid=itinerary',
					onSuccess: function(t) {
														
						},
						// Handle 404
						on404: function(t) {
							alert('404');
						},
						// Handle other errors
						onFailure: function(t) {
							alert("Unexpected error encountered.");
						}					
				  };
	 
	new Ajax.Request('assets/libraries/lib_xmlItineraryOrder.php', options); 
	
}



//
// updateItinerary()
// Adds a location to the user's itinerary session. //EDIT: Added in extraUrl to pass a bunch of junk into the view my itinerary
//
function updateItinerary(id, extraUrl) {
	addUrl = extraUrl; //Globalise the extraUrl 
	var update;
	update = document.getElementById('update');
	update.style.backgroundImage = "none";
	update.innerHTML = '<img src="images/icon-loading.gif" alt="Working..">';
	
	var options = {
					method : 'post',
					parameters : 'id='+id,
					onSuccess: function(t) {
							setTimeout( "finishItinerary()", 350 );							
						},
						// Handle 404
						on404: function(t) {
							alert('404');
						},
						// Handle other errors
						onFailure: function(t) {
							alert("Failed");
						}					
				  };
	 
	new Ajax.Request('assets/libraries/lib_xmlItinerary.php', options);
	return false;
}

function finishItinerary() {
	var update;
	update = document.getElementById('update');
	update.style.background = "none";
	update.innerHTML = '<img src="images/icon-check.gif" alt="Added!"> <a href="myitinerary.php?cat=1013&' + addUrl + '">View your itinerary</a><br /><img src="images/returnit.gif" alt="Continue Planning your Itinerary"><a href="browse.php?' + addUrl + '">Continue Planning</a>';	
}