// ekfeed_month_top.js 
//
// javascript code to draw small calendar with EventKeeper EKFeed
// also includes Dynamic Drive TOOLTIPS code
// in case you're wondering, there's some code here that isn't really
// necessary but I like the URL month numbers to be standard notation e.g. 
// january = 1, but javascript arrays start with 0 so... fuss, fuss, fuss.

var Calendar = new Date();
var month, year;
var thisMonth = Calendar.getMonth();
var thisYear = Calendar.getFullYear();
var thisURL = document.location.href;

function GetMonthYear()
{
	if (thisURL.indexOf("ekm=") != -1)
	{
		varArray = document.location.href.split('?')[1].split('&');
	
		for(var x=0; x<varArray.length; x++)
		{
			var tmp = varArray[x].split('=');
			eval(unescape(tmp[0]) + '="' + unescape(tmp[1]) + '"');
			// alert(tmp[0]);alert(tmp[1]);
		}
	}
		
	if (isNotDefined("ekm"))
	{
		month = thisMonth;    // Returns month (0-11)
	}
	else if (ekm > 12)
	{
		alert("URL value for the month is too big\n\nResetting to December");
		ekm = 12;
		month = ekm - 1;
	}
	else if (ekm < 1)
	{
		alert("URL value for the month is too small\n\nResetting to January");
		ekm = 1;
		month = ekm - 1;
	}
	else
		month = ekm - 1;

	if (isNotDefined("eky"))
	{
		year = thisYear;	    // Returns year
	}
	else
		year = eky;
}

GetMonthYear();

// alert(month + ' & ' + year);

function isNotDefined(someVar) 
{ 
	return (typeof(window[someVar]) == "undefined")? true: false; 
} 

function EKShowMonthWithFeed(orgcode,month,year)
{


//  SET ARRAYS
var day_of_week = new Array('S','M','T','W','T','F','S');
var month_of_year = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var mon_of_year = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

//  DECLARE AND INITIALIZE VARIABLES

// month += monthOffset;

var ekm = month + 1;
var eky = year;

if (month > 11) // 
{
 	month = month - 12;
	year = year - 1 + 2;
}
else if (month < 0)
{
	month = month + 12;
	year =  year - 1;
}

var nextMonth = ekm + 1;
var prevMonth = ekm - 1;
var nextYear = eky;
var prevYear = eky;

if (nextMonth > 12)
{
 	nextMonth = 1;
	nextYear = nextYear - 0 + 1;
}
else if (prevMonth < 1)
{
	prevMonth = 12;
	prevYear =  prevYear - 1;
}

var nextURL = thisPage + "?ekm=" + nextMonth + "&eky=" + nextYear;
var prevURL = thisPage + "?ekm=" + prevMonth + "&eky=" + prevYear;

var today = Calendar.getDate();    // Returns day (1-31)
var weekday = Calendar.getDay();    // Returns day (1-31)

var DAYS_OF_WEEK = 7;    // "constant" for number of days in a week
var DAYS_OF_MONTH = 31;    // "constant" for number of days in a month
var cal;    // store the string to be displayed

Calendar.setDate(1);    // Start the calendar day at '1'
Calendar.setMonth(month);    // Start the calendar month at now
Calendar.setYear(year);    // Start the calendar year at now

var TR_start = '<TR>';
var TR_end = '</TR>';

var TD_start_weekday = '<TD class="dow" height="3">';
var TD_start_today = '<TD class="today">';
var TD_start_boring = '<TD class="boring">';
var TD_start_events = '<TD class="events" align="center">';
var TD_start_padding = '<TD class="padding">';

var TD_end = '</TD>';

cal =  '<TABLE class="outer" align=center><TR><TD>';
cal += '<TABLE class="ekcal">' + TR_start;
cal += '<TD class="header"><a href=' + prevURL + '> << </a>' + TD_end;
cal += '<TD COLSPAN="5" class="header">';
cal += month_of_year[month]  + '   ' + year + TD_end;
cal += '<TD class="header"><a href=' + nextURL + '> >> </a>' + TD_end;
cal += TR_end;

// Day of week header
cal += TR_start;
for(index=0; index < DAYS_OF_WEEK; index++)
{
	cal += TD_start_weekday + day_of_week[index] + TD_end;
}
cal += TR_end;

cal += TR_start;

// FILL IN BLANK GAPS UNTIL TODAY'S DAY
for(index=0; index < Calendar.getDay(); index++)
	cal += TD_start_padding + ' ' + TD_end;

// LOOPS FOR EACH DAY IN CALENDAR
for(index=0; index < DAYS_OF_MONTH; index++)
{
if( Calendar.getDate() > index )
{
  	// get the next day to deal with
  	week_day = Calendar.getDay();

  	// START NEW ROW FOR FIRST DAY OF WEEK
	if(week_day == 0)
  		cal += TR_start;
	
		if(week_day != DAYS_OF_WEEK)
		{
	
			// SET VARIABLE INSIDE LOOP FOR INCREMENTING PURPOSES
			var day  = Calendar.getDate();
			var tipLen;
			// build date string to compare to Event date
			var compDay = mon_of_year[month] + ' ' + day + ', ' + year;
	  					
			// loop through event array looking for events that match current day
			var numEvents = 0;
			for (i=0; i<evtArray.length; i++)
			{
				if (evtArray[i][evt_date] == compDay)
				{
					if (numEvents == 0)
					{
						var linkurl = 'http://www.eventkeeper.com/code/events.cfm?curOrg=' + orgcode;
						linkurl += "#" + evtArray[i][evt_id];
		
						var dayTitle = compDay + ' ' + evtArray[i][evt_time] + ' ' + evtArray[i][evt_name];
						dayTitle += ' [' + evtArray[i][evt_key1] + ']';
					}
					else
					{
						dayTitle += '<br>' + compDay + ' ' + evtArray[i][evt_time] + ' ' + evtArray[i][evt_name];
						dayTitle += ' [' + evtArray[i][evt_key1] + ']';

					}
					numEvents++;
					
				}	
			}

		if (numEvents > 0)
		{
			tipLen = (dayTitle.length * 4)/numEvents;
			
			var daylink = '<a href="' + linkurl + '" target="_blank" ';
			// daylink += dayTitle + '"';
			daylink += ' onMouseover="ddrivetip(' + '\'' + dayTitle + '\'' + ',' + tipLen + ')" onMouseout="hideddrivetip()">';
			day = daylink + day + '</a>';
		}
	
	  	// format this day's cell
  		if( (today==Calendar.getDate()) && (month==thisMonth) && (year==thisYear)) // today
  		{
			cal += TD_start_today + day + TD_end;
		}
		else  // day has events 
		if (numEvents> 0)
  			cal += TD_start_events + day + TD_end;

  		// boring day
  		else
  			cal += TD_start_boring + day + TD_end;
  	}

  	// END ROW FOR LAST DAY OF WEEK
  	if(week_day == DAYS_OF_WEEK)
  		cal += TR_end;
  }

  // INCREMENTS UNTIL END OF THE MONTH
  Calendar.setDate(Calendar.getDate()+1);

}// end for loop

cal += '</TR></TABLE></TD></TR></TABLE>';

//  PRINT CALENDAR
document.write(cal);

//  End -->

}

/***********************************************
* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetfromcursorX=12 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip

var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>') //write out tooltip DIV
document.write('<img id="dhtmlpointer" src="arrow2.gif">') //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var nondefaultpos=false
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth){
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=curX-tipobj.offsetWidth+"px"
nondefaultpos=true
}
else if (curX<leftedge)
tipobj.style.left="5px"
else{
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
pointerobj.style.left=curX+offsetfromcursorX+"px"
}

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight){
tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
nondefaultpos=true
}
else{
tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
pointerobj.style.top=curY+offsetfromcursorY+"px"
}
tipobj.style.visibility="visible"
if (!nondefaultpos)
pointerobj.style.visibility="visible"
else
pointerobj.style.visibility="hidden"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
pointerobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

// EKShowMonthWithFeed(myOrgCode,month,year);
