//jQuery.noConflict();

$(function() {

    $('#nav-how, .nav-how').click(function(){
        show_ajax_lightbox('/content/how_it_works');
    });
    $('#nav-about, .nav-about').click(function() {
        show_ajax_lightbox('/content/about_us');
    });
    $('#nav-security, .nav-security').click(function(){
        show_ajax_lightbox('/content/security_deposit');
    });
    $('#nav-faqs, .nav-faqs').click(function(){
        show_ajax_lightbox('/content/faqs');
    });
    $('#nav-contact, .nav-contact').click(function(){
        show_ajax_lightbox('/content/contact_us');
    });
    $('#nav-privacy, .nav-privacy').click(function(){
        show_ajax_lightbox('/content/privacy');
    });
    $('.nav-fees').click(function(){
        show_ajax_lightbox('/content/fees');
    });
    $('#nav-terms-bottom, .nav-terms-bottom, #nav-terms, .nav-terms').click(function(){
        show_ajax_lightbox('/content/terms_and_conditions');
    });
    $('img.autocheck').click(function(){
        show_ajax_lightbox('/content/autocheck');
    });
    $('img.run_and_drive').click(function(){
        show_ajax_lightbox('/content/run_and_drivers');
    });
    $('img.enhanced_vehicle').click(function(){
        show_ajax_lightbox('/content/enhanced_vehicle');
    });
    $('img.start_engine_program').click(function(){
        show_ajax_lightbox('/content/engine_start_program');
    });

    $('#droppy').droppy({
        speed: 100
    });

    $('.listing tbody tr').mouseover(function(event) {
        $(event.currentTarget).addClass('selected');
    });

    $('.listing tbody tr').mouseout(function(event) {
        $(event.currentTarget).removeClass('selected');
    });
        
    $('#how-to-buy, #vehicle-search, #contact-us, #services').mouseover(function () {
        var offset = $(this).offset();
        $('#' + this.id + '-menu').css({
            top:    (offset.top + 20) + 'px',
            left:   offset.left + 'px'
        });
    });
});

function show_ajax_lightbox(url) {
    show_loader();   
    show_lightbox('#lightbox');
    $.post(url, function(data) {
        jQuery('#lightbox_content').html(data);
    });
}

function show_lightbox(id) {
    var width = parseInt($(id + ' .page').css('width'));
    $.blockUI({
        centerX: true,
        centerY: true,
        css: {
            top: ((screen.height/100) * 15) + 'px',
            left: ($(window).width() - width) /2 + 'px',   
            border: 0,
            padding: '0px'          
        },
        overlayCSS:  {
            backgroundColor: '#000',
            opacity:         0.75           
        },
        message: $(id)
    });
    jQuery('.blockOverlay, div.close img').click(function () {
        $.unblockUI();
    });
}

function show_loader() {
    $('#lightbox_content').html('<div class="ajax_loader">Please Wait...</div>');
}

function fillinfo(checkbox) {
    if (checkbox.checked) {
        $.post('/payment_information/load', {}, function(json) {
            $('#first_name').val(json.first_name);
            $('#last_name').val(json.last_name);
            $('#company').val(json.company);
            $('#address_1').val(json.address_1);
            $('#address_2').val(json.address_2);
            $('#country_id').val(json.country_id);
            $('#city').val(json.city);
            $('#state').val(json.state);
            $('#zip').val(json.zip);
        }, 'json');
    } else {
        $('#first_name').val('');
        $('#last_name').val('');
        $('#company').val('');
        $('#address_1').val('');
        $('#address_2').val('');
        $('#country_id').val('');
        $('#state').val('');
        $('#city').val('');
        $('#zip').val('');
    }
}

function disable_elem(elem) {
    $(elem).attr('disabled', 'disabled');
}

function send_confirm(value) {
    $('#dispatcher').attr('name', value);
    $('#confirm-form').submit();
    return true;
}

function check_citizenship(not_reg) {
    var obj;
    if (not_reg) {
        obj = $("#is_us_canada_resident");
    } else {
        obj = $(":radio[name=is_us_canada_resident]").filter(":checked");
    }
    if (obj.val() == 'y') {
        $('#driver-license').show();
        $('#passport').hide();
    } else {
        $('#driver-license').hide();
        $('#passport').show();
    }
}

function send_msg(uri) {
    $.ajax({
        url: uri,
        type: "POST",
        data: ({
            rand    : Math.random(),
            name    : jQuery('#c_name').val(),
            email   : jQuery('#c_email').val(),
            phone   : jQuery('#c_phone').val(),
            msg     : jQuery('#c_msg').val(),
            captcha : jQuery('#c_captcha').val()
        }),
        dataType: "json",
        success: function(json){
            if (json.errors.length == 0) {
                jQuery('#c_name').val('');
                jQuery('#c_email').val('');
                jQuery('#c_phone').val('');
                jQuery('#c_msg').val('');
                jQuery('#c_captcha').val('');
                jQuery('#c_errors').attr('class', 'c_success');
                jQuery('#c_errors').html('<ul><li>' + json.success + '</li></ul>');
                jQuery('#c_errors').show();
            } else {
                var res = '<ul>';
                for (var elem in json.errors) {
                    res += '<li>' + json.errors[elem] + '</li>';
                }
                res += '</ul>';
                jQuery('#c_errors').attr('class', 'c_errors');
                jQuery('#c_errors').html(res);
                jQuery('#c_errors').show();
            }
        }
    })
}

function calculate_fee(price) {
    var fee = 0;
    if (isNaN(parseFloat(price))) {
        price = 0;
    }
    if (price > 0 && price < 100) {
        fee = 20;
    }
    if (price >= 100 && price < 200) {
        fee = 45;
    }
    if (price >= 200 && price < 400) {
        fee = 75;
    }
    if (price >= 400 && price < 600) {
        fee = 95;
    }
    if (price >= 600 && price < 800) {
        fee = 125;
    }
    if (price >= 800 && price < 1000) {
        fee = 150;
    }
    if (price >= 1000 && price < 1200) {
        fee = 175;
    }
    if (price >= 1200 && price < 1400) {
        fee = 200;
    }
    if (price >= 1400 && price < 1700) {
        fee = 225;
    }
    if (price >= 1700 && price < 2000) {
        fee = 250;
    }
    if (price >= 2000 && price < 2500) {
        fee = 275;
    }
    if (price >= 2500 && price < 3000) {
        fee = 300;
    }
    if (price >= 3000 && price < 4000) {
        fee = 325;
    }
    if (price >= 4000 && price < 17500) {
        fee = 350;
    }
    if (price >= 17500) {
        fee = (price/100) * 2 + 65;
        fee = Math.round(fee);
    }

    if (price > 0 && price < 1000) {
        fee += 35;
    }
    if (price >= 1000 && price < 2000) {
        fee += 50;
    }
    if (price >= 2000) {
        fee += 75;
    }
    if (price > 0) {
        fee += 30;
    }

    return fee;
}

