/* method overruling the default renderMethod in the YAHOO calendar 
YAHOO.widget.Calendar.prototype.renderCellDefault = function(workingDate, cell) {
	cell.innerHTML = this.buildDayLabel(workingDate);
};
*/
EventCalendar = function () {
	this.eventDate = document.getElementById('eventdatum');
	this.noEvents = document.getElementById('noevents');
	this.prev = document.getElementById('eventVorige');
	this.next = document.getElementById('eventVolgende');
	this.stripe = document.getElementById('stripe');
	this.allEventsLink = document.getElementById('allevents');
	this.currentPage = 1;
	this.nrOfPages = 0;
	this.ITEMS_PER_PAGE = 3;
	this.prev.style.visibility = 'hidden';
	this.stripe.style.visibility = 'hidden';
	this.next.style.visibility = 'hidden';
	this.allEventsLink.style.visibility = 'hidden';

	YAHOO.util.Event.addListener(this.prev,'click',this.previousPage, this, true);
	YAHOO.util.Event.addListener(this.next,'click',this.nextPage, this, true);
	YAHOO.util.Event.addListener(this.allEventsLink,'click',this.allEventsThisMonth, this, true);

	/* array with events to show on currently selected month/day */
	this.activeEvents = new Array();
	this.getEvents();
	this.initYahooCalendar();
}

EventCalendar.prototype.getEvents = function () {
	var dateClass, date;
	this.events = YAHOO.util.Dom.getElementsByClassName('event');
	for (var i = 0; i < this.events.length; i++) {
		dateClass = this.events[i].className.split(' ')[1];
		date = dateClass.split('_');
		this.events[i].year = parseInt(date[0], 10);
		this.events[i].month = parseInt(date[1], 10);
		this.events[i].day = parseInt(date[2], 10);
	}
}

EventCalendar.prototype.updateEvents = function () {
	this.allEventsLink.style.visibility = 'hidden';
	this.noEvents.style.display = 'none';
	this.activeEvents.length = 0;
	this.currentMonth = this.yahooCal.cfg.getProperty("pagedate").getMonth() + 1;
	this.eventDate.innerHTML = this.yahooCal.cfg.getProperty("MONTHS_LONG")[this.currentMonth-1];
	this.currentYear = this.yahooCal.cfg.getProperty("pagedate").getFullYear();

	for (var i=0; i<this.events.length; i++) {
		this.events[i].style.display = 'none';
		if ((this.events[i].year == this.currentYear) && (this.events[i].month == this.currentMonth)) {
			this.activeEvents.push(this.events[i]);
		}
	}
	if (this.activeEvents.length == 0) {
		this.noEvents.style.display = 'block';
		this.prev.style.visibility = this.next.style.visibility = this.stripe.style.visibility = 'hidden';
		return;
	}
	for (var i=0; i < 3; i++) {
		if (this.activeEvents[i]) this.activeEvents[i].style.display = 'block';
	}
	if (this.activeEvents.length > 3) {
		this.initPaging();
	} else {
		this.prev.style.visibility = this.next.style.visibility = this.stripe.style.visibility = 'hidden';
	}
}

EventCalendar.prototype.updateEventsInDay = function () {
	var selectedDay = this.yahooCal.getSelectedDates()[0];
	this.noEvents.style.display = 'none';
	this.activeEvents.length = 0;
	this.currentDay = selectedDay.getDate();
	this.currentMonth = selectedDay.getMonth() + 1;
	this.eventDate.innerHTML = this.currentDay + ' ' + this.yahooCal.cfg.getProperty("MONTHS_LONG")[this.currentMonth-1];
	this.currentYear = selectedDay.getFullYear();

	for (var i=0; i<this.events.length; i++) {
		this.events[i].style.display = 'none';
		if ((this.events[i].year == this.currentYear) && (this.events[i].month == this.currentMonth) && (this.events[i].day == this.currentDay)) {
			this.activeEvents.push(this.events[i]);
		}
	}
	if (this.activeEvents.length == 0) {
		this.noEvents.style.display = 'block';
		this.prev.style.visibility = this.next.style.visibility = this.stripe.style.visibility = 'hidden';
		return;
	}
	for (var i=0; i < 3; i++) {
		if (this.activeEvents[i]) this.activeEvents[i].style.display = 'block';
	}
	if (this.activeEvents.length > 3) {
		this.initPaging();
	} else {
		this.prev.style.visibility = this.next.style.visibility = this.stripe.style.visibility = 'hidden';
	}
	this.allEventsLink.style.visibility = 'visible';
}

EventCalendar.prototype.allEventsThisMonth = function () {
	this.yahooCal.deselectAll();
	this.yahooCal.render();
}
EventCalendar.prototype.initPaging = function () {
	this.nrOfPages = Math.ceil(this.activeEvents.length / this.ITEMS_PER_PAGE);
	this.currentPage = 1;
	this.next.style.visibility = 'visible';
}

EventCalendar.prototype.previousPage = function () {
	this.currentPage--;
	for (var i=0; i < this.activeEvents.length; i++) {
		if ((i < (this.currentPage * this.ITEMS_PER_PAGE)) && (i >= ((this.currentPage-1) * this.ITEMS_PER_PAGE))) {
			this.activeEvents[i].style.display = 'block';
		} else {
			this.activeEvents[i].style.display = 'none';
		}
	}
	this.updatePagingArrows();
}

EventCalendar.prototype.nextPage = function () {
	this.currentPage++;
	for (var i=0; i < this.activeEvents.length; i++) {
		if ((i < (this.currentPage * this.ITEMS_PER_PAGE)) && (i >= ((this.currentPage-1) * this.ITEMS_PER_PAGE))) {
			this.activeEvents[i].style.display = 'block';
		} else {
			this.activeEvents[i].style.display = 'none';
		}
	}
	this.updatePagingArrows();
}

EventCalendar.prototype.updatePagingArrows = function () {
	if (this.currentPage == this.nrOfPages) 
		this.next.style.visibility = 'hidden';
	else 
		this.next.style.visibility = 'visible';
	if (this.currentPage > 1) 
		this.prev.style.visibility = 'visible';
	else 
		this.prev.style.visibility = 'hidden';
	if ((this.prev.style.visibility == 'visible') && (this.next.style.visibility == 'visible')) 
		this.stripe.style.visibility = 'visible';
	else 
		this.stripe.style.visibility = 'hidden';
}

EventCalendar.prototype.addCellRenderers = function () {
	var dayToRender;
	// render all dates default without link 
	for (var i = 1; i < 8; i++) {
		this.yahooCal.addWeekdayRenderer(i, this.yahooCal.renderBodyCellRestricted); 
	}
	// add link to eventdates
	myRenderer = function (workingDate, cell) {
		cell.innerHTML = '<a href="javascript:void(null);">' + workingDate.getDate() + '</a>';
		YAHOO.util.Dom.addClass(cell, 'selectable');
		return YAHOO.widget.Calendar.STOP_RENDER; 
	}
	for (var i = 0; i < this.events.length; i++) {
		dayToRender = this.events[i].month + "/" + this.events[i].day + "/" + this.events[i].year;
		this.yahooCal.addRenderer(dayToRender, myRenderer); 
	}
}

EventCalendar.prototype.initYahooCalendar = function () {
	this.yahooCal = new YAHOO.widget.Calendar("yahooCal","cal1Container");
	this.yahooCal.cfg.setProperty("START_WEEKDAY",'1');
	this.yahooCal.cfg.setProperty("MONTHS_SHORT", ["Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"]);
	this.yahooCal.cfg.setProperty("MONTHS_LONG", ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December"]);
	this.yahooCal.cfg.setProperty("WEEKDAYS_SHORT", ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"]);
	this.yahooCal.cfg.setProperty("WEEKDAYS_1CHAR", ["Z", "M", "D", "W", "D", "V", "Z"]);
	this.yahooCal.cfg.setProperty("WEEKDAYS_MEDIUM",["Zon", "Maa", "Din", "Woe", "Don", "Vri", "Zat"]);
	this.yahooCal.cfg.setProperty("WEEKDAYS_LONG",  ["Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag"]);
	this.yahooCal.cfg.setProperty("NAV_ARROW_LEFT",'./images/Systeem/abnamroNL/Content/includes/images/System/Includes/gfx/callt.gif');
	this.yahooCal.cfg.setProperty("NAV_ARROW_RIGHT",'./images/Systeem/abnamroNL/Content/includes/images/System/Includes/gfx/calrt.gif');

	this.yahooCal.selectEvent.subscribe(this.updateEventsInDay, this, true);
	this.yahooCal.renderEvent.subscribe(this.updateEvents, this, true);

	this.addCellRenderers();
	this.yahooCal.render();
}
