﻿$.fn.dropdown = function()
{
    var args =
    {
        list: null
        ,valueChanged: null
    }

    if(arguments.length > 0)
        args = arguments[0];

    $(this).click(function(event)
    {
        var menu = $(this).find('ul:first');
        
        if (menu.css('display') == 'none')
        {
            $(document).find('.dropdown-wrapper-selected').each(function()
            {
                $(this).attr('class', 'dropdown-wrapper').find('ul:first').hide();
            });
            
            menu.empty();
            
            var list = args.list.get_list();
            var available = args.list.get_availableCount();
            
            if(available == 0)
                menu.hide();
            else
            {
                for(var i = 0; i < list.length; i++)
                {
                    var item = list[i];
                    if(item.selected != true)
                        menu.append('<li phone="' + item.number + '">' + item.description + '</li>');
                }
                
                $(this).find('li').click(function(event)
                {
                    event.stopPropagation();
                    
                    var currentValue = $(this).parent().parent().find('input:hidden').attr('value');
                    var newValue = $(this).attr('phone');
                    
                    $(this).parent().parent().find('div:first').html($(this).html());
                    $(this).parent().parent().find('input:hidden').attr('value', $(this).attr('phone'));
                    $(this).parent().slideUp(10);
                    $(this).parent().parent().attr('class', 'dummy').attr('class', 'dropdown-wrapper');
                    
                    //if(currentValue != null)
                    //    Numbers.set_selected(currentValue, false);
                    
                    //Numbers.set_selected(newValue, true);
                    if(args.valueChanged != null)
                        args.valueChanged(currentValue, newValue);
                });
                
                if($(this).find('li').size() > 7)
                {
					menu.height(200);
					menu.css('overflow', 'auto');
				}
                
                menu.css({ left: $(this).position().left
                  , top: $(this).position().top + $(this).height() + 1
                  , width: $(this).width() - 21}).slideDown(100);
            }
        }
        else
        {
            menu.slideUp(100);
        }
        $(this).attr('class', 'dropdown-wrapper-selected');
        
        // prevent event to reach document handler and close this menu
        event.stopPropagation();
    });
    
    $(this).hover(function()
    {
        var menu = $(this).find('ul:first');
        if (menu.css('display') == 'none')
        {
            $(this).find('div:first').css('width', $(this).width() - 23);
            $(this).attr('class', 'dropdown-wrapper-hover');
        }
    }
    ,function()
    {
        var menu = $(this).find('ul:first');
        if (menu.css('display') != 'block')
            $(this).attr('class', 'dropdown-wrapper');
    });
}

$(document).click(function()
{
    $(document).find('.dropdown-wrapper-selected').each(function()
    {
        $(this).attr('class', 'dropdown-wrapper').find('ul:first').hide();
    });
});
