function fare_details(){
    
    $(".show_fare_details_trigger").bind('click', function(){
        // hide other
        //$('.more_offer_details').hide();
        //$('.show_fare_details_button').show();
        //$('.hide_fare_details_button').hide();
        //$('.default_offer_details').show();

        // open actual
        $(this).parents('.singleOfferBox').find('.show_fare_details_button').hide();
        $(this).parents('.singleOfferBox').find('.default_offer_details').hide();
        $(this).parents('.singleOfferBox').find('.more_offer_details').animate({
                "height": "toggle", 
                "opacity": "toggle"
            }, "fast", 'linear', function(){
                $(this).parents('.singleOfferBox').find('.hide_fare_details_button').show();
            });
    });

    $(".hide_fare_details_trigger").bind('click', function(){
        // close actual
        $(this).parents('.singleOfferBox').find('.hide_fare_details_button').hide();
        $(this).parents('.singleOfferBox').find('.show_fare_details_button').show();
        $(this).parents('.singleOfferBox').find('.default_offer_details').show();
        $(this).parents('.singleOfferBox').find('.more_offer_details').hide();
        // reset offer price to default  
        var default_price = parseFloat($(this).parents('.singleOfferBox').find('.repricing_default_price').val());
        $(this).parents('.singleOfferBox').find('.repricing_total_price').html(default_price.toFixed(2));

        // reset chosen prices 
        var outbound_price = $(this).parents('.singleOfferBox').find('.outbound_fare_strip.default').find('.repricing_outbound_price').val();
        $(this).parents('.singleOfferBox').find('.repricing_chosen_outbound_price').val(outbound_price);
        var inbound_price = $(this).parents('.singleOfferBox').find('.inbound_fare_strip.default').find('.repricing_inbound_price').val();
        $(this).parents('.singleOfferBox').find('.repricing_chosen_inbound_price').val(inbound_price);

        // reset chosen fares to default ones
        // reset strip indication colors
        $(this).parents('.singleOfferBox').find('.outbound_fare_strip').removeClass('chosen_fare_outbound');
        $(this).parents('.singleOfferBox').find('.inbound_fare_strip').removeClass('chosen_fare_inbound');
        // reset radio checked
        $(this).parents('.singleOfferBox').find('.outbound_fare_strip.default').addClass('chosen_fare_outbound').find('input:radio').attr('checked', 'checked');
        $(this).parents('.singleOfferBox').find('.inbound_fare_strip.default').addClass('chosen_fare_inbound').find('input:radio').attr('checked', 'checked');
        //$(this).parents('.singleOfferBox').find('.choose_offer_button').hide();
    });

    $("#price_table .price").bind('mouseover', function(){
        var id = $(this).attr('id');
        var ids = id.split('_');
        $(this).addClass('mouseover');
        $("#"+ids[0]).addClass('mouseover');
        $("#cr_" + ids[1]).addClass('mouseover');
    });
    
    $("#price_table .price").bind('mouseout', function(){
        var id = $(this).attr('id');
        var ids = id.split('_');
        $(this).removeClass('mouseover');
        $("#"+ids[0]).removeClass('mouseover');
        $("#cr_"+ids[1]).removeClass('mouseover');
    });
}

function zeroFill(number, length) {
    // Setup
    var result = number.toString();
    var pad = length - result.length;

    while(pad > 0) {
        result = '0' + result;
        pad--;
    }
    return result;
}

function lang_open(){
    $("#langSelector .popup").animate({
        'height': ['toggle']
    }, 'fast');
}

function lang_close(){
    $("#langSelector .popup").hide();
}

function chopTimeString(choptime) {
    if(choptime==undefined) {
        return;
    } else {
        var hours = choptime.substr(0,2);
        var minutes = choptime.substr(3,2);
        var intmin = parseInt(minutes, 10);
        var inthours = parseInt(hours, 10);
        if(intmin < 30) {
            if(inthours < 9){
                return '0'+ inthours;
            } else {
                return inthours;
            }
        } else {
            if(inthours + 1 < 9){
                return '0'+ inthours+1;
            } else {
                return inthours + 1;
            }
        }
    }
}

$(document).ready(function() {

    $("#langSelector > .trigger").toggle(lang_open, lang_close);

    $("#roundTrip").bind('click', function(){
        // enable return trip    
        $("#fltSecondDate").datepicker("enable");
        $("#secondStartHours").slider("enable");
        $("#secondStartHoursShow").removeClass('disabled');
    });

    $("#oneWay").bind('click', function(){
        // disable return trip    
        $("#fltSecondDate").datepicker("disable");
        $("#secondStartHours").slider("disable");
        $("#secondStartHoursShow").addClass('disabled');
    });

    var datesRange = $("#datesRange").val();
    $('#MainForm .calendar').ypscalendar({
        startInput: '#fltFirstDate',
        endInput: '#fltSecondDate',

        dateFormat: 'dd.mm.yy',
        minDate: +datesRange,
        range: 0,

        startAlt: {
          date: '#MainForm #day',
          monthYear: '#MainForm #month_1'
        },

        endAlt: {
          date: '#MainForm #retDay',
          monthYear: '#MainForm #month_2'
        }
    });

/*    $("#firstStartHours").slider({
        range: true,
        min: 0,
        max: 24,
        step: 1,
        values: [0, 24],
        change: function(event, ui) {
            var hours = zeroFill(ui.values[0], 2) + ' - ' + zeroFill(ui.values[1], 2);
            if (ui.values[1] == 24){
                var hours_val = zeroFill(ui.values[0], 2) + ':01-23:59';
            } else {
                var hours_val = zeroFill(ui.values[0], 2) + ':01-' + zeroFill(ui.values[1], 2) + ':00';
            }
            $("#firstStartHoursShow").html(hours);
            $("#depTimeFromTo").val(hours_val);
        }
    });
*/
    
    var ff = $("#depTimeEarliest").val();
    var depFirst = chopTimeString(ff);
    var fs = $("#depTimeLatest").val();
    var depSecond = chopTimeString(fs);
    $("#firstStartHoursShow").html(depFirst + ' - ' + depSecond);
    var sf = $("#retTimeEarliest").val(); 
    var dstFirst = chopTimeString(sf);
    var ss = $("#retTimeLatest").val();
    var dstSecond = chopTimeString(ss);
    $("#secondStartHoursShow").html(dstFirst + ' - ' + dstSecond);

    $("#firstStartHours").slider({
        range: true,
        min: 0,
        max: 24,
        step: 1,
        values: [depFirst, depSecond],
        change: function(event, ui) {
            var hours = zeroFill(ui.values[0], 2) + ' - ' + zeroFill(ui.values[1], 2);
            if (ui.values[1] == 24){
                var first_start_hours_val = zeroFill(ui.values[0], 2) + ':01';
                var second_start_hours_val = '23:59';
            } else {
                var first_start_hours_val = zeroFill(ui.values[0], 2) + ':00';
                var second_start_hours_val = zeroFill(ui.values[1], 2) + ':00';
            }
            $("#firstStartHoursShow").html(hours);
            $("#depTimeEarliest").val(first_start_hours_val);
            $("#depTimeLatest").val(second_start_hours_val);
        }
    });

    $("#secondStartHours").slider({
        range: true,
        min: 0,
        max: 24,
        step: 1,
        values: [dstFirst, dstSecond],
        change: function(event, ui) {
            var hours = zeroFill(ui.values[0], 2) + ' - ' + zeroFill(ui.values[1], 2);
            if (ui.values[1] == 24){
                var first_end_hours_val = zeroFill(ui.values[0], 2) + ':00';
                var second_end_hours_val = '23:59';
            } else {
                var first_end_hours_val = zeroFill(ui.values[0], 2) + ':01';
                var second_end_hours_val = zeroFill(ui.values[1], 2) + ':00';
            }
            $("#secondStartHoursShow").html(hours);
            $("#retTimeEarliest").val(first_end_hours_val);
            $("#retTimeLatest").val(second_end_hours_val);

        }
    });

    /*
    try {
        // tooltip for selector
        $('#lang_selector').qtip({
            //content: 'Test qtipa',
            content: $('#tooltip_body').html(),
            position: {
                corner: {
                    target: 'bottomRight',
                    tooltip: 'topRight'
                }
            },
            show: 'click',
            hide: {
                fixed: true,
                when: {
                    event: 'unfocus'
                }
            },
            style: {
                width: {
                    max: 320
                }
            }
        });
    } catch (err) {
    }
*/


    // overlay in details
    $(".overinfo.right").tooltip({
        showURL: false,
        delay: 0,
        track: true,
        showBody: " - ",
        extraClass: "overlay",
        positionRight: true,
        //        showBody: true,
        bodyHandler: function() {
            return $(this).find(".overinfobox").html();
        }
    });
    
    $(".overinfo.left").tooltip({
        showURL: false,
        delay: 0,
        track: true,
        showBody: " - ",
        extraClass: "overlay",
        positionLeft: true,
        //        showBody: true,
        bodyHandler: function() {
            return $(this).find(".overinfobox").html();
        }
    });

/*
	function viewport() {
		return {
			x: $(window).scrollLeft(),
			y: $(window).scrollTop(),
			cx: $(window).width(),
			cy: $(window).height()
		};
	}

    $(".overinfo").mouseover(function(e){
        var v = viewport();
        var box = $(this).find(".overinfobox");
        var pos = $(this).offset();
        var top = pos.top;
        var left = pos.top;
        var h = $(this).height();
        var m_x = e.pageX;
        var m_y = e.pageY;
        $(box).css('position', 'absolute').show().css('left', (m_x +10) + 'px').css('top', (m_y + h) + 'px');
    });
    
    $(".overinfo").mouseout(function(){
        $(this).find(".overinfobox").hide();
    });
*/

    $(".select_fare").bind('mouseover', function(){
        $(this).addClass('selected_fare');
    }); 

    $(".select_fare").bind('mouseout', function(){
        $(this).removeClass('selected_fare');
    });

    $(".select_fare").bind('click', function(){
        $(this).find('input:radio').attr('checked', 'checked');

        $(this).siblings().removeClass('chosen_fare_outbound');
        $(this).siblings().removeClass('chosen_fare_inbound');

        if ($(this).hasClass('outbound_fare_strip')){
            $(this).addClass('chosen_fare_outbound');

            var outbound_price = $(this).find('.repricing_outbound_price').val();
            $(this).parents('.singleOfferBox').find('.repricing_chosen_outbound_price').val(outbound_price);

            var inbound_price = ($(this).parents('.singleOfferBox').find('.repricing_chosen_inbound_price').val() || 0);
            var tax = $(this).parents('.singleOfferBox').find('.repricing_outbound_tax').val();
            var sum = parseFloat(outbound_price) + parseFloat(inbound_price) + parseFloat(tax);
            //alert(parseFloat(outbound_price));
            //alert(parseFloat(inbound_price));
            //alert(parseFloat(tax));
            //alert(sum);

            //var price = $(this).parents('.singleOfferBox').find('.repricing_chosen_outbound_price').val() + $(this).parents('.singleOfferBox').find('.repricing_chosen_outbound_price').val();
            $(this).parents('.singleOfferBox').find('.repricing_total_price').html(sum.toFixed(2));
        } else {
            $(this).addClass('chosen_fare_inbound');
            var inbound_price = $(this).find('.repricing_inbound_price').val();
            $(this).parents('.singleOfferBox').find('.repricing_chosen_inbound_price').val(inbound_price);
            
            var outbound_price = ($(this).parents('.singleOfferBox').find('.repricing_chosen_outbound_price').val() || 0);
            var tax = $(this).parents('.singleOfferBox').find('.repricing_inbound_tax').val();
            var sum = parseFloat(outbound_price) + parseFloat(inbound_price) + parseFloat(tax);
            //$(this).parents('.singleOfferBox').find('.repricing_chosen_inbound_price').val($(this).find('.repricing_inbound_price').val());
            $(this).parents('.singleOfferBox').find('.repricing_total_price').html(sum.toFixed(2));
        }

    });

    fare_details(); 

    if ($('#oneWay:checked').length >= 1) {
        // disable return trip    
        $("#fltSecondDate").datepicker("disable");
        $("#secondStartHours").slider("disable");
        $("#secondStartHoursShow").addClass('disabled');
    }
});
