jQuery.fn.wmEmpty = function(a){
    return $(this).attr('wmEmpty',a);
};

jQuery.fn.wmIsEmpty = function(){
    return $(this).attr('wmEmpty')=='1';
};

jQuery.fn.showWm = function(){
    var e = $(this);
    if (e.wmIsEmpty()){
        e.hide().next('.wmRplc').show();
    }
};

jQuery.fn.hideWm = function(){
    var e = $(this);
    if (e.wmIsEmpty()){
        e.show().focus().next('.wmRplc').hide();
    }
};

jQuery.fn.watermark = function(options) {
    if (undefined == options)
        var opt = {};
    else
        var opt = options;
    if (undefined == opt.cls)
        opt.cls = 'wm';
    var ins = $(this).find('input[type=text],input[type=password]');
    ins.each(function(i){
        var a = $(this);
        a.attr('wmClass',opt.cls);
        a.after('<input class="wmRplc" type="text" value="'+a.attr('title')+'"></input>');
        a.next('.wmRplc').css(a.css).width(a.width()).hide().focus(function(){
            $(this).prev('input').hideWm();
        }).addClass(opt.cls);
            
        a.wmEmpty(a.val()==''?'1':'0');
        a.showWm();
        /*a.focus(function(){
            $(this).hideWm();
        });*/
        a.blur(function(){
            var a = $(this);
            a.wmEmpty(a.val()==''?'1':'0');
            a.showWm();
        });
    });
};
