$(document).ready(function() {
	initPlaceSelectionDragDrop();
	
	$.getJSON(FRONT_URL+"Ajax/updateroadtripplace.php?place=0", updateRoadTripPlacesList);
	
	$("#roadtripadd").droppable({
		accept: '.roadtripplaceselectionitemlink',
		drop: function(event, ui)
		{
			$.getJSON(FRONT_URL+"Ajax/updateroadtripplace.php?place="+ui.draggable[0].id, updateRoadTripPlacesList);
			$("#roadtripplacesselectionloading").show();
			$("#roadtripplaceslistloading").show();
			$(this).css("background-image", "url(img/roadtripadd.gif)");
		},
		over: function(event, ui) { $(this).css("background-image", "url(img/roadtripaddover.gif)"); },
		out: function(event, ui) { $(this).css("background-image", "url(img/roadtripadd.gif)"); }
    });

	$("#searchplace").keyup(function() {
		$("#roadtripplacesselectionloading").show();
		$.getJSON(FRONT_URL+"Ajax/searchplaces.php?keyword="+$(this).val(), updateRoadTripPlacesSelection);
	});
	
	$(".roadmapduration").each(function() {
		$(this).blur(function () {
			if($(this).val() != $(this).attr("default"))
				$.getJSON(FRONT_URL+"Ajax/updateroadtripduration.php?id="+$(this).attr("ref")+"&duration="+$(this).val());
			else
				$.getJSON(FRONT_URL+"Ajax/updateroadtripduration.php?id="+$(this).attr("ref")+"&duration=");
		});
    });
	
	$(".roadmapdetails").each(function() {
		$(this).blur(function () {
			if($(this).val() != $(this).attr("default"))
				$.getJSON(FRONT_URL+"Ajax/updateroadtripdetails.php?id="+$(this).attr("ref")+"&details="+$(this).val());
			else
				$.getJSON(FRONT_URL+"Ajax/updateroadtripdetails.php?id="+$(this).attr("ref")+"&details=");
		});
    });
});

function initPlaceSelectionDragDrop()
{
	$(".roadtripplaceselectionitemlink").each(function()
	{
		$(this).hover(function () {
			pos = $(this).offset();
			$("#dragselectionitemhelp").css("left", pos.left+20).css("top", pos.top+20).show();
		}, 
		function () {
			$("#dragselectionitemhelp").hide();
		});

		$(this).draggable({
			zIndex: 1000,
			helper: 'clone',
			appendTo: 'body',
			opacity: 0.8,
			scroll: false,
			revert: true,
			stop: function(event, ui) { refreshRoadTripPlacesSelection(); }
		});
	});
}

function initPlaceListDragDrop()
{
	$("#roadtripplaceslist").sortable(
	{
		items: 'div',
		update: function(event, ui)
		{
			$("#roadtripplaceslistloading").show();
			$.getJSON(FRONT_URL+"Ajax/updateroadtripplacesorder.php?"+$(this).sortable('serialize'), updateRoadTripPlacesList);
		}
	});
}

function refreshRoadTripPlacesSelection()
{
	$("#roadtripplacesselectionloading").show();
	if($('#searchplace').val() == $('#searchplace').attr("default"))
		keyword = "";
	else
		keyword = $('#searchplace').val();
	$.getJSON(FRONT_URL+"Ajax/searchplaces.php?keyword="+keyword, updateRoadTripPlacesSelection);
}

function eraseAll()
{
	$("#roadtripplaceslistloading").show();
	$.getJSON(FRONT_URL+"Ajax/updateroadtripplace.php?place=eraseall", updateRoadTripPlacesList);
	refreshRoadTripPlacesSelection();
}

function updateRoadTripPlacesSelection(placesJSON)
{
	placesListHTML = "";
	jQuery.each(placesJSON, function(i, item) {
		placesListHTML += "<div class='roadtripplaceselectionitem'><a id='"+item.PlaceID+"' href='"+FRONT_URL+"decouverte/"+item.CountryNameUrl+"/place/"+item.City+"-"+item.CityNameUrl+"/"+item.PlaceID+"-"+item.NameUrl+"' target='_blank' class='roadtripplaceselectionitemlink'>"+item.Name+"</a> "+item.CityName+", "+item.CountryName+"</div>\n";
	});
	if(placesListHTML == "")
		placesListHTML = "Aucun r&eacute;sultat";
	$("#roadtripplacesselection").html(placesListHTML);
	
	initPlaceSelectionDragDrop();
	$("#roadtripplacesselectionloading").hide();
	$("#roadtripplaceslistloading").hide();
}

function updateRoadTripPlacesList(placesJSON)
{
	placesListHTML = "";
	previousCountry = 0;
	previousCity = 0;
	jQuery.each(placesJSON, function(i, item) {
		if(previousCity != item.CountryName)
			placesListHTML += "<h1>"+item.CountryName+"</h1>";
		if(previousCountry != item.CityName)
			placesListHTML += "<h3>"+item.CityName+"</h3>";
		placesListHTML += "<div id='order_"+item.PlaceID+"' class='roadtripplacelistitem'><a href='"+FRONT_URL+"decouverte/"+item.CountryNameUrl+"/place/"+item.City+"-"+item.CityNameUrl+"/"+item.PlaceID+"-"+item.NameUrl+"' target='_blank'>"+item.Name+"</a><font class='small'><a href='javascript:removeItem("+item.PlaceID+")' class='deleteitem'>supprimer</a></font></div>\n";
		previousCity = item.CountryName;
		previousCountry = item.CityName;
	});
	if(placesListHTML == "")
		placesListHTML = "Aucun lieu ajout&eacute; &agrave; votre circuit";
	$("#roadtripplaceslist").html(placesListHTML);
	
	initPlaceListDragDrop();
	$("#roadtripplaceslistloading").hide();
	
	$("#roadtripplacenumber").html(placesJSON.length);
}

function removeItem(itemId)
{
	$("#roadtripplaceslistloading").show();
	$.getJSON(FRONT_URL+"Ajax/updateroadtripplace.php?place=-"+itemId, updateRoadTripPlacesList);
	refreshRoadTripPlacesSelection()
}

function getComments(roadTripOpinionId)
{
	$("#comments"+roadTripOpinionId).show();
	$.getJSON(FRONT_URL+"Ajax/getroadtripopinioncomments.php?roadtripopinion="+roadTripOpinionId, updateRoadTripOpinionComments);
}

function addComment(roadTripOpinionId)
{
	$("#sendcomment"+roadTripOpinionId).html("Envoi en cours...");
	$.getJSON("Ajax/addroadtripopinioncomment.php?roadtripopinion="+roadTripOpinionId, $("#addroadtripopinioncomment"+roadTripOpinionId).serialize(), updateRoadTripOpinionComments);
}

function updateRoadTripOpinionComments(commentsJSON)
{
	roadtripopinionid = commentsJSON[0].RoadTripOpinion;
	commentsListHTML = "";
	jQuery.each(commentsJSON, function(i, item) {
		commentsListHTML += "<div class='comment'><div class='commentdate'>"+item.CreationDate+"</div><b>"+item.FirstName+" "+item.LastName+"</b>&nbsp;&nbsp;&nbsp;"+item.Comment+"</div>";
	});
	$("#comments"+roadtripopinionid).html(commentsListHTML);
	$("#sendcomment"+roadtripopinionid).html("<a href='javascript:addComment("+roadtripopinionid+");'>Envoyer</a>");
	$("#commenttext"+roadtripopinionid).val("");
}
