var zielegal = {
    addToolTip : function() {
        jQuery("p.hint").each(function() {
            var title = jQuery(this).html();
            jQuery(this).attr('title', title);
        }); 
    },

    loadPage : function(url, history) {
        if (arguments.length == 1 || history) {
            window.location.href = url;
        } else {
            location.replace(url);
        }
    },
    
    loadList : function(none) {
        none = (none == null || none == false) ? false : true;
        url = '/reisen' + this.getQuery(none);
        this.loadPage(url);
    },
    
    updateList : function(none) {
        none = (none == null || none == false) ? false : true;
        url = '/reisen/format/html' + this.getQuery(none);
        jQuery('.detail-right').load(url);    
    },

    updateMixer : function() {
        var url = '/ajax/mixer/format/json' + this.getQuery();
        jQuery.getJSON(url, function(data) {
            jQuery('span.country').html(data.countries);
        });
    },

    formatPrice : function(price) {
        price = price.toString();
        for (i = price.length - 3; i > 0; i -= 3) {
            price = price.substring(0, i) + '.' + price.substring(i);
        }
        price = price + ' €';
        return price;
    },

    getQuery : function(none) {
        var categories = '/categories/' 
                       + jQuery('#category-1').val() + ','
                       + jQuery('#category-2').val() + ','
                       + jQuery('#category-3').val() + ','
                       + jQuery('#category-4').val() + ','
                       + jQuery('#category-5').val();

        var price = '';
        if (jQuery('#price').length != 0) {
            price = '/price/' + jQuery('#price').val();
        }

        var sep = '';
        var selftravels = '';
        if (jQuery('#selftravel-no').is(':checked')) {
            selftravels += sep + '0';
            sep = ',';
        }
        if (jQuery('#selftravel-yes').is(':checked')) {
            selftravels += sep + '1';
            sep = ',';
        }
        if (none == true && selftravels == '') {
            selftravels = 'none';
        }
        if (selftravels != '') {
            selftravels = '/selftravels/' + selftravels + '/';
        }

        sep = '';
        var climates = '';
        if (jQuery('#climate-cold').is(':checked')) {
            climates += sep + 'cold';
            sep = ',';
        }
        if (jQuery('#climate-mild').is(':checked')) {
            climates += sep + 'mild';
            sep = ',';
        }
        if (jQuery('#climate-warm').is(':checked')) {
            climates += sep + 'warm';
            sep = ',';
        }
        if (none == true && climates == '') {
            climates = 'none';
        }
        if (climates != '') {
            climates = '/climates/' + climates;
        }

        sep = '';
        var seasons = '';
        if (jQuery('#season-spring').is(':checked')) {
            seasons += sep + 'spring';
            sep = ',';
        }
        if (jQuery('#season-summer').is(':checked')) {
            seasons += sep + 'summer';
            sep = ',';
        }
        if (jQuery('#season-autumn').is(':checked')) {
            seasons += sep + 'autumn';
            sep = ',';
        }
        if (jQuery('#season-winter').is(':checked')) {
            seasons += sep + 'winter';
            sep = ',';
        }
        if (none == true && seasons == '') {
            seasons = 'none';
        }
        if (seasons != '') {
            seasons = '/seasons/' + seasons;
        }

        var duration = '';
        if (jQuery('#duration').length != 0) {
            duration = '/duration/' + jQuery('#duration').val();
        }

        sep = '';
        var continents = '';
        if (jQuery('#continent-eu').is(':checked')) {
            continents += sep + 'eu';
            sep = ',';
        }
        if (jQuery('#continent-na').is(':checked')) {
            continents += sep + 'na';
            sep = ',';
        }
        if (jQuery('#continent-sa').is(':checked')) {
            continents += sep + 'sa';
            sep = ',';
        }
        if (jQuery('#continent-af').is(':checked')) {
            continents += sep + 'af';
            sep = ',';
        }
        if (jQuery('#continent-as').is(':checked')) {
            continents += sep + 'as';
            sep = ',';
        }
        if (jQuery('#continent-oc').is(':checked')) {
            continents += sep + 'oc';
            sep = ',';
        }
        if (none == true && flags == '') {
            continents = 'none';
        }
        if (continents != '') {
            continents = '/continents/' + continents;
        }
        
        sep = '';
        var flags = '';
        if (jQuery('#single').is(':checked')) {
            flags += sep + 'single';
            sep = ',';
        }
        if (jQuery('#group').is(':checked')) {
            flags += sep + 'group';
            sep = ',';
        }
        if (jQuery('#short_distance').is(':checked')) {
            flags += sep + 'short_distance';
            sep = ',';
        }
        if (jQuery('#long_distance').is(':checked')) {
            flags += sep + 'long_distance';
            sep = ',';
        }
        if (jQuery('#eco').is(':checked')) {
            flags += sep + 'eco';
            sep = ',';
        }
        if (jQuery('#family').is(':checked')) {
            flags += sep + 'family';
            sep = ',';
        }
        if (none == true && flags == '') {
            flags = 'none';
        }
        if (flags != '') {
            flags = '/flags/' + flags;
        }

        var query = categories 
                  + price 
                  + climates 
                  + seasons 
                  + duration 
                  + flags
                  + continents
                  + selftravels;
        return query;
    },

    initSlider : function(slider, input, orientation, val, callback) {
        if (val < 0 || val > 2) {
            val = 1;
        }
        /* http://bugs.jqueryui.com/ticket/4168 */
        jQuery(slider).slider({
            orientation : orientation,
            range : 'min',
            min : 0,
            max : 2,
            step : 1,
            value : val,
            change : function(event, ui) {
                callback();
            },
            slide : function(event, ui) {
                jQuery(input).val(ui.value);
            }
        });
    },

    initSliders : function(orientation, callback) {
        for ( var i = 1; i <= 5; i++) {
            var val = jQuery('#category-' + i).val();
            this.initSlider('#slider-' + orientation + '-' + i, 
                            '#category-' + i, 
                            orientation, val, callback);
        }
    }
};

