var sh_popuptimeout = null;
var g_iEventCount = 0;
var g_EventArray = new Array;

var iCurrentEventCount = 0;
var iCurrentX = 0;
var iCurrentY = 0;
var scrolltimer = null;
var loop = false;
//  ########  TRACKS MOUSE POSITION FOR POPUP PLACEMENT
//var isNav = (navigator.appName.indexOf("Netscape") !=-1);
var isIE = (navigator.userAgent.indexOf("MSIE") !=-1);


function handlerMM(e)
{
	
	iCurrentX = (!isIE) ? e.pageX : (document.documentElement && document.documentElement.scrollLeft) ? event.clientX + document.documentElement.scrollLeft : event.clientX + document.body.scrollLeft;
	
	iCurrentY = (!isIE) ? e.pageY : (document.documentElement && document.documentElement.scrollTop) ? event.clientY + document.documentElement.scrollTop : event.clientY + document.body.scrollTop;

	//HidePopup();
	return true;
}

if(!isIE)
{
	document.captureEvents(Event.MOUSEMOVE);
}

document.onmousemove = handlerMM;

function SH_Start_Chart(obj, iEvent)
{
  //Find the position of the graph, return array
  var coors = SH_FindPos(obj);

 clearTimeout(sh_popuptimeout);
	try
	{
		if(iEvent > 0 && iEvent <= g_iEventCount)
		{
			var eventInfo = g_EventArray[iEvent-1];	// Arrays are zero based, but the xsl numbering is 1 based.
			
			var popupObj = document.getElementById?document.getElementById("popupmsg"):document.all?d.all["popupmsg"]:d.layers["popupmsg"];
			
			var contentObj = document.getElementById?document.getElementById("pricechart"):document.all?d.all["pricechart"]:d.layers["pricechart"];

			// See if we have more than one event on the same day
			var iStartEvent = iEvent - 1;
			var iEndEvent = iEvent - 1;
			for(; iStartEvent - 1 >= 0; iStartEvent--)
			{
				var eventInfoTmp = g_EventArray[iStartEvent-1];	
				if(eventInfoTmp.sCoords != eventInfo.sCoords)
					break;
					
			}
	
			for(; iEndEvent + 1 < g_iEventCount; iEndEvent++)
			{
				var eventInfoTmp = g_EventArray[iEndEvent+1];	
				if(eventInfoTmp.sCoords != eventInfo.sCoords)
					break;
			}
	
			// Set up popup message
			var sTypeDesc
			var sTxt = "";
			var sLink

			iCurrentEventCount = iEndEvent - iStartEvent + 1;

			for(var i = iStartEvent; i <= iEndEvent; i++)
			{
//				if(i > iStartEvent)
	//				sTxt += '<BR>';
	
				eventInfo = g_EventArray[i];
				switch(eventInfo.sSource)
				{
					case "E":
						switch(eventInfo.sType)
						{
							case "1":
								sTypeDesc = "Split";
								break;
							case "10":
								sTypeDesc = "Spinoff";
								break;
							case "11":
								sTypeDesc = "Stock Dividend";
								break;
							case "13":
								sTypeDesc = "Cash Dividend";
								break;
							case "14":
								sTypeDesc = "IPO";
								break;
							case "15":
								sTypeDesc = "Quiet Period";
								break;
							case "16":
								sTypeDesc = "Lockup";
								break;
							case "17":
								sTypeDesc = "Insider Buy";
								break;
							case "18":
								sTypeDesc = "Insider Sale";
								break;
							case "19":
								sTypeDesc = "Insider 144";
								break;
							case "20":
								sTypeDesc = "EPS Change";
								break;
						}
	
						if(eventInfo.sLabel.length > 0)
							sTxt += '<div class="StockEventBlock">' + eventInfo.sLabel + ' ' + sTypeDesc + ' on ' + eventInfo.sDate + '</div>';
						else
							sTxt += '<div class="StockEventBlock">' + sTypeDesc + ' on' + eventInfo.sDate + '</div>';
			
						break;
          case "S":
						switch(eventInfo.sType)
						{
							case "1":
								sTypeDesc = "News Releases";
								break;
							case "2":
								sTypeDesc = "Financial Results";
								break;
							case "3":
								sTypeDesc = "Webcasts";
								break;
							case "4":
								sTypeDesc = "Event";
								break;
						}
			
						if(eventInfo.sLabel.length > 0)
							{sTxt += '<div class="StockEventBlock"><a href="ChartEventRedirect.cfm?ChartEventID=' + eventInfo.sEventId + '&ChartEventType=' + eventInfo.sType + '&ChartEventCategory=' + eventInfo.sSource + '" target="_blank">' + eventInfo.sLabel + '</a><br />' + sTypeDesc + ' on ' + eventInfo.sDate + '</div>';}
						else
							sTxt += '<div class="StockEventBlock">' + sTypeDesc + ' on ' + eventInfo.sDate + '</div>';
		
						break;
					case "P":
						switch(eventInfo.sType)
						{
							case "1":
								sTypeDesc = "10K";
								break;
							case "2":
								sTypeDesc = "10Q";
								break;
							case "9":
								sTypeDesc = "6K or 8K";
								break;
							case "11":
								sTypeDesc = "DEF";
								break;
							case "13":
								sTypeDesc = "Insider";
								break;
						}
			
						if(eventInfo.sLabel.length > 0)
						{
							if(eventInfo.sType == "13")
								sLink = 'Form ' + eventInfo.sLabel;
							else
								sLink = eventInfo.sLabel;
								
								
						}
						else
							sLink = sTypeDesc;
			
						sTxt += '<div class="StockEventBlock">' + sLink + ' on ' + eventInfo.sDate + '</div>';
		
						break;
					case "I":
						switch(eventInfo.sType)
						{
							case "1":
								sTypeDesc = "Insider Buy(s)";
								break;
							case "2":
								sTypeDesc = "Insider Sell(s)";
								break;
							case "144":
								sTypeDesc = "Insider Form 144(s)";
								break;
						}

						if(eventInfo.sLabel.length > 0)
							sTxt += '<div class="StockEventBlock">' + eventInfo.sLabel + ' ' + sTypeDesc + ' on ' + eventInfo.sDate + '</div>';
						else
							sTxt += '<div class="StockEventBlock">' + sTypeDesc + ' on ' + eventInfo.sDate + '</div>';
			
						break;
				}
			}
	
			var msgObj = document.getElementById?document.getElementById("EventDesc"):document.all?d.all["EventDesc"]:d.layers["EventDesc"];
		
			msgObj.innerHTML = sTxt;

      //Chech browser
      var SH_Browser = navigator.appName;
      
      //Set location
      if(SH_Browser=="Microsoft Internet Explorer")
      {
        //Add marker location to graph location
  			var x = coors[0];
  			var y = coors[1] - 140;
        //Use array to set
      	document.getElementById("popupmsg").style.left= x + 'px';
      	document.getElementById("popupmsg").style.top= y + 'px';
      }
      else
      { 
  			var iStart = 0;
  			var rectArray = new Array(4);
  		
  			for(i = 0; i < 3; i++)
  			{
  				var iPos = eventInfo.sCoords.indexOf(',', iStart);
  				if(iPos >= 0)
  					rectArray[i] = parseInt(eventInfo.sCoords.substr(iStart, iPos));
  		
  				iStart = iPos + 1;
  			}
  			rectArray[3] = parseInt(eventInfo.sCoords.substr(iStart));
  		  
        //Add marker location to graph location
  			var x = rectArray[0] + coors[0];
  			var y = coors[1] - 100;
        
      	document.getElementById("popupmsg").style.left=x + 'px';
      	document.getElementById("popupmsg").style.top=y + 'px';
      }
			ShowPopup2(iEvent);
		}
	}
	catch(e)
	{
		alert("ShowPopup:  " + e);
	}
	
}


function ShowPopup2(iEvent)
{
	clearTimeout(sh_popuptimeout);
	document.getElementById("popupmsg").style.visibility='visible';
	document.getElementById("popupmsg").style.zIndex=1000;
}

function SH_FindPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
  {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
    {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
  //Return array
  return [curleft,curtop];
}

function HidePopup()
{
	//to make sure that the popup is visible at least for sometime even after moving away from the position 
	sh_popuptimeout = setTimeout("HidePopup2()",250);
}
function HidePopup2()
{
	
	try
	{
		if(document.getElementById("popupmsg").style.visibility != 'hidden')
		{
				document.getElementById("popupmsg").style.visibility='hidden';
				document.getElementById("popupmsg").style.zIndex=0;
		}
	}
	catch(e)
	{
		alert(e);
	}
}

function EventInfo(Coords, Source, Type, Date, CompanyId, EventId, Label) 
{
	this.sCoords = Coords;
	this.sSource = Source; 
	this.sType = Type; 
	this.sDate = Date; 
	this.sCompanyId = CompanyId; 
	this.sEventId = EventId;
	this.sLabel = Label; 
}

function AddEvent(Coords, Source, Type, Date, Label, CompanyId, EventId) 
{
	
	g_EventArray[g_iEventCount] = new EventInfo(Coords, Source, Type, Date, Label, CompanyId, EventId);
	g_iEventCount++;
	

}

