$(document).ready(function() {
    // Formulaire de recherche d'artiste
    if( $('#artistForm').attr('id') ){
        $('#artistForm #autocomplete').autocomplete('/artist/artistlist',{
                matchContains: true,
				formatItem: function(row){return row[0]+' '+row[1];},
				formatResult: function(row){return row[0].replace('/(<.+?>)/gi', '')+' '+row[1];}
            }).result(function(e, item){ $('#artistForm #key').val(item[2]); });
        
        $('#artistForm').submit(function(e){
            e.preventDefault();
			target = $('#artistForm').attr('action');
            if( $('#artistForm #key').val() && ($('#artistForm #key').val() !== '-'))
                document.location = target+'art/'+ $('#artistForm #key').val();
            //else alert( $('#selectart').val() );
        });
        $('#artistForm a').click(function(e){ e.preventDefault(); $('#artistForm').submit(); });
    }

	// Envoi du formulaire de recherche
	$('#searchForm').submit(function(e) {
        e.preventDefault();
        target = $('#searchForm').attr('action');
        $('#searchResult').html( $('#load').html() ).show();
        $.post(target, $("#searchForm").serialize(), 
            function(res){
                //$('#searchResult').show();
                $('#searchResult').html(res);
            }
        );
    });
    
    // Gestion des focus quand 2 champs de recherche
    $('#field1').focus(function(){
        if( $(this).val() == $(this).attr('title') )
        {
            $(this).val('').css('color','#000');
            $('#field2').val( $('#field2').attr('title') ).css('color','#636363');
        }
    });
    $('#field2').focus(function(){
        if( $(this).val() == $(this).attr('title') )
        {
            $(this).val('').css('color','#000');
            $('#field1').val( $('#field1').attr('title') ).css('color','#636363');
        }
    });
    $('#field1, #field2').blur(function(){
        if( $(this).val() == '' )
            $(this).val( $(this).attr('title') ).css('color','#636363');
    });

    
    $('#searchForm a').click(function() {
        $('#searchForm').submit();
        return false;
    });
});
