$(document).ready(function()
{
    /** Active submit button */
    $("div#loanSlider input").mouseover(function() {
        $("div#loanSlider input").css("background-position", "0 57px");
    });
    
    $("div#loanSlider input").mouseout(function() {
        $("div#loanSlider input").css("background-position", "0 0");
    });
    
    /** Slider */
    function renderSlider(min, max, step, value)
    {
        $("#slider").slider({
            min: min,
            max: max,
            step: step,
            value: value
        });
    }
    
    /** Slider intervals */
    function renderIntervals(intervalType)
    {
        var shortSliderIntervals = '<span id="minInterval">10 000 Kč</span><span id="next11Interval">50 000 Kč</span><span id="next12Interval">100 000 Kč</span><span id="maxInterval">150 000 Kč</span>';
        var longSliderIntervals = '<span id="minInterval">40 000 Kč</span><span id="next21Interval">100 000 Kč</span><span id="next22Interval">200 000 Kč</span><span id="maxInterval">300 000 Kč</span>';
        
        if (intervalType == 'short') {
            $('#loanSlider div#intervals').html(shortSliderIntervals);
        } else {
            $('#loanSlider div#intervals').html(longSliderIntervals);
        }
    }
    
    /** Check right format of loan amount value */
    function checkLoanAmountValue(min, max, step)
    {
        var localValue = intPriceFromString($('#loanAmountValue').val());
        
        if (localValue < min) {
            $('#loanAmountValue').val(min);
        } else if (localValue > max) {
            $('#loanAmountValue').val(max);
        } else if (localValue % step != 0) {
            $('#loanAmountValue').val(localValue - localValue % step);
        }
    }

    /** Parameters by manually set loan amount value */
    function setByLoanAmountValue(loanType)
    {
        $('#slider').slider('option', 'value', intPriceFromString($('#loanAmountValue').val()));
        $('input#loanAmountValue').val(priceFormat($('#loanAmountValue').val()));

        /** Výše půjčky pro export */
        $('input#customerLoanAmount').val(intPriceFromString($('#loanAmountValue').val()));

        if (loanType == 1) {
            $('#loanInstalment p').text(priceFormat(pujckaSplatky[intPriceFromString($('#loanAmountValue').val())][3]) + ' Kč');
        } else {
            var maxIndex = 0;
            $.each(uverSplatky[intPriceFromString($('#loanAmountValue').val())], function(i) {if (i > 0) {maxIndex = i}});
            $('#loanInstalment p').text(priceFormat(uverSplatky[intPriceFromString($('#loanAmountValue').val())][maxIndex]) + ' Kč');
        }
    }
    
    /** Parameters by manually change loan type */
    function setByLoanTypeValue(loanType)
    {
        $('#loanAmountValue').val('50 000');

        /** Výše půjčky pro export */
        $('input#customerLoanAmount').val(intPriceFromString($('#loanAmountValue').val()));

        $('#loanInstalment p').text(priceFormat(uverSplatky[intPriceFromString($('#loanAmountValue').val())][72]) + ' Kč');
        
        if (loanType == 1) {
            $('#loan form').attr('action', '/online-pujcka');
            $('#loanType ul li:eq(0)').addClass('activeItem');
            $('#loanType ul li:eq(1)').removeClass('activeItem');
        } else {
            $('#loan form').attr('action', '/uver-na-cokoliv');
            $('#loanType ul li:eq(0)').removeClass('activeItem');
            $('#loanType ul li:eq(1)').addClass('activeItem');
        }
    }

    /**
     * Default parameters
     */
    renderSlider(10000, 150000, 10000, 50000);
    renderIntervals('short');
    $('#loan form').attr('action', '/online-pujcka');
    $('input#customerLoanAmount').val('50000');
    $('#loanInstalment p').text(priceFormat(pujckaSplatky[intPriceFromString($('#loanAmountValue').val())][3]) + ' Kč');

    /**
     * Change parameteres by manually set loan amount value
     */
    $('#loanAmountValue').change(function() {
        if ($('#loanType input:checked').val() == 1) {
            checkLoanAmountValue(10000, 150000, 10000);
            setByLoanAmountValue(1);
        } else {
            checkLoanAmountValue(40000, 300000, 10000);
            setByLoanAmountValue(2);
        }
    });

    /**
     * Change parameteres by manually change type of loan
     */
    $('.typUveru').click(function() {
        if ($('#loanType input:checked').val() == 1) {
            renderSlider(10000, 150000, 10000, 50000);
            renderIntervals('short');
            setByLoanTypeValue(1);
            $('#loanInstalment p').text(priceFormat(pujckaSplatky[intPriceFromString($('#loanAmountValue').val())][3]) + ' Kč');
        } else {
            renderSlider(40000, 300000, 10000, 50000);
            renderIntervals('long');
            setByLoanTypeValue(2);
            var maxIndex = 0;
            $.each(uverSplatky[intPriceFromString($('#loanAmountValue').val())], function(i) {if (i > 0) {maxIndex = i}});
            $('#loanInstalment p').text(priceFormat(uverSplatky[intPriceFromString($('#loanAmountValue').val())][maxIndex]) + ' Kč');
        }
    });

    /**
     * Change parameteres by slider handle
     */
    $("#slider").slider({
        slide: function(event, ui) {
            $('#loanAmountValue').val(priceFormat(ui.value));

            /** Výše půjčky pro export */
            $('input#customerLoanAmount').val(intPriceFromString($('#loanAmountValue').val()));
            if ($("#loanType input:checked").val() == 1) {
                $('#loanInstalment p').text(priceFormat(pujckaSplatky[intPriceFromString($('#loanAmountValue').val())][3]) + ' Kč');
            } else {
                var maxIndex = 0;
                $.each(uverSplatky[intPriceFromString($('#loanAmountValue').val())], function(i) {if (i > 0) {maxIndex = i}});
                $('#loanInstalment p').text(priceFormat(uverSplatky[intPriceFromString($('#loanAmountValue').val())][maxIndex]) + ' Kč');
            }
        }
    });
});
