﻿var allEvents = 'div.event';

function Calendar() {
    this.events = $$(allEvents);
}

Calendar.prototype.init = function() {
    // set up event mouse events
    for (i = 0, j = this.events.length; i < j; i++) {
        this.events[i].oldClassName = this.events[i].className;
        this.events[i].hidden = false;
	    this.events[i].childElements()[1].onclick = function() {
	        $(this.parentNode).className = 'event_over';		  
	    };
	    this.events[i].firstDescendant().onclick = function() {
	        if ($(this.parentNode).hidden) {
    		    $(this.parentNode).className = $(this.parentNode).oldClassName + ' event_hidden';
    		} else {
    		    $(this.parentNode).className = $(this.parentNode).oldClassName;
    		}
	    };
    }
    FixDayHeight();    
};

// note - this breaks if anything starts as 'hidden'.  if any events need to be hidden at the start, run the hide function after the page loads.
Calendar.prototype.hide = function(name) {
    var a = $$('div.'+name);
    for (i = 0, j = a.length; i < j; i++) {
        a[i].hidden = true;
        a[i].className = a[i].oldClassName + ' event_hidden';              
    }
};
Calendar.prototype.show = function(name) {
    var a = $$('div.'+name);
    for (i = 0, j = a.length; i < j; i++) {
        a[i].hidden = false;
        a[i].className = a[i].oldClassName;   
    }    
};

function FixDayHeight() {
    // fix scroll height on days that have lots of events
    var a = $$('td.day');

    for (i = 0, j = a.length; i < j; i++) {
	    if (a[i].scrollHeight > 106) {
		    a[i].setStyle('height:'+a[i].scrollHeight+'px;');
	    }
    }
}

function toggleCalButton(button, c) {
    if (c) {
        cn = button.id;
        button.toggled = !button.toggled;
        
        if (button.toggled) {
            button.className = 'button';
            c.hide(cn);
        } else {
            button.className = 'button active';
            c.show(cn);
        }
    }
}
