/*
 * Плагин замены select на ul-li аналоги, с гибкими настройками
 */
// Настройки
var jquerySelectDefaultArround = '<div class="jquerySelect"></div>';
var jquerySelectDefaultInput = '<span></span>';
var jquerySelectDefaultMenu = '<ul></ul>';
var jquerySelectDefaultMenuItemArround = '<li></li>';
var jquerySelectDefaultMenuItem = '<a></a>';
function jquerySelect(selector,arround,input,menu,menuItemArround,menuItem)
{
        if (arround == undefined) arround = jquerySelectDefaultArround;
        if (input == undefined) input = jquerySelectDefaultInput;
        if (menu == undefined) menu = jquerySelectDefaultMenu;
        if (menuItemArround == undefined) menuItemArround = jquerySelectDefaultMenuItemArround;
        if (menuItem == undefined) menuItem = jquerySelectDefaultMenuItem;
        
        $(selector).each(function(){                
                var option = [];
                var optionList = $(this).find('option');
                var key;
                var elem;                
                var id = Math.random().toString();                
                id = id.split('.').join('');
                
                while ($('#select'+id).attr('id'))
                {
                        id = Math.random().toString();                
                        id = id.split('.').join('');
                }
                
                var select = $(this);
                
                select.attr('id','select_'+id);
                
                elem = $(arround);
                elem.attr('id',id);
                
                $(this).after(elem);
                
                elem = $(input);
                elem.attr('id',id+'input');
                $('#'+id).append(elem);
                
                elem = $(menu);
                elem.attr('id',id+'menu');
                elem.css({
                        'z-index':'999999',
                        'display':'none',
                        'max-height':'200px',
                        'overflow-y':'auto',
                        'overflow-x':'none'
                });
                
                $('#'+id).append(elem);
                
                var elemFormBind = $('#'+id+'menu');
                //alert(elemFormBind[0]['id']);
                var offsetPos = offsetPosition(elemFormBind[0]);
                
                //alert(offsetPos[0]+'x'+offsetPos[1]);
                
                var i = 0;
                while (optionList[i])
                {
                        option[i] = {
                                'value': $(optionList[i]).attr('value'),
                                'html': $(optionList[i]).html(),
                                'selected': $(optionList[i]).attr('selected')
                        };
                        i++;
                }      
                
                $('#'+id+'input').html(option[0]['html']);
                
                for (i in option)
                {
                        if (option[i]['selected'])
                        {
                                $('#'+id+'input').html(option[i]['html']);
                        }
                                
                }
                
                $('#'+id+'input').attr('id','');
                
                for (key in option)
                {
                        elem = $(menuItem);
                        elem.html(option[key]['html'])
                        elem.attr('id','jsactelem');
                        
                        $('#'+id+'menu').append(elem);
                        
                        $('#jsactelem').wrap(menuItemArround);
                        $('#jsactelem').parent().attr('onclick',"$('#"+select.attr('id')+"').find('option').attr('selected',false); $('#"+select.attr('id')+" option:nth-child("+(parseInt(key)+1)+")').attr('selected','selected'); $('#"+select.attr('id')+"').change(); $(this).parent().parent().find($('span')).html($(this).html()); setTimeout(function(){$(this).parent().css('display','none');},100);");
                        $('#jsactelem').attr('id','false');
                }
                
                $('#'+id).click(function()
                {
                        if($(this).find($('ul')).css('display') == 'none')
                                $(this).find($('ul')).show();
                        else
                                $(this).find($('ul')).hide();
                });
                
                $('#'+id+'menu').attr('id','');
                
                select.css({'height':'0px', 'width':'0px', 'font-size':'0px', 'visibility':'hidden','float':'left'});
                select.bind('change',function(){
                        var optionList = $(this).find('option');
                        var i = 0;
                        
                        $('#'+id).find('span').html('');
                        while (optionList[i])
                        {
                                if (optionList[i].selected == 'true')
                                {
                                        alert($(optionList[i]).html());
                                        $('#'+id).find('span').html($(optionList[i]).html());
                                        break;
                                }
                                i++;
                        }
                        
                        if ($('#'+id).find('span').html() == '')
                        {
                                $('#'+id).find('span').html($(optionList[0]).html());
                        }
                });
        });
}
