/* Clicktip.js - display ballon-style helps on click.
 * Copyright (C) 2006 Salvatore Sanfilippo <antirez at gmail dot com>
 * All Rights Reserved.
 *
 * http://antirez.com/page/clicktip
 *
 * LICENSE: Clicktip is released under the GPL license version 2.0
 * Please send bugfix to antirez at gmail dot org */

function registerClicktip(e,text) {
    e.clickTipActive = false;
    e.onclick = function (event) {
        return handleClickTip(event,text,e);
    };
}

function registerClicktipId(id,text) {
    var e = document.getElementById(id);
    if (e) {
        registerClicktip(e,text);
    } else {
        alert("Clicktip error: no such element ID '"+id+"'");
    }
}

/* Set a clicktip to all the elements of a given type/class */
function registerClicktipBulk(type,classname,text) {
    if (typeof(document.getElementsByTagName) != 'undefined') {
        var e = document.getElementsByTagName(type);
        var i;
        for (i = 0; i < e.length; i++) {
            if (!classname || e[i].className == classname) {
                registerClicktip(e[i], text);
            }
        }
    }
}

function delClickTip() {
    var div = this;
    try {
        try { div.clickTipTarget.clickTipActive = false; } catch(e) {};
        document.body.removeChild(div);
        delete(div);
    } catch(e) {};
}

function getDivHeight(div) {
    if (typeof(div.offsetHeight) != 'undefined') {
        /* IE ... */
        return div.offsetHeight;
    } else {
        /* W3C way, supported by Gecko */
        try {
            return document.defaultView.getComputedStyle(div,"").getPropertyValue("height");
        } catch(e) {
            return false;
        }
    }
}

function handleClickTip(e,text,target) {
    /* A clicktip is already on screen for this object? Return */
    if (target.clickTipActive) return false;
    target.clickTipActive = true;
    /* The target object have a tipclick attribute? Use it as text */
    if (target.getAttribute('clicktip')) text=target.getAttribute('clicktip');
    if (!text) return; /* No text attribute nor one specified on registration */
    showClickTip(e,text,target);
}

function showClickTip(e, text,target) {
    /* Get the mouse position */
    if(!e) var e = window.event;
    var d = new Date();
    var time = d.getTime();
    var x = e.clientX;
    var y = e.clientY;
    if (typeof(x) == 'undefined') {
        x = e.pageX;
        y = e.pageY;
    } else {
    	x += document.body.scrollLeft+document.documentElement.scrollLeft;
	y += document.body.scrollTop+document.documentElement.scrollTop;
    }

    /* Show a DIV with the right message */
    var div = document.createElement('div');
    div.className = 'clicktip';
    div.style.visibility = 'hidden';
    div.style.position = 'absolute';
    div.style.left = x+"px";
    div.style.top = y+"px";
    /* We set the DIV content usign innerHTML,
       If you are a purist append a text node instead ;) */
    div.innerHTML = text;

    /* When the clicktip gets clicked we hide it */
    div.clickTipTarget = target;
    div.onclick = delClickTip;
    document.body.appendChild(div);

    /* Try to fix the 'top' in order to display the div just over the pointer */
    var h = getDivHeight(div);
    if (h) {
        /* Check if there is space on top to display the clicktip */
        if (h < y) {
            div.style.top = (y-h)+"px";
        } else {
            /* No space on top, display the tip on the bottom, i.e.
               just don't alter the current position. */
        }
    }
    div.style.visibility = 'visible';

    /* Compute how long the clicktip should be shown */
    var milliseconds = 2000; /* base time */
    var textlen = text.length;

    /* Add one second for every 50 characters */
    while(textlen > 30) {
        milliseconds += 1000;
        textlen -= 30;
    }

    /* Register a timer to remove the DIV after few seconds */
    setTimeout(function() {
        try {
            target.clickTipActive = false;
            document.body.removeChild(div);
            delete(div);
        } catch(e) {};
    }, milliseconds);
    return false;
}

function registerVoteExpiredTip(){
        registerClicktipBulk('a','voteExpired','Questo voto e\' piu\' vecchio di 15 minuti e non puo\' essere modificato.');
}



