(function($) {


    // Extended search control
    jQuery.fn.resolveExtendedSearch = function(options) {
        var defaults = {
            id: 'listing',
            caption: 'Waar wilt u heen?',
            buttonCaption: 'Zoek reizen',
            dropDownCaption: 'of kies uit een lijst',
            formName: 'search_form',
            submitUrl: '#',
            searchFieldName: 'destination',
            entries: []
        };
        options = $.extend(defaults, options);

        return this.each(function() {
            var $root = $(this);
            ;


            var dropdown = $(document.createElement('div'))
              .addClass('dropdown')
              .appendTo($root);

            var dropdownLink = $(document.createElement('a'))
              .attr('href', 'javascript: void(0);')
              .html(options.dropDownCaption)
              .appendTo(dropdown)

            dropdownLink.click(function() {
                $("div#listing").slideDown(250);
            });

            //            form.appendTo($root);

            // INFO, GR: Fix to align the button with the input field.
            $('.submit_button').css('top', $('div.destination_container').attr('offsetTop') - $('.submit_button').attr('offsetTop'));
            // INFO, GR: Fix for search caption in IE Compatibility Mode
            if ($('div.destination_container').attr('offsetTop') >= 175) {
                $('.searcher').find('div.caption').css('top', '-1px');                
            }

            resolveListing();

            function resolveListing() {
                var listingOptions = options.listings;
                var i = 0;
                var j = 0;
                var k = 0;
                var entry = null;
                var destination = null;

                var listing = $(document.createElement('div'))
                  .attr('id', options.id)
                  .addClass(options.id);

                var closeContainer = $(document.createElement('div'))
                  .addClass(options.id + '_close_banner')
                  .appendTo(listing);

                var closeLink = $(document.createElement('a'))
                  .html(listingOptions.close)
                  .appendTo(closeContainer);

                closeLink.click(function() {
                    listing.slideUp(250);
                });

                $(document.createElement('div'))
                  .addClass(options.id + '_header')
                  .html(listingOptions.caption)
                  .appendTo(listing);

                var table = $(document.createElement('table'))
                  .appendTo(listing);

                var row = $(document.createElement('tr'))
                  .appendTo(table);

                for (i; i < listingOptions.columns.length; i++) {
                    entry = listingOptions.columns[i];

                    var cell = $(document.createElement('td'))
                      .attr('vAlign', 'top')
                      .css('width', Math.floor(100 / listingOptions.columns.length) + '%')
                      .appendTo(row);

                    j = 0;
                    for (j; j < entry.sections.length; j++) {
                        section = entry.sections[j];

                        $(document.createElement('div'))
                          .addClass(options.id + '_sub_header')
                          .html(section.caption)
                          .appendTo(cell);

                        var list = $(document.createElement('table'))
                          .appendTo(cell);

                        k = 0;
                        for (k; k < section.destinations.length; k++) {
                            destination = section.destinations[k];

                            var item = $(document.createElement('tr'))
                              .appendTo(list);

                            var d = $(document.createElement('td'))
                              //.css('width', '50%')
                              .appendTo(item);

                            $(document.createElement('a'))
                              .attr('href', destination.countryUrl)
                              .html(destination.country)
                              .appendTo(d);

                            if (destination.city != null) {
                                var cities = destination.city.split(';');
                                var citieUrls = destination.cityUrl.split(';');
                                var separator = '';
                                var e = $(document.createElement('td'))
                                            .attr('valign', 'top')
                                            .appendTo(item);
                                for (n = 0; n < cities.length; n++) {
                                    if (citieUrls.length > n) {
                                        $(document.createElement('span'))
                                       .html(separator)
                                       .appendTo(e);
                                        $(document.createElement('a'))
                                       .attr('href', citieUrls[n])
                                       .html(cities[n])
                                       .appendTo(e);
                                        separator = ', ';
                                    }
                                }
                            }
                        }
                    }
                }

                listing.hide();
                listing.appendTo($root);
            }
        });

    };

})(jQuery);

