/*
 * Style File - jQuery plugin for styling file input elements
 *  
 * Copyright (c) 2007-2008 Mika Tuupola
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Based on work by Shaun Inman
 *   http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
 *
 * Revision: $Id: jquery.relookinputfile.js 2011-09-01 ComputedBy.com $
 *
 */

(function($) {
    
    $.fn.relookinputfile = function(options) {
                
        var settings = {
            width: "80px",
			height: "22px",
			label: "Browse..."
        };
                
        if(options) {
            $.extend(settings, options);
        };
                        
        return this.each(function() {
            
            var self = this;
			if ($(self).hasClass('relookinputfile_done')) return true;		// Avoid to do the relooking more than once! (true will 'skip' to next each, not stop the foreach)
			$(self).addClass('relookinputfile_done');
			
            var wrapper = $("<span>")
                            .css({
                                "width": settings.width,
                                "height": settings.height,
                                "display": "inline-block",
                                "position": "relative",
                                "overflow": "hidden"
                            });
 
            var button = $('<button>')
                             .css({
                                 "position": "absolute",
								 "margin": "0",
								 "width": "100%",
                                 "top": "0px",
                                 "left": "0px",
								 "overflow": "hidden"
                             })
							 .html(settings.label);

            $(self).wrap(wrapper);
            $(self).before(button);
            $(self).css({
                        "position": "absolute",
                        "top": "0px",
                        "right": "0px",
                        "display": "inline",
                        "cursor": "pointer",
                        "opacity": "0.01"
                    });
            $(self).bind("change", function() {
				var name=$(self).val();		//Look for the last part of the filename (if C:\windows\filename.txt -> filename.txt)
				name=name.replace("/","\\");
				name=name.split("\\")[name.split("\\").length-1]
                button.html(name);
            });
        });
    };
    
})(jQuery);

