$(document).ready(function () {
    $(".show-data").click(function () {
        $(this).next().slideToggle();
        $(this).toggleClass("hide-data");

        if ($(this).hasClass("hide-data")) {
            $(this).text("Show");
        } else {
            $(this).text("Hide");
        }
    });
    SearchAgain();
    DisplaySessionTimeout();
    /*
    $(".nearby-maindiv ul li").click(function () {
        $(".nearby-maindiv ul li").removeClass("active");
        $(this).addClass("active");
    });*/
});

var sessionTimeout = 30;

function DisplaySessionTimeout() {
    sessionTimeout = sessionTimeout - 1;
    if (sessionTimeout >= 0)
        window.setTimeout("DisplaySessionTimeout()", 30000);
    else {
        $('#divSessionOut').show();
    }
}
function SearchAgain() {
    $('#divSessionOut').hide();
    if ($("#hfTripType").val() == "3") {
        var strHtml = "";
        var totSector = parseInt($("#totSector").val());
        var percent = "50%";
        if (totSector == 3)
            percent = "33%";
        else if (totSector == 4)
            percent = "25%";
        for (var i = 1; i <= totSector ; i++) {
            var fromDate = $("#departure_date" + i).val().split('-');
            var depDate = "";
            if (fromDate.length == 3) {
                depDate = months[parseInt(fromDate[1]) - 1] + " " + fromDate[2] + ", " + fromDate[0];
            }
            else {
                depDate = $("#departure_date" + i).val();
            }
            strHtml += "<div class='wait_multi-city' style='width:" + percent + "'><h3>" + getAirportCode($("#fromCity" + i).val()) + " - " + getAirportCode($("#toCity" + i).val()) + "</h3><p>" + depDate + "</p></div>"
        }


        $("#Multi_City_wait").html(strHtml);
        $("#oneway_roundTrip_wait1").hide();
        $("#oneway_roundTrip_wait2").hide();
        $("#Multi_City_wait").show();
    }
    else {
        var arrFromCity = $("#fromCity").val().split(',');
        var restFromCity = "";
        for (var i = 1; i < arrFromCity.length; i++) {
            restFromCity += ((i == 1 ? "" : ", ") + arrFromCity[i]);
        }
        var arrFCity = arrFromCity[0].split(')')
        var fromCityName = arrFCity.length >= 2 ? arrFCity[1] : $("#fromCity").val();
        var fromArpName = getAirportCode($("#fromCity").val());

        var arrToCity = $("#toCity").val().split(',');
        var restToCity = "";
        for (var i = 1; i < arrToCity.length; i++) {
            restToCity += ((i == 1 ? "" : ", ") + arrToCity[i]);
        }
        var arrTCity = arrToCity[0].split(')')
        var toCityName = arrTCity.length >= 2 ? arrTCity[1] : $("#toCity").val();
        var toArpName = getAirportCode($("#toCity").val());

        var fromDate = $("#departure_date").val().split('-');
        var fDate = "";
        if (fromDate.length == 3) {
            fDate = months[parseInt(fromDate[1]) - 1] + " " + fromDate[2] + ", " + fromDate[0];
        }
        else {
            fDate = $("#departure_date").val();
        }
        var toDate = $("#return_date").val().split('-');;
        var tDate = "";
        if (toDate.length == 3) {
            tDate = months[parseInt(toDate[1]) - 1] + " " + toDate[2] + ", " + toDate[0];
        }
        else {
            fDate = $("#departure_date").val();
        }
        $("#porgressBarOrg").html("<h3>" + fromArpName + "</h3><p>" + fromCityName + "</p><span>" + fromArpName + " - " + restFromCity + "</span>");
        $("#porgressBarDest").html("<h3>" + toArpName + "</h3><p>" + toCityName + "</p><span>" + toArpName + " - " + restToCity + "</span>");
        $("#porgressBarDate").html((fDate + " - " + ($('#hfTripType').val() == 1 ? "Oneway" : tDate)));
        $("#oneway_roundTrip_wait1").show();
        $("#oneway_roundTrip_wait2").show();
        $("#Multi_City_wait").hide();
    }

    $("#topDivWebsite").hide();
    $("#SearchingProgressBar").show();
}

$(function () {
    $("#slider-range").slider({
        range: true,
        min: 0,
        max: 500,
        values: [0, 500],
        slide: function (event, ui) {
            $("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
        }
    });
    $("#amount").val("$" + $("#slider-range").slider("values", 0) +
        " - $" + $("#slider-range").slider("values", 1));
});


$(".show-airline-detail,.airline_detail").hide();
$(".view-flight-detail").click(function () {
    $(this).parents(".ticket-desc-div").find(".show-airline-detail,.airline_detail").slideToggle();
    $(this).parents(".ticket-desc-div").find(".airline-information").slideToggle();

    $(this).toggleClass("hide-data");

    if ($(this).hasClass("hide-data")) {
        $(this).text("Hide Flight Detail");
    } else {
        $(this).text("View Flight Detail");
    }

});

$("#mobileFilterShow").click(function () {
    $(".left-filter").show();
    $("#mobileFilterShow").hide();
    $("#mobileFilterHide").show();

});
$("#mobileFilterHide").click(function () {
    $(".left-filter").hide();
    $("#mobileFilterShow").show();
    $("#mobileFilterHide").hide();

});
$(".close-filter").click(function () {
    $(".left-filter").hide();
    $("#mobileFilterShow").show();
    $("#mobileFilterHide").hide();
});

$(".modify-show").click(function () {
    $(".modify-banner").slideToggle();
    $(this).toggleClass("hide-data2");

    if ($(this).hasClass("hide-data2")) {
        $(this).text("Hide Search");
    } else {
        $(this).text("Modify Search");
    }
    $(".left-filter").hide();
    $("#mobileFilterShow").show();
    $("#mobileFilterHide").hide();
    topFunction();
});
function topFunction() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}


function showDetails(ctr) {
    $(ctr).parents(".ticket-desc-div").find(".show-airline-detail,.airline_detail").slideToggle();
    $(ctr).parents(".ticket-desc-div").find(".airline_detail_third").slideToggle();
    $(ctr).parents(".ticket-desc-div").find(".airline-information").slideToggle();

    $(ctr).toggleClass("hide-data");

    if ($(ctr).hasClass("hide-data")) {
        $(ctr).text("Hide Flight Detail");
    } else {
        $(ctr).text("View Flight Detail");
    }
}
var app = angular.module("myApp", ['infinite-scroll']).value("THROTTLE_MILLISECONDS", 250);;
app.controller('myCtrl', function ($scope, $http) {
   
    $scope.selectedStop = [];
    $scope.selectedAirline = [];
    $scope.airlineM = "0";
    $scope.stopM = "0";
    $scope.nearBy = "0";
    $scope.flexiFare = "0";
    $scope.allFare = "true";
    $scope.selectedDepTime = [];
    $scope.selectedArrTime = [];
    $scope.SelectedGds = [];
    $scope.SelectedFareType = []

    $scope.lower_price_bound = 0;
    $scope.upper_price_bound = 5000;
    $scope.lower_price_slider = 0;
    $scope.upper_price_slider = 5000;
    $scope.numberToDisplay = 5;

    $scope.isNonStop = 0;
    $scope.isOneStop = 0;
    $scope.isTwoStop = 0;

    $scope.Gds = [];
    $scope.GdsList = [];

    $scope.fareType = [];
    $scope.FareTypeList = [];

    $scope.matrixActive = '00';

    $http.get("/flight/getDataLive")
    .then(function (response) {
        
        if (response.data.redirectURl != null && response.data.redirectURl != "") {
            $("#topDivWebsite").hide();
            window.location.replace(response.data.redirectURl);
        }
        $scope.flight = response.data;
        
        $scope.result = $scope.flight.result;
        $scope.lower_price_slider = Math.floor($scope.flight.minPrice);
        $scope.lower_price_bound = Math.floor($scope.flight.minPrice);
        $scope.upper_price_slider = Math.ceil($scope.flight.maxPrice);
        $scope.upper_price_bound = Math.ceil($scope.flight.maxPrice);

        for (var i = 0; i < $scope.flight.stop.length; i++) {
            if ($scope.isNonStop == 0 && $scope.flight.stop[i].stop == 1) {
                $scope.isNonStop = 1;
            }
            else if ($scope.isOneStop == 0 && $scope.flight.stop[i].stop == 2) {
                $scope.isOneStop = 1;
            }
            else if ($scope.isTwoStop == 0 && $scope.flight.stop[i].stop == 3) {
                $scope.isTwoStop = 1;
            }
        }

        angular.forEach($scope.result, function (result, index) {
            if ($scope.Gds.indexOf(result.GDS) == -1) {
                $scope.Gds.push(result.GDS);
                $scope.GdsList.push({ "gds": result.GDS, "price": result.price }); /**/
            }
            if ($scope.fareType.indexOf(result.fareType) == -1) {
                $scope.fareType.push(result.fareType);
                $scope.FareTypeList.push({ "fareType": result.fareType, "price": result.price }); /**/
            }
        });



        $("#slider-range").slider({
            range: true,
            min: $scope.lower_price_slider,
            max: $scope.upper_price_slider,
            values: [$scope.lower_price_slider, $scope.upper_price_slider],
            slide: function (event, ui) {
                $("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);

                var scope = angular.element(document.getElementById('flightmozoData')).scope();
                scope.$apply(function () {
                    scope.lower_price_bound = ui.values[0];
                    scope.upper_price_bound = ui.values[1];
                });
            }
        });
        $("#amount").val("$" + $("#slider-range").slider("values", 0) +
          " - $" + $("#slider-range").slider("values", 1));

    }).finally(function () {
        /* called no matter success or failure*/
      
        $("#topDivWebsite").show();
        $("#SearchingProgressBar").hide();
    });

    $scope.filterByStop = function (flights) {
        return $scope.selectedStop[flights.stop] || noFilter($scope.selectedStop);
    };
    $scope.filterByAirline = function (flights) {
        return $scope.selectedAirline[flights.allAirline] || noFilter($scope.selectedAirline);
    };
    $scope.filterDepTimeDur = function (flights) {
        return $scope.selectedDepTime[flights.depTimeDur] || noFilter($scope.selectedDepTime);
    };
    $scope.filterArrTimeDur = function (flights) {
        return $scope.selectedArrTime[flights.arrTimeDur] || noFilter($scope.selectedArrTime);
    };
    $scope.filterByGds = function (flights) {
        return $scope.SelectedGds[flights.GDS] || noFilter($scope.SelectedGds);
    };
    $scope.filterByFareType = function (flights) {
        return $scope.SelectedFareType[flights.fareType] || noFilter($scope.SelectedFareType);
    };

    function noFilter(filterObj) {
        for (var key in filterObj) {
            if (filterObj[key]) {
                return false;
            }
        }
        return true;
    }
    $scope.itemPerPageDiv = true;
    $scope.statusChecked1 = function (a, b, listSize) {
        if (listSize > 10) {
            $scope.itemPerPageDiv = true;
        }
        else {
            $scope.itemPerPageDiv = false;
        }
    }
    $scope.clearFilter = function () {
        $scope.selectedStop = [];
        $scope.selectedAirline = [];
        $scope.airlineM = "0";
        $scope.stopM = "0";
        $scope.nearBy = "0";
        $scope.flexiFare = "0";
        $scope.allFare = "true";
        $scope.matrixActive = '00';
        $scope.selectedDepTime = [];
        $scope.selectedArrTime = [];

        $scope.SelectedGds = [];
        $scope.SelectedFareType = [];

        $scope.lower_price_bound = $scope.lower_price_slider;
        $scope.upper_price_bound = $scope.upper_price_slider;
        var $slider = $("#slider-range");
        $slider.slider("values", 0, $scope.lower_price_slider);
        $slider.slider("values", 1, $scope.upper_price_slider);
        $("#amount").val("$" + $("#slider-range").slider("values", 0) + " - $" + $("#slider-range").slider("values", 1));
    }
    $scope.clearTabFilter = function () {
        $scope.nearBy = "0";
        $scope.flexiFare = "0";
        $scope.allFare = "0";
    }

    $scope.MAirline = function (airline, stop) {
        $scope.clearFilter();
        $scope.allFare = "0";
        $scope.airlineM = airline;
        $scope.stopM = stop;
        $scope.matrixActive = airline + stop;
    }
    $scope.AirlineMatrixFilter = function (result) {
        if ($scope.airlineM != "0") {
            if ($scope.stopM == "0") {
                if (result.allAirline == $scope.airlineM) {
                    return result;
                }
            }
            else {
                if ($scope.stopM == "1") {
                    if (result.allAirline == $scope.airlineM && result.stop == "1") {
                        return result;
                    }
                }
                else if ($scope.stopM == "2") {
                    if (result.allAirline == $scope.airlineM && result.stop == "2") {
                        return result;
                    }
                }
                else if ($scope.stopM == "3") {
                    if (result.allAirline == $scope.airlineM && result.stop == "3") {
                        return result;
                    }
                }
            }
        }
        else {
            return result;
        }
    }

    $scope.FlexiFareT = function (ff) {
        $scope.clearFilter();
        $scope.flexiFare = ff;
        $scope.allFare = "0";
    }
    $scope.FlexiFareFilter = function (result) {
        if ($scope.flexiFare != "0") {
            if (result.isFlexiFare == $scope.flexiFare) {
                return result;
            }
        }
        else {
            return result;
        }
    }
    $scope.NearByT = function (n) {
        $scope.clearFilter();
        $scope.nearBy = n;
        $scope.allFare = "0";
    }
    $scope.NearByFilter = function (result) {
        if ($scope.nearBy != "0") {
            if (result.isNearBy == $scope.nearBy) {
                return result;
            }
        }
        else {
            return result;
        }
    }

    $scope.AllFareT = function (af) {
        $scope.clearFilter();
        $scope.allFare = af;
    }
    $scope.AllFareFilter = function (result) {
        if ($scope.allFare != "0") {
            if (result.isAllFare == $scope.allFare) {
                return result;
            }
        }
        else {
            return result;
        }
    }


    $scope.clearMatrixFilter = function () {
        $scope.airlineM = "0";
    }

    $scope.priceRange = function (result) {
        return (parseInt(result.price) >= parseInt($scope.lower_price_bound) && parseInt(result.price) <= parseInt($scope.upper_price_bound));
    };

    $scope.getBaggage = function (aCode) {
        $http.get("/Search/getAirlineBaggage?id=" + aCode)
        .then(function (response) {
            $scope.bagg = response.data;
        });
    };
    $scope.loadMore = function () {
        if ($scope.numberToDisplay + 1 < 500) {
            $scope.numberToDisplay += 1;
        } else {
            $scope.numberToDisplay = 500;
        }
    };
    $scope.ReturnInt = function (n) {
        return parseInt(n);
    }
});