var search_page = false;
$(document).ready(function(){
    
    $('#lightbox a').lightBox({
        imageLoading:'/i/jquery/lightbox-images/lightbox-ico-loading.gif',
        imageBtnClose:'/i/jquery/lightbox-images/lightbox-btn-close2.gif',
        imageBtnPrev:'/i/jquery/lightbox-images/lightbox-btn-prev2.gif',
        imageBtnNext:'/i/jquery/lightbox-images/lightbox-btn-next2.gif',
        imageBlank:'/i/jquery/lightbox-images/lightbox-blank.gif',
        txtImage:'Изображение',
        txtOf:'из'
    });
    
    $(".jMyCarousel").jMyCarousel({
        visible:jMyCarousel_calc_width(),
        eltByElt:true,
        circular:false
    });
    
    if (document.documentElement.clientHeight>$('#all').height()) {
        $('#last').attr('height', document.documentElement.clientHeight-$('#all').height()+25);
    }
        
    $('#show_form').click(function(){
        $('#send_button').hide();
        $('#send_form').slideDown();
    });
    
    $('.calc').change(calc);
    $('.calc').keyup(calc);
    $('.calc').click(calc);
    
    calc();
    
    $('input[name="search_ok"]').click(function(){
        location.href = '/catalogue/search/producer-'+$('select[name="search_producer"]').val()+'/?model='+$('select[name="search_model"]').val()+'&transmission='+$('select[name="search_transmission"]').val();
    });
    
    $('select[name="search_producer"]').change(function(){
        $.ajax({
            cache:true,
            type:'POST',
            dataType:'json',
            url:'/catalogue/models/id-'+$(this).val()+'/',
            success:function(response){
                $('#td_search_model').html(response.html);
                search_model_init();
                $('select[name="search_model"]').change();
            }
        });
    });
    
    search_model_init();
    
    if (search_page) {
        search_show();
    }
    
});

function search_model_init() {
    
    $('select[name="search_model"]').change(function(){
        $.ajax({
            cache:true,
            type:'POST',
            dataType:'json',
            url:'/catalogue/transmissions/id-'+$('select[name="search_producer"]').val()+'/?model='+$(this).val(),
            success:function(response){
                $('#td_search_transmission').html(response.html);
            }
        });
    });
    
}

var jMyCarousel_width = 0;
function jMyCarousel_calc_width() {
    
    $(".jMyCarousel img").each(function(){
        jMyCarousel_width += this.width;
    });
    
    return jMyCarousel_width>$("#td_jMyCarousel").width() ? ($("#td_jMyCarousel").width()-10)+'px' : jMyCarousel_width+'px';
    
}

function query(url, div, data) {
    
    $.ajax({
        cache:false,
        type:'POST',
        dataType:'json',
        url:url,
        data:data,
        success:function(response){
            $('#'+div).html(response.html);
        }
    });
    
}

function checknum(e) {
    
    return (e.keyCode >= 48 && e.keyCode <= 57)
    
}

function fixPNG(element) {
    
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
        var src;
        if (element.tagName=='IMG') {
            if (/\.png$/.test(element.src)) {
                src = element.src;
                element.src = "/i/spacer.gif";
            }
        }
        else {
            src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
            if (src) {
                src = src[1];
                element.runtimeStyle.backgroundImage="none";
            }
        }
        if (src) {
            element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
        }
    }
    
}

function calc() {
    
    var v = 0;
    var cost = 0;
    var power = 0;
    var delivery = 0;
    var f = 0;  // Таможня для физ. лица
    var u = 0;  // Таможня для юр. лица
    var ua = 0; // Акциз
    var un = 0; // НДС
    var ft = 0; // Всего для физ. лица
    var ut = 0; // Всего для юр. лица
    var result_currency = 1;
    
    
    if (v = parseInt($('input[name="v"]').val())) {
        $('input[name="v"]').val(v);
    }
    else {
        $('input[name="v"]').val('');
    }
    if (cost = parseInt($('input[name="cost"]').val())) {
        $('input[name="cost"]').val(cost);
        switch ($('input[name="cost_currency"]:checked').val()) {
            case 'dollar': cost *= dollar; break;
            case 'euro': cost *= euro; break;
            case 'yen': cost *= yen; break;
        }
    }
    else {
        $('input[name="cost"]').val('');
    }
    if (power = parseInt($('input[name="power"]').val())) {
        $('input[name="power"]').val(power);
        if (parseInt($('input[name="power_type"]:checked').val())) {
            power = power*4/3;
        }
    }
    else {
        $('input[name="power"]').val('');
    }
    
    if (v && cost && power) { // Calc here 
        
        switch(parseInt($('input[name="age"]:checked').val())) {
            
            case 0: // age<3
            if (cost<=325000) {
                f = Math.max(cost*0.48, 2.5*v*euro);
            }
            else if (cost<=650000) {
                f = Math.max(cost*0.48, 3.5*v*euro);
            }
            else if (cost<=1625000) {
                f = Math.max(cost*0.48, 5.5*v*euro);
            }
            else if (cost<=3250000) {
                f = Math.max(cost*0.48, 7.5*v*euro);
            }
            else if (cost<=6500000) {
                f = Math.max(cost*0.48, 15*v*euro);
            }
            else {
                f = Math.max(cost*0.48, 20*v*euro);
            }
            break;
            
            case 1: // 3>=age>=7
            if (v<=1000) {
                f = 0.85*v*euro;
            }
            else if (v<=1500) {
                f = 1*v*euro;
            }
            else if (v<=1800) {
                f = 1.5*v*euro;
            }
            else if (v<=200) {
                f = 1.75*v*euro;
            }
            else if (v<=3000) {
                f = 2*v*euro;
            }
            else {
                f = 2.25*v*euro;
            }
            break;
            
            default: // age>7
            if (v<=2500) {
                f = 2*v*euro;
            }
            else {
                f = 3*v*euro;
            }
            break;
            
        }
        
        if ($('input[name="engine"]:checked').val()=='diesel') {
            switch(parseInt($('input[name="age"]:checked').val())) {
                
                case 0: // age<3
                if (v<=1500) {
                    u = Math.max(cost*0.25, 1.2*v*euro);
                }
                else if (v<=2500) {
                    u = Math.max(cost*0.25, 1.8*v*euro);
                }
                else {
                    u = Math.max(cost*0.25, 2.35*v*euro);
                }
                break;
                
                case 1: // 3<=age<=7
                if (v<=1500) {
                    u = Math.max(cost*0.25, 0.4*v*euro);
                }
                else if (v<=2500) {
                    u = Math.max(cost*0.25, 0.55*v*euro);
                }
                else {
                    u = Math.max(cost*0.25, 1*v*euro);
                }                
                break;
                
                default: // age>=7
                if (v<=1500) {
                    u = 1.5*v*euro;
                }
                else if (v<=2500) {
                    u = 2.2*v*euro;
                }
                else {
                    u = 3.2*v*euro;
                }                
                
            }
        }
        else {
            switch(parseInt($('input[name="age"]:checked').val())) {
                
                case 0: // age<3
                if (v<=1000) {
                    u = Math.max(cost*0.25, 1*v*euro);
                }
                else if (v<=1500) {
                    u = Math.max(cost*0.25, 1.2*v*euro);
                }
                else if (v<=1800) {
                    u = Math.max(cost*0.25, 1.25*v*euro);
                }
                else if (v<=3000) {
                    u = Math.max(cost*0.25, 1.8*v*euro);
                }
                else {
                    u = Math.max(cost*0.25, 2.35*v*euro);
                }
                break;
                
                case 1: // 3<=age<=7
                if (v<=1000) {
                    u = Math.max(cost*0.25, 0.45*v*euro);
                }
                else if (v<=1500) {
                    u = Math.max(cost*0.25, 0.5*v*euro);
                }
                else if (v<=1800) {
                    u = Math.max(cost*0.25, 0.45*v*euro);
                }
                else if (v<=3000) {
                    u = Math.max(cost*0.25, 0.55*v*euro);
                }
                else {
                    u = Math.max(cost*0.25, 1*v*euro);
                }                
                break;
                
                default: // age>=7
                if (v<=1000) {
                    u = 1.4*v*euro;
                }
                else if (v<=1500) {
                    u = 1.5*v*euro;
                }
                else if (v<=1800) {
                    u = 1.6*v*euro;
                }
                else if (v<=3000) {
                    u = 2.2*v*euro;
                }
                else {
                    u = 3.2*v*euro;
                }                
                
            }
        }
        if (power<=90) {
            ua = 0;
        }
        else if (power<=150) {
            ua = 19.26*power;
        }
        else {
            ua = 194*power;
        }
        un = 0.18*(cost + u + ua);
        delivery = parseInt($('select[name="city"]').val());
        ft = cost + f + 15000 + delivery + 10000;
        ut = cost + u + ua + un + 15000 + delivery + 10000;
        
        switch ($('input[name="result_currency"]:checked').val()) {
            case 'dollar': result_currency = dollar; break;
            case 'euro':   result_currency = euro;   break;
            case 'yen':    result_currency = yen;    break;
        }
        
        $('#td_calc_cost').html(parseInt(cost/result_currency));
        $('#td_calc_f').html(parseInt(f/result_currency));
        $('#td_calc_u').html(parseInt(u/result_currency));
        $('#td_calc_ua').html(parseInt(ua/result_currency));
        $('#td_calc_un').html(parseInt(un/result_currency));
        $('#td_calc_delivery').html(parseInt(delivery/result_currency));
        $('#td_calc_ft').html(parseInt(ft/result_currency));
        $('#td_calc_ut').html(parseInt(ut/result_currency));
        $('#td_calc_broker').html(parseInt(broker/result_currency));
        $('#td_calc_company').html(parseInt(company/result_currency));
        $('#div_calc').show();
    }
    else { // Nothing to calc
        $('#div_calc').hide();
    }
    
    return true;
    
}