;(function($) {

    var locationChooser = function (element, options) {
        var el = $(element);
        var obj = $(this);
        var defaults = {
            redirect_url: false
        };
        var settings = $.extend(defaults, options || {});

        function onChange() {
            if( ! settings.redirect_url) {
                settings.redirect_url = location.pathname+location.hash;
            } 

            $.blockUI({ message: '<h3>Please, wait...</h3>' });
            //alert('/user/toloc/'+this.value+'/'+ encodeURIComponent(settings.redirect_url));
            location.href = '/user/toloc/'+this.value+'/'+ encodeURIComponent(settings.redirect_url);
        }

        this.init = function() {
        	el.unbind('change').change(onChange);
        }

        this.setOptions = function(options) {
            if (typeof(options) === 'object') {
                $.extend(settings, options);
            }
        }
    };

    $.fn.locationChooser = function(options) {
        return this.each(function() {
            var element = $(this);
            if (element.data('locationChooser')) {
                return;
            }
            var lc = new locationChooser(this, options);
            lc.init();
            element.data('locationChooser', lc);
        });
    }

})(jQuery);
