$(function(){
    $(".imgDetail img").mouseover(function() {
        $(".imgDetail img").removeClass("selected");
        $(this).addClass("selected");
        var src =  $(this).attr('src');
        src = src.replace("miniature_detail", "grande_detail");
        $('#zoom img').fadeOut('slow', function () {
            $('#zoom img').attr('src',src);
            $('#zoom img').fadeIn('slow');
        });
        
    });

    //link
    $(".containSelect").click(function(){
        window.location=$(".consulterSelect a", this).attr('href');
    })
    $(".diaporama").click(function(){
        window.location=$("#ContentAllCDC a").attr('href');
    })
    //endlink

    $(".diaporama").diaporama({
        animationSpeed: 3000,
        delay:4,
        controls:false
    }); 

    $("#montant_acquisition").keyup(function() {
        calcul_somme_finance();
        caul_taux_endettement();
        calcul_mensualite();
    });

    $("#apport_personnels").keyup(function() {
        calcul_somme_finance();
        caul_taux_endettement();
        calcul_mensualite();
    });

    $("#pourcent_notaire").keyup(function() {
        calcul_somme_finance();
        caul_taux_endettement();
        calcul_mensualite();
    });

    $("#taux").keyup(function() {
        calcul_mensualite();
        caul_taux_endettement();
    });

    $("#dure").change(function() {
        calcul_mensualite();
        caul_taux_endettement();
    });

    $("#mensualite").keyup(function() {
        calcul_know_mensualite();
        caul_taux_endettement();
    });

    $("#revenus").keyup(function() {
        caul_taux_endettement();
    });




    $("#mensualite_dette").keyup(function() {
        calcul_max_emprunt();
    });

    $("#taux_dette").keyup(function() {
        calcul_max_emprunt();
    });

    $("#dure_dette").change(function() {
        calcul_max_emprunt();
    });

    $("#revenus_dette").keyup(function() {
        calcul_max_emprunt();
    });

    $("#planBox").click(function(){
        $(this).removeClass("titlePlanFinancementOff");
        $(this).addClass("titlePlanFinancementOn");

        $("#empruntBox").removeClass("titleSommeEmpruntOn");
        $("#empruntBox").addClass("titleSommeEmpruntOff");

        $('.outils').hide();
        $('#financement_plan').show();
        
    });

    $("#empruntBox").click(function(){
        $(this).removeClass("titleSommeEmpruntOff");
        $(this).addClass("titleSommeEmpruntOn");

        $("#planBox").removeClass("titlePlanFinancementOn");
        $("#planBox").addClass("titlePlanFinancementOff");

        $('.outils').hide();
        $('#taux_endettement').show();

    });

});

function calcul_max_emprunt(){
    var mensualite = format_number($("#mensualite_dette").val());
    var dure_mois = $("#dure_dette").val()*12;
    var taux = format_number($("#taux_dette").val());
    var revenus_dette = format_number($("#revenus_dette").val());

    //calcul de la mensualite
    var mensualite = (33/100)*revenus_dette
    $("#mensualite_dette").val(Math.round(mensualite));

    if(taux!=0){
        taux=taux/1200;
        taux=(taux/(1-Math.pow(1+taux, -dure_mois)));
    }
    else{
        taux=1/dure_mois;
    }

    //somme a financer
    var somme_a_financer = Math.round(mensualite/taux);
    $("#somme_finance_dette").val(Math.round(somme_a_financer));

}

function get(id){
    return document.getElementById(id);
}

function testMini(id){
    if (get(id).value=='Minimum') get(id).value='';
    else if (get(id).value=='') get(id).value='Minimum';

}

function testMax(id){
    if (get(id).value=='Maximum') get(id).value='';
    else if (get(id).value=='') get(id).value='Maximum';
}

function search_checkbox(checkbox,idLabel){
    var label = $('#'+idLabel);
    //alert ('label => '  + '\n checkbox =>' + $('#'+ checkbox).value());
    if($('#'+checkbox).attr('checked')) {
        label.addClass('active');
    }else{
        label.removeClass('active');
    }
}

function calcul_somme_finance(){
    var calcul ="";
    var frais_de_notaire = "";
    var mensualite ="";
    var taux ="";




    var pourcent_notaire = format_number($("#pourcent_notaire").val());  
    var montant_acquisition = format_number($("#montant_acquisition").val());
    var apport_perso = format_number($("#apport_personnels").val())
    var taux_pret = format_number($("#taux").val());

    frais_de_notaire=(pourcent_notaire /100) * montant_acquisition;
    calcul = montant_acquisition - apport_perso + frais_de_notaire;
    taux = (taux_pret/100);
    
    mensualite = (calcul + (taux * $("#dure").val()*calcul))/($("#dure").val()*12);

    //affichage resultat
    $("#somme_finance").val(Math.round(calcul));
    $("#notaire").val(Math.round(frais_de_notaire));
    $("#mensualite").val(Math.round(mensualite));

}

function calcul_mensualite(){
    var somme_finance = format_number($("#somme_finance").val());
    var dure_mois = $("#dure").val()*12;
    var taux = format_number($("#taux").val());

    //    if(taux!=0){
    //        taux=taux/1200;
    //        taux=(taux/(1-Math.pow(1+taux, -dure_mois)));
    //    }
    //    else{
    //        taux=1/dure_mois;
    //    }
    //    
    //mensualite=Math.round((somme_finance*taux)*100)/100;
    var mensualite=Math.round((((somme_finance*taux)/1200)/(1-(1/Math.pow(1+(taux/1200),dure_mois))))*100)/100;

    $("#mensualite").val(mensualite);
}

function calcul_know_mensualite(){
    var mensualite = format_number($("#mensualite").val());
    var dure_mois = $("#dure").val()*12;
    var taux = format_number($("#taux").val());
    var pourcent_notaire = format_number($("#pourcent_notaire").val());
    var apport_perso = format_number($("#apport_personnels").val());

    if(taux!=0){
        taux=taux/1200;
        taux=(taux/(1-Math.pow(1+taux, -dure_mois)));
    }
    else{
        taux=1/dure_mois;
    }

    //somme a financer
    var somme_a_financer = Math.round(mensualite/taux);
    $("#somme_finance").val(somme_a_financer);

    if(somme_a_financer != 0){
        var montant_acquis= Math.round((apport_perso*1 + somme_a_financer*1) / (1 + (pourcent_notaire/100)));
        $("#montant_acquisition").val("0");
        $("#montant_acquisition").val(montant_acquis);
    }else{
        $("#montant_acquisition").val("0");
        $("#notaire").val("0");
    }
    //calcul montant acquis


    //calcul de frais de notaire
    var frais_de_notaire =Math.round((pourcent_notaire /100) * montant_acquis);
    $("#notaire").val(frais_de_notaire);

}

function caul_taux_endettement(){
    var mensualite = format_number($("#mensualite").val());
    var revenu = format_number($("#revenus").val());

    if(revenu!=0){
        var taux_endettement =  mensualite/revenu
    }else{
        return false;
    }

    if (taux_endettement < 0) {
        return false
    }else {
        $("#endettement").val(Math.round(taux_endettement*100));
        if ($("#endettement").val()> 33) {
            $("#endettement").css("border-color", "#D71522");
        }else{
            $("#endettement").css("border-color", "#F4CC1D");
        }
        return true;
    }

}

function format_number(value){
    value = value.replace(',', '.');

    if (isNaN(value)){
        alert("Merci de rentrer un chiffre.");
        return false;
    }else{
        return value;
    }
}

function getDim(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) {	// all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }

    return [pageWidth,pageHeight];
}
