//var section = '';

$(function(){
    
    //Gestion des transparences png sur ie6
    if(typeof DD_belatedPNG != 'undefined' && typeof DD_belatedPNG.fix == 'function') {
        DD_belatedPNG.fix("#logo, .png");
        $("#submitIdentification").css({"margin-top" : "-2px", "margin-right" : "-1px"}); //car le hack png semble ajouter des marges chelous !
    }
    
    
    //Gestion du menu deroulant
    $("li.menuNiveau1").hover(
        function(){//focusin
            section = $(this).prop("id");
            
            switch(section.substr(0, 7)) {
                case 'section' :
                    $("#a" + section).addClass(section + "hover");
                    
                    if(typeof pageQuestion != 'undefined' && section == 'section5') {
                        break;
                    }
                    
                    $.ajax({
                        url : 'xhr.sousmenu.php',
                        async : false,
                        cache : false,
                        data : {section : section},
                        success : function(data) {
                            if(data.length) {
                                $("#" + section).children(".submenu").show();
                                $("#" + section).children(".submenu").html(data); 
                            }
                           
                        }
                    });
                break;
                
                case 'quiSom':
                    $("#aquiSom").addClass("quiSomhover");
                break;
                //case 'headerQ' :
                //    $("#menuTopQuestions").addClass(section + "hover");
                //break;
            
                case 'menuSau' :
                    $("#menuSauvezDetails").show();
                break;
            }
            
            
            
            
        },
        function(){//focusout
            section = $(this).prop("id");
            
            switch(section.substr(0, 7)) {
                case 'section' :
                    //on masque le sous menu
                    $(".submenu").hide();
                    //on enleve la couleur de survol
                    $("#a" + section).removeClass(section + "hover");
                break;
                
                case 'quiSom':
                    $("#aquiSom").removeClass("quiSomhover");
                break;
            
                //case 'headerQ' :
                //   $("#menuTopQuestions").removeClass(section + "hover");
                //break;
            
                case 'menuSau' :
                    $("#menuSauvezDetails").hide();
                break;
            }
            
        }
    );
    
    //valeur par defaut des champs
    $(".defaultValue").on("focus",
        function() {
            if($(this).val() == $(this).prop("defaultValue")) {
                $(this).val("")
            }
        }
    );
    
    $(".defaultValue").on("focusout",
        function() {
            if($(this).val() == "") {
                $(this).val($(this).prop("defaultValue"));
            }
        }
    );
    
    //chargement des news
    chargerFilNews(1, "#homeVosFinances");
    chargerFilNews(2, "#homeBourse");
    chargerFilNews(3, "#homeImmobilier");
    
    
    //Poser une question
    $("#formPoserQuestion").on("submit",
        function(e){
            $.post(
                'xhr.poser.question.php',
                $("#formPoserQuestion").serialize(),
                function(data){
                    if(data != '') {
                        alert(data);
                        
                        if(data == "Votre question a été enregistrée avec succès !") {
                            $("#poserQuestion").val("").blur();
                        }
                    }
                }
            );
            e.preventDefault();
        }
    );
});


function chargerFilNews(section, target, scrollTarget) {
    
    $.ajax({
        url : 'xhr.filnews.php',
        cache : false,
        data : {section : section},
        success : function(data) {
            //on force le focus à la zone de fil info
            if(scrollTarget != null) {
                var top = $(target).offset();
                $(window).scrollTop((top.top) - 10);
            }
            $(target).html(data);
        }
    });
}

function chargerFilNewsPolo(section, target, scrollTarget) {
    
    $.ajax({
        url : 'xhr.filnews.php',
        cache : false,
        data : {section : section, polo : 1},
        success : function(data) {
            //on force le focus à la zone de fil info
            if(scrollTarget != null) {
                var top = $(target).offset();
                $(window).scrollTop((top.top) - 10);
            }
            $(target).html(data);
        }
    });
}


function chargerFilInfoBourse(categorie, page, target, scrollTarget) {
    $.get(
        'xhr.filnews.php',
        {section : 2, categorie : categorie, page : page, limit : 10},
        
        function(data) {
            //on force le focus à la zone de fil info
            if(scrollTarget != '') {
                var top = $(target).offset();
                $(window).scrollTop((top.top) - 10);
            }
            
            $(target).fadeOut('', function(){$(this).html(data)}).fadeIn();
        }
    );
}

