﻿/// <reference path="jquery-1.4.2-vsdoc.js" />
(function ($) {
    var count = 0;


    $.AirportSearchControl = function () {

        this.invoke = function (containerId, airportId, searchPath, selectedValueID, EmptyOption, countryDDId, airportDDId, id, hdnCountryID, mapLinkId, mapLinkNotActiveId) {
            var $airport = $(airportId);
            //var $mapLink = $(mapLinkId);
            var _I = this;
            _I.getListPath = searchPath;

            if ($(selectedValueID).val() != "") {
                $(mapLinkNotActiveId).hide();
                $(mapLinkId).show();
            } else {
                $(mapLinkId).hide();
                $(mapLinkNotActiveId).show();
            }


            if ($.browser.SafariMobile || false) {
                $(airportDDId).removeClass("hidden");
                /*  $(airportDDId).html("<select id='" + id + "'><option>select airport</option></select>");*/
                $(airportId).addClass("hidden");
                $(countryDDId).bind("change", function () { GetAirports($(this).val(), airportDDId, _I.getListPath, hdnCountryID, selectedValueID, mapLinkId, mapLinkNotActiveId); });
                //alert($(mapLinkId));
                $(airportDDId).bind("change", function () { AirportDDSelect($(this).val(), airportDDId, airportId, _I.getListPath, selectedValueID, mapLinkId, mapLinkNotActiveId); });
                if ($(hdnCountryID).val() != "") {
                    GetAirports($(hdnCountryID).val(), airportDDId, _I.getListPath, hdnCountryID, selectedValueID, mapLinkId, mapLinkNotActiveId);
                }

            } else {

                $airport.VictorGlobalAutoComplete3(containerId + ' .ulContainer', _I.getListPath, selectedValueID, countryDDId, hdnCountryID, mapLinkId, mapLinkNotActiveId);

                //if (typeof (Sys) === 'undefined')


                $airport.click(function () {
                    if ($airport.autocomplete("widget").is(":visible")) {
                        $airport.autocomplete("close");
                        return false;
                    }
                    // pass empty string as value to search for, displaying all results
                    $airport.autocomplete("search", "");
                    $airport.focus();
                    return false;
                });

                $airport.blur(function (e, ui) {
                    if ($airport.val() == EmptyOption) {
                        $(selectedValueID).val("");
                    }
                    //if (jQuery.isFunction(jQuery.fn.UpdateFlightEstimatesSummaryRow)) {
                    //jQuery.fn.UpdateFlightEstimatesSummaryRow();
                    //}

                    return false;
                });
            }
        }
    }

    function AirportDDSelect(countryCode, airportDDId, airportId, getListPath, selectedValueID, mapLinkId, mapLinkNotActiveId) {
        var val = countryCode;
        var long = $(airportDDId + ' :selected').attr("long");
        var lat = $(airportDDId + ' :selected').attr("lat");
        $(airportId).val($(airportDDId).val());
        $(selectedValueID).val(val);
        $(mapLinkId).attr("code", val);
        $(mapLinkId).attr("long", long);
        $(mapLinkId).attr("lat", lat);

        $(mapLinkNotActiveId).hide();
        $(mapLinkId).show();

        if (jQuery.isFunction(jQuery.fn.UpdateFlightEstimatesSummaryRow)) {
            jQuery.fn.UpdateFlightEstimatesSummaryRow();
        }

        if ((window.location.toString().indexOf("airports") >= 0) || (window.location.toString().indexOf("hafen") >= 0)) {
            var url = "?IATA=" + val;
            location.assign(url);
        }
    }

    function GetAirports(countryCode, airportDDId, getListPath, hdnCountry, selectedValueID, mapLinkId, mapLinkNotActiveId) {
        $(airportDDId).get(0).options.length = 0;
        $(airportDDId).get(0).options[0] = new Option("Loading airports", "-1");
        var args = JSON.stringify({
            input: "",
            countrycode: countryCode,
            loginlang: $('#loginlang').html()
        });

        $.ajax({
            url: getListPath,
            data: args,
            success: function (data) {
                if (data.d.success == true) {
                    array = new Array();
                    count = 0;
                    $(airportDDId).get(0).options.length = 0;
                    $(airportDDId).get(0).options[0] = new Option("select airport", "-1");
                    for (var i in data.d.list) {

                        var airport = data.d.list[i];
                        var test = airport.Description + ' (' + airport.AirportCode + ')' + ', ' + airport.CITY_NAME

                        var o1 = new Option(test, airport.AirportCode);
                        $(airportDDId).get(0).options[$(airportDDId).get(0).options.length] = o1;
                        o1.setAttribute("lat", airport.NLATITUDE);
                        o1.setAttribute("long", airport.NLONGITUDE);

                    }
                    if ($(selectedValueID).val() != "") {
                        $(airportDDId).find("option:contains('" + $(selectedValueID).val() + "')").attr('selected', 'true');
                        //$(selectedValueID).val("");
                    }
                }
            },
            error: function (data) {
                alert("An Error occured - please try again later");
            }
        });
    }

    $.fn.VictorGlobalAutoComplete3 = function (containerIdOrClass, getListPath, selectedValueID, countryDDId, hdnCountry, mapLinkId, mapLinkNotActiveId) {
        return $(this).autocomplete({
            source: function (request, response) {
                var $this = $(this);
                var args = JSON.stringify({
                    input: request.term,
                    countrycode: $(countryDDId).find('option:selected').val(),
                    loginlang: $('#loginlang').html()
                });

                $.ajax({
                    url: getListPath,
                    data: args,
                    success: function (data) {
                        if (data.d.success == true) {
                            array = new Array();
                            count = 0;

                            for (var i in data.d.list) {
                                var airport = data.d.list[i];
                                array.push({
                                    value: airport.Description + ' (' + airport.AirportCode + ')' + ', ' + airport.CITY_NAME,
                                    label: airport.Description + ', ' + airport.CITY_NAME + ', ' + airport.Country,
                                    country: airport.Country,
                                    code: airport.AirportCode,
                                    latitude: airport.NLATITUDE,
                                    longitude: airport.NLONGITUDE
                                });

                            }
                            response(array);

                        }
                    }
                });

            },
            minLength: 0,
            focus: function (e, ui) {
                return false;
            },
            position: {
                my: 'left top',
                at: 'left bottom'
            },
            select: function (e, ui) {
                var val = ui.item.code;
                $(selectedValueID).val(val);
                if (jQuery.isFunction(jQuery.fn.UpdateFlightEstimatesSummaryRow)) {
                    jQuery.fn.UpdateFlightEstimatesSummaryRow();
                }

                $(mapLinkId).attr("code", ui.item.code);
                $(mapLinkId).attr("lat", ui.item.latitude);
                $(mapLinkId).attr("long", ui.item.longitude);

                if ((window.location.toString().indexOf("airports") >= 0) || (window.location.toString().indexOf("hafen") >= 0)) {
                    var code = ui.item.code;
                    var url = "?IATA=" + code;
                    location.assign(url);
                }


                $(this).removeClass("suggestText");

                $(mapLinkNotActiveId).hide();
                $(mapLinkId).show();


            },
            appendTo: containerIdOrClass
        })


        .each(function () {
            $(this).data('autocomplete')._renderItem = function (ul, item) {
                ul.addClass('globalList');
                var difCountry = false;
                if (ul.data('country') == null || ul.data('country') != item.country) {
                    difCountry = true;
                    ul.data('country', item.country);
                }
                ul.data('AirportCode', item.code);
                if (difCountry || ul.find('li').size() == 0) {
                    if (count != 0) {
                        ul.append('<li class="customListTitle">&nbsp;</li>');
                    }
                    count++;

                    ul.append('<li class="customListTitle">' + item.country + '</li>');

                }
                return $('<li>')
                    .data('item.autocomplete', item)
                    .append('<a href="">' + item.value + '</a>')
                    .appendTo(ul);
            }
        });
    }
})(jQuery)

