var PlannedActions = Class.create();
PlannedActions.prototype = {
    
    initialize: function(coverElementId, plannedActions, lastDate) {
        this._coverElementId = coverElementId;
        this._plannedActions = {};
        
        plannedActions.each ( function(plannedAction) {
        	this._plannedActions[plannedAction['date']] = {
        			id: plannedAction['id'],
        			name: plannedAction['name'],
        			date: plannedAction['date']};
        }.bind(this) );
        
        this._months = new Array(
        	"Leden", "Únor", "Březen",
        	"Duben", "Květen", "Červen",
        	"Červenec", "Srpen", "Září",
        	"Říjen", "Listopad", "Prosinec");
        
        this._todayDate  = new Date();
        this._todayMonth = this._todayDate.getMonth();
        this._todayYear  = this._todayDate.getFullYear();
        
        this._lastDate   = Date.fromDbDateString(lastDate);
        this._lastMonth  = this._lastDate.getMonth();
        this._lastYear   = this._lastDate.getFullYear();
        
        new Insertion.Top(this._coverElementId,
        		"<div class=\"headline\">Plánované akce</div>");
        
        this._prepareTable();
        
        this._drawActions(
        		this._todayMonth,
        		this._todayYear);
    },
    
    /**
     * Vykresli tabulku s planovanymi akcemi, TBODY necha zatim prazdne
     */
    _prepareTable: function() {
    	var html =
    	"<table id=\"plannedActions-table\">" +
		"<thead>" +
		    "<tr>" +
		        "<th>Po</th>" +
		        "<th>Út</th>" +
		        "<th>St</th>" +
		        "<th>Čt</th>" +
		        "<th>Pá</th>" +
		        "<th>So</th>" +
		        "<th>Ne</th>" +
		    "</tr>" +
		"</thead>" +
		"<tbody id=\"plannedActions-table-tbody\">" +
		"</tbody>" +
		"<tfoot>" +
	    	"<tr>" +
		        "<td id=\"month-previous\">" +
		        	tooltip.start("Předchozí měsíc", {text: "<div>&nbsp;</div>"})+ "</td>" +
		        "<td id=\"month-name\" colspan=\"5\"></td>" +
		        "<td id=\"month-next\">" +
		        	tooltip.start("Následující měsíc", {text: "<div>&nbsp;</div>"}) + "</td>" +
		    "</tr>" +
		"</tfoot>" +
		"</table>";
    	
    	new Insertion.Bottom(this._coverElementId, html);
    	
    	Event.observe('month-previous', 'click', this._moveMonth.bindAsEventListener(this, "previous"));
    	Event.observe('month-next', 'click', this._moveMonth.bindAsEventListener(this, "next"));
    },
    
    _moveMonth: function(event, direction) {
    	var month = this._curMonth;
		var year = this._curYear;
    	switch (direction) {
    		case "previous":
    			if (this._curMonth != this._todayMonth || this._curYear != this._todayYear) {
    				month = this._curMonth - 1;
    				if (month < 0) {
    					month = 11;
    					year--;
    				}
    		    	
    		    	this._drawActions(month, year);
    			}
    			break;
    		default:
    			if (this._curMonth != this._lastMonth || this._curYear != this._lastYear) {
    				month = this._curMonth + 1;
    				if (month > 11) {
    					month = 0;
    					year++;
    				}
    		    	
    		    	this._drawActions(month, year);
    			}
    	}
    },
    
    /**
     * Vykresli obsah TBODY - seznam akci na jednotlive dny
     */
    _drawActions: function(month, year) {
        this._curMonth = month;
        this._curYear  = year;
    	
        var numOfDaysInMonth = this._getNumOfDaysInMonth(month, year);
        
        var firstDay = new Date(this._curYear, this._curMonth, 1);
        var firstWeekday = this._getWeekday(firstDay);
        var lastDay = new Date(this._curYear, this._curMonth, numOfDaysInMonth);
        var lastWeekday = this._getWeekday(lastDay);
        
        var html = "<tr>";
        var numOfRows = 1;
        if (firstWeekday > 1) {
        	html += "<td class=\"none\" colspan=\"" + (firstWeekday - 1) + "\"></td>";
        }
        
        for (var day = 1; day <= numOfDaysInMonth; day++) {
        	var dayDate = new Date(this._curYear, this._curMonth, day);
        	var dayWeekday = this._getWeekday(dayDate);
        	
        	if (this._plannedActions[dayDate.toDbDateString()]) {
        		var title = dayDate.toLocDateString() + " - <b>" +
        			this._plannedActions[dayDate.toDbDateString()]['name'] + "</b>";
        		html +=
        		"<td class=\"actioned\">" +
       				tooltip.start(title, {text: "<a href=\"/open-turnaje-detail/?opeId=" +
           				this._plannedActions[dayDate.toDbDateString()]['id'] + "\">" + day + "</a>"}) + "</td>";
        	} else {
        		var className = "active";
        		if (dayDate.toDbDateString() < this._todayDate.toDbDateString()) {
        			className += " old";
        		}
	        	html += "<td class=\"" + className + "\">" + day + "</td>";
        	}
        	
        	if (dayWeekday == 7 && day != numOfDaysInMonth) {
        		html += "</tr><tr>";
        		numOfRows++;
        	}
        }
    	
        if (lastWeekday < 7) {
        	html += "<td colspan=\"" + (7 - lastWeekday) + "\"></td>";
        }
        html += "</tr>";
        
        for (var row = numOfRows; row < 6; row++) {
        	html += "<tr><td colspan=\"" + 7 + "\"></td></tr>";
        }
        
        var tbody = $("plannedActions-table-tbody");
        while (tbody.childNodes[0]) {
            Element.remove(tbody.childNodes[0]);
        }
        new Insertion.Bottom(tbody, html);
        
    	$("month-name").innerHTML = this._months[month] + " " + year;
    	
    	$("month-previous").style.visibility =
    		(this._curMonth == this._todayMonth && this._curYear == this._todayYear) ?
    		"hidden" : "visible";
   		$("month-next").style.visibility =
   			(this._curMonth == this._lastMonth && this._curYear == this._lastYear) ?
   			"hidden" : "visible";
    },
    
    /**
     * Ziska pocet dni v danem mesici a roce
     * 
     * @param int month mesic (0 .. leden, 11 ... prosinec)
     * @param int year  rok
     * @return int pocet dni
     */
    _getNumOfDaysInMonth: function(month, year) {
    	return 32 - new Date(year, month, 32).getDate();
    },
    
    /**
     * Ziska den v tydnu, ktery odpovida datumu v parametru "date"
     * 
     * @param Date date
     * @return int den v tydnu (1 ... pondeli, 7 ... nedele)
     */
    _getWeekday: function(date) {
    	var weekday = date.getDay();
    	return (weekday == 0) ? 7 : weekday;
    }
};