var tb = 't'; // (t or b) sets top or bottom
var headbg = '#2aace2';  
var todaybg = '#a8c8d6';
var textclr = '#000000'; 
var linkclr = '#ff9999'; 
var fweight = 'normal';
var bubbleImg ='popup bubble3.png';
var popcolor = '42, 172, 226';
var noMessage =  'No event messages for today'; //

var dA = new Array();
var x = 0;
// first 8 characters in ccyymmdd format for single date events
// first 8 characters in 0000mmdd format for every year events

//call built in function for today's date and save it in a variable. Use it to compare with year+mmdd blah instead of selDate

var pageLoaded = 0;
window.onload = function() 
{
	pageLoaded = 1;
}

function loaded(i,f)
{
	if (document.getElementById && document.getElementById(i) != null)
		f();
	else if (!pageLoaded)
		setTimeout('loaded(\''+i+'\','+f+')',100);
}

function monthlength(month,year)
{
	var dd = new Date(year, month, 0);
	return dd.getDate();
}

var moy = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var today = new Date();
var selDate = today.getFullYear()+getmmdd(today.getMonth()+1,today.getDate());
var todayDate = selDate;

function dispCal(yy,mm) 
{
	if (mm < 0 || mm > 12)
	{	alert('month must be between 1 and 12');
		 return false;
	}
	if (yy != 0 && (yy < 1901 || yy > 2100))
	{
		alert('year must be after 1900 and before 2101');
		return false;
	}
	
	var dow = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
	var calendar = new Date(); 
	var today = calendar.getDate();
	calendar.setDate(1);
	if (yy > 1900)
		calendar.setFullYear(yy);
	if (mm > 0)
		calendar.setMonth(mm - 1);
	var yy = year = calendar.getFullYear();
	var month = calendar.getMonth();
	calendar.setDate(today);
	var weekday = calendar.getDay();
	var daysInMonth = monthlength(month+1,year);
	//sets the background color
	var hilite_start = '<td width="30" style="background:' + todaybg + '" align="center"><b>';
	var td_start = '<td width="30" align="center">';
	//Displays the current month
	var cal = '<div id="cal"><div style="border:1pt solid #cccccc;width:220px"><table border="0" cellspacing="0" cellpadding="2" align="center"><tr><td colspan="9" style="background:' + headbg + '" align="center"><b>' + moy[month]  + '  ' + year + '<\/b><\/td><\/tr><tr>';
	for(dex=0; dex < 7; dex++)
	{
		cal += td_start + dow[dex] + '</td>';
	}
	cal += '<\/tr><tr>';
	
	var day2 = today;
	
	for (dex = today; dex > 6; dex -=7)
		day2 = dex;
		
	weekday -= day2 - 1;
	
	while (weekday < 0)
		weekday += 7;
		
	for(dex=0; dex < weekday; dex++)
		cal += td_start + ' <\/td>';
		
	for(dex=1; dex <= daysInMonth; dex++)
	{
		if(weekday == 7)
		{
			cal += '</tr><tr>';
			weekday = 0;
		}
		if(todayDate==year+getmmdd(month+1,dex))
			cal += hilite_start +'<span '+clickDate(dex,month,year) + '>'+ dex + '<\/span><\/b><\/td>';
		else
			cal += td_start + '<span '+clickDate(dex,month,year) + '>' + dex + '<\/span><\/td>';
		
		weekday += 1;
	}

	for(dex=weekday; dex < 7; dex++)
		cal += td_start + ' <\/td>';
	cal += '<\/tr><\/table><\/div>';
	
	if (document.getElementById)
	{
		var mmb = month;
		mm = month + 1;
		var yya = yyb = yy;
		if (mmb <1)
		{
			mmb += 12;
			yyb--;
		}
		var mma = month + 2;
		if (mma > 12)
		{
			mma -= 12; yya++;
		}
		var yb = yy -1;
		var ya = yy +1;
		cal += '<table border="0" cellspacing="0" cellpadding="2" width="200"><tr><td><a href="#" onclick="if (cala = dispCal('+yb+','+mm+')) {document.getElementById(\'cal\').innerHTML = cala; return false;}"><span style="color:#2aace2; font-weight: bold;"><<</span><span style="color:black">Year&nbsp&nbsp&nbsp</span></a></td><td><a href="#" onclick="if (cala = dispCal('+yyb+','+mmb+')) {document.getElementById(\'cal\').innerHTML = cala; return false;}"><span style="color:black"><span style="color:#2aace2; font-weight: bold;"><</span> Month&nbsp&nbsp</span></a></td><td align="right"><a href="#" onclick="if (cala = dispCal('+yya+','+mma+')) {document.getElementById(\'cal\').innerHTML = cala; return false;}"><span style="color:black" >Month <span style="color:#2aace2; font-weight: bold;">>&nbsp&nbsp&nbsp</span></span></a></td><td align="right"><a href="#" onclick="if (cala = dispCal('+ya+','+mm+')) {document.getElementById(\'cal\').innerHTML = cala; return false;}"><span style="color:black">Year <span style="color:#2aace2; font-weight: bold;">>></span></span></a></td></tr></table>';
	}
	else
	{
		cal += '<div> </div>';
	}
	
	cal += '</div>';
	return cal;
}

function start()
{
	//var x = '<div id="calDate" style="border:1pt solid #cccccc;width:220px"><\/div>';

	//This is the popup that shows at hover over a date;
//    var x = '<div id="shadow" style="visibility:hidden; position:absolute; height:100px; top:30; left:-180px; background-color: #666; "> <div id="hoverpopup" style="visibility:hidden; position: relative; height:100px; width:200px; bottom: 3px; right: 3px; background-color:rgb('+popcolor+');"><table bgcolor="#0000FF"><tr><td><font color="#FFFFFF">This is my popup</font></td></tr></table></div></div>';
      var x = '<div id="shadow" style="visibility:hidden; position:absolute; width:215px; height:110px; top:30; left:-180px; background-image:url(\'Images/popups/popup bubble2.png\'); background-repeat:no-repeat; /*background-image:url(\'Images/popups/popup bubble2.png\') */ "> <div id="hoverpopup" style="visibility:hidden; position: relative; height:100px; width:190px; bottom: -15px; right: -10px; color:#000000">This is my popup</div></div>';
//    var x = '<div id="hoverpopup" style="visibility:hidden; position: relative; color:#000000; width:200px; right:3px ">This is my popup</div>';

	var y = '';
	if (tb == 't')
		y = x + dispCal(0,0);
	else y = dispCal(0,0) + x;
	
	document.getElementById('calendar').innerHTML = y;
	ev();
}


loaded('calendar',start);

function clickDate(day, month, year)
{
	var ct = nextDate(year + getmmdd(month+1,day));
	if (ct == '')
		{
			ct = nextDate('0000' + getmmdd(month+1,day));
		}
	if (ct == '')
		{
			return 'style="color:'+textclr+'"';
		}
	else

	//colours the date, sets properties of it
	return 'style="cursor:pointer;color:'+linkclr+'; font-weight:'+fweight+'" onmouseover="selDate = '+year+getmmdd(month+1,day)+'; isDate(' + day + ',' + month + ',' + year + '); showPopup(event);return false;" onmouseout="hidePopup();"';
}

//Accepts a date then runs through all the events to check if the date matches any of the events.
//Otherwise it returns everything after the eighth character.
function nextDate(yymmdd)
{
	var x = dA.length;
	for (var i = 0; i < x; i++)
	{
		fweight="bold";
					
		if (dA[i].substr(0,8) == yymmdd)
		{
			
			if(dA[i].substr(9,4) == "STDY")
			{
				linkclr="#009f07"; //009f4f
				popupImg="stdy bubble.png";
				//popcolor="200, 240, 205";
			}
			else if(dA[i].substr(9,4) == "PIAG")
			{
			    linkclr="#0061a3";
			    popupImg="piag bubble.png";
			    //popcolor="170, 220, 240";
			}
			else if(dA[i].substr(9,4) == "0IJC")
			{
			    linkclr="#f78704"; 
			    popupImg="ijc bubble.png"
			    //popcolor="200, 175, 130";
			}
			else if(dA[i].substr(9,4) == "OTHR")
			{   
			    linkclr="#ffcc00";
			    popupImg="othr bubble.png";
   			    //popcolor="255, 240, 180";
   			    //add a black border colour if possible to contrast the yello
			}
			else if(dA[i].substr(9,4) == "0TWG")
			{   
			    linkclr="#f01919";
			    popupImg="twg bubble.png";
			}
			else if(dA[i].substr(9,4) == "00TT")
			{   
			    linkclr="#8c00ba";
			    popupImg="tt bubble.png";
			}
			return dA[i].substr(13);
		}
	}
	
	return '';
}

//selDate variable is the one the date displayed above the ct message

function isDate(dayVal,monthVal,yearVal)
{
	var ct = nextDate(yearVal + getmmdd(monthVal+1,dayVal));
	if (ct == '')
		ct = nextDate('0000' + getmmdd(monthVal+1,dayVal));
	if (ct == '')
		ct = noMessage;
		
	//var dateToDisplay = selDate.slice(0,4) + '/' + selDate.slice(4,-2) + '/' + selDate.slice(6);
	//selDate = dateToDisplay;

//	ct = '<font style="color:'+linkclr+'">' + ct + '</font>';
//	ct = '<div id="shadow" style="visibility:hidden; position:absolute; background-repeat:no-repeat; height:110px; width:210px; left:-180px; background-image:url(\'images/'+popupImg+'\'); "><br />'  + ct + '</div>';
	
	//here change the shado div property to include the correct popup image
	newImage = "url('Images/popups/"+popupImg+"')"; //syntax (double and single quotes) are extremely important for it to work in Firefox
	 //"url('http://image.jpg')"
	
	document.getElementById('shadow').style.backgroundImage = newImage;
	ct = '<b>' + ct + '</b>';

	//alert(ct);

//This is where the date and message are printed out. - change it to a popup
	//document.getElementById('calDate').innerHTML = selDate + ':<br \/>' + ct;
	//document.getElementById('hoverpopup').innerHTML = selDate + ':<br \/>' + ct;
	document.getElementById('hoverpopup').innerHTML = ct;

	return false;
}

function getmmdd(mm,dd)
{
	return (mm > 9 ? '' + mm : '0' + mm) + (dd > 9 ? dd : '0' + dd);
}

function ev()
{
	var ct = nextDate(selDate);
	var ct = nextDate(selDate);
	if (ct == '')
		ct = nextDate('0000' + selDate.substr(4));
	if (ct == '')
		ct = noMessage;

	var dateToDisplay = selDate.slice(0,4) + '/' + selDate.slice(4,-2) + '/' + selDate.slice(6);
	selDate = dateToDisplay;
	
//	alert(ct);

//	document.getElementById('calDate').innerHTML = selDate + ':<br \/> ' +ct;
//	document.getElementById('hoverpopup').innerHTML = selDate + ':<br \/>' + ct;
	document.getElementById('hoverpopup').innerHTML = ct;
}


///////// functions for showing event in a popup on hover ////////

var IE = document.all?true:false;

if (!IE) document.captureEvents(Event.MOUSEMOVE);

document.onmouseclick = showPopup;

var x =0;
var y=0;

function showPopup(e) {


 if (IE) { // grab the x-y pos.s if browser is IE
    x = event.clientX + document.body.scrollLeft;
    y = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    x = e.pageX;
    y = e.pageY;
  }  

/*
  x = (window.Event) ? e.pageX : event.clientX + 45;
  y = (window.Event) ? e.pageY : event.clientY + 5;
*/
 // document.ee.sd.value = x+":"+y;
  
  hp = document.getElementById("hoverpopup");
  shd = document.getElementById("shadow");

  shd.style.top = y - 600 + "px";
  shd.style.left = x - 1200 + "px"; //add "px" or it won't work for firefox
	
/*
  document.ee.sd2.value = shd.style.left; 
  document.ee.sd3.value = shd.style.top ;
  */	
  hp.style.visibility = "Visible";
  shd.style.visibility = "Visible";


}

function hidePopup()
{
	hp = document.getElementById("hoverpopup");
	shd = document.getElementById("shadow");
	hp.style.visibility = "Hidden";
	shd.style.visibility = "Hidden";
}

dA[x++] = "20100325 0IJC <b> March 25</b><br />St Nicholas' Day";
dA[x++] = "20100622 00TT <b>June 22-24</b><br /> Joint Study Board and Task Team Meeting - Burlington, ON ";
dA[x++] = "20100623 00TT <b>June 22-24</b><br /> Joint Study Board and Task Team Meeting - Burlington, ON ";
dA[x++] = "20100624 STDY <b>June 22-24</b><br /> Joint Study Board and Task Team Meeting - Burlington, ON ";
dA[x++] = "20100715 PIAG <b>July 15</b><br /> Public Interest Advisory Group Meeting - Muskegon, MI ";
dA[x++] = "20100614 OTHR <b>June 14-18</b><br /> CWRA Conference - Vancouver, BC";
dA[x++] = "20100615 OTHR <b>June 14-18</b><br /> CWRA Conference - Vancouver, BC";
dA[x++] = "20100616 OTHR <b>June 14-18</b><br /> CWRA Conference - Vancouver, BC";
dA[x++] = "20100617 OTHR <b>June 14-18</b><br /> CWRA Conference - Vancouver, BC";
dA[x++] = "20100618 OTHR <b>June 14-18</b><br /> CWRA Conference - Vancouver, BC";
dA[x++] = "20100705 OTHR <b>July 5-8</b><br /> Water 2010 Conference - Quebec City, QC";
dA[x++] = "20100706 OTHR <b>July 5-8</b><br /> Water 2010 Conference - Quebec City, QC";
dA[x++] = "20100707 OTHR <b>July 5-8</b><br /> Water 2010 Conference - Quebec City, QC";
dA[x++] = "20100708 0TWG <b>July 5-8</b><br /> Hydroclimate Group Meeting - Quebec City, QC";
dA[x++] = "20100609 0TWG <b>June 9-10</b><br /> Information Management GIS Meeting - Ottawa, ON";
dA[x++] = "20100610 0TWG <b>June 9-10</b><br /> Information Management GIS Meeting - Ottawa, ON";
dA[x++] = "20100525 PIAG <b>May 25</b> <br /> Public Interest Advisory Group Teleconference";
dA[x++] = "20100714 0TWG <b>July 14</b><br /> Water Use TWG Meeting - Ann Arbor, Michigan";
dA[x++] = "20100921 STDY <b>September 21-23</b><br /> Study Board Meeting - Romulus, Michigan";
dA[x++] = "20100922 STDY <b>September 21-23</b><br /> Study Board Meeting - Romulus, Michigan";
dA[x++] = "20100923 STDY <b>September 21-23</b><br /> Study Board Meeting - Romulus, Michigan";
dA[x++] = "20101019 0IJC <b>October 19-22</b><br /> Semi-Annual Meeting - Ottawa, Ontario";
dA[x++] = "20101020 0IJC <b>October 19-22</b><br /> Semi-Annual Meeting - Ottawa, Ontario";
dA[x++] = "20101021 0IJC <b>October 19-22</b><br /> Semi-Annual Meeting - Ottawa, Ontario";
dA[x++] = "20101022 0IJC <b>October 19-22</b><br /> Semi-Annual Meeting - Ottawa, Ontario";
dA[x++] = "20100920 00TT <b>September 20-21</b><br />Task Team Meeting - Romulus, Michigan";
dA[x++] = "20100921 00TT <b>September 20-21</b><br />Task Team Meeting - Romulus, Michigan";
dA[x++] = "20101027 PIAG <b>October 27</b><br />PIAG Meeting - Thunderbay";
dA[x++] = "20100728 0TWG <b>July 28</b><br />AM Teleconference - 10:00-12:00";
dA[x++] = "20101116 OTHR <b>November 16-17</b><br />CCGLHHD Coordinating Committee - Chicago, IL";
dA[x++] = "20101117 OTHR <b>November 16-17</b><br />CCGLHHD Coordinating Committee - Chicago, IL";
dA[x++] = "20101014 0TWG <b>October 14</b><br />Plan Formulation Meeting";
dA[x++] = "20101015 0TWG <b>October 15</b><br />Plan Formulation Meeting";
dA[x++] = "20101015 0TWG <b>October 15</b><br />Plan Formulation Meeting - Burlington, ON";
dA[x++] = "20100914 0TWG <b>September 14-15</b><br />AMG Meeting";
dA[x++] = "20100915 0TWG <b>September 14-15</b><br />AMG Meeting<br /><b>September 15-16</b><br />IM TWG Meeting - Ottawa, ON";
dA[x++] = "20100916 0TWG <b>September 15-16</b><br />IM TWG Meeting - Ottawa, ON";
dA[x++] = "20101208 OTHR <b>December 08-10</b><br />Firm Workshop";
dA[x++] = "20101209 OTHR <b>December 08-10</b><br />Firm Workshop";
dA[x++] = "20101210 OTHR <b>December 08-10</b><br />Firm Workshop";
dA[x++] = "20101026 0TWG <b>October 26</b><br />IM TWG Meeting";
dA[x++] = "20101130 STDY <b>November 30-December 02</b><br />Board Meeting - Windsor";
dA[x++] = "20101201 STDY <b>November 30-December 02</b><br />Board Meeting - Windsor";
dA[x++] = "20101202 STDY <b>November 30-December 02</b><br />Board Meeting - Windsor";
dA[x++] = "20101102 0TWG <b>November 02-04</b><br />PFEG/AMG Meeting - Burlington";
dA[x++] = "20101103 0TWG <b>November 02-04</b><br />PFEG/AMG Meeting - Burlington";
dA[x++] = "20101104 0TWG <b>November 02-04</b><br />PFEG/AMG Meeting - Burlington";
dA[x++] = "20110110 0TWG <b>January 10-14</b><br />Plan Formulation & Evaluation Meeting - Montreal, QC";
dA[x++] = "20110111 0TWG <b>January 10-14</b><br />Formulation & Evaluation Meeting - Montreal, QC";
dA[x++] = "20110112 0TWG <b>January 10-14</b><br />Formulation & Evaluation Meeting - Montreal, QC";
dA[x++] = "20110113 0TWG <b>January 10-14</b><br />Formulation & Evaluation Meeting - Montreal, QC";
dA[x++] = "20110114 0TWG <b>January 10-14</b><br />Formulation & Evaluation Meeting - Montreal, QC";
dA[x++] = "20110201 0TWG <b>February 01-02</b><br />Adaptive Management Workshop on Institutional Analysis - Windsor, ON";
dA[x++] = "20110202 0TWG <b>February 01-02</b><br />Adaptive Management Workshop on Institutional Analysis - Windsor, ON";
dA[x++] = "20110203 0TWG <b>February 03</b><br />Ecosystem Restoration Analysis Workshop - Windsor, ON";
dA[x++] = "20110222 STDY <b>February 22-24</b><br />Study Board 18th Meeting - Windsor, ON";
dA[x++] = "20110223 STDY <b>February 22-24</b><br />Study Board 18th Meeting - Windsor, ON";
dA[x++] = "20110224 STDY <b>February 22-24</b><br />Study Board 18th Meeting - Windsor, ON";
dA[x++] = "20110224 PIAG <b>February 24-25</b><br />PIAG Meeting - Windsor, ON";
dA[x++] = "20110225 PIAG <b>February 24-25</b><br />PIAG Meeting - Windsor, ON";
dA[x++] = "20110411 STDY <b>April 11-12</b><br />Study Board 19th Meeting - Washington, DC";
dA[x++] = "20110412 STDY <b>April 11-12</b><br />Study Board 19th Meeting - Washington, DC";
dA[x++] = "20110413 PIAG <b>April 13-14</b><br />PIAG Meeting - Washington, DC";
dA[x++] = "20110414 PIAG <b>April 13-14</b><br />PIAG Meeting - Washington, DC";
dA[x++] = "20110607 STDY <b>June 07-09</b><br />Study Board 20th Meeting - Niagara Falls, ON";
dA[x++] = "20110608 STDY <b>June 07-09</b><br />Study Board 20th Meeting - Niagara Falls, ON";
dA[x++] = "20110609 STDY <b>June 07-09</b><br />Study Board 20th Meeting - Niagara Falls, ON";
