﻿(function($) {
    $.fn.AddXbutton = function(options) {
        var defaults = {
            img: 'x.gif'
        };
        var opts = $.extend(defaults, options);
        var objs = new Array();
        $(this).each(
        function(i) {
            $obj = $(this);
            objs[i] = $obj;
            var width = $(this).width() - 12;
            $(this).before(
                $('<input type="image" id="xButton' + i + '" src="' + opts['img'] + '" />')
                    .css({ 'display': 'none', 'cursor': 'pointer', 'marginLeft': width + 'px', 'position':'absolute', 'marginTop':'3px' })
                    .click(function() {
                        objs[i].val('').focus();
                        $("#xButton" + i).hide();
                    }))
            .keyup(function() {
                if ($(this).val().length > 0) {
                    $("#xButton" + i).show();
                } else {
                    $("#xButton" + i).hide();
                }
                
            }); 
            if ($obj.val() != '') $("#xButton" + i).show();
        });

    };
})(jQuery);

