$(document).ready( function() {
    $('.html-options').each( function() {

        // Creating container
        var div = document.createElement('div');
        div.className = 'custom-select';
        div = $(div).insertBefore( $(this));

        // Creating selected element
        var a = document.createElement('a');
        a.className = 'selected';

        // Getting selected option
        var option = $('option:selected', $(this));
        var index = $('option', $(this)).index(option);

        // Getting image for selected option
        var images = $('ul#html-' + $(this).attr('name') + '-images');
        var image = $('li', images).eq(index).html();

        // Adding selected element to container
        a.innerHTML = image + $('option:selected', $(this)).text();
        a.href = $('option:selected', $(this)).val();
        var a_selected = $(a).appendTo(div);

        // Creating a list of other opitons
        var options = document.createElement('ul');
        options.className = 'select-options';
        options = $(options).appendTo(div);

        var self = $(this);

        $('option:not(:selected)', $(this)).each( function() {
            var index = $('option', self).index($(this));

            var li = document.createElement('li');
            li = $(li).appendTo(options);

            var image = $('li', images).eq(index).html();
            var a = document.createElement('a');
            a.innerHTML = image + $(this).text();
            a.href = $(this).val();
            $(a).appendTo(li);
        });

        // Hiding current select box
        $(this).hide();

        // Show/Hide select options
        $(a_selected).click( function() {
            return !options.toggle();
        });

        // Change select option value
        $('a', options).click( function() {

            var ins_html = a_selected.html();
            var ins_href = a_selected.attr('href');

            a_selected.html( $(this).html());
            var navigate_to = $(this).attr('href');
            a_selected.attr( 'href', navigate_to);

            $(this).html( ins_html);
            $(this).attr( 'href', ins_href);

            location.href = navigate_to;
            return !$(options).hide();
        });
    });
});
