// -----
// Toolip Fade In Plugin by iGadget
// (c) 2010 iGadget.de
// GPL
// -----
(function() {

jQuery.fn.fadeTipp = function(settings) {

var tipped = this;     // The jquery objects that will flash

// defaults
settings = jQuery.extend({
slidein: 20,
slideout: 150,
indur: 50,
outdur: 300,
tipp: "#fadetipp",
hoverclass: "hover",
copyclass: true,
copyfrom: false,
exceptclass: "cashout",
tippoffset: -35,
encapsulate: true,
stayonhover: false // not yet implemented
}, settings);

if(!jQuery(settings.tipp).data("oldclass"))
    jQuery(settings.tipp).data("oldclass", jQuery(settings.tipp).attr("class"));

//Loop through the jquery objects
return tipped.each(function(){

    if(settings.encapsulate) {
        if(jQuery(settings.tipp).find(".bubble").length < 1) {
            var tmp = (jQuery(settings.tipp).html().length > 0) ? jQuery(settings.tipp).html() : jQuery(settings.tipp).text();
            jQuery(settings.tipp).html('<div class="bubble"/ >').children(".bubble").append(tmp);
        }
    }
        
    if(settings.copyfrom)
        jQuery(this).attr(settings.copyfrom, jQuery(this).attr("title")).removeAttr("title");
       	    
    jQuery(this).unbind("mouseenter mouseleave").bind("mouseenter", function(event){
        jQuery(settings.tipp).stop(true);
    	var dis = this;
    	var pos = jQuery(dis).offset();
    	var wid = jQuery(dis).innerWidth()/2;
    	var lft =  pos.left + wid - (jQuery(settings.tipp).outerWidth()/2);
    	var tp =  pos.top + jQuery(dis).height() - jQuery(settings.tipp).outerHeight()+settings.tippoffset;
        jQuery(settings.tipp).attr("class",jQuery(settings.tipp).data("oldclass")).removeClass(settings.hoverclass).addClass(settings.hoverclass).css({
        	left: lft,
        	top: tp
       	}).show();
        jQuery(settings.tipp).css({"opacity": 1});
       	if(settings.copyclass)
       	    jQuery(settings.tipp).addClass(jQuery(dis).attr("class")).removeClass(settings.exceptclass);
       	if(settings.copyfrom)
       	    jQuery(settings.tipp).find(".bubble").text(jQuery(dis).attr(settings.copyfrom));
    }).bind("mouseleave", function(event){
            jQuery(settings.tipp).delay(300).fadeOut(settings.outdur, function(e){
                jQuery(settings.tipp).attr("class",jQuery(settings.tipp).data("oldclass"))
            });
    });

});

};

})(jQuery);

