﻿// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false;

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) {
	document.captureEvents(Event.MOUSEMOVE);
}

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

var ns4 = document.layers;
var ie4 = document.all;
var nn6 = document.getElementById && !document.all; 
var	mouse_x = 0;
var mouse_y = 0;
var viewportwidth = 0;
var viewportheight = 0;

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
	tempX = event.clientX + document.body.scrollLeft;
	tempY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
	tempX = e.pageX;
	tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0;}
  if (tempY < 0){tempY = 0;}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  mouse_x = tempX;
  mouse_y = tempY;
}

function fetch_object(idname)
{
	if (document.getElementById)
	{
		return document.getElementById(idname);
	}
	else if (document.all)
	{
		return document.all[idname];
	}
	else if (document.layers)
	{
		return document.layers[idname];
	}
	else
	{
		return null;
	}
}

function getBrowserViewport()
{ 
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) 
    //use window.innerWidth and window.innerHeight
 
    if (typeof window.innerWidth != 'undefined')
    {
        viewportwidth = window.innerWidth;
        viewportheight = window.innerHeight;
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
             && typeof document.documentElement.clientWidth !=
             'undefined' && document.documentElement.clientWidth != 0)
    {
        viewportwidth = document.documentElement.clientWidth;
        viewportheight = document.documentElement.clientHeight;
    }

    // older versions of IE

    else
    {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
        viewportheight = document.getElementsByTagName('body')[0].clientHeight;
    }
}

function showObject(idObj, html_tooltip) {
    // Lấy width và heigh của browser
    getBrowserViewport();
    
    // Tìm div theo id "idObj"
	var obj = fetch_object(idObj);
	obj.innerHTML = html_tooltip;
	
	var pos = 15;
	mouse_x = mouse_x + pos;
	mouse_y = mouse_y + pos;
	
	if(viewportheight < document.body.clientHeight)
	{
	    viewportheight = document.body.clientHeight;
	}
	
	if(viewportheight < document.documentElement.clientHeight)
	{
	    viewportheight = document.documentElement.clientHeight;
	}
	
	// Trường hợp chiều rộng của thẻ div vượt quá chiều rộng của browser
	if(mouse_x + obj.clientWidth > viewportwidth)
	{
	    mouse_x = viewportwidth - obj.clientWidth - (pos + 5);
	}
	
	// Trường hợp chiều cao của thẻ div vượt quá chiều cao của browser
	if(document.documentElement.clientHeight > 0)
	{
	    if(mouse_y + obj.clientHeight > viewportheight)
	    {
	        mouse_y = viewportheight - obj.clientHeight - (pos + 5);
	    }
	}
		
	if (ns4) {
		obj.visibility = "show";
		obj.left = mouse_x;
		obj.top = mouse_y;
	}
	else if (ie4) {
		obj.style.visibility = "visible";
		obj.style.left = mouse_x;
		obj.style.top = mouse_y;
	}
	else if (nn6) {
		obj.style.visibility = "visible";
		obj.style.left = mouse_x + "px";
		obj.style.top = mouse_y + "px";
	}
}

function hideObject(idObj) {
	var obj = fetch_object(idObj);
	if (ns4) {
	 	obj.visibility = "hide";
	}
	else if (ie4) {
	 	obj.style.visibility = "hidden";
	}
	else if (nn6) {
	 	obj.style.visibility = "hidden";
	}
}

