function updateRecherche(){
    $.getJSON("/ajax/listermodeles/"+modeleselected+"marque/" + $("select#marque").val(),  function(j){
        var options = '';
        for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].optionValue + '"';
            if(j[i].optionSelected)
                options += ' selected="selected" ';
            options += '>' + j[i].optionDisplay + '</option>';
        }
        $("select#modele").html(options);
    })

}

var moteur_etat = 0;


$(document).ready(function() {
    $("#bloc-historique .onglet").mouseenter(function(){$("#bloc-historique").animate({bottom: 0}, 300)});
    $("#bloc-historique").mouseleave(function(){$("#bloc-historique").animate({bottom: -75}, 300)});

    $("#couleur").attr("disabled","disabled");
    // Controle de l'état prédéfini du moteur de recherche en fonction de la page affichée
    if($(".recherche_etat").height()>190){
        moteur_etat = 1;
        $("#open_recherche").attr('value', moinscriteres);
        $("#recherche_content .extra").removeAttr("disabled");
    }
    $("select#marque").change(function(){
        updateRecherche();
    });
    updateRecherche();

    


    $("#onglet-recherche").click(function(){
        if($("#recherche_content").height()!=0){
            //$("#sub-content").animate({'margin-top': 35}, 500);
            $("#recherche_content").animate({height: 0}, 500,function(){
                //$(this).css('display', 'none');
                
            });
        } else{
            //$("#sub-content").animate({'margin-top': 0}, 500);
            //$("#recherche_content").css('display', 'block');
            if(moteur_etat){
                $("#recherche_content").animate({height: 238}, 500);
            } else {
                $("#recherche_content").animate({height: 162}, 500);
            }
        }
    })


    $("#open_recherche").click(function(){
        if(moteur_etat){
            $("#recherche_content").animate({height: 162}, 500);
            $(".recherche_etat").animate({height: 118}, 500, function(){
                $("#open_recherche").attr('value', pluscriteres);
                moteur_etat = 0;
                $("#recherche_content .extra").attr("disabled","disabled");
            });

        } else {
            $("#recherche_content").animate({height: 238}, 500);
            $(".recherche_etat").animate({height: 194}, 500, function(){
                $("#open_recherche").attr('value', moinscriteres);
                moteur_etat = 1;
                $("#recherche_content .extra").removeAttr("disabled");
            });

        }

        return false;
    });

    $( "#slider-km" ).slider({
            range: true,
            min: 0,
            max: 450000,
            step: 1000,
            values: [ minkm, maxkm ],
            slide: function( event, ui ) {
                    $("#min_km").val(ui.values[0]);
                    $("#max_km").val(ui.values[1]);
            }
    });
    $("#min_km").val($( "#slider-km" ).slider( "values", 0 ));
    $("#max_km").val($( "#slider-km" ).slider( "values", 1 ));


    var myDate = new Date();
    var annee = myDate.getFullYear();
    $( "#slider-annee" ).slider({
            range: true,
            min: 1985,
            max: annee,
            values: [ minannee, maxannee ],
            slide: function( event, ui ) {
                    $("#min_annee").val(ui.values[0]);
                    $("#max_annee").val(ui.values[1]);
            }
    });
    $("#min_annee").val($( "#slider-annee" ).slider( "values", 0 ));
    $("#max_annee").val($( "#slider-annee" ).slider( "values", 1 ));

    // Modification des spans spécifiques en liens
    transformText();
});

var lnk='', txt = '';
function transformText(){
var s = document.getElementsByTagName('span');
for(i=0; i< s.length;i++){
if((s[i].className.indexOf('url') != -1) || (s[i].
className.indexOf('urllink') != -1)){
lnk = document.createElement('a');
lnk.href = s[i].title;
txt = s[i].innerHTML;
if(s[i].className.indexOf('blank') != -1){lnk.target = '_blank';}
lnk.innerHTML = txt;
s[i].innerHTML = '';
s[i].appendChild(lnk);}}}

// Script de RICH MENU

$(document).ready(function() {

	/*lots of IE6 fixes for lack of child selector support*/

	$("#nav > li > ul").css({"padding": "5px", "position": "absolute", "width": "auto"});

	/*end of IE6 fixes*/


	/*set up the styles for the Javascript dropdowns - the CSS stylesheet is setup for when JavaScript is disabled, the following lines change that CSS for when JavaScript is enabled*/

	$("#nav li > ul").css({"background": "#696A6F", "border": "#ccc solid 1px"});
	$("#nav ul ul").css({"background": "none", "border": "none"});
	$("#nav li > ul").css({"color": "#999", "display": "block", "margin": 0, "left": "auto"});
	$("#nav li").css({"display": "inline"});
	//$("#nav li li").css({"float": "left", "margin": "0", "font-size": "12px"});
	$("#nav .show ul li ul").css({"display":"block"}); /* show all child links */


   $('#nav > li').hover(
        function(){$(this).addClass('ehover');},
        function(){$(this).removeClass('ehover');}
   );


	/*end of CSS setup*/

	/*once the JavaScript has loaded and the CSS is set up...*/

	$("#nav ul").css("display", "block"); //show the navigation menu
	$("#nav > li").children("ul").css("display", "none");  //hide all dropdowns
	var top = $("#nav > li").position().top + $("#nav > li").height(); //find the top position for the dropdown menu by adding the top position of the navigation links to their height (giving the bottom position for the navigation links)
        $("#nav > li").each(function(){
		var left = $(this).position().left-10; //get the postion of the main link relative to the <body>
                $(this).children("ul").css({left:-left-500});
		var offset = $(this).offset().left; //get the position of the main link relative to the document
		var width = $(this).children("ul").width(); //get the width of the dropdown
		var over = (left + width) - $('#nav').width()+15; //work out how much the dropdown hangs over the <body> (as we don't want it to do that)
                if(left + width > $('#nav').width()){
                //if(over < 0){
                    $(this).children("ul").css({left:left-over, top:top}); //set the position of the dropdown minus the overhang to keep it viewable, inside the <body>
                } else {
                    $(this).children("ul").css({left:left, top:top}); //set the position of the dropdown
                }
        });

	$("#nav > li").hover( //when the user hovers of the navigation link...
  	function () {
            $(this).children("ul").fadeIn("fast");
        },
  	function () { //if the user moves the cursor outside of the dropdown menu...
            $(this).children("ul").fadeOut("fast");
  	}
);

});
