(function($) {
    
    $.fn.niceform = function() {

		$(function(){
	        $(document).click(function(e) {
	            if(!$(e.target).parents('.customSelectBox').length){
	                $('.customSelectBox').removeClass('opened').find('.opts:visible').hide();
	            }
	        });
	    });
	    
	    var sm = this.find('[type="submit"]');
        if (sm.length>0) sm.replaceSubmitBtn();
	    
        return this.each(function() {

            var $form = $(this);
            
            $form.find('input[type=text]:not(.fixed)').each(function() {
                var $t = $(this).addClass('fixed'),
                    name = this.name.replace('[]',''), 
                    $p = $t.wrap($('<div>').addClass('customText')).parent(), 
                    $l = $('label[for=' + $t.attr('id') + ']'), 
                    $s = $('input[name^="' + name + '"]').not($t);
                    
                $t.focus(function() {
                    $p.addClass('hovered');
                }).blur(function() {
                    $p.removeClass('hovered');
                });
                $p.add($l).hover(function() {
                    $p.addClass('hovered');
                }, function() {
                    $p.removeClass('hovered');
                });
            });
            
            $form.find('input[type=checkbox]:not(.fixed)').each(function() {

                var $t = $(this),
                    name = this.name.replace('[]',''), 
                    $p = $t.wrap($('<div>').addClass('customCheckBox')).parent(), 
                    $l = $('label[for=' + $t.attr('id') + ']'), 
                    $a = $('input[name^="' + name + '"]');

                $t.addClass('fixed').css({
                    opacity : 0
                });

                if($t.attr('checked'))$p.addClass('checked');
                    
                
                
                $t.bind('click.checkboxNiceForm',function() {
                    if(!$t.attr('checked')) {
                        if($t.hasClass('allChecked')) {
                            return false;
                        } else {
                            $a.filter('.allChecked').attr('checked', 0).parent().removeClass('checked');
                            $t.attr('checked', 0).parent().removeClass('checked');
                            if($a.filter(':checked').not('.allChecked').length == 0 && $a.filter('.allChecked').length) {
                                $a.filter('.allChecked').attr('checked', 1).parent().addClass('checked');
                            }
                        }
                    } else {
                        if($t.hasClass('allChecked')) {
                            $t.attr('checked', 1).parent().addClass('checked');
                            $a.not('.allChecked').attr('checked', 0).parent().removeClass('checked');
                        } else {
                            $t.attr('checked', 1).parent().addClass('checked');
                            if($a.filter(':checked').not('.allChecked').length == $a.length - 1 && $a.filter('.allChecked').length) {
                                $a.attr('checked', 0).parent().removeClass('checked');
                                $a.filter('.allChecked').attr('checked', 1).parent().addClass('checked');
                            } else {
                                $a.filter('.allChecked').attr('checked', 0).parent().removeClass('checked');
                            }

                        }
                    }
                });

                $p.add($l).hover(function() {
                    $p.addClass('hovered');
                }, function() {
                    $p.removeClass('hovered');
                });
            });

            $form.find('input[type=radio]:not(.fixed)').each(function() {
                var $t = $(this),
                    name = this.name.replace('[]',''), 
                    $p = $t.wrap($('<div>').addClass('customRadioBox')).parent(), 
                    $l = $('label[for=' + $t.attr('id') + ']'), 
                    $s = $('input[name^="' + name + '"]').not($t);
                $t.addClass('fixed').css({
                    opacity : 0
                });
                if($t.attr('checked'))
                    $p.addClass('checked');
                $t.bind('click.radioNiceForm',function() {
                    if($t.attr('checked')) {
                        $s.each(function() {
                            $(this).parent().removeClass('checked');
                        });
                        $p.addClass('checked');
                    }
                });
                $p.add($l).hover(function() {
                    $p.addClass('hovered');
                }, function() {
                    $p.removeClass('hovered');
                });
                if($t.attr('checked')) {
                    $s.each(function() {
                        $(this).parent().removeClass('checked');
                    });
                    $p.addClass('checked');
                }
            });
            
            $form.find('select:not(.fixed)').each(function() {
                var $t = $(this),
                    name = this.name.replace('[]',''),
                    sw = $t.outerWidth() || parseInt($t.css('width')), 
                    $p = $t.wrap($('<div>').addClass('customSelectBox')).parent().css({width:1000}),
                    $b = $('<a href="#" class="btn"><em></em></a>').appendTo($p), 
                    $s = $('<span>' + $t.find('option:selected').html() + '</span>').prependTo($b), 
                    $d = $('<div class="opts"><div class="ulcnt"><ul></ul></div></div>'), 
                    $dsb = $('<div class="selbottom"><div>&nbsp;</div></div>'),
                    $dst = $('<div class="seltop"><div>&nbsp;</div></div>'),
                    $u = $d.find('.ulcnt'),
                    $l = $('label[for=' + $t.attr('id') + ']'),
                    left = $p.offset().left, 
                    maxW = 0;
                
                $t.addClass('fixed').css({
                    display : 'none'
                });
                
                $t.find('option').each(function(i) {
                    var $o = $(this),
                        selCls = (i==0) ? 'selected' : '',
                        $li = $('<li class="'+selCls+'" id="ov-' + $o.attr('value') + '"><p><span><strong>' + $o.html() + '</strong></span></p></li>');
                        
                    $li.click(function(e) {
                        $(this).addClass('selected').siblings().removeClass('selected');
                        $t.find('option[value=' + $li.attr('id').split('ov-')[1] + ']').attr('selected', 1);
                        $s.html($o.html());
                        $d.hide();
                        
                        $t.trigger('change');
                        $p.removeClass('opened');
                    }).hover(
                        function() {
                            $(this).addClass('hovered');
                        },
                        function() {
                            $(this).removeClass('hovered');
                        }
                    );
                    $d.find('ul').append($li);
                });

                $d.prepend($dst);
                $d.append($dsb);
                $p.append($d);

                $d.find('li p').css({
                    'float' : 'left',
                    'width' : 'auto'
                }).each(function() {
                    var $lip = $(this);
                    ow = $lip.outerWidth();
                    $lip.css({
                        'float' : 'none'
                    });
                    if(ow > maxW) {
                        maxW = ow;
                    }
                });
                
                var listHeight = $d.outerHeight(),
                    listMaxHeight = 200,
                    scWidth = (listHeight > listMaxHeight) ? $.scWidth() : 0,
                    pad = $u.outerWidth()-$u.width(),
                    listWidth = (sw > maxW + scWidth) ? sw - pad : maxW + scWidth;
                
                $dst.css({width:listWidth+pad});
                $dsb.css({width:listWidth+pad});
                
                if (left<listWidth-sw){
                    $d.css({left:0, right:'auto'});     
                }   
                else $d.css({left:'auto', right:0});
                
                $d.hide().find('ul').css({
                    height: ((listHeight > listMaxHeight) ? listMaxHeight : 'auto'),
                    width : listWidth
                });
                
                $p.css({
                    width : sw
                });
                
                $b.css({
                    width : sw
                });    

                $b.click(function(e) {
                    e.preventDefault();
                    if($p.hasClass('opened')) {
                        $d.hide();
                        $p.removeClass('opened');
                    } else {
                        $('.customSelectBox.opened').removeClass('opened').find('.opts:visible').hide();
                        $d.show();
                        $p.addClass('opened');
                    }
                });

                $p.add($l).hover(function() {
                    $p.addClass('hovered');
                }, function() {
                    $p.removeClass('hovered');
                });
            });
            
            $form.find('textarea:not(.fixed)').each(function() {
                var $t = $(this),
                    name = this.name.replace('[]',''), 
                    $p = $t.wrap($('<div>').addClass('customTextArea')).parent(), 
                    $l = $('label[for=' + $t.attr('id') + ']');
                    
                $t.focus(function() {
                    $p.addClass('hovered');
                }).blur(function() {
                    $p.removeClass('hovered');
                });
                $p.add($l).hover(function() {
                    $p.addClass('hovered');
                }, function() {
                    $p.removeClass('hovered');
                });
            });
        });
    };
    
})(jQuery);

