//Scripts coded by Huaming Chen

function load_GEBD_MAP_IMG()
{
	imageYcoord = 0;
	imageXcoord = 0;
	var obj = document.AtG1001_MAP_IMG;

	imageYheight = obj.height;
	imageXwidth = obj.width;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			imageXcoord += obj.offsetLeft;
			imageYcoord += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else
	{
		imageXcoord += obj.x;
		imageYcoord += obj.y;
	}
}

function gh (help, ix, iy, on)
{
	if ( on == 0 )
	{
		document.getElementById('tailHelp').style.visibility = "hidden";
	}
	else
	{
		load_GEBD_MAP_IMG ();

		if (help=="!!") string = ""+document.getElementById('tailHelp').innerHTML;
		else string = ""+help;

		if (help!="!!")
		{
			var sTop = parseInt(document.getElementById("innerRight").scrollTop);
			var vPosn = imageYcoord+iy+24;
			if (sTop>0) vPosn = imageYcoord+iy+24-sTop;
		}
		else vPosn = iy;
		document.getElementById('tailHelp').style.top = vPosn.toString()+"px";

		if ( string.length > 50 ) tailHelpWidth = 360;
		else tailHelpWidth = (string.length)*5+20;

		var windowWidth = screen.width;
                if (typeof(window.innerWidth)=='number')
                        windowWidth = window.innerWidth;
                else if (document.documentElement && document.documentElement.clientWidth)
                        windowWidth = document.documentElement.clientWidth;
                else
                        windowWidth = document.body.clientWidth;

		if ((xcoord+tailHelpWidth)>windowWidth)
		{
			vPosn = xcoord-tailHelpWidth+Math.floor((windowWidth-xcoord)*0.75);
		}
		else
		{
			 vPosn = xcoord + 10;
		}
		document.getElementById('tailHelp').style.left = vPosn.toString()+"px";

		if ( string.length > 50 )
		{
			vPosn = (string.length+49)/50*15+15 ;
			document.getElementById('tailHelp').style.height = vPosn.toString()+"px";
			document.getElementById('tailHelp').style.width = "360px"; 
		}
		else
		{
			vPosn = (string.length+2)*6+35;
			document.getElementById('tailHelp').style.width = vPosn.toString()+"px";
			document.getElementById('tailHelp').style.height = "30px" ;
		}

		if (help!="!!")
		document.getElementById('tailHelp').innerHTML = "<table id='tailHelpTab' bgcolor=#6666AA border=0 cellpadding=1 cellspacing=0><tr><td ><table cellpadding=4 cellspacing=6 bgcolor=#CCDDFF  width=100%><tr><td><font size=1 color=#222288 face=\"Arial\">"+help+"</td></tr></table></td></tr></table>";
		document.getElementById('tailHelp').style.visibility = "visible";
	}
}

function followmouse (e)
{
	if (!e)
	{
		if ( window.event ) e = window.event;
		else
		{
			return;
		}
	}

	if (document.getElementById("innerRightGEBrowser") && document.getElementById("innerRightGEBrowser").style.display=="none") return;

	//if (document.geView.View.value=="Text") return;

	load_GEBD_MAP_IMG();

	if (typeof(e.pageX) == 'number' )
	{
		xcoord = e.pageX;
		ycoord = e.pageY;
	}
	else if (typeof(e.clientX)=='number')
	{
		xcoord=e.clientX;
		ycoord=e.clientY;

		//var badOldBrowser = (window.navigator.userAgent.indexOf('Opera')+1 )||
		var badOldBrowser = (window.ScriptEngine && ScriptEngine().indexOf('InScript' )+1) || (navigator.vendor=='KDE')

		if (!badOldBrowser)
		{
			if (document.body && (document.body.scrollLeft || document.body.scrollTop))
			{
				xcoord += document.body.scrollLeft;
				ycoord += document.body.scrollTop;
			}
			else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
			{
				xcoord += document.documentElement.scrollLeft;
				ycoord += document.documentElement.scrollTop;
			}
		}
	}
	else
	{
		return;
	}

	if (document.geView.View.value=="Text") return;

	var oLeft = document.getElementById("innerRight").offsetLeft;
	var oTop = document.getElementById("innerRight").offsetTop;
	var sLeft = document.getElementById("innerRight").scrollLeft;
	var sTop = document.getElementById("innerRight").scrollTop;

	var xLeft = imageXcoord - sLeft;
	if (xLeft < oLeft) xLeft=oLeft;
	var xTop = imageYcoord+imageYheight-sTop;
	if (xTop < oTop) xTop=oTop;
	var xRight = oLeft+parseInt(document.getElementById("innerRight").offsetWidth)-16;
	if (xRight > imageXcoord+imageXwidth-sLeft) xRight = imageXcoord+imageXwidth-sLeft;
	var xBottom = oTop+parseInt(document.getElementById("innerRight").offsetHeight)-16;

	if (xcoord<xLeft || ycoord<xTop || xcoord>=xRight || ycoord>xBottom) 
	{

		document.getElementById('topDown').style.visibility = "hidden";
		document.getElementById('tailPosition').style.visibility = "hidden";
	}
	else
	{
		var vPosn = xcoord+1;
		document.getElementById('topDown').style.left = vPosn.toString()+"px";
		vPosn = imageYcoord;
		if (sTop>0)
		{
			if (sTop<imageYcoord) vPosn -= sTop;
			else vPosn = oTop;
		}
		document.getElementById('topDown').style.top = vPosn.toString()+"px";

		vPosn = xBottom - oTop;
		if (sTop<imageYcoord) vPosn -= (imageYcoord-sTop-oTop);
		document.getElementById('topDown').style.height = vPosn.toString()+"px";

		Posn = parseInt(document.geView.Position.value)+(parseInt(document.geView.Zoom.value))*(xcoord-imageXcoord+sLeft);

		Posn = Math.round ( Posn );
		document.getElementById('tailPosition').innerHTML = Posn;

		var ln = Posn.toString();
		vPosn = ycoord-20;
		document.getElementById('tailPosition').style.top = vPosn.toString()+"px";
		vPosn = ln.length*7+2;
		document.getElementById('tailPosition').style.width = vPosn.toString()+"px";
		vPosn = xcoord-(ln.length*7)/2-1;
		document.getElementById('tailPosition').style.left = vPosn.toString()+"px";


		document.getElementById('topDown').style.visibility = "visible";
		document.getElementById('tailPosition').style.visibility = "visible";
	}
}

function ghv (strain, e)
{
	var vhtml = "./gebrowser_Map_Info.php?Project="+Project+"&Version="+Version+"&Table=true&Label="+strain;
	ajax_new (vhtml, "tailHelp");
	if (!e) e=window.event;
	var h = 0;

	if (typeof(e.pageY)=='number') h = e.pageY;
	else if (typeof(e.clientY)=='number') h = e.clientY;

	if (typeof(window.innerWidth)=='number') windowHeight = window.innerHeight;
	else if (document.documentElement && document.documentElement.clientWidth)
		windowHeight = document.documentElement.clientHeight;
	else windowHeight = document.body.clientHeight;
        
	if (h > windowHeight-280)
		h = windowHeight-280;

	gh ("!!", 0, h, 1);
}

function NP_turn (id, np)
{
        if (npNo%2 == 0) document.geView.Code.value="N";
        else document.geView.Code.value="P";
	document.getElementById(id).src=imgshift[2+npNo%2].src;
        if (np==1) updatediv();
        npNo++;
}

function TG_turn (id, np)
{
        if (tgNo%2 == 0) document.geView.View.value="Graphic";
        else document.geView.View.value="Text";
	document.getElementById(id).src=imgshift[4+tgNo%2].src;
        if (np==1) updatediv();
        tgNo++;
}

function CDS_turn (id, np)
{
	document.geOptions.SNPDisplay[cdsNo%2].checked=true;
	document.getElementById(id).src=imgshift[8+cdsNo%2].src;
	if (np==1) updatediv();
	cdsNo++;
}

function toolbar_location ()
{
	var windowWidth = screen.width;
        if (typeof(window.innerWidth)=='number')
                windowWidth = window.innerWidth;
        else if (document.documentElement && document.documentElement.clientWidth)
                windowWidth = document.documentElement.clientWidth;
        else windowWidth = document.body.clientWidth;

        var toolbarLeft = windowWidth - parseInt (document.getElementById("Toolbar").style.width) -25;
        document.getElementById("Toolbar").style.left = toolbarLeft+"px";
}

function toolbar_initial ()
{
	toolbar_location ();

        if (document.geView.Code.value=="N") npNo=0;
        else npNo = 1;

	if (document.geView.View.value=="Graphic") tgNo=0;
	else tgNo = 1;

	if (document.geOptions.SNPDisplay[0].checked==true) cdsNo=0;
	else cdsNo=1;

        NP_turn ("NP", 0);
	TG_turn ("TG", 0);
	CDS_turn ("CDS", 0);

	if (document.geOptions.ViewToolbar.checked==true)
		document.getElementById("Toolbar").style.visibility = "visible";
}


function geOptionView (who)
{
        if (who == "Toolbar")
        {
                if (document.geOptions.ViewToolbar.checked==true)
		{
			toolbar_initial ();
                        document.getElementById("Toolbar").style.visibility = "visible";
                }
		else document.getElementById("Toolbar").style.visibility = "hidden";
        }
}

