function m_option(value, text) {
    var option = document.createElement('option');
    option.value = value;
    option.innerHTML = text;
    return option;
}

Event.observe(window, 'load', function() {
    var post = 'action=get&type=continents';
    /**
     * Get continents and put them in first select
     */
     /*
    new Ajax.Request('includes/ajax.php', {
        method: 'post',
        postBody: post,
        onSuccess: function(transport) {
            var search_continent = $('search_continent');
            search_continent.options.length = 0;
            // If success, move node to new parent in frontend
            var option = document.createElement('option');
            option.value = 0;
            option.innerHTML = 'Kies een continent';
            search_continent.appendChild(option);
            if (transport.responseText != 'failed') {
                search_continent.innerHTML += transport.responseText;
            }
            search_continent.style.display = 'block';
        }
    });
    */
    /**
     * On first select click, get countries and populate second select
     */
    var contclick = {
        generic: function(event) {
            var val = $('search_continent').value;
            var search_country = $('search_country');
            search_country.options.length = 0;
            if (val != 0) {
                var post = 'action=get&type=countries&parent_id='+val;
                // Ajax call to move the item in backend
                new Ajax.Request('includes/ajax.php', {
                    method: 'post',
                    postBody: post,
                    onSuccess: function(transport) {
                        var option = document.createElement('option');
                        option.value = 0;
                        option.innerHTML = 'Alle landen';
                        search_country.appendChild(option);
                        // If success, move node to new parent in frontend
                        if (transport.responseText != 'failed') {
                            var json = eval('('+transport.responseText+')');
                            var countries = json.countries;
                            for (var i = 0; i < countries.length; i++) {
                                var country = countries[i];
                                search_country.appendChild(m_option(country.value, country.text));
                            }
                        }
                        search_country.style.display = 'block';
                        $('search_category').style.display = 'block';
                        populateCategories(val);
                    }
                });
            } else {
                search_country.style.display = 'none';
                search_country.value = 0;
                var sc = $('search_city');
                sc.style.display = 'none';
                sc.value = 0;
                var search_category = $('search_category');
                search_category.value = 0;
                search_category.style.display = 'none';
            }
        }
    };
    Event.observe('search_continent', 'change', contclick.generic.bindAsEventListener(contclick));

    /**
     * On second select click, get cities and populate third select
     */
    var countclick = {
        generic: function(event) {
            var val = $('search_country').value;
            var search_city = $('search_city');
            search_city.options.length = 0;
            if (val != 0) {
                var post = 'action=get&type=cities&parent_id='+val;
                // Ajax call to move the item in backend
                new Ajax.Request('includes/ajax.php', {
                    method: 'post',
                    postBody: post,
                    onSuccess: function(transport) {
                        var option = document.createElement('option');
                        option.value = 0;
                        option.innerHTML = 'Alle steden en streken';
                        search_city.appendChild(option);
                        // If success, move node to new parent in frontend
                        if (transport.responseText != 'failed') {
                            var json = eval('('+transport.responseText+')');
                            var cities = json.cities;
                            for (var i = 0; i < cities.length; i++) {
                                var city = cities[i];
                                search_city.appendChild(m_option(city.value, city.text));
                            }
                        }
                        search_city.style.display = 'block';
                        populateCategories(val);
                    }
                });
            } else {
                search_city.value = 0;
                search_city.style.display = 'none';
                populateCategories($('search_continent').value);
            }
        }
    };
    Event.observe('search_country', 'change', countclick.generic.bindAsEventListener(countclick));

    Event.observe('search_city', 'change', function () {
        var val = $('search_city').value;
        if (val == 0) {
            val = $('search_country').value;
        }
        populateCategories(val);
    });;

    Event.observe('widget_button', 'click', function () {
        var tmp = $('search_category').value;
        var value = '';
        if (tmp != 0) {
            value = tmp;
        }
        tmp = $('search_city').value;
        if (tmp != 0) {
            value += tmp+'/';
        }
        else {
            tmp = $('search_country').value;
            if (tmp != 0) {
                value += tmp+'/';
            }
            else {
                tmp = $('search_continent').value;
                if (tmp != 0) {
                    value += tmp+'/';
                }
            }
        }
        var base = document.getElementsByTagName('base')[0].getAttribute('href');
        document.location = base + value;
    });
});

function populateCategories(world_id) {
    var post = 'action=categories&world_id='+world_id;
    // Ajax call to move the item in backend
    new Ajax.Request('includes/ajax.php', {
        method: 'post',
        postBody: post,
        onSuccess: function(transport) {
            var search_category = $('search_category');
            var option = document.createElement('option');
            option.value = 0;
            option.innerHTML = 'Alle categorieën';
            search_category.options.length = 0;
            search_category.appendChild(option);
            var options = transport.responseXML.getElementsByTagName('option');
            for (var i = 0; i < options.length; i++) {
                var option = options[i];
                search_category.appendChild(m_option(option.getAttribute('value'), option.firstChild.nodeValue));
            }
        }
    });
}
