
jQuery(document).ready(function() {

    jQuery("a[rel='offer']").colorbox();
    jQuery("a[rel='gallery1']").colorbox();
    jQuery("a[rel='gallery2']").colorbox();
    jQuery("a[rel='gallery3']").colorbox();
    jQuery("a[rel='gallery4']").colorbox();

    update_countries();
    var car_type = jQuery("#car_type").val();
    if(car_type != '') {
        jQuery("#car_gallery").show();
        jQuery("#gallery_"+car_type).show();
        jQuery("#car_fleet").show();
    } else {
        jQuery("#car_gallery").hide();
        jQuery("#car_fleet").hide();
    }



    jQuery("input[name='direction']").click(function(){
        var direction = this.value;
        if(direction == 'return') {
            jQuery("#return_time_container").children().attr("disabled", false);
        } else {
            jQuery("#return_time_container").children().attr("disabled", true);
        }
    });

    jQuery("#formReserv").submit(function(){
        var valid = true;
        var country = jQuery("#country");
        var from_loc = jQuery("#from_loc");
        var to_loc = jQuery("#to_loc");
        var car_type = jQuery("#car_type");
        var direction = jQuery("input[name=$'direction']:checked").val();
        var payment_type = jQuery("#payment_type").val();

        if(country.val() == '') {
            valid = false;
            country.css('border', '1px solid red');
        }

        if(from_loc.val() == '') {
            valid =  false;
            from_loc.css('border', '1px solid red');
        }

        if(to_loc.val() == '' || from_loc.val() == to_loc.val()) {
            valid =  false;
            to_loc.css('border', '1px solid red');
        }

        if(car_type.val() == '') {
            valid =  false;
            car_type.css('border', '1px solid red');
        }

        if(!direction) {
            valid = false;
            jQuery("#lblOneway").css('color', "red");
            jQuery("#lblReturn").css('color', "red");
        }

        if(!payment_type) {
            valid = false;
            jQuery("#payment_type").css('border', '1px solid red');
        }

        return valid;
    });

    jQuery("input[type='radio']").click(function(){
        jQuery("#lblOneway").css('color', "#1C1C1C");
        jQuery("#lblReturn").css('color', "#1C1C1C");
    });

    jQuery("#country").change(function(){
        jQuery("#country").css('border', '1px solid #cecece');
        var from_loc_select = jQuery("#from_loc").empty();
        var to_loc_select = jQuery("#to_loc").empty();
        from_loc_select.append('<option value="">select ...</option>');
        if(this.value == 'ITALY') {
            from_loc_select.append('<option value="MILANO CITY">MILANO CITY</option>');
            from_loc_select.append('<option value="LINATE AIRPORT">LINATE AIRPORT</option>');
            from_loc_select.append('<option value="MALPENSA AIRPORT">MALPENSA AIRPORT</option>');
            from_loc_select.append('<option value="" disabled="disabled">--------------------</option>');
        }
        to_loc_select.append('<option value="">select ...</option>');

        jQuery("#car_type option")[0].selected=true;
        jQuery("#car_gallery").hide();
        jQuery("#gallery_1").hide();
        jQuery("#gallery_2").hide();
        jQuery("#gallery_3").hide();
        jQuery("#gallery_4").hide();

        jQuery("#priceHolder").val('');

        if(this.value == '') return;
        var url = URL_SITE + 'routes/get_from_locations?jsonp=&country='+this.value;
        $json(url, function(data) {
            for(var i = 0; i < data.length; i++){
                from_loc_select.append('<option value="'+data[i]+'">'+ucfirst(data[i])+'</option>');
            }
        });
    });

    jQuery('#from_loc').change(function(){

        jQuery("#car_type option")[0].selected=true;
        jQuery("#car_gallery").hide();
        jQuery("#gallery_1").hide();
        jQuery("#gallery_2").hide();
        jQuery("#gallery_3").hide();
        jQuery("#gallery_4").hide();

        jQuery("#priceHolder").val('');
        jQuery("#from_loc").css('border', '1px solid #cecece');
        var to_loc_select = jQuery("#to_loc").empty();
        var country = jQuery("#country").val();
        to_loc_select.append('<option value="">select ...</option>');
        if(this.value == '') return;
        var url = URL_SITE + 'routes/get_to_locations?jsonp=&country='+country+'&from_loc='+this.value;
        $json(url, function(data) {
            jQuery.each(data, function(i){
                to_loc_select.append('<option value="'+data[i]+'">'+ucfirst(data[i])+'</option>');
            });
        });
    });

    jQuery("#to_loc").change(function(){
        jQuery("#priceHolder").val('');

        jQuery("#to_loc").css('border', '1px solid #cecece');

        jQuery("#car_type option")[0].selected=true;
        jQuery("#car_gallery").hide();
        jQuery("#gallery_1").hide();
        jQuery("#gallery_2").hide();
        jQuery("#gallery_3").hide();
        jQuery("#gallery_4").hide();
    });

    jQuery("#car_type").change(function(){
        jQuery("#car_type").css('border', '1px solid #cecece');
        var i = this.value;
        for(var j = 1; j <= 4; j++) {
            jQuery("#gallery_"+j).hide();
        }
        if(i == '') {
            jQuery("#car_gallery").hide();
            jQuery("#car_fleet").hide();
            return;
        }
        jQuery("#car_gallery").show();
        jQuery("#gallery_"+i).show();
        jQuery("#car_fleet").show();

        update_price();
    });

    jQuery("#return").click(function(){
        update_price();
    });

    jQuery("#oneway").click(function(){
        update_price();
    });

    jQuery("#payment_type").change(function(){
        jQuery("#payment_type").css('border', '1px solid #cecece');
        update_price();
    });

    jQuery("#pick_up_hour").change(function(){
        update_price();
    });

    jQuery("#return_hour").change(function(){
        update_price();
    });

});

function update_countries() {
    var country_select = jQuery("#country").empty();
    country_select.append('<option value="">select ...</option>');
    var url = URL_SITE + 'routes/get_countries?jsonp';
    $json(url, function(data) {
        jQuery.each(data, function(i){
            country_select.append('<option value="'+data[i]+'">'+ucfirst(data[i])+'</option>');
        });
    });
}

function update_price() {
    var pay_type = jQuery('select[name="payment_type"] option:selected').val();
    var discount = pay_type == 'Cash to the Driver (10% discount)' ? 10 : 0;
    var country = jQuery("#country").val();
    var from_loc = jQuery("#from_loc").val();
    var to_loc = jQuery("#to_loc").val();
    var car_type = jQuery("#car_type").val();
    var pick_up_hour = jQuery("#pick_up_hour").val();
    var return_hour = jQuery("#return_hour").val();
    var direction = jQuery("input[name=direction]:checked").val();
    var url = URL_SITE + 'routes/get_route?jsonp=&country='+country+'&from_loc='+from_loc+'&to_loc='+to_loc;
    $json(url, function(data) {

        var original_price = data['price_'+car_type] !== undefined ? data['price_'+car_type] : 0;
        original_price = parseFloat(original_price);
        var oneway_price = 0
        var return_price = 0;
        var total_price = 0;
        var discount_amount = 0;
        var oneway_night_tax = 0;
        var return_night_tax = 0;

        if(direction == 'oneway' && (pick_up_hour > 20 || pick_up_hour < 7 ) ) {
            oneway_night_tax = 10 / 100 * original_price;
        }
        

        if(direction == 'return') {
            if((pick_up_hour > 20 || pick_up_hour < 7 )) {
                oneway_night_tax = 10 / 100 * original_price;
            }
            if(return_hour > 20 || return_hour < 7) {
                return_night_tax = 10 / 100 * original_price;
            }
            return_price = original_price + return_night_tax;
        }

        oneway_price = original_price + oneway_night_tax;
        

        if(discount) {
            discount_amount = discount / 100 * (oneway_price + return_price) ;
        }

        /*
        alert('route id: ' + data['id']);
        alert('original price: ' + original_price);
        alert('oneway night tax: ' + oneway_night_tax);
        alert('oneway price: ' + oneway_price);
        alert('return night tax: ' + return_night_tax);
        alert('return price: ' + return_price);
        alert('discount amount: ' + discount_amount);
        //*/

        total_price = oneway_price + return_price - discount_amount;
        total_price = total_price.toFixed(2);
        
        var txtPrice = total_price + " &euro;";
        if(total_price != 0) {
            jQuery("#price_wrapper").show();
            jQuery("#priceHolder").val(total_price);
            jQuery("#lblPriceHolder").html(txtPrice);
        }
    });
}
