jQuery.fn.disableTextSelect = function()
{
    return this.each(function()
    {
        if( $.browser.mozilla )
        {
            $(this).css('MozUserSelect','none');
        }
        else if( $.browser.msie )
        {
            $(this).bind('selectstart',function(){
                return false;
            });
        }
        else
        {
            $(this).mousedown(function(){
                return false;
            });
        }
    });
}

jQuery.fn.selectControl = function(options)
{
    var settings = jQuery.extend(
    {
    }, options);

    return this.each(function()
    {
        this.target = $(this);
        this.buildControl = buildControl;
        this.initEvents = initEvents;
        this.controlClick = controlClick;
        this.elementClick = elementClick;
        this.createControlContainer = createControlContainer;
        this.createControlViewContainer = createControlViewContainer;
        this.createControlPopupContainer = createControlPopupContainer;
        this.createElementsContainer = createElementsContainer;
        this.createElements = createElements;
        this.selectActualElement = selectActualElement;
        this.buildControl();
    });

    function createControlContainer()
    {
        var randomId = Math.floor(Math.random() * 100000000000);
        this.target.after('<div id="selectcontrol' + randomId + '" class="selectcontrol"></div>');
        return $('#selectcontrol' + randomId);
    }

    function createControlViewContainer()
    {
        this.control.append('<div class="controlview"><div class="controlviewinner"></div>');
        return $('.controlview', this.control);
    }

    function createControlPopupContainer()
    {
        this.control.append('<div class="controlpopup" style="display:none"></div>');
        return $('.controlpopup', this.control);
    }

    function createElementsContainer()
    {
        this.control_popup.append('<div class="controlelements"></div>');
        return $('.controlelements', this.control);
    }

    function createElements()
    {
        for (var i = 0; i < settings.options.length; i++)
        {
            var newelement = $('<div class="element level' + settings.options[i].level + '">' + settings.options[i].value + '</div>');
            var element = newelement.appendTo(this.control_elements);
            element.data('value', settings.options[i].id);
            element.data('base', this);
            element.data('settings', settings);
            element.data('text', settings.options[i].value);
        }
        return $('.element', this.control);
    }

    function buildControl()
    {
        this.control = this.createControlContainer();
        this.control.disableTextSelect();
        this.control_view = this.createControlViewContainer();
        this.control_popup = this.createControlPopupContainer();
        this.control_elements = this.createElementsContainer();
        this.elements = this.createElements();
        this.body = $('body');
        this.initEvents();
        this.selectActualElement();
    }

    function initEvents()
    {
        this.control_view.data('controlpopup', this.control_popup);
        this.control_view.data('body', this.body);
        this.control_view.data('this', this);
        this.body.data('this', this);
        this.control_view.unbind();
        this.control_view.click(this.controlClick);
        this.elements.data('controlview', this.control_view);
        this.elements.unbind();
        this.elements.click(this.elementClick);
    }

    function controlClick(event)
    {
        event.stopPropagation();
        var control_popup = $(this).data('controlpopup');
        var body = $(this).data('body');
        var ref = $(this).data('this');
        control_popup.fadeIn('fast');
        body.data('controlpopup', control_popup);
        body.click(bodyClick);
        ref.control_view.unbind('click');
        ref.control_view.click(bodyClick);
    }

    function bodyClick(event)
    {
        $(this).unbind(event);
        var ref = $(this).data('this');
        ref.control_view.click(ref.controlClick);
        $(this).data('controlpopup').fadeOut('fast');
    }

    function elementClick(event)
    {
        $(this).data('base').target.attr('value', $(this).data('value'));
        $(this).data('base').selectActualElement();
        func = $(this).data('settings').callback;
        func($(this).data('value'));
    }

    function selectActualElement()
    {
        var value = this.target.attr('value');
        var selected = null;
        this.elements.each(function()
        {
            if ($(this).data('value') == value) selected = $(this);
        });
        if (selected == null) selected = $(this.elements.get(0));
        this.elements.removeClass("selected");
        selected.addClass('selected');
        $('.controlviewinner', this.control_view).html(selected.data('text'));
    }
}

