
/*
$(function() {
    $.ajax({
        type: "GET",
        url: "data/molecules.xml",
        dataType: "xml",
        success: function(xml) {

            // Count elements */
            //var count = jXML.getCount(xml,{"Molecule":"molecule","Item":"item","Association":"asso:has(text)"});
            //alert( count["Molecule"] + ", " + count["Item"] + ", " + count["Association"] );

            // Return objects with a specific attribute
            //            checkmol = jXML.getAttribute(xml,{"Molecule":["molecule:eq('"+$('.unique').val()+"')", null]});
            //var attribute = jXML.getAttribute(xml,{"Molecule":["molecule.text:eq('"+$('.unique').val()+"')", null]});
            //var attribute = jXML.getAttribute(xml,{"Molecule":["molecule", null]});
            //alert( "test:"+ attribute["Molecule"][0]);
/*
            // Return objects with a specific attribute
            var attribute = jXML.getAttribute(xml,{"GUID":["guid","isPermaLink",null],"Channel":["channel","id",null]});
            // alert( attribute["GUID"] + ", " + attribute["Channel"] );

            // Return objects with a specific attribute value 
            var attribute = jXML.getAttribute(xml,{"GUID":["guid","isPermaLink","false"],"Channel":["channel","id","channel_1"]});
           // alert( attribute["GUID"] + ", " + attribute["Channel"] );

            // Return a number of objects randomized
            var random = jXML.getRandomObj(xml,{"Title":["item > title",null],"URL":["item > link",3]});
            for (i = 0; i < random["Title"].length; i++) {
                //alert( $(random["Title"][i]).text() );
            }
            for (i = 0; i < random["URL"].length; i++) {
                //alert( $(random["URL"][i]).text() );
            }
*/


/*
        }
    });
});

var jXML = {
    getCount: function(xml,nodes) {
        var response = {};
        for (var node in nodes) {
            response[node] = $(nodes[node],xml).length;
        }
        return response;
    },
    getAttribute: function(xml,nodes) {
        var response = {};
        for (var node in nodes) {
            if (nodes[node][2] == null) {
                response[node] = $(nodes[node][0] + "[" + nodes[node][1] + "]",xml);
            } else {
                if ($(nodes[node][0],xml).attr(nodes[node][1]) == nodes[node][2]) {
                    response[node] = $(nodes[node][0],xml);
                }
            }
        }
        return response;
    },
    getRandomObj: function(xml,nodes) {
        var response = {};
        for (var node in nodes) {
            var a = [];
            var b = [];
            $(nodes[node][0],xml).each(function(i) {
                b[i] = this;
            });
            var c = b.length;
            if (nodes[node][1] != null && nodes[node][1] < c) {
                c = nodes[node][1];
            }
            for (i = 0; i < c; i++) {
                var e = Math.floor(Math.random() * b.length);
                a[i] = b[e];
                b.splice(e,1);
            }
            response[node] = a;
        }
        return response;
    }
};
*/
// Autocompleter

function findValue(li) {
	if(li == null) return alert("Aucun résultat !");

	// if coming from an AJAX call, let's use the CityId as the value
	if(!!li.extra) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	$('#form_recherche').submit();
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0] + " (id: " + row[1] + ")";
}

$(document).ready(
  function() {
    mydata = $("#form_recherche #trouver").autocomplete(
      "data/molecules.php", 
      { 
			delay:400,
			minChars:2,
			//matchSubset:1,
			//matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			//onFindValue:findValue,
			//formatItem:formatItem,
			autoFill:false
    });
    $('.uniqueXXX').change(function() {
      //alert($('.unique').val());
    });
  }
);



// autres fonctions
var destruction = false;

function edit_fiche(id) {
  $('#adminform #fiche').attr('value',id);
  $('#adminform').submit();
}

function detruire_fiche(id) {
  if (destruction != true) {
    var div = 'm'+id;
    var html = '<div id="alert">';
      html += 'Confirmez vouloir détruire la fiche ci-dessus ? ';
      html += '<a href="#" onclick="detruire_oui('+id+')" class="oui">OUI</a> • ';
      html += '<a href="#" onclick="detruire_non(); return false" class="non">NON</a>';
      html += '</div>';
    $('#'+div).append(html);
    destruction = true;
  }
}

function detruire_non() {
  $('#alert').remove();
  destruction = false;
  
}  

function detruire_oui(id) {
  if(destruction == true) {
    $('#manager #detruire').val('true');
    $('#manager #id').val(id);
    $('#manager').submit();
  }
}



