var DateTimeSelector = new Class({
    initialize: function(el) {
        this.el = {};
        this.date = new Date(0, 0, 0, 0, 0, 0);
        
        this.el.day = $E(".day", el);
        //this.el.day.addEvent("change", this.dayChangeHandler.bind(this));
        this.el.month = $E(".month", el);
        //this.el.month.addEvent("change", this.changeHandler.bind(this));
        this.el.year = $E(".year", el);
        //this.el.year.addEvent("change", this.changeHandler.bind(this));
        
        //this.repopulateDays();
    },
    
    repopulateDays: function() {
                            //return;
        var year = parseInt(this.el.year.getValue(), 10);
        
        var isLeap = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
        var nDays = [31, 28 + (isLeap ? 1 : 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][this.el.month.selectedIndex];
        var el = this.el.day;
        var n, opt, index = el.selectedIndex;
        el.options.length = 0;
        for (n = 1; n <= nDays; n++) {
            var paddedNum = (n < 10) ? "0" + n : n;
            el.options[el.options.length] = new Option(paddedNum, paddedNum);
        }
        
        el.selectedIndex = Math.min(el.options.length - 1, index);
        
        this.date = new Date(0, 0, 0, 0, 0, 0);
        this.date.setFullYear(parseInt(this.el.year.getValue(), 10), parseInt(this.el.month.getValue(), 10) - 1, parseInt(this.el.day.getValue(), 10));
    },
    
    setDate: function(date) {
        this.el.year.selectedIndex = date.getFullYear() - parseInt(this.el.year.options[0].value, 10);
        this.el.month.selectedIndex = date.getMonth();
        this.repopulateDays();
        this.el.day.selectedIndex = date.getDate() - 1;
        
        this.date = new Date(0, 0, 0, 0, 0, 0);
        this.date.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
    },
    
    changeHandler: function() {
        this.repopulateDays();
        this.fireEvent("onchange");
    },
    
    dayChangeHandler: function() {
        this.date.setDate(parseInt(this.el.day.getValue(), 10));
        this.fireEvent("onchange");
    }
});
DateTimeSelector.implement(new Events());

var EventSearch = new Class({
    initialize: function(el) {
        this.el = {};
        this.el.root = $(el);
        
        this.calParent = $E(".eventsearchcalendar", this.el.root);
        this.calParent.addEvent("mouseenter", function() {
            this.removeClass("eventsearchcalendarcollapsed");
            this.parentNode.parentNode.addClass("eventsearchhideselects");
        });
        this.calParent.addEvent("mouseleave", function() {
            this.addClass("eventsearchcalendarcollapsed");
            this.parentNode.parentNode.removeClass("eventsearchhideselects");
        });
        this.calParent.addEvent("mouseup", this.calMouseUp.bindWithEvent(this));
        this.el.tbody = $E("tbody", this.el.root);
        
        this.tds = $ES("td", this.el.root);
        
        this.tds.each(function(td) {
            td.addEvent("mousedown", this.tdMouseDown.bindWithEvent(this));
            //td.addEvent("mouseup", this.tdMouseUp.bindWithEvent(this));
            td.addEvent("mouseover", this.tdMouseOver.bindWithEvent(this));
            td.addEvent("mousemove", function(e) { new Event(e).preventDefault(); return false; });
            var parts = td.title.split(".")
            td.date = new Date(0, 0, 0, 0, 0, 0);
            td.date.setFullYear(parseInt(parts[2], 10), parseInt(parts[1], 10) - 1, parseInt(parts[0], 10));
            td.title = "";
        }.bind(this));
        
        this.fromDate = new DateTimeSelector($E(".eventsearchfromdate", this.el.root));
        this.fromDate.addEvent("onchange", this.updateHighlight.bind(this));
        this.toDate = new DateTimeSelector($E(".eventsearchtodate", this.el.root));
        this.toDate.addEvent("onchange", this.updateHighlight.bind(this));
        
        this.updateHighlight();
        
        this.dragging = false;
    },
    
    updateHighlight: function() {
        if (this.fromDate.date > this.toDate.date) {
            var temp = this.toDate.date;
            this.toDate.setDate(this.fromDate.date);
            this.fromDate.setDate(temp);
        }
        this.tds.each(function(td) {
            td.className = (td.hasClass("today") ? "today " : "") +
                ((this.fromDate.date <= td.date && td.date <= this.toDate.date) ? "selected" : "");
        }.bind(this));
    },
    
    tdMouseDown: function(e) {
        this.dragging = true;
        this.fromDate.setDate(e.target.date);
        this.toDate.setDate(e.target.date);
        this.dragOrigin = e.target.date;
        this.updateHighlight();
        
        e.preventDefault();
        return false;
    },
    
    tdMouseOver: function(e) {
        if (!this.dragging) {
            return;
        }
        
        if (e.target.date < this.dragOrigin) {
            this.fromDate.setDate(e.target.date);
            this.toDate.setDate(this.dragOrigin);
        } else {
            this.fromDate.setDate(this.dragOrigin);
            this.toDate.setDate(e.target.date);
        }
        
        this.updateHighlight();
        
        e.preventDefault();
        return false;
    },
    
    calMouseUp: function(e) {
        this.dragging = false;
        
        e.preventDefault();
        return false;
    }
});

var EventRotator = new Class({
    initialize: function(el, conf) {
        this.el = $(el);
        this.conf = $extend({animDuration: 2000, rotateInterval: 1000, className: ""}, conf);

        this.el.addClass("eventrotatorhasjs");
        if (this.conf.className) {
            this.el.addClass(this.conf.className);
        }
        this.items = $ES("li", this.el);
        var y = 10;
        this.items.each(function(li) {
            li.height = li.getSize().size.y;
            li.setStyles({"top": y + "px", "position": "absolute"});
            li.ly = y;
            y += li.height;
        });
        
        this.areaHeight = this.el.getSize().size.y;
        
        // Only rotate if there are enough items
        if (y > this.areaHeight) { 
            this.el.addEvent("mouseenter", this.suspend.bind(this));
            this.el.addEvent("mouseleave", this.resume.bind(this));
        
            if (!window.ie6) {
                this.el.appendChild(new Element("li", {"class": "shader"}));
            }
        
            this.anim = new Fx.Elements(this.items, {duration: this.conf.animDuration, transition: Fx.Transitions.Sine.easeInOut});
            this.anim.addEvent("onComplete", this.animComplete.bind(this));
            this.head = 0;

            this.resume();
        }
    },
    
    rotate: function() {
        var i, item, d = -this.items[this.head].height, first = true;
        var y = d + 10;
        var animParams = {};
        for (i = this.head; first || i != this.head; i = (i + 1) % this.items.length) {
            item = this.items[i];
            item.setStyle("top", item.ly + "px");
            if (y <= this.areaHeight) {
                animParams[i.toString()] = {top: [item.ly, y]};
            }
            item.ly = y;
            y += item.height;
            first = false;
        }
        this.head = (this.head + 1) % this.items.length;
        this.anim.start(animParams);
    },
    
    animComplete: function() {
        if (!this.suspended) {
            this.resume();
        }
    },
    
    suspend: function() {
        this.suspended = true;
        $clear(this.timer);
    },
    
    resume: function() {
        this.suspended = false;
        this.timer = this.rotate.delay(this.conf.rotateInterval, this);
    }
});

var ImageSwitcher = new Class({
    initialize: function(el) {
        this.el = $(el);
        
        this.img = $E(".mainimage", this.el);
        this.thumbLinks = $ES("ul.thumbs a");
        this.thumbLinks.each(function(a) {
            a.addEvent("click", this.thumbClick.bindWithEvent(this));
            a.imageUrl = a.title;
            a.title = "";
        }, this);
    },
    
    thumbClick: function(e) {
        var t = e.target;
        if (t.nodeName.toLowerCase() == "img") {
            t = t.parentNode;
        }
        this.img.src = t.imageUrl;
        this.thumbLinks.removeClass("current");
        t.addClass("current");
        e.preventDefault();
        return false;
    }
});

var Tabber = new Class({
    initialize: function(el) {
        this.tabs = $ES(".Tabs a", el);
        this.content = $ES(".TabContent ul", el);
        
        this.tabs.each(function(a) {
            a.contentId = a.href.replace(/^.*#/, "");
            a.addEvent("click", function(e) { this.showTab(a.contentId); e.preventDefault(); return false; }.bindWithEvent(this));
        }, this);
        
        if (this.tabs.length > 0)
            this.showTab(this.tabs[0].contentId);
    },
    
    showTab: function(id) {
        this.content.each(function(tab) { tab.setStyle("display", "none"); });
        this.tabs.each(function(tab) { tab[(tab.contentId == id) ? "addClass" : "removeClass"]("selected"); });
        
        $(id).setStyle("display", "block");
    }
});

window.addEvent("domready", function() {
    // Atlas overrides this, hopefully this doesn't break anything in it
    Array.forEach = function(arr, fn, bind) { Array.prototype.forEach.call(arr, fn.bind(bind || arr)) };
});