function TripFinder(id)
{
    this.id = id;
    this.timeoutId = null;
    this.regions = [];
    this.countries = {};
    this.visible = false;
    this.currentType = '';
    this.searchMenuItem = '';

    this.getObject = function()
    {
        return $('#' + this.id);
    }

    this.setRegionCountries = function(regionId, countries)
    {
        this.regions[regionId] = countries;
    }

    this.setCurrentType = function(type)
    {
        this.currentType = type;
    }

    this.setSearchMenuItem = function(id)
    {
        this.searchMenuItem = id;
    }

    this.init = function()
    {
        // FIXME: make the toggle-functions real toggle-functions
        var tripfinder = this.getObject();
        if (tripfinder.size()) {

            var self = this;

            $('#country option').each(function()
                {
                    value = this.value;

                    if (!value) value = 0;

                    self.countries[value] = this.innerHTML;
                }
            );

            tripfinder.find('.arrangementfinder-button').after('<p id="tripfinder-results"><span></span></p>');
            tripfinder.find(':input').bind('change', function(e) { self.onChange(e); });
            tripfinder.find(':input:first').trigger('change');

            // initialize links
            var newslink = $('#inline-list-link');
            var tripfinderlink = $('#tripfinder-link');
            var mdlink = $('#md-login-link');
            var searchboxlinkmore = $('#toggle-search-more');
            var searchboxlinkless = $('#toggle-search-less');
            var tabSelected = getCookie('portal_tab_selected');

            var quicksearch = $('#quicksearch-arrangementfinder');
            var showsearchoverlay = $('#'+this.searchMenuItem);
            var searchoverlay = $('#tripfinder-overlay');
            var arrangementfinderClose = $('#arrangementfinder-close');

            // do this only at highest level
            if (tabSelected != null && tabSelected != "" && this.currentType == "portal") {
                if (tabSelected == 'tripfinder') {
                    $('.tripfinder').css('z-index', '4');
                    $('.tripfinder').toggle('slide', { direction: 'right' }, 400);
                    $('#inline-list-header').removeClass("selected");
                    $('.md-login').removeClass("selected");
                    $('#tripfinder-header').addClass("selected");
                } else if (tabSelected == 'md-login') {
                    $('#loginform').css('z-index', '4');
                    $('#loginform').toggle('slide', { direction: 'right' }, 400);
                    $('#tripfinder-header').removeClass("selected");
                    $('#inline-list-header').removeClass("selected");
                    $('.md-login').addClass("selected");
                }
            }

            if (tripfinderlink.size()) {
                tripfinderlink.click(function()
                    {
                        $('.tripfinder').css('z-index', '4');
                        $('#loginform').css('z-index', '3');
                        if ($('#loginform').css('display') == 'block') {
                            $('#loginform').toggle('slide', { direction: 'right' }, 800);
                        }
                        if ($('.tripfinder').css('display') == 'none') {
                            $('.tripfinder').toggle('slide', { direction: 'right' }, 400);
                        }
                        $('#inline-list-header').removeClass("selected");
                        $('.md-login').removeClass("selected");
                        $('#tripfinder-header').addClass("selected");

                        return false;
                    }
                );
            }

            if (newslink.size()) {
                newslink.click(function(e)
                    {
                        if ($('.tripfinder').css('display') == 'block') {
                            $('.tripfinder').toggle('slide', { direction: 'right' }, 800);
                        }
                        if ($('#loginform').css('display') == 'block') {
                            $('#loginform').toggle('slide', { direction: 'right' }, 800);
                        }
                        $('.md-login').removeClass("selected");
                        $('#tripfinder-header').removeClass("selected");
                        $('#inline-list-header').addClass("selected");

                        return false;
                    }
                );
            }

            if (mdlink.size()) {
                mdlink.click(function(e)
                    {
                        $('.tripfinder').css('z-index', '3');
                        $('#loginform').css('z-index', '4');
                        if ($('.tripfinder').css('display') == 'block') {
                            $('.tripfinder').toggle('slide', { direction: 'right' }, 800);
                        }
                        if ($('#loginform').css('display') == 'none') {
                            $('#loginform').toggle('slide', { direction: 'right' }, 400);
                        }
                        $('#tripfinder-header').removeClass("selected");
                        $('#inline-list-header').removeClass("selected");
                        $('.md-login').addClass("selected");

                        return false;
                    }
                );
            }


            // TODO: change this to some sort of toggle
            if (searchboxlinkmore.size()) {
                searchboxlinkmore.click(function()
                    {
                        $(".tripfinder-homepage dl").animate({
                            height: "158px"
                          }, 500 );
                        searchboxlinkmore.addClass("nodisplay");
                        searchboxlinkless.removeClass("nodisplay");
                        quicksearch.removeClass("nodisplay");
                        return false;
                    }
                );
            }

            if (searchboxlinkless.size()) {
                searchboxlinkless.click(function()
                    {
                        $(".tripfinder-homepage dl").animate({
                            height: "60px"
                          }, 500 );
                        searchboxlinkless.addClass("nodisplay");
                        searchboxlinkmore.removeClass("nodisplay");
                        quicksearch.addClass("nodisplay");
                        return false;
                    }
                );
            }

            if (this.currentType == 'homepage') {
                if (showsearchoverlay.size()) {
                    showsearchoverlay.click(function()
                        {
                            $(".tripfinder-homepage dl").animate({
                                height: "158px"
                              }, 500 );
                            searchboxlinkmore.addClass("nodisplay");
                            searchboxlinkless.removeClass("nodisplay");
                            quicksearch.removeClass("nodisplay");
                            return false;
                        }
                    );
                }
            } else if (this.currentType != 'arrangementfinder_results') {
                if (showsearchoverlay.size()) {
                    showsearchoverlay.click(function()
                        {
                            quicksearch.removeClass("nodisplay");
                            $('#tripfinder-overlay').toggle('slide', { direction: 'left' }, 400);
                            return false;
                        }
                    );
                }
                if (arrangementfinderClose.size()) {
                    arrangementfinderClose.click(function()
                        {
                            quicksearch.removeClass("nodisplay");
                            $('#tripfinder-overlay').toggle('slide', { direction: 'left' }, 400);
                            return false;
                        }
                    );
                }
            } else if (this.currentType == 'arrangementfinder_results') {
                if (showsearchoverlay.size()) {
                    showsearchoverlay.click(function()
                        {
                            $('html, body').animate({
                                scrollTop: $("#arrangementfinder").offset().top
                            }, 300);
                            return false;
                        }
                    );
                }
            }
        }
    }

    this.onChange = function(e)
    {
        // enable/disable theme filter
        if ($('#destinationtype_id').val() == 5) {
            $('#theme').removeAttr('disabled');
        } else {
            $('#theme').attr('disabled', 'disabled');
        }

        // change countries values
        if ($(e.target).attr('name') == 'region_id') {

            var country = $('#country');
            country.empty();

            var regionId = $('#region_id').val();
            if (regionId in this.regions) {

                // always add first value
                country.append('<option value="">' + this.countries[0] + '</option>');

                for (var i = 0; i < this.regions[regionId].length; i++) {
                    country.append('<option value="' + this.regions[regionId][i] + '">' + this.countries[this.regions[regionId][i]] + '</option>');
                }
            } else {
                for (var a in this.countries) {
                    country.append('<option value="' + a + '">' + this.countries[a] + '</option>');
                }
            }
        }

        // call submit handler
        var self = this;
        if (this.timeoutId) clearTimeout(this.timeoutId);
        this.timeoutId = setTimeout(function() { self.submit(); }, 200);
    }

    this.submit = function()
    {
        var tripfinder = this.getObject();
        if (tripfinder.size()) {

            var url = tripfinder.attr('action') + '?output=json&pagesize=0';

            // add all inputs to url
            tripfinder.find(':input').each(function()
                {
                    var input = $(this);
                    var name = input.attr('name');
                    var val = input.val();

                    if (name == 'special_price') {
                        if (input.attr('checked') === true) {
                            url += '&' + input.attr('name') + '=' + input.val();
                        }
                    } else {
                        if (name && val) url += '&' + input.attr('name') + '=' + input.val();
                    }
                }
            );

            $('#tripfinder-results span').html('<img class="png" src="/images/2009/default/portal/i_loading.png" alt=""/>');

            $.getJSON(url, this.updateResults);

        }
    }

    this.updateResults = function(result)
    {
        if (result.page_data.num_results !== undefined) {
            $('#tripfinder-results').html('<span>' + getText('DefaultPortalArrangementFinderNumResults').replace(/\%s/, result.page_data.num_results) + '</span>');
        }
    }
}