﻿/**
 * @author FINSA Consulting s.r.l.
 * @classDescription Display Calendar and manage day selection, day highlighting, etc. according to news' presence in that very day.
*/


// Object to store news reference
function calMyNews(day, url, title) {
	this.day=day;
	this.url=url;
	if (title===undefined) title = '';
	this.title=title;
}

function gotoURL(url){
	window.location.href = url;
}


function calendar(date, lang, url_ical, link_title)
{
  try {
  	//If no parameter is passed use the current date.
  	if(date == null)
  		date = new Date();
  	
  	

  	
  	day = date.getDate();
  	month = date.getMonth();
  	year = date.getFullYear();  	
  	
	lNews = getMyNews(month, year);
	
	if (lang == 'ital'){
		months = new Array('Gennaio',
  	                'Febbraio',
  	                'Marzo',
  	                'Aprile',
  	                'Maggio',
  	                'Giugno',
  	                'Luglio',
  	                'Agosto',
  	                'Settembre',
  	                'Ottobre',
  	                'Novembre',
  	                'Dicembre');
	
	}else if (lang == 'japa'){
		months = new Array(
			'1月',
			'2月',
			'3月',
			'4月',
			'5月',
			'6月',
			'7月',
			'8月',
			'9月',
			'10月',
			'11月',
			'12月');
		
	}else{
		months = new Array('January',
  	                'February',
  	                'March',
  	                'April',
  	                'May',
  	                'June',
  	                'July',
  	                'August',
  	                'September',
  	                'October',
  	                'November',
  	                'December');
  	}
  	
  	prev_month = new Date(year, month - 1, 1);
  	this_month = new Date(year, month, 1);
  	next_month = new Date(year, month + 1, 1);
  	
  	
  	//Find out when this month starts and ends.         
  	first_week_day = this_month.getDay();
  	days_in_this_month = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
  	  	
	if (lang == 'japa') {
	  	calendar_html = '<tbody><tr><td><a href="#_self" onclick="calendar(prev_month, \''+lang+'\', \''+url_ical+'\', \''+link_title+'\'); return false;"> << </a></td><td colspan="5"> <a title="'+link_title+'" href="'+url_ical+'">' + 
  	            year + '年' + months[month] + '</a></td><td><a href="#_self" onclick="calendar(next_month, \''+lang+'\', \''+url_ical+'\', \''+link_title+'\' ); return false;"> >> </a></td></tr>';	
	}
	else {
  	  	
  	calendar_html = '<tbody><tr><td><a href="#_self" onclick="calendar(prev_month, \''+lang+'\', \''+url_ical+'\', \''+link_title+'\'); return false;"> << </a></td><td colspan="5"> <a title="'+link_title+'" href="'+url_ical+'">' + 
  	              months[month] + ' ' + year + '</a></td><td><a href="#_self" onclick="calendar(next_month, \''+lang+'\', \''+url_ical+'\', \''+link_title+'\' ); return false;"> >> </a></td></tr>';
  	
  	}
  	
  	calendar_html += '<tr>';
  	
  	//Fill the first week of the month with the appropriate number of blanks.       
  	for(week_day = 0; week_day < first_week_day; week_day++)
  	{
  		calendar_html += '<td>&nbsp;</td>';   
  	}
  	
  	week_day = first_week_day;
  	for(day_counter = 1; day_counter <= days_in_this_month; day_counter++)
  	{
  		week_day %= 7;
  		
  		if(week_day == 0)
  		   calendar_html += '</tr><tr>';
  		
  		//Do something different for the current day.
  		calendar_html += '<td align="center"';
		// Look if in this day there are news
		for (i=0;i<lNews.length;i++){
			if (lNews[i].day==day_counter){
				if (lNews[i].url) {
					calendar_html +=" class=\"calendar-news\" onclick= \"gotoURL(lNews["+i+"].url)\" title=\""+lNews[i].title+"\" ";
				}
				else {
					calendar_html +=" class=\"calendar-news\" title=\""+lNews[i].title+"\" ";
				}
				break;	
			}
		}
		calendar_html += '>&nbsp;' + day_counter;
		calendar_html += '&nbsp;</td>';
  		
  		week_day++;
  	}
  	
  	calendar_html += '</tr></tbody>';
  	     
  	//Display the calendar.
  	var cal = document.getElementById('Calendarium');
  	if ( cal == null) {
  	  calendar_html = '<table id="Calendarium" class="calendarium calColor">' + calendar_html + '</table>';
    	document.write(calendar_html);
  	}
  	else {
  	   if (isIE) {
          calendar_html = '<table id="Calendarium" class="calendarium calColor">' + calendar_html + '</table>';
          cal.outerHTML=calendar_html;
       }
  	   else {
  	       purge(cal);
  	       cal.innerHTML=calendar_html;
       }
  	}

  }
  catch (e) {
    alert(e.message);
  }
}
