﻿// JScript File
var browserName=navigator.appName; // appName is the browser's name
var browserVersion=navigator.appVersion; // appVersion is the browser's version

// 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;
var IE9 = (browserVersion.indexOf("MSIE 9.0") >= 0)?true:false;;
var IE6 = (browserVersion.indexOf("MSIE 6.0") >= 0 || browserVersion.indexOf("MSIE 7.0") >= 0)?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 && !IE9) { // 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 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 showTooltipNoContent(idObj) 
{
    showTooltip(idObj, "");
}

function showTooltip(idObj, html_tooltip) 
{
    // Lấy width và heigh của browser
    getBrowserViewport();
    
    // Tìm div theo id "idObj"
	var obj = fetch_object(idObj);
	if(html_tooltip != "")
	    obj.innerHTML = html_tooltip;
	
	var pos = 10;
	mouse_x = mouse_x + pos;
	mouse_y = mouse_y + pos;
	
	if(viewportheight < document.body.clientHeight && !IE6)
	{
	    viewportheight = document.body.clientHeight;
	}

	if(viewportheight < document.documentElement.clientHeight && !IE6)
	{
	    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
	var maxwidth;
	
	if(IE6)
	    maxwidth = parseInt(obj.style.width.replace("px","").replace("%",""));
	else
    	maxwidth = parseInt(obj.style.maxWidth.replace("px","").replace("%",""));
    
	if(mouse_x + maxwidth > viewportwidth)
	{
	    mouse_x = viewportwidth - maxwidth - (pos + 15);
	}
	
	if(IE6)
	{
	    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;

        //var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
        var dsoctop=document.all? iebody.scrollTop : pageYOffset;
        
        mouse_y = mouse_y + dsoctop;
	}
	
	// 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)
	{
	    var maxheight;
	    
	    if(IE6)
	        maxheight = parseInt(obj.style.height.replace("px","").replace("%",""));
	    else
    	    maxheight = parseInt(obj.style.maxHeight.replace("px","").replace("%",""));
    	
	    if(mouse_y + maxheight > viewportheight && !IE6)
	    {
	        mouse_y = viewportheight - maxheight - (pos + 15);
	    }
	    else if(mouse_y + maxheight > viewportheight && IE6)
	    {
	        mouse_y = mouse_y - maxheight - (pos + 15);
	    }
	}
	//alert("obj.style.display: " + obj.style.display)
		
	obj.style.display = "";
	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;
		
		if(IE9)
		{
		    obj.style.left = mouse_x + "px";
		    obj.style.top = mouse_y + "px";
		}
	}
	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);
	obj.style.display = "none";
	/*if (ns4) {
	 	obj.visibility = "hide";
	}
	else if (ie4) {
	 	obj.style.visibility = "hidden";
	}
	else if (nn6) {
	 	obj.style.visibility = "hidden";
	}*/
}

var timeoutMenu;
function showPopupMenu(idObj)
{
    var obj = fetch_object(idObj);
    if(obj.style.display == "none")
    {
        showTooltipNoContent(idObj);
        timeoutMenu = setTimeout("hideObject('" + idObj + "')", 20000);
    }
    else
    {
        hideObject(idObj);
        clearTimeout(timeoutMenu);
    }
}


