$(function() {
    
    // FONT REPLACEMENT
    if(($('body#news').length == 1) || ($('body#forum').length == 1)) {
        Cufon.replace(['h1', 'h4', '.primary-menu a', '.secondary-menu a', '.info', '.akkurat']); 
    } else {
        Cufon.replace(['h1', 'h2', 'h3', 'h4', '.primary-menu a', '.secondary-menu a', '.info', '.akkurat']); 
    }
    
    
    // SEARCHING
    $('.search .search-input')      
        .focus(function(){
            if(this.value=='Rechercher sur le site'){
                this.value='';
            }
        })
        .blur(function() {
            if(this.value==''){
                this.value='Rechercher sur le site';
            }
        });
        
    $('.search-big .search-input, .search-medium .search-input')        
        .focus(function() {
            if(this.value=='Trouver une question' || 'Rechercher un métier') {
                this.value='';
            }
        })
        .blur(function() {
            if(this.value=='') {
                if($('body#forum').length == 1) {
                    this.value='Trouver une question';
                } else {
                    this.value='Rechercher un métier';
                }
            }
        });
        
    $('.search-job .search-input, .search-question .search-input').keyup(function() {
        var value = $(this).val();
        if(value == '')
        {
            $('ul.autocomplete').remove();
            return;
        }
        
        var container = $(this).parent().parent();
        $.getJSON($(container).attr("action"),
            { search: value },
            function(data){
                $('ul.autocomplete').remove();
                if(data.length) {

                    var list = $('<ul class="autocomplete"></ul>');
                    
                    for(var i=0; i<data.length; i++) {
                        $('<li><a href="'+data[i].url+'">'+data[i].name+'</a></li>').appendTo($(list));
                    }
                    
                    $(container).append(list);
                }
            }
        );        
    });
        
    //LOGIN
    $('#login-form .login')      
        .focus(function(){
            if(this.value=='Identifiant'){
                this.value='';
            }
        })
        .blur(function() {
            if(this.value==''){
                this.value='Identifiant';
            }
        });   
    $('#login-form .password')      
        .focus(function(){
            if(this.value=='Password'){
                this.value='';
            }
        })
        .blur(function() {
            if(this.value==''){
                this.value='Password';
            }
        });    
        
        
    // BTN REPLACEMENT 
    $('input.btn-brown, input.btn-cream').each(function() {
        var inputButton = $(this);
        var linkButton = $('<a class="'+inputButton.attr('class')+'"><span class="left"></span><span class="center">'+ $(this).attr('value') +'</span><span class="right"></span></a>');
        $(this).after(linkButton).hide();
        linkButton.bind('click', function() {
            inputButton.click();
            return false;
        })
    })

    // FORUM - POPIN LOGIN
    if($('body#forum').length == 1) {
        $('.asking').hide();
        
        var anchor = window.location.hash;
        anchor = anchor.substring(1,anchor.length);
        if(anchor == 'ask') $('.asking').show();
       
        $('#popin-login').dialog({
            autoOpen: false, modal: true, width:435, draggable:false, resizable:false 
        });
        $('.login').click(function(){ 
            $('#popin-login').dialog('open').css('width', '100%'); 
            return false; 
        }); 
        $('.ask').click(function(){ 
            $('.asking').slideDown(); 
            return false; 
        });     
    }
    
    // METIER - FICHE
    if($('body#metiers-fiche').length == 1) {   
        $('.grid-2-615 .button').click(function(){
            if($(this).parent().parent().find('div.teaser').is(':visible')) {
                $(this).parent().parent().parent().addClass('expand');
            } else { 
                $(this).parent().parent().parent().removeClass('expand');
            }
            return false;
        });
        
        $('.formation h3 a').click(function() {
            if($('.formation ul').is(':visible')) {
                $('.formation ul').slideUp();
                $(this).find('span').removeClass('less').addClass('plus');
            } else {
                $('.formation ul').slideDown();
                $(this).find('span').removeClass('plus').addClass('less');
            }
            return false; 
        });
    }

    // ACCORDION
    if($('body#artisanat-entreprises').length == 1) {
        $('#accordion .item div').hide();
        $('#accordion .item h5 a').click(function() {
            var item = $(this).parent().parent();
            if($(this).parent().parent().find('> div').is(':visible')) {
                $(item).removeClass('expand');
                $(item).find('div').slideUp();
            }
            else {
                $(item).addClass('expand');
                $(item).find('div').slideDown();
            }
            
            return false;
        });
       
    
    }
    
    
});
