$(document).ready(function(){

	// Clear cookie
	// $.cookie('itinerary', '', { expires: -1, path: '/' });

	// Fix up the fact that IE doesn't support indexOf
	if(!Array.indexOf){
		Array.prototype.indexOf = function(obj, start){
			for(var i=(start||0); i<this.length; i++){
				if(this[i]==obj){ return i; }
			}
		}
	}

	var destination_id = 0;
	var destination_pos = 0 ;
	var destinations = $.cookie('itinerary');
	destinations = (!destinations) ? new Array() : destinations.split(','); // Make sure destinations is an array

	$('a.itin-add').each(function() {
		destination_id = $(this).attr('title');
		destination_pos = destinations.indexOf(destination_id);
		$(this).children('img').attr('src', (destination_pos >= 0) ? '/fe_images/remove.gif' : '/fe_images/add.gif');
	});

 	// Add or remove destination
  	$('a.itin-add').click(function() {
  		var destinations = $.cookie('itinerary');
		destinations = (!destinations) ? new Array() : destinations.split(','); // Make sure destinations is an array
  		destination_id = $(this).attr('title');
  		destination_pos = destinations.indexOf(destination_id);
  		if(destination_pos >= 0) // If destination is alread present, remove it
  		{
  			destinations.splice(destinations.indexOf(destination_id), 1); // Delete the destination
			$(this).children('img').attr('src', '/fe_images/add.gif');
  		}
  		else // Add destination if not present
  		{
  			destinations.push(destination_id);
  			$(this).children('img').attr('src', '/fe_images/remove.gif');
  		}
  		$.cookie('itinerary', destinations.join(','), { expires: 1, path: '/' }); // Serialise and write cookie. Expires after 1 day.
  		return false;
  	});
  	
  	$('a#clear-itinerary').click(function() {
  		$.cookie('itinerary', '', { expires: -1, path: '/' }); // Clear the cookie
  		window.location.reload();
  		return false;
  	});
});