/*
 * 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.filestyle.js 303 2008-01-30 13:53:24Z tuupola $
 *
 */

(function($) {
    
    $.fn.filestyle = function(options) {
                
        /* TODO: This should not override CSS. */
        var settings = {
            width : 250
        };
                
        if(options) {
            $.extend(settings, options);
        };
                        
        return this.each(function() {
            
            var self = this;
            /*var wrapper = $('<div class="file_button">')
                            .css({
                                "width": settings.imagewidth + "px",
                                "height": settings.imageheight + "px",
                                "background": "url(" + settings.image + ") 0 0 no-repeat",
                                "background-position": "right 0px",
                                "display": "inline",
                                "position": "absolute",
                                "overflow": "hidden"
                            }).text("Browse");
                            
            var filename = $('<input class="file">')
                             .addClass($(self).attr("class"))
                             .css({
                                 "display": "inline",
                                 "width": settings.width + "px"
                             });

            $(self).before(filename);
            $(self).wrap(wrapper);*/

            var wrapper		= $('<div class="jquery_file_input">').css({
                                "width": (settings.width + settings.imagewidth + 21) + "px",
                                "overflow": "hidden",
                                "position": "relative"
                            });
            var filename	= $('<input type="text" readonly>').addClass($(self).attr("class")).css({
                                 "display": "inline",
                                 "width": settings.width + "px",
                                 "position": "relative",
                                 "z-index": "2"
                             });
            var button		= $('<div class="jquery_file_input_button">').html('<span class="jquery_file_input_button_text">Browse</span>').css({"width": settings.imagewidth + "px"});
                        
            $(self).wrap(wrapper);
            /*$(self).css({"cursor":	 "pointer",
            			 "position": "absolute",
            			 "opacity":	 "0.0"});
*/
            $(self).css({
                        "position": "absolute",
                        "height": settings.imageheight + "px",
                        "width": (settings.width + settings.imagewidth + 15) + "px",
                        "display": "inline",
                        "cursor": "pointer",
                        "opacity": "0.0"
                    });

            $(self).before(filename);
            $(self).before(button);

            if ($.browser.mozilla) {
                if (/Win/.test(navigator.platform)) {
                    $(self).css("margin-left", "-252px");                    
                } else {
                    $(self).css("margin-left", "-168px");                    
                };
            } else if($.browser.opera) {
                $(self).css({"margin-left": "0px"});
            } else {
                $(self).css({"margin-left": ((settings.width + settings.imagewidth + 20) * (-1)) + "px"});
            };

            $(self).bind("change", function() {
                filename.val($(self).val());
            });
      
        });
        

    };
    
})(jQuery);

