$(document).ready(run);

function run(){

    setTimeout("upoutavka()",3000);
    if (jQuery.browser.msie) {
        $("select").css("visibility","hidden");
    }

    // trigger pre checkboxy vo vyhladavani
    $("div.box input:checkbox").each(function(){
        $(this).bind("click", function(){
            var parent = this;
            // zaskrtnem potomkov
            $("div.box input[@rel='" + this.value + "']").each(function(){
                this.checked = parent.checked;
            });
            // odskrtnem rodica
            $("div.box input[@rel='p_" + $(this).attr("rel") + "']").each(function(){
                    this.checked = false;
            });
        });
    });
    
    // trigre pre cenove okienka
    $(".second input[@name='pricemin']").focus(function(){
        if (this.value == 'Od') {
            this.value = '';
        }
    });
    $(".second input[@name='pricemin']").blur(function(){
        if (this.value == '') {
            this.value = 'Od';
        }
    });
    $(".second input[@name='pricemax']").focus(function(){
        if (this.value == 'Do') {
            this.value = '';
        }
    });
    $(".second input[@name='pricemax']").blur(function(){
        if (this.value == '') {
            this.value = 'Do';
        }
    });
    
    
    $("#lifestyle input[@name='contact']").focus(function(){
        if (this.value == 'Váš email / mobilní telefon') {
            this.value = '';
        }
    });
    $("#lifestyle input[@name='contact']").blur(function(){
        if (this.value == '') {
            this.value = 'Váš email / mobilní telefon';
        }
    });
    
    //kalkulacka
    $("#vypocti").bind("click", function(){
        return vypocti();
    });
}

function format_nums_only(e,s){
    switch (e.keyCode) {
        case 8:
        case 9:
        case 13:
        case 46:
            return true;
            break;
    }

    if (!(document.all) && (e.keyCode == 116)) {
        return true;
    }

    k = (e.which) ? e.which : e.keyCode;

    if (!((k < 48 || k > 57) && (k < 35 || k > 40))) {
        // format numbers to currency
        var tmp = s.value.toString().replace(/\./g,'');

        numArr = new String(tmp).split('').reverse();

        for (i=2; i<numArr.length; i+=3) {
            numArr[i] += '.';
        }

       s.value = numArr.reverse().join('');

       return true;
    }

    return false;
}

function vypocti(){
    var d  = Math.round($("#do").attr('value').replace(/\./g, ''));
    var i  = Math.round($("#i").attr('value').replace(',', '.'));
    var n  = Math.round($("#n").attr('value'));
    var o  = Math.round($("#o").attr('value'));
    var o1 = Math.round($("#o1").attr('value'));
    var o2 = Math.round($("#o2").attr('value'));
    var o3 = Math.round($("#o3").attr('value'));
    var o4 = Math.round($("#o4").attr('value'));
    var h1 = d * i / 100;
    var h2 = 1 /(1 + i / 100);
    var A  = Math.round((h1 / (1 - Math.pow(h2, n))) / 12, 2);
    var abcd = o1 * 1690 + o2*1890 + o3 * 2230 + o4 * 2450;
    var e = (o - (o1 + o2 + o3 + o4)) * 2320;
    var f;

    switch (Math.round(o, 1)) {
        case 1:
            f = 1780;
            break;
        case 2:
            f = 2320;
            break;
        case 3:
        case 4:
            f = 2880;
            break;
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
            f = 3230;
            break;
    }

    var ZM  = abcd + e + f;
    var MCP = Math.round(A * 1.25 + 250 + ZM, 2);

    $("p#stat").html("Výše <strong>anuitní splátky</strong>: " + A + " Kč<br />Výše <strong>životního minima</strong>: " + ZM + " Kč<br />Výše <strong>minimálního čistého příjmu</strong>: " + MCP + " Kč");
    return false;
}

function upoutavka() {
    $("#upoutavka").hide("slow");
    $("select").css("visibility","visible");
}
