(function ($) {   
    function getElementCoordinates(el) {
        var offset = $(el).offset();

        return {
            left : offset.left,
            right : el.offsetLeft + el.offsetWidth,
            top : offset.top,
            bottom : el.offsetTop + el.offsetHeight
        };
    }
    $.fn.customInputFile = function (settings) {
        var config = {           
            replacement : $('<div />', {
                "text" : "",
                "class": "customInputFile"
            }),
            filename : $('<div />', {
                "class": "customInputFileName"
            }),            
            changeCallback: $.noop
        };

        if (settings) {
            $.extend(config, settings);
        }
        var $input,
            $replacement = $(config.replacement),
            $filename = $(config.filename),
            currentId = 0;
        
        function bind() {
            function onChange() {
                var val = $(this).val().replace(/.*(\/|\\)/, "");

                if ($filename.get(0).nodeName.toUpperCase() === 'INPUT') {
                    $filename.val(val);
                } else {
                    $filename.text(val);
                }

                if (val) {
                    $filename.show();
                } else {
                    $filename.hide();
                }               
                config.changeCallback(val);
            }
            
            function onClear() {
              var el    = $input.get(0);
              var attrs = {};
              for (var i = 0; i < el.attributes.length; i += 1) {
                  var attrib = el.attributes[i];
                  if (attrib.specified === true && attrib.name !== 'value' && !/jQuery/.test(attrib.name)) {
                      attrs[attrib.name] = attrib.value;
                  }
              }
              attrs.value = "";
              $input = $('<input />', attrs);
              var t = $(this);
              window.setTimeout(function () {
                t.replaceWith($input);              
              }, 50);
            }

            $input.bind('change', onChange).bind('clear', onClear);
        }       

        function createFileName() {
            try {
                if (! jQuery.contains(document.body, $filename.get(0))) {
                    throw ("not found");
                }
            } catch (e) {
                if (! $filename.length) {
                    throw ("filename is empty");
                }                
            }        
        }        
        function replace() {
            $replacement 
                .bind('init', function () {                    
                    createFileName();
                })
                .trigger('init');
        }
        function init(el) {
            currentId = 1;
            $input = $(el);
            bind();
            replace();
        }
        this.each(function () {
            try {
                init(this);
            } catch (e) {
            	throw ("error");
            }
        });
        return this;
    };
}(jQuery));
