var clrmain       = "#000000";  // Background color for Main Date
var clrdays       = "#222222";  // Background color for Days of the Week
var clrpopup      = "#222222";  // Background color for Allevents popup
var clrdayspassed = "#666666";  // Background color for days already passed
var clrdaysahead  = "#444444";  // Background color for days ahead and current

var days = new Array('Po','Ut','St','Št','Pi','So','Ne');

/* months stores from 0-11: jan=0 dec=11 */
var months = new Array(['január',31],['február',28],['marec',31],['apríl',30],['máj',31],['jún',30],['júl',31],['august',31],['september',30],['október',31],['november',30],['december',31]);

var now = new Date();

function CalenderMonth(){
	this.dayofweek = now.getDay()
	this.month = now.getMonth()
	this.dayofmonth = now.getDate()
	this.year = now.getYear()

	/* makes leap year and y2k correction. */
	if( this.year%4==0 )
		months[1][1]=29
	/* if the host comp gives 2 digits, make correction */
	/* leaves bug for now<1990 and now>2090 */
	if( this.year < 1000 ) 
	if( this.year > 90 )
		this.year += 1900
	else 
		this.year += 2000

	/* extrapolates from the current dayofweek, the dayofweek */
    /* of the beginning of the month                          */
	this.firstofmonth = this.dayofweek-this.dayofmonth%7
		if( this.firstofmonth<0 )
			this.firstofmonth = this.firstofmonth + 7

	/* loads this.events with EventList object... defined later */
	this.events = new EventList(this.month,this.year)
}// CALENDER CLASS


CalenderMonth.prototype.lastmonth = function(){
	this.month--
		if(this.month<0){
			this.month += 12
			this.year--
		}
	this.firstofmonth = this.firstofmonth - months[this.month][1]%7
		if(this.firstofmonth<0)
			this.firstofmonth += 7
	this.events = new EventList(this.month,this.year)
}


CalenderMonth.prototype.nextmonth = function(){
	this.firstofmonth = this.firstofmonth + months[this.month][1]%7
		if(this.firstofmonth>6)
			this.firstofmonth -= 7
	this.month++
		if(this.month>11){
			this.month -= 12
			this.year++;
		}
	this.events = new EventList(this.month,this.year)
}


CalenderMonth.prototype.changemonth = function(x){
	if(x){
		for(var i=0;i<Math.abs(x);i++){
			if(x<0)
				this.lastmonth()
			else
				this.nextmonth()
		}//for
	}//if
}


/**********************************************************
 displayMonth(m,type)                                   
                                                        
 this function displays a passed calendermonth object.  
 by default it will have every day link to a popup      
 window with that days event. User may specify a 'type' 
 if type is set to '1' then each day with at least one  
 event will link internally to an anchor set up by a    
 seperate user call to the showEvents() function.       
 'varname' is the string of the variable name of the    
 calender object is question:                           
 	var a = new CalenderMonth();                        
      displayMonth(a,"a");                              
 this makes and displays a new popup style calender a   
 **********************************************************/
function displayMonth(m,varname,style)
{
	var daycounter = 1
	var when
	var disp

	if(style)
		type = style
	else
		type = 0

	var curr_month = now.getMonth()
	var curr_year = now.getYear()
		/* if the host comp gives <4 digits, make correction */
		if( curr_year < 1000 )
		if(curr_year > 90)
			curr_year += 1900
		else
			curr_year += 2000

	if( (m.year<curr_year) || ((m.year==curr_year)&&(m.month<curr_month)) )
		when = -1;
	if( (m.year==curr_year) && (m.month==curr_month) )
		when = 0;
	if( (m.year>curr_year) || ((m.year==curr_year)&&(m.month>curr_month)) )
		when = 1;

	document.writeln('<table border="0" width="200">')

	document.writeln('<tr align="center" bgcolor='+clrmain+'>')

	document.writeln('<td colspan="7"><b>' + months[m.month][0] + ' ' + m.year + '</b></td>')
	document.writeln('</tr>')
	if(type==0)
	{
		document.writeln('<tr align="center" bgcolor='+clrpopup+'>')	
		document.writeln('<td colspan=7><small><a href="javascript:popupevent(' + varname + ',0)">Zobraz všetko za ' + months[m.month][0] + '</a></small></td>')
		document.writeln('</tr>')
	}//if

	/* show the days of the week */

	document.writeln('<tr align="center" bgcolor='+clrdays+'>')

	for(var dw=0;dw<7;dw++)
	{
		document.writeln('<td width="14%" align="center"><small><b>&nbsp;' + days[dw] + '&nbsp;</b></small></td>')
	}//for

        /* run 6 iterations. one for each possible week the month might span */
	for(var wk=1; wk<=6; wk++)
	{
         /* if we have not exceed the number of days in the month continue */
         //if(daycounter<=months[m.month][1])
	 //{
		document.writeln('<tr align="center">')
                /* for each of the seven days of the week... */
		for(dw=0;dw<7;dw++)
		{
                 
		/* if its not yet the first of the month, or if its after the end */
		 if( (wk==1 && dw<m.firstofmonth) || daycounter>months[m.month][1] )
			document.writeln('<td width="14%"><small>&nbsp;</small></td>')
		 else
		 {
                        /* if the date being displayed has passed */
			if( when==-1 || (when==0 && daycounter<m.dayofmonth) ) {bgclr=clrdayspassed}
			/* if the date being displayed has not passed */
			else {bgclr=clrdaysahead}
                        
			if(m.events.event[daycounter][0])
				if(type==1)
					document.writeln('<td width="14%" bgcolor="' + bgclr + '"><small><a href="#' + daycounter + '_' + m.month + '_' + m.year + '">' + daycounter + '</a></small></td>')
				else
					document.writeln('<td width="14%" bgcolor="' + bgclr + '"><small><a href="javascript:popupevent(' + varname + ', ' + daycounter + ')">' + daycounter + '</a></small></td>')
			 else
				document.writeln('<td width="14%" bgcolor="' + bgclr + '"><small>' + daycounter + '</small></td>')
			daycounter++
		 }//ifelse
		}//for
	document.writeln('</tr>')
	 //}//if
	}//for
	document.writeln('</table>')
}//END DISPLAYMONTH



function EventList(month,year){

	this.month = month;
	if(year)
		this.year = year
	else { 
		this.year = now.getYear()
		/* if the host comp gives 2 digits, make correction */
		if( this.year < 1000 )
		if( this.year > 90 )
			this.year += 1900
		else 
			this.year += 2000
	}//else

	this.event = new Array(months[this.month][1]+1);

	for(var k=0; k<this.event.length; k++)
		this.event[k] = new Array()
}//EVENTLIST


CalenderMonth.prototype.addEvent = function(day,time,name,location,description){
	this.events.addEvent(day,time,name,location,description)
}


EventList.prototype.addEvent = function(day,time,name,location,description){
	this.event[day][this.event[day].length] = [name,time,location,description]
}//ADDEVENT


var eventopen = 0;

function popupevent(m,i)
{
	if(eventopen==1){eventopen=0;eventWindow.close();popupevent(m,i);return;}
	
	
	eventWindow = window.open("","CalenderPopup","width=300,scrollbars=yes,resizable=yes")
	if(eventWindow)
		eventopen=1

	eventWindow.document.writeln('<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1250">')
	eventWindow.document.writeln('<link rel="stylesheet" href="astro.css" type="text/css">')
	eventWindow.document.writeln('<title>Prehľad úkazov za ' + months[m.month][0] + '</title>')
	eventWindow.document.writeln('</head><body style="margin: 1em;">')

	if(i==0)
		limit=m.events.event.length;
	else
		limit=i+1;

	for(i; i<limit; i++)
	{
	if(m.events.event[i][0])
	{
		eventWindow.document.write('<table align="center" border="0" cellpadding="2" cellspacing="0" width="250">')
		eventWindow.document.write('<tr><td bgcolor="'+clrmain+'"><b>' + i + '. ' + months[m.month][0] + ' ' + m.year+ '</b></td></tr>')

		for(var j=0;j<m.events.event[i].length;j++)
		{
			eventWindow.document.write('<tr><td bgcolor="'+clrpopup+'"><b><small>' + m.events.event[i][j][0] + '</small></b></td></tr>')
			eventWindow.document.write('<tr><td bgcolor="'+clrdayspassed+'"><small>' + m.events.event[i][j][1] + ': ' +  m.events.event[i][j][2] + '</small></td></tr>')
			eventWindow.document.write('<tr><td bgcolor="'+clrdayspassed+'"><small><li>' + m.events.event[i][j][3] + '</small></td></tr>')
		}//for

		eventWindow.document.writeln('</table>')
	}//if
	}//for
	
	eventWindow.document.writeln('<center><form><input type="button" value="Zatvoriť" onClick="self.opener.eventopen=0;self.close();"></form></center>')
	eventWindow.document.writeln('</body></html>')
	eventWindow.focus();
	return;
}


var CalMonth = new Array()

var this_month = now.getMonth()
var this_month_year = now.getYear()
	/* if the host comp gives < 4 digits, make correction */
	if(this_month_year < 1000 )
		if(this_month_year > 90 )
			this_month_year += 1900
		else 
			this_month_year += 2000

/* Create Calender years for l999 through two years from now */
for(var yr=1999; yr<=this_month_year+1; yr++)
{
	CalMonth[yr] = new Array()
		for(var i=0; i<12; i++)
		{
			CalMonth[yr][i] = new CalenderMonth()
			CalMonth[yr][i].changemonth( i-this_month+(12*(yr-this_month_year)) )
		}//for
}

function incrementgrabmonth(arr,offset)
{
	if(offset>0)
		for(var i=0; i<offset; i++)
		{
			arr[0] += 1
			if( arr[0]>11 )
			{
				arr[0] -= 12
				arr[1] += 1
			}//if
		}//FOR
	if(offset<0)
		for(var i=0; i>offset; i--)
		{
			arr[0] -= 1
			if(arr[0]<0)
			{
				arr[0] += 12
				arr[1] -=1
			}//if
		}//for
}//INCREMENTGRABMONTH

function grabmonth(yr, mnth)
{
	if(mnth>=0)
		return CalMonth[yr][mnth]
	var temp_month	
	var temp_arr = new Array()
		temp_arr[0] = this_month
		temp_arr[1] = this_month_year
	incrementgrabmonth(temp_arr,yr)
		var temp_month = temp_arr[0]
		var temp_year = temp_arr[1]
	return CalMonth[temp_year][temp_month]
}//GRABMONTH

