$(window).load(function(){ 

//������� ����������������� ������� ������ � �����
function reselect(select, addclass) {

  $(select).each(function(){
      
    addclass = typeof(addclass) != 'undefined' ? addclass : '';

    $(this).wrap('<div class="sel_wrap ' + addclass + '"/>');

    var sel_options = '';
    var sel_sd = false;
    var text='';
    $(this).children('option').each(function() {
        sel_sd = false;
        if($(this).attr('selected')=='selected'){
            sel_sd = true;
            text = $(this).html();
        }
        sel_options = sel_options + '<div class="sel_option'+(sel_sd==true ? " sel_ed" : "")+'" value="' + $(this).val() + '">' + $(this).html() + '</div>';
    });
    
    if(!text) text = $(this).children('option').first().html();

    var sel_imul = '<div class="sel_imul">\
                <div class="sel_selected">\
                    <div class="selected-text">' + text + '</div>\
                    <div class="sel_arraw"></div>\
                </div>\
                <div class="sel_options">' + sel_options + '</div>\
            </div>';

    $(this).before(sel_imul);
    
  });
}

reselect('#category_room', 'sec');

$('.sel_imul').live('click', function() {

    $('.sel_imul').removeClass('act');
    $(this).addClass('act');

    if ($(this).children('.sel_options').is(':visible')) {

        $('.sel_options').hide();

    }
    else {

        $('.sel_options').hide();
        $(this).children('.sel_options').show();

    }

});

$('.sel_option').live('click', function() {

    //������ �������� �� ���������
    var tektext = $(this).html();
    $(this).parent('.sel_options').parent('.sel_imul').children('.sel_selected').children('.selected-text').html(tektext);

    //���������� �������
    $(this).parent('.sel_options').children('.sel_option').removeClass('sel_ed');
    $(this).addClass('sel_ed');

    //������������� �������� ��� �������
    var tekval = $(this).attr('value');
    tekval = typeof(tekval) != 'undefined' ? tekval : tektext;
    $(this).parent('.sel_options').parent('.sel_imul').parent('.sel_wrap').children('select').children('option').removeAttr('selected').each(function() {
        if ($(this).val() == tekval) {
            $(this).attr('selected', 'select');
        }
    });
});

var selenter = false;
$('.sel_imul').live('mouseenter', function() {
    selenter = true;
});
$('.sel_imul').live('mouseleave', function() {
    selenter = false;
});
$(document).click(function() {
    if (!selenter) {
        $('.sel_options').hide();
        $('.sel_imul').removeClass('act');
    }
});

});
