jQuery(document).ready(function(){
    jQuery(".niceRadio").each(function() {
        changeRadioStart(jQuery(this));      
    }); 
    jQuery("input:checkbox").each(function(){
        $(this).wrap("<span class='niceCheck' />");
        $(this).parent().attr('style',$(this).attr('style'));
    });
    jQuery(".niceCheck").mousedown(function() {
        changeCheck(jQuery(this));
    });
    jQuery(".niceCheck").each(function() {
        changeCheckStart(jQuery(this));
    });
    jQuery("form input[type=reset]").click(function(){
        var c_form = this.form;
        $('input:checkbox',c_form).each(function(){
            $(this).parent().css("background-position","0 1px");
        });
    });
    jQuery("form input:text").addClass('inp');
    jQuery("form table.editlev1 select").addClass('inp');
    jQuery("form input:password").addClass('inp');
    jQuery("form input[type=reset]").addClass('but-form');
    jQuery("form input.butsave").click(function(){
        var c_form = this.form;
    //        $('input[type=submit]',c_form).trigger('click');
    });
    $('form a.select-file').file().choose(function(e, input){
        var href = $(this);
        var rel = href.attr('rel');
        var file = $('form input[name='+rel+']');
        var stylefile = file.attr('style');
        input.attr('name',rel);
        input.attr('style',stylefile);
        file.replaceWith(input);
        if(input.val() != ''){
            if($('img[rel='+rel+']').length > 0){
                $('img[rel='+rel+']').replaceWith('<div>\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043e. \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u044b, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435.</div>');
            } else {
                $('<div style="margin-bottom: 5px;">\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u043e. \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f.</div>').insertBefore(href);
            }
        }
        input.trigger("clear");
    });
    if(jQuery('font.notetext').length != 0){
        jQuery('font.notetext').parent().delay(1500).slideUp('slow');
    }
});

var init_form = function(){
    jQuery("form input:text").addClass('inp').css('padding','5px !important');
    jQuery("form textarea").addClass('inp').css('padding','5px !important');
    jQuery("form input:password").addClass('inp');
    jQuery("input:checkbox").each(function(){
        $(this).wrap("<span class='niceCheck' />");
        $(this).parent().attr('style',$(this).attr('style'));
        $(this).css('margin-left','-40px !important;');
    });
    jQuery(".niceCheck").each(function() {
        changeCheckStart(jQuery(this));
    });
    jQuery(".niceCheck").mousedown(function() {
        changeCheck(jQuery(this));
    });
    jQuery('form a.select-file').file().choose(function(e, input){
        var href = $(this);
        var rel = href.attr('rel');
        var file = $('form input[name='+rel+']');
        var stylefile = file.attr('style');
        input.attr('name',rel);
        input.attr('style',stylefile);
        file.replaceWith(input);
        if(input.val() != ''){
            if($('img[rel='+rel+']').length > 0){
                $('img[rel='+rel+']').replaceWith('<div>\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043e. \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u044b, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435.</div>');
            } else {
                $('<div style="margin-bottom: 5px;">\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u043e. \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f.</div>').insertBefore(href);
            }
        }
        input.trigger("clear");
    });
    $('#savebtn').addClass('but-form');
    $('#cancel').addClass('but-form');
}

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function changeCheck(el){
    var input = jQuery('input:checkbox',el).eq(0);
    if(!input.attr("checked")) {
        el.css("background-position","0 -23px");
        input.attr('checked','checked');
    } else {
        el.css("background-position","0 1px");	
        input.removeAttr('checked');
    }
    return true;
}

function changeCheckStart(el){
    var input = el.find("input").eq(0);
    if(input.prop("checked")) {
        el.css("background-position","0 -23px");	
    }
    return true;
}


	 
function changeRadio(el)
/*
	    функция смены вида и значения radio при клике на контейнер
*/
{
    var el = el,
    input = el.find("input").eq(0);
    var nm=input.attr("name");   
    jQuery(".niceRadio input").each(
     
        function() {
            if(jQuery(this).attr("name")==nm)
            {
                jQuery(this).parent().removeClass("radioChecked");
            }
 
        });                  
    
     
    if(el.attr("class").indexOf("niceRadioDisabled")==-1)
    {  
        el.addClass("radioChecked");
        input.attr("checked", true);
    }
     
    return true;
}
 
function changeVisualRadio(input)
{
    /*
    меняем вид radio при смене значения
	*/
    var wrapInput = input.parent();
    var nm=input.attr("name");
         
    jQuery(".niceRadio input").each(
    
        function() {
    
            if(jQuery(this).attr("name")==nm)
            {
                jQuery(this).parent().removeClass("radioChecked");
            }
       
        
        });
 
    if(input.attr("checked"))
    {
        wrapInput.addClass("radioChecked");
    }
}
 
function changeRadioStart(el)
/*
    новый контрол выглядит так <span class="niceRadio"><input type="radio" name="[name radio]" id="[id radio]" [checked="checked"] /></span>
    новый контрол получает теже name, id и другие атрибуты что и были у обычного
	*/
{
	 
    try
    {
        var el = el,
        radioName = el.attr("name"),
        radioId = el.attr("id"),
        radioChecked = el.attr("checked"),
        radioDisabled = el.attr("disabled"),
        radioTab = el.attr("tabindex"),
        radioValue = el.attr("value");
        if(radioChecked)
            el.after("<span class='niceRadio radioChecked'>"+
                "<input type='radio'"+
                "name='"+radioName+"'"+
                "id='"+radioId+"'"+
                "checked='"+radioChecked+"'"+
                "tabindex='"+radioTab+"'"+
                "value='"+radioValue+"' /></span>");
        else
            el.after("<span class='niceRadio'>"+
                "<input type='radio'"+
                "name='"+radioName+"'"+
                "id='"+radioId+"'"+
                "tabindex='"+radioTab+"'"+
                "value='"+radioValue+"' /></span>");
    
        /* если контрол disabled - добавляем соответсвующий класс для нужного вида и добавляем атрибут disabled для вложенного radio */    
        if(radioDisabled)
        {
            el.next().addClass("niceRadioDisabled");
            el.next().find("input").eq(0).attr("disabled","disabled");
        }
     
        /* цепляем обработчики стилизированным radio */    
        el.next().bind("mousedown", function(e) {
            changeRadio(jQuery(this))
        });
        el.next().find("input").eq(0).bind("change", function(e) {
            changeVisualRadio(jQuery(this))
        });
        if(jQuery.browser.msie)
        {
            el.next().find("input").eq(0).bind("click", function(e) {
                changeVisualRadio(jQuery(this))
            });  
        }
        el.remove();
    }
    catch(e)
    {
    // если ошибка, ничего не делаем
    }
 
    return true;
}
