$(document).ready(function(){
	var idTag = "";
	$( "#mot_cle" ).autocomplete({
		source: "/default/alertes/searchtag",
		minLength: 2,
		open: function( event, ui ) {
			$(this).unbind("blur");
			idTag = "";
		},
		select: function( event, ui ) {
			idTag = ui.item.id;
			libTag = ui.item.value;
			$('#listeMC').append("<div class='motClef'>"+libTag+"<a href='javascript:void(0);' class='supprTagElmt'>x</a><input type='hidden' name='tag_id["+idTag+"]' value='"+libTag+"' /></div>");
			$(this).removeClass("autocompleteWarning");
		},
		close: function( event, ui ) {
			if(idTag != '')
				$('#mot_cle').val('');
		}
	});
	
	$('#okMC').click(function(){
		var value = $(this).prev().val();
		$("#listeMC").append("<div class='motClef'>"+value+"<a href='javascript:void(0);' class='supprTagElmt'>x</a><input type='hidden' name='motsclefs[]' value='"+value+"' /></div>");
		$(this).prev().val("");
	});
	
	$('.supprTagElmt').live('click',function(){
		$(this).parent().remove();
	});
	
	$('#selectThema').change(function(){
		var value = $(this).val();
		if(!$('#the_id_'+value).val() && value != 0){
			libelle = $(this).find("option[value="+value+"]").html();
			$("#listeThema").append("<div class='thema'>"+libelle+"<a href='javascript:void(0);' class='supprThemaElmt'>x</a><input type='hidden' name='the_id["+value+"]' id='the_id_"+value+"' value='"+libelle+"' /></div>");
		}
		$(this).val(0);
	});
	
	$('.supprThemaElmt').live('click',function(){
		$(this).parent().remove();
	});
	
	$("#btnSubmit-element").append($("#listeGeo1"));
	$("#btnRegister-element").append($("#listeGeo2"));
			
	$('#selectGeoMC').change(function(){
		var value = $(this).val();
		if(!$('.geo_id_'+value).val() && value != 0){
			libelle = $(this).find("option[value="+value+"]").html();
			$("#listeGeoMC").append("<div class='geo'>"+libelle+"<a href='javascript:void(0);' rel='mc' id='geo_id_"+value+"' class='supprGeoElmt'>x</a></div>");
			$("#listeGeo1").append("<input type='hidden' name='geoMC_id["+value+"]' class='geo_mc_id_"+value+"' value='"+libelle+"' />");
			$("#listeGeo2").append("<input type='hidden' name='geoMC_id["+value+"]' class='geo_mc_id_"+value+"' value='"+libelle+"' />");
		}
		$(this).val(0);
	});
	
	$('#selectGeoThema').change(function(){
		var value = $(this).val();
		if(!$('.geo_id_'+value).val() && value != 0){
			libelle = $(this).find("option[value="+value+"]").html();
			$("#listeGeoThema").append("<div class='geo'>"+libelle+"<a href='javascript:void(0);' rel='thema' id='geo_id_"+value+"' class='supprGeoElmt'>x</a></div>");
			$("#listeGeo1").append("<input type='hidden' name='geoThema_id["+value+"]' class='geo_thema_id_"+value+"' value='"+libelle+"' />");
			$("#listeGeo2").append("<input type='hidden' name='geoThema_id["+value+"]' class='geo_thema_id_"+value+"' value='"+libelle+"' />");
		}
		$(this).val(0);
	});
	
	$('.supprGeoElmt').live('click',function(){
		$(this).parent().remove();
		type = $(this).attr('rel');
		idGeo = $(this).attr('id');
		$('.geo_'+type+'_id_'+idGeo).remove();
	});
});
