/*
 * jfwtip - tooltip
 * by jatt www.w3page.eu
 * by class....    <a href="aaa" class="jfwtip" title="title|content"></a>
 * any all ohers tags and attributes 
 * $(".jfwtip").jfwtip();
 * $(".jfwtip").jfwtip({ border: 1, speed: 'slow' });
 *
 */

(function($) 
{
	$.fn.jfwtip = function(o) {

		o = $.extend( {
			xoffset: -10,
			yoffset: 19,
			speed: 'fast'
		}, o || {});
        var eh;
        var ew;

		return this.each(function() 
		{
			$(this).hover(function(e) 
			{
				// temprary clear the title to suppress browser jfwtip
				this.t = this.title;
				this.title = "";	
				var has_contents = 0;
				var has_title = 0;
				var tt_title, tt_contents;
				var textarray = new Array();

				if(this.t != "") {
					textarray = this.t.split("|");
					if(textarray.length >= 1) {
						if(textarray[0] != '') { // title is not empty
							has_title = 1;
							tt_title = textarray[0];
						}
					}
					if(textarray.length > 1) {
						if(textarray[1] != '') { // contents is not empty
							has_contents = 1;
							tt_contents = textarray[1];	
						}
					}
				    // construct jfwtip div data
				    tt_data = "<div id='jfwtip'>";
                    //tt_data += "<div id='icon'><img src='img/qm.png' alt='qm'/></div>";
				    if(has_title)    tt_data += "<div id='title'>" + tt_title + "</div>";
				    if(has_contents) tt_data += "<div id='contents'>" + tt_contents + "</div>";
				    tt_data += "</div>";
				    $("body").append(tt_data);
				    // if both title and contents exists, remove some borders
				    if(has_title && has_contents) {
					    $("#jfwtip #title").css({ "border-bottom-width":"0px"});
					    $("#jfwtip #contents").css({ "border-top-width":"0px"});
				    }
                    //size of tip
                    eh = $("#jfwtip").height();
                    ew = $("#jfwtip").width();
                    //calculating  position 
                    var v = viewport();
                    if((v.w+v.sw - e.pageX)<ew){
                        posx = e.pageX - ew - o.xoffset;
                    }
                    else{
                        posx = e.pageX + o.xoffset;
                    }
                    if((v.h+v.sh - e.pageY)<eh){
                        posy = e.pageY - eh - o.yoffset;
                    }
                    else{
                        posy = e.pageY + o.yoffset;
                    }
                    $("#jfwtip").css("top",(posy) + "px");
                    $("#jfwtip").css("left",(posx) + "px");
				    $("#jfwtip").fadeIn(o.speed);		
                }//not empty title...
			},
			function() 
			{
				this.title = this.t;
				$("#jfwtip").remove();
			});

			$(this).mousemove(function(e) 
			{
                //calculating  position 
                var v = viewport();
                if((v.w+v.sw - e.pageX)<ew){
                    posx = e.pageX - ew - o.xoffset;
                }
                else{
                    posx = e.pageX + o.xoffset;
                }
                if((v.h+v.sh - e.pageY)<eh){
                    posy = e.pageY - eh - o.yoffset;
                }
                else{
                    posy = e.pageY + o.yoffset;
                }
				$("#jfwtip").css("top",(posy) + "px");
                $("#jfwtip").css("left",(posx) + "px");
			});
		
		});
	};
    
    function viewport() {
        return {
            sw: $(window).scrollLeft(),
            sh: $(window).scrollTop(),
            w: $(window).width(),
            h: $(window).height()
        };
    }
    
})(jQuery);

