$(document).ready(function(){
    $('input').each(function(){
        var inputType = $(this).attr('type');
        if( inputType != 'radio' && inputType != 'checkbox' && inputType != 'button' && inputType != 'reset' && inputType != 'submit' && inputType != 'range' ){
            $(this).addClass('input');
        }
    });
    
    $('input[type=checkbox]').each(function(){
        if($(this).attr('checked')){
            $(this).after('<span class="checkbox checkbox2"></span>');
        }
        else{
            $(this).after('<span class="checkbox"></span>');
        }
    });
    
    $('input[type=checkbox]').change(function(){
        if($(this).attr('checked')){
            $(this).parent().children('span.checkbox').addClass('checkbox2');
        }
        else{
            $(this).parent().children('span.checkbox').removeClass('checkbox2')
        }
    });
    
    $('input[type=radio]').each(function(){
        if($(this).attr('checked')){
            $(this).after('<span class="radio radio2"></span>');
        }
        else{
            $(this).after('<span class="radio"></span>');
        }
    });
       
    $('input[type=radio]').change(function(){
            $('input[name=' + $(this).attr('name') + ']').parent().children('span.radio').removeClass('radio2');
            $(this).parent().children('span.radio').addClass('radio2');
    });
    
    $('button').each(function(i){
            $(this).after('<a href="#" class="button" id="sb-' + i + '" onclick="' + "$('#sbs-" + i + "').trigger('click');return false;" + '" >' + $(this).html() + '</a>');
            $(this).attr('id', 'sbs-' + i ).hide();
    });
    
    $('input[type=submit] , input[type=reset] , input[type=button]').each(function(i){
            $(this).after('<a href="#" class="button" id="ib-' + i + '" onclick="' + "$('#ibs-" + i + "').trigger('click');return false;" + '" >' + $(this).val() + '</a>');
            $(this).attr('id', 'ibs-' + i ).hide();
    });
/* Select */
    $('select').each(function(i){
        var selFirst = '...';
        if( $(this).attr('multiple') != 'multiple' ){
            $(this).after('<div class="selectcontainer" rel="' + 's-' + i + '" id="ss-' + i + '" ><div class="selectname" >' 
            + selFirst + '</div><div class="dropdownicon"><span>*</span></div></div>');
            $(this).attr('id', 's-' + i );
        }
    });
    
    $('.selectcontainer').live( 'mousemove' , function(e){
        var offset = $(this).offset();
        $( '#' + $(this).attr('rel') ).css({'margin-left' : 0, 'margin-top' : e.pageY - offset.top - 10,'display':'block' , width : $(this).width() });
    });
    
    $('select').live( 'mouseout' , function(e){
        if( $(this).attr('multiple') != 'multiple' ){
            $(this).next().removeClass('selecthover');
        }
    });
    
    $('select').live('mouseover' , function(){
        if( $(this).attr('multiple') != 'multiple' ){
            $(this).next().addClass('selecthover');
        }
    });
    
    $('select').live('change' ,function(){
        if( $(this).attr('multiple') != 'multiple' ){
            var selText = $(this + 'option:selected').text();
            $('#s' + $(this).attr('id') + ' .selectname').html(selText);
        }
    });
    $('select').change();

    /* File upload */
    $('input[type=file]').each(function(i){
        var selFirst = 'Select file';
        if($(this).attr('title')){ selFirst = $(this).attr('title'); }
            $(this).after('<div class="filecontainer" rel="' + 'f-' + i + '" id="if-' + i + '" ><div class="fileuploadtitle" >' 
            + selFirst + '</div><div class="filenamediv"><span class="filename"></span></div></div>');
            $(this).attr('id', 'f-' + i ).hide();
    });
    
    $('input[type=file]').change(function(){
        var spanid = $(this).attr('id');
        $(this).hide();
        var myfilename = $(this).val();
        myfilename = myfilename.replace(/C:\\fakepath\\/gi, "");
           if(myfilename != myfilename.substring(0,26) ){
               myfilename = myfilename.substring(0,25) + '...'
           }
           if($( '#i' + spanid + ' .filename').html() != myfilename ){
              $( '#i' + spanid +  ' .filename').html(myfilename);
           }
    });
    
    if(!$.browser.mozilla){
        $('.filecontainer').live('mousemove' , function(e){
                var offset = $(this).offset();
                $( '#' + $(this).attr('rel') ).css({
                    'margin-left' : 0, 
                    'position':'absolute',
                    'margin-top' : e.pageY - offset.top - 40,
                    'margin-left' : e.pageX - offset.left - 10,
                    'display':'block',
                    'opacity': 0,
                    'width' : '30'
                });
        });        
        
        $('input[type=file]').live( 'mouseout' , function(e){
           $(this).hide();
        });
    }
    
    $('.filecontainer').click(function(){
        $( '#' + $(this).attr('rel') ).trigger('click');
    });
});
