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

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

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

    this.init = function()
    {
        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 searchboxlinkmore = $('#toggle-search-more');
            var searchboxlinkless = $('#toggle-search-less');

            if (tripfinderlink.size()) {
                tripfinderlink.click(function()
                    {
                        $('.tripfinder').toggle('slide', { direction: 'right' }, 400);
                        self.visible = !self.visible;
                    }
                );
            }

            if (newslink.size()) {
                newslink.click(function(e)
                    {
                        if (self.visible) {
                            $('.tripfinder').toggle('slide', { direction: 'right' }, 400);
                            self.visible = !self.visible;
                        }
                        return false;
                    }
                );
            }

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

        }
    }

    this.onChange = function(e)
    {
        // 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 && 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>');
        }
    }
}