(function($){
    $.selectNav = function(el, options){
        var base = this;
        var $el = $(el);
        base.init = function(){
            // Hide original list
            $el.hide();
            var id = $el.attr('id');
            var classes = $el.attr('class');

            // Build the select forms html
            var selectHtml = '<form id="'+id+'" class="'+classes+'"><select name="'+id+'">\n';
            $el.find('a').each(function() {
                selectHtml += '<option value="'+$(this).attr('href')+'">'+this.innerHTML+'</option>\n';
            });
            selectHtml += '</select></form>\n';

            // find parent and replace the list with select
            var p = $el.parent();
            $el.replaceWith(selectHtml);

            var select = p.find('select');
            // set value to current url
            select.val(window.location.pathname);
            if(select.val() != window.location.pathname) {
                // Hack for opera unescaping pathname
                select.val(encodeURI(window.location.pathname));
            }

            // Register change event
            select.change(function(e) {
                window.location = $(this).val();
            });
        };
        // Run init
        base.init();
    };
    $.fn.selectNav = function(options){
        return this.each(function(){
            (new $.selectNav(this, options));
        });
    };
})(jQuery);

