/**
 * jQuery inline help
 * 
 * Copyright (c) 2009 
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @version 0.1 Alpha 1
 * @author Peskov Denis
**/

(function($){
    $.fn.inline_caption=function(options){
        
        if ( typeof(this.attr('inline_caption'))  == 'undefined') {
            var options = $.extend({
                    img:      false,
                    width:    0,
                    height:   0,
                    left:     0,
                    top:      0,
                    txt:      false,
                    txtStyle: false,
                    divtop:   0,
                    divleft:  0
                }, options
            );

            if ( options.img == false && options.txt == false ) { return this; }

            var w=this.width();
            var h=this.height();

            this.wrap("<div style='position: absolute; width: "+w+"px; height: "+h+"px;'></div>");

            var dv = this.parent().children("div");
            if ( options.img != false ) {

                this.parent().append("<div style='position: relative; top: "+(0+options.divtop)+"px; left: 0px; width: "+options.width+"px; height: "+options.height+"px; background: url("+options.img+") "+(0+options.left)+"px "+(0+options.top)+"px no-repeat;'></div>");
            }
            if ( options.txt != false ) {

                txt_w = options.txt.length;
                this.parent().append("<div style='position: relative; top: "+(0+options.divtop)+"px; left: 0px; width: "+w+"px; height: "+(h+2)+"px;'>"+"<font style='"+options.txtStyle+"'>"+options.txt+"</font>"+"</div>");
            }
            this.parent().children("div").click( function(){ $(this).parent().children("input").focus(); });
            this.focus( function(){ $(this).parent().children("div").hide(); } );
            this.blur(  function(){ if ( $.trim(this.value) == '' ) { $(this).parent().children("div").show(); } } );

            this.attr('inline_caption', 'true');

            if ( $.trim(this.val()) != '' ) {
                this.parent().children("div").hide();
            }
        }
        return this;
    };
})(jQuery);

