var st_time=null;
var st_time_delay=null;
var _v1tooltip_on_element=null;

function prikazi_tooltip(event, id, _v1jsxr, _v1ttdata, delay, width, className) {
    if(id == "") {
        $.error("Id is not defined");
        return;
    }
    if(event === undefined) {
        $.error("event is not defined");
        return;
    }

    clearTimeout(st_time);
    clearTimeout(st_time_delay);
    
    var target = $(event.target);
    /*
     * this hides tooltip to quickly when moving mouse
    if(_v1tooltip_on_element != target) {
        sakrij_tooltip(id);
    }
    */
    
    
    var x, y;
    x = event.pageX;
    y = event.pageY;
    //console.log("xy", x, y);
    etip = $("#" + id);
    if(etip.length == 0) {
        etip = $('<div id="' + id + '" class="v1tooltip" style="position: absolute;"></div>');
        etip.hide();
        $("html > body").append(etip);
        etip.hover(
            function () {
                provjeri_tooltip(0, id);
            }, 
            function () {
                provjeri_tooltip(1, id);
            }
        );
        if(delay > 0) {
            target.bind('mousemove.v1tooltip', function(event) {
                x = event.pageX;
                y = event.pageY;
                //console.log("mousemove", x, y);
            });
        }
    }
	
	if(delay < 1) {
	    delay = 0;
	}
	
    if(width == "" || width === null || width === undefined) {
        width = 200;
    }
    
    var jsxr;
    var html;
    if(_v1jsxr != '') {
        jsxr = true;
    } else {
        jsxr = false;
    }
    
    _v1tooltip_on_element = target;
    //TODO: pass whole event object
	st_time_delay = setTimeout(function() {
	    target.unbind('mousemove.v1tooltip');
	    //console.log("xy", x, y);
	    
	    if(className == null) {
            etip.removeAttr('class');
        } else {
            etip.attr('class', className);
        }
        etip.width(width);
        
        //console.log(etip);
  
        /*
        if (browser.isNS) {
            x = mouseX;
            y = mouseY;
        } else {
            x = mouseX + document.body.scrollLeft;
            y = mouseY + document.body.scrollTop;
        }   
        */
        x += 15;
        y += 15;  
          
        // otkrij gdje se na prozoru nalazi i napravi pomak gore, dolje ili lijevo
        var maxX, maxY;
        var scrolled_size = get_scrolled_size();
        //console.log(etip.width(), etip.outerWidth(true), etip.outerWidth());
        
        maxX = scrolled_size.width;
        maxY = scrolled_size.height;
        maxX -= etip.outerWidth() + 20;
        maxY -= etip.outerHeight() + 20;
        
        if (x > maxX) {
            x = Math.max(0, maxX);
        }
        if (y > maxY) {
            y = Math.max(0, maxY);
        }
        etip.css('left', x);
        etip.css('top', y);
        //console.log("xy", x, y);       
        //console.log("x, y", { top: y, left: x });
        //etip.offset({ top: y, left: x });
        //console.log("offset", etip.offset());
        
        
        if(jsxr) {
            ajax_call_async(_v1jsxr, _v1ttdata, [],
                    function(html) {
                        postavi_html_tooltip(etip, html);
                    },
                    'v1_tooltip_seq'
            );
        } else {
            _v1ttdata = _v1ttdata.replace(/&#039;/g, "'");
            _v1ttdata = _v1ttdata.replace(/&quot;/g, '"');
            html = _v1ttdata;
            postavi_html_tooltip(etip, html);
        }
	}, delay);
}

function postavi_html_tooltip(etip, html) {
    etip.html(html);
    etip.css('visibility', 'visible');
    etip.show();
    clearTimeout(st_time);
}

function sakrij_tooltip(id) {
    //console.log('removed');
    $("*").unbind('mousemove.v1tooltip');
    $('#' + id).remove();
    _v1tooltip_on_element = null;
}

function provjeri_tooltip(num, id) {
	clearTimeout(st_time_delay);
	if (num==0) {
		clearTimeout(st_time);	
	} else if (num==1) {
		st_time = setTimeout('sakrij_tooltip(\''+id+'\')',1000);	
	}
}

