/*
	Simple Tooltip
	© 2011 borninteractive.com
	@author Makram
*/

var tip_height = 35;
var tip_visible = false;
var tip_default_message = "";
var tip_current_message = "";
var tip_lastMouseX = 0, tip_lastMouseY = 0;
var force_hide = false;

function tip_init()
{
    $("#tooltip").remove();
    $('body').append('<div id="tooltip"><div class="tipBody"></div></div>');
    $("#tooltip").hide();
    $(document).mousemove(tip_updatePos);
    
    //tip_setDefault("loading...");
}
function tip_updatePos(e)
{
    if(!tip_visible) return;
    // move the tooltip to position
    var top = e.pageY + 15;
    if(!top && !tip_lastMouseY) {
        $("#tooltip").css("visibility", "hidden");
    } else {
        $("#tooltip").css("visibility", "visible");
    }
    var st = Math.max($("html").scrollTop(), $("body").scrollTop());
        
    var left = e.pageX + 15;
    var tip_width = $("#tooltip").width() + 5;
    top = Math.min(top, $(window).height() - tip_height + st);
    left = Math.min(left, $(window).width() - tip_width);
    if(isNaN(top)) {
        // center it in the window
        top = tip_lastMouseY;
        left = tip_lastMouseX;
    }
    
    tip_lastMouseX = left;
    tip_lastMouseY = top;
    
    $('#tooltip').css('top', top+"px");
    $('#tooltip').css('left', left+"px");
}

function tip_setDefault(text)
{
    if(text == "") {
        tip_default_message = "";
        tip_hide();
    } else {
        if(tip_current_message == "") {
            tip_show(text, true);
        }
    }
    tip_default_message = text;
}
function tip_show(text, default_text)
{
    if(force_hide) return;
    
    tip_visible = true;
    $("#tooltip .tipBody").text(text);
    //Show the tooltip with faceIn effect
    $('#tooltip').fadeTo('fast', 0.9);
    $(document).mousemove();
    if(!default_text) tip_current_message = text;
}
function tip_hide()
{
    $('#tooltip').hide();
    tip_current_message = "";
    tip_visible = false;
    
    if(tip_default_message != "") {
        tip_show(tip_default_message, true);
    }
}

function tip_force_hide(force)
{
    force_hide = force;
    if(force) {
        tip_hide();
    } else {
        if(tip_default_message != "") {
            tip_show(tip_default_message, true);
        }
    }
}

function tip_addToLinksWithTitle()
{
    //Select all anchor tag with rel set to tooltip
    $('*[rel=tooltip], *[tooltip]').mouseover(function(e) {
        //Grab the title attribute's value and assign it to a variable
        var text = $(this).attr('title') || $(this).attr('tooltip');
        $(this).attr('tooltip', $(this).attr('title'));
        //Remove the title attribute's to avoid the native tooltip from the browser
        $(this).attr('title', '');
        
        tip_show(text);
        
    }).mouseout(function() {
        //Put back the title attribute's value
        $(this).attr('title', $('.tipBody').html());
        
        tip_hide();
    });
}

if(!isiPad()) {
    $(document).ready(tip_init);
    $(document).ready(tip_addToLinksWithTitle);
}
