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 sh_datediff(sh_eventdate)
{
	var sh_evtdt=sh_eventdate;
	
	sh_eventyr=sh_evtdt.substring(6);
	sh_eventdt=sh_evtdt.substring(3,5);
	sh_eventmt=sh_evtdt.substring(0,2);
	

	var sh_currentTime = new Date();
	var sh_year = sh_currentTime.getYear();
	if (sh_year < 1000)
		sh_year+=1900;
	var sh_daymonth=sh_currentTime.getDate()
	if (sh_daymonth<10)
		sh_daymonth="0"+sh_daymonth;
	var sh_day = sh_currentTime.getDay();
	
	var sh_month = sh_currentTime.getMonth() + 1;
	if (sh_month<10)
		sh_month="0"+sh_month;
	
	var sh_currentyr = new Date(sh_year,sh_month,sh_daymonth);
	var sh_eventdate1 = new Date(sh_eventyr,sh_eventmt,sh_eventdt);
	
	var sh_noofdays=Math.floor((sh_currentyr-sh_eventdate1)/ (1000 * 60 * 60 * 24));

	return sh_noofdays;
	
	
	//alert(Math.floor((new Date()-new Date(2006,07,31))/ (1000 * 60 * 60 * 24)))
	
	
}



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 ShowPopup(iEvent)
{ 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 Release";
								break;
							case "2":
								sTypeDesc = "Financial Results";
								break;
							case "3":
								sTypeDesc = "Webcast";
								break;
							case "4":
								sTypeDesc = "Event";
								break;
						}
						
						if(eventInfo.sLabel.length > 0)
							{
								//checking if they are events - if they are make sure that older events are not linkable
								if(eventInfo.sType == "4")
								{
									//finding the total number of days between current date and event date
									sh_datediffvalue=sh_datediff(eventInfo.sDate);
									if (sh_datediffvalue >= 1) //if event is more than a yr old (rolling yr) do not show links, this number cab n be changed to 620 for 2 years 
									{
										sTxt += '<div class="StockEventBlock">' + eventInfo.sLabel + '<br />' + sTypeDesc + ' on ' + eventInfo.sDate + '</div>';	
										
									}
									else
									{
										sTxt += '<div class="StockEventBlock">' + eventInfo.sLabel + '<br />' + sTypeDesc + ' on ' + eventInfo.sDate + '</div>';	
									}
								}
								else
								{
								sTxt += '<div class="StockEventBlock">' + eventInfo.sLabel + '<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;

			if(!isIE)
			{

				if(iCurrentEventCount <= 3)
				{
					msgObj.style.height = 25 * iCurrentEventCount;
				}
				else
				{
					msgObj.style.height = 90;
				}
			}
			else
			{
				if(iCurrentEventCount <= 3)
				{
					msgObj.style.height = 25 * iCurrentEventCount;
				}
				else 
				{
					msgObj.style.height = 90;
				}
			}

			// Now position the popup
			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));
		
			var x = rectArray[0] + (rectArray[2] - rectArray[0]) / 2;
			var y = rectArray[1];
	
			var newx = iCurrentX - popupObj.offsetWidth/2;
			var newy = iCurrentY - popupObj.offsetHeight/2;
			var iEventHeight = rectArray[3] - rectArray[1];
			var iPopupHeight = msgObj.offsetHeight;

// Move Notification to 10,10 from the mouse	
			popupObj.style.left = (iCurrentX+10)+"px";
			popupObj.style.top = (iCurrentY+10)+"px";

			msgObj.scrollTop = 0;

//      alert("Left: "+popupObj.style.left+"; Top: "+popupObj.style.top+";");
    
      
			ShowPopup2();
		}
	}
	catch(e)
	{
		alert("ShowPopup:  " + e);
	}
	
}

function ShowPopup2()
{
	clearTimeout(sh_popuptimeout);
	document.getElementById("popupmsg").style.visibility='visible';
	document.getElementById("popupmsg").style.zIndex=1000;
}

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++;
	

}

