// JavaScript Document

var mX;
var mY;

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function follow(evt)
{
	mX = parseInt(mouseX(evt));
	mY = parseInt(mouseY(evt));
}

document.onmousemove = follow;
                    

function DaysBetween(date1, date2)
{
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();
    var difference_ms = Math.abs(date1_ms - date2_ms);
    return Math.round(difference_ms/ONE_DAY);
}


function Calendar(tag, dates, events)
{
	this.Today = new Date();
	this.currDate = new Date();
	//if(this.currDate.getYear()<1000) this.currDate.setFullYear(this.currDate.getYear()+1900);
	this.div = document.getElementById(tag);
	this.divid = tag;
	this.div.obj = this;
	this.dates = dates;
	this.events = events;
	
	this.tipTimeout = null;
	
	this.tooltip = document.createElement("DIV");
	this.tooltip.className = "calendar_tooltip";

	document.body.appendChild(this.tooltip);
		
	this.getMonth = function()
	{
		var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
		return months[this.currDate.getMonth()];
	}
	
	this.getYear = function()
	{
		return this.currDate.getFullYear();
	}
	
	this.ShowToolTip = function(i)
	{
		this.tooltip.innerHTML = "<div class='calendar_tooltipimg'><img src='Images/arrowup.gif' height='12' /></div><div class='calendar_tooltipbody'>"+this.events[i]+"</div>";
		this.tooltip.style.left = (mX - 170) + "px";
		this.tooltip.style.top = (mY) +"px";
		this.tooltip.style.visibility = "visible";
		if(this.tipTimeout) clearTimeout(this.tipTimeout);
	}
	
	this.HideToolTipDo = function()
	{
		this.tooltip.style.visibility = "hidden";
	}
	
	this.HideToolTip = function()
	{
		this.tipTimeout = setTimeout('document.getElementById("'+this.divid+'").obj.HideToolTipDo()', 300);
	}
	
	this.SelectPrevDate = function(date)
	{
		if(this.currDate.getMonth()==0)
		{
			this.currDate.setMonth(11);
			this.currDate.setFullYear(this.currDate.getFullYear()-1);
		}
		else
			this.currDate.setMonth(this.currDate.getMonth()-1);
		this.currDate.setDate(date);
		this.populate();
	}
	
	this.SelectDate = function(date)
	{
		this.currDate.setDate(date);
		this.populate();
	}
	
	this.SelectNextDate = function(date)
	{
		if(this.currDate.getMonth()==11)
		{
			this.currDate.setMonth(0);
			this.currDate.setFullYear(this.currDate.getFullYear()+1);
		}
		else
			this.currDate.setMonth(this.currDate.getMonth()+1);
		this.currDate.setDate(date);
		this.populate();
	}
	
	this.PrevYear = function()
	{
		this.currDate.setFullYear(this.currDate.getFullYear()-1);
		this.populate();
	}
	
	this.PrevMonth = function()
	{
		if(this.currDate.getMonth()==0)
		{
			this.currDate.setMonth(11);
			this.currDate.setFullYear(this.currDate.getFullYear()-1);
		}
		else
			this.currDate.setMonth(this.currDate.getMonth()-1);
		this.populate();
	}
	
	this.GoToday = function()
	{
		this.currDate.setFullYear(this.Today.getFullYear());
		this.currDate.setMonth(this.Today.getMonth());
		this.currDate.setDate(this.Today.getDate());
		this.populate();
	}
	
	this.NextMonth = function()
	{
		if(this.currDate.getMonth()==11)
		{
			this.currDate.setMonth(0);
			this.currDate.setFullYear(this.currDate.getFullYear()+1);
		}
		else
			this.currDate.setMonth(this.currDate.getMonth()+1);
		this.populate();
	}
	
	this.NextYear = function()
	{
		this.currDate.setFullYear(this.currDate.getFullYear()+1);
		this.populate();
	}
	
	this.populate = function()
	{
		var d = new Date(this.currDate.getFullYear(),this.currDate.getMonth(), 1);
		var pre = d.getDay();
		d = new Date(this.currDate.getFullYear(), this.currDate.getMonth()+1, 0);
		var cnt = d.getDate();
		d = new Date(this.currDate.getFullYear(), this.currDate.getMonth(), 0);
		var lst = d.getDate();
		
		var pst = 42-cnt-pre;
		var dates = new Array(42);
		var styles = new Array(42);
		var clickcmd = new Array(42);
		var i;
		
		for(i=0;i<pre;i++)
		{
			dates[i] = lst-pre+i+1;
			styles[i] = ' otherDay';
			clickcmd[i] = "onclick='CalendarSelectPrevDate(\""+this.divid+"\", "+(lst-pre+i+1)+")'";
		}
		for(i=0;i<cnt;i++)
		{
			dates[pre+i] = i+1;
			styles[pre+i] = '';
			clickcmd[pre+i] = "onclick='CalendarSelectDate(\""+this.divid+"\", "+(i+1)+")'";
		}
		if((this.currDate.getMonth()==this.Today.getMonth()) && (this.currDate.getYear()==this.Today.getYear())) styles[pre+this.Today.getDate()-1] += ' today';
		
		for(i=0;i<pst;i++)
		{
			dates[pre+cnt+i] = i+1;
			styles[pre+cnt+i] = ' otherDay';
			clickcmd[pre+cnt+i] = "onclick='CalendarSelectNextDate(\""+this.divid+"\", "+(i+1)+")'";
		}
		var start = new Date(this.currDate.getFullYear(), this.currDate.getMonth()-1, lst-pre+1);
		var end = new Date(this.currDate.getFullYear(), this.currDate.getMonth()+1, pst);
		for(i=0;i<this.dates.length; i++)
		{
			if(this.dates[i]>=start && this.dates[i]<=end)
			{
				styles[DaysBetween(this.dates[i], start)] += ' event';
				clickcmd[DaysBetween(this.dates[i], start)] += ' onmouseover="CalendarTooltip(\''+this.divid+'\', '+i+')"';
				clickcmd[DaysBetween(this.dates[i], start)] += ' onmouseout="CalendarTooltipHide(\''+this.divid+'\')"';
			}
		}
		styles[pre+this.currDate.getDate()-1] += ' selected';
		var str = '<table>';
		str += '<thead>';
        str += '<tr>';
        str += '<td class="supertitle" colspan="7">Robosapiens Calendar</td>';
        str += '</tr>';
 		str += '<tr>';
        str += '<tr>';
        str += '<td class="title" colspan="7">' + this.getMonth() + ' ' + this.getYear() + '</td>';
        str += '</tr>';
 		str += '<tr>';
        str += '<td class="button" onclick="CalendarYearSub(\''+this.divid+'\')">&#171</td>';
        str += '<td class="button" onclick="CalendarMonthSub(\''+this.divid+'\')">&#139</td>';
        str += '<td class="button" onclick="CalendarToday(\''+this.divid+'\')" colspan="3">Today</td>';
        str += '<td class="button" onclick="CalendarMonthAdd(\''+this.divid+'\')">&#155</td>';
        str += '<td class="button" onclick="CalendarYearAdd(\''+this.divid+'\')">&#187</td>';
      	str += '</tr>';
      	str += '<tr>';
        str += '<th class="weekend">S</th>';
        str += '<th>M</th>';
        str += '<th>T</th>';
        str += '<th>W</th>';
        str += '<th>T</th>';
        str += '<th>F</th>';
        str += '<th class="weekend">S</th>';
      	str += '</tr>';
    	str += '</thead>';
    	str += '<tbody>';
      	str += '<tr class="days">';
        str += '<td '+clickcmd[0]+' class="weekend'+styles[0]+'">'+dates[0]+'</td>';
        str += '<td '+clickcmd[1]+' class="'+styles[1]+'">'+dates[1]+'</td>';
        str += '<td '+clickcmd[2]+' class="'+styles[2]+'">'+dates[2]+'</td>';
        str += '<td '+clickcmd[3]+' class="'+styles[3]+'">'+dates[3]+'</td>';
        str += '<td '+clickcmd[4]+' class="'+styles[4]+'">'+dates[4]+'</td>';
        str += '<td '+clickcmd[5]+' class="'+styles[5]+'">'+dates[5]+'</td>';
        str += '<td '+clickcmd[6]+' class="weekend'+styles[6]+'">'+dates[6]+'</td>';
      	str += '</tr>';
      	str += '<tr class="days">';
        str += '<td '+clickcmd[7]+' class="weekend'+styles[7]+'">'+dates[7]+'</td>';
        str += '<td '+clickcmd[8]+' class="'+styles[8]+'">'+dates[8]+'</td>';
        str += '<td '+clickcmd[9]+' class="'+styles[9]+'">'+dates[9]+'</td>';
        str += '<td '+clickcmd[10]+' class="'+styles[10]+'">'+dates[10]+'</td>';
        str += '<td '+clickcmd[11]+' class="'+styles[11]+'">'+dates[11]+'</td>';
        str += '<td '+clickcmd[12]+' class="'+styles[12]+'">'+dates[12]+'</td>';
        str += '<td '+clickcmd[13]+' class="weekend'+styles[13]+'">'+dates[13]+'</td>';
      	str += '</tr>';
      	str += '<tr class="days">';
        str += '<td '+clickcmd[14]+' class="weekend'+styles[14]+'">'+dates[14]+'</td>';
        str += '<td '+clickcmd[15]+' class="'+styles[15]+'">'+dates[15]+'</td>';
        str += '<td '+clickcmd[16]+' class="'+styles[16]+'">'+dates[16]+'</td>';
        str += '<td '+clickcmd[17]+' class="'+styles[17]+'">'+dates[17]+'</td>';
        str += '<td '+clickcmd[18]+' class="'+styles[18]+'">'+dates[18]+'</td>';
        str += '<td '+clickcmd[19]+' class="'+styles[19]+'">'+dates[19]+'</td>';
        str += '<td '+clickcmd[20]+' class="weekend'+styles[20]+'">'+dates[20]+'</td>';
      	str += '</tr>';
      	str += '<tr class="days">';
        str += '<td '+clickcmd[21]+' class="weekend'+styles[21]+'">'+dates[21]+'</td>';
        str += '<td '+clickcmd[22]+' class="'+styles[22]+'">'+dates[22]+'</td>';
        str += '<td '+clickcmd[23]+' class="'+styles[23]+'">'+dates[23]+'</td>';
        str += '<td '+clickcmd[24]+' class="'+styles[24]+'">'+dates[24]+'</td>';
        str += '<td '+clickcmd[25]+' class="'+styles[25]+'">'+dates[25]+'</td>';
        str += '<td '+clickcmd[26]+' class="'+styles[26]+'">'+dates[26]+'</td>';
        str += '<td '+clickcmd[27]+' class="weekend'+styles[27]+'">'+dates[27]+'</td>';
      	str += '</tr>';
      	str += '<tr class="days">';
        str += '<td '+clickcmd[28]+' class="weekend'+styles[28]+'">'+dates[28]+'</td>';
        str += '<td '+clickcmd[29]+' class="'+styles[29]+'">'+dates[29]+'</td>';
        str += '<td '+clickcmd[30]+' class="'+styles[30]+'">'+dates[30]+'</td>';
        str += '<td '+clickcmd[31]+' class="'+styles[31]+'">'+dates[31]+'</td>';
        str += '<td '+clickcmd[32]+' class="'+styles[32]+'">'+dates[32]+'</td>';
        str += '<td '+clickcmd[33]+' class="'+styles[33]+'">'+dates[33]+'</td>';
        str += '<td '+clickcmd[34]+' class="weekend'+styles[34]+'">'+dates[34]+'</td>';
      	str += '</tr>';
      	str += '<tr class="days">';
        str += '<td '+clickcmd[35]+' class="weekend'+styles[35]+'">'+dates[35]+'</td>';
        str += '<td '+clickcmd[36]+' class="'+styles[36]+'">'+dates[36]+'</td>';
        str += '<td '+clickcmd[37]+' class="'+styles[37]+'">'+dates[37]+'</td>';
        str += '<td '+clickcmd[38]+' class="'+styles[38]+'">'+dates[38]+'</td>';
        str += '<td '+clickcmd[39]+' class="'+styles[39]+'">'+dates[39]+'</td>';
        str += '<td '+clickcmd[40]+' class="'+styles[40]+'">'+dates[40]+'</td>';
        str += '<td '+clickcmd[41]+' class="weekend'+styles[41]+'">'+dates[41]+'</td>';
      	str += '</tr>';
    	str += '</tbody>';
  		str += '</table>';
		this.div.innerHTML = str;
	}
}

function CalendarMonthAdd(divid){document.getElementById(divid).obj.NextMonth();}
function CalendarMonthSub(divid){document.getElementById(divid).obj.PrevMonth();}
function CalendarYearAdd(divid){document.getElementById(divid).obj.NextYear();}
function CalendarYearSub(divid){document.getElementById(divid).obj.PrevYear();}
function CalendarToday(divid){document.getElementById(divid).obj.GoToday();}
function CalendarSelectDate(divid, date){document.getElementById(divid).obj.SelectDate(date);}
function CalendarSelectPrevDate(divid, date){document.getElementById(divid).obj.SelectPrevDate(date);}
function CalendarSelectNextDate(divid, date){document.getElementById(divid).obj.SelectNextDate(date);}
function CalendarTooltip(divid, i){document.getElementById(divid).obj.ShowToolTip(i);}
function CalendarTooltipHide(divid){document.getElementById(divid).obj.HideToolTip();}