/**
 * This is a basic Helper that prints a Tabled Calendar within a specified element. The tabled calendar
 * will start at today and show the next X number of weeks
 *
 * @param   element    Id of the element
 * @param   calendar   the calendar to initialize the upcoming view
 * @param   checkpoints	   A map of date strings (YYYY-M-D) to an array of checkpoints on that day
 * @param   tasks	   A map of date strings (YYYY-M-D) to an array of tasks on that day
 * @param   numWeeks   int for the number of weeks to show (optional, 2 by default)
 * @return             void
 */
var JiveProjectCalendar = Class.create(JiveUpcomingCalendar, {
    initialize: function($super, element, calendar, numWeeks, checkpoints, tasks, jiveprojecttooltip, checkpointKey, checkpointsKey, taskKey, tasksKey) {
        this.checkpoints    = checkpoints;
        this.tasks          = tasks;
        this.jiveprojecttooltip = jiveprojecttooltip;
        this.checkpointKey = checkpointKey;
        this.checkpointsKey = checkpointsKey;
        this.taskKey = taskKey;
        this.tasksKey = tasksKey;
        $super(element, calendar, numWeeks);
    },

    loadObjects: function(dayTd, day) {
        // checkpoints
        if (this.checkpoints) {
            var daycheckpoints = this.checkpoints[this.calendar.getYear() + '-' + this.calendar.getMonth() + '-' + day];
            if (daycheckpoints && daycheckpoints.length > 0) {
                var checkPoint = new Element('a', {href: '#', 'class': 'jive-link-checkpoint jiveTT-hover-checkpoint'});
                dayTd.insert(checkPoint);
                checkPoint.observe('click', function() { return false; });
                checkPoint.observe('mouseover', this.jiveprojecttooltip.getCheckpointsTooltip.bind(this.jiveprojecttooltip, daycheckpoints));
                checkPoint.observe('mouseout', this.jiveprojecttooltip.cancelTooltip);
                checkPoint.insert(new Element('strong').update(daycheckpoints.length + ' '));
                checkPoint.insert((daycheckpoints.length == 1) ? this.checkpointKey : this.checkpointsKey);
            }
        }
        // tasks
        if (this.tasks) {
            var daytasks = this.tasks[this.calendar.getYear() + '-' + this.calendar.getMonth() + '-' + day];
            if (daytasks && daytasks.length > 0) {
                var dayCal = new JiveCalendar(this.calendar.getYear(), this.calendar.getMonth(), day);
                var tasksElm = new Element('a', {href: '#', 'class': 'jive-link-task jiveTT-hover-tasks'});
                dayTd.insert(tasksElm);
                tasksElm.observe('click', function() { return false; });                
                tasksElm.observe('mouseover', this.jiveprojecttooltip.getTasksTooltip.bind(this.jiveprojecttooltip, dayCal.getDate().getTime()));
                tasksElm.observe('mouseout', this.jiveprojecttooltip.cancelTooltip);
                tasksElm.insert(new Element('strong').update(daytasks.length + ' '));
                tasksElm.insert((daytasks.length == 1) ? this.taskKey : this.tasksKey);
            }
        }
    }
});

var JiveProjectTooltip = Class.create();
JiveProjectTooltip.prototype = {
    initialize: function(projectID, checkpointTT, taskTT, taskTTURL, textTTLoading, textTTError, textCPEdit, textCPDelete) {
        this.loadingContent = '<strong class="jive-tooltip2-loading">' + textTTLoading + '</strong>';
        this.timeoutExecutor = null;
        this.projectID = projectID;
        this.checkpointTT = checkpointTT;
        this.taskTT = taskTT;
        this.taskTTURL = taskTTURL;
        this.textTTError = textTTError;
        this.textCPEdit = textCPEdit;
        this.textCPDelete = textCPDelete;
        this.jiveUserTips = new SuperNote('jiveTT', {showDelay: 700, hideDelay: 30, cssProp: 'visibility', cssVis: 'visible', cssHid: 'hidden'});
    },

    getCheckpointsTooltip: function(checkpoints) {
        this.cancelTooltip();
        $(this.checkpointTT).innerHTML = this.loadingContent;
        this.timeoutExecutor = new TimeoutExecutor(this.getCheckpoints.bind(this, checkpoints), 700);
    },

    getCheckpoints: function(checkpoints) {
        var text='';
        for (var i=0; i<checkpoints.length; i++) {
            text += "<strong class='jive-link-checkpoint'>";
            text += checkpoints[i].name;
            text += "</strong>";
            if (checkpoints[i].duedate) {
                text += "<p>";
                text += checkpoints[i].duedate;
                text += "</p>";
            }
            if (checkpoints[i].description) {
                text += "<p>";                
                text += checkpoints[i].description;
                text += "</p>";
            }
            if (checkpoints[i].editURL && checkpoints[i].deleteURL) {
                text += "<p>";
                text += "<a href='" + checkpoints[i].editURL + "'>" + this.textCPEdit + "</a>";
                text += "<a href='" + checkpoints[i].deleteURL + "'>" + this.textCPDelete + "</a>";
                text += "</p>";
            }
        }
        $(this.checkpointTT).innerHTML = text;
    },

    getTasksTooltip: function(day) {
        this.cancelTooltip();
        $(this.taskTT).innerHTML = this.loadingContent;
        this.timeoutExecutor = new TimeoutExecutor(this.getTasks.bind(this, day), 700);
    },

    getTasks: function(day) {
        new Ajax.Updater(this.taskTT, this.taskTTURL, {
            method: 'get',
            asynchronous:true,
            parameters: {
                'project': this.projectID,
                'day': day
            },
            onError: function() {
                $(this.taskTT).innerHTML = this.textTTError;
            }
        });
    },

    cancelTooltip: function() {
        if (this.timeoutExecutor) {
            this.timeoutExecutor.cancel();
        }
    }
}