﻿/* Mini Calendar Class */
var MiniCal = Class.create({
    initialize: function() {
        this.registerCalendar();
        this.a_continueBtn = $$("a.btn_continue")[0];
        
        this.hideCal();
        
        this.selectField.observe("click", this.showCal.bindAsEventListener(this));
    },
    
    registerCalendar: function() {
        this.selectField = $("date_time_field");
        this.calPopup = $("perf_calendar");
        this.a_calClose = this.calPopup.select("a.cal_close")[0];
        
        this.a_nextMonth = $$("div#cal_month_select a.next_month")[0];
        this.a_prevMonth = $$("div#cal_month_select a.prev_month")[0];
        this.a_dates = this.calPopup.select("td a.event");
        
        if (this.a_nextMonth) {
            this.a_nextMonth.observe("click", this.requestCalUpdate.bindAsEventListener(this));
        }
        if (this.a_prevMonth) {
            this.a_prevMonth.observe("click", this.requestCalUpdate.bindAsEventListener(this));
        }
        if (this.a_dates) {
            for(var i = 0; i < this.a_dates.length; i++) {
                this.a_dates[i].observe("click", this.requestCalUpdate.bindAsEventListener(this));
            }
        }
        this.a_calClose.observe("click", this.hideCal.bindAsEventListener(this));
    },
    
    showCal: function(event) {
        if(event) event.stop();
        this.calPopup.show();
        if (this.perfPopup && this.perfPopup.visible()) {
            this.hidePerfInfo();
        }
    },
    
    hideCal: function(event) {
        if(event) event.stop();
        this.calPopup.hide();
    },
    
    hidePerfInfo: function(event) {
        if(event) event.stop();
        this.perfPopup.hide();
    },
    
    setDateTime: function(event) {
        this.hideCal(event);
        var perfTime = Event.findElement(event, "dd").previous("dt").innerHTML;
        var perfDate = Event.findElement(event, "dd").up().previous("strong").innerHTML;
        var linkURL = Event.findElement(event).href;
        
        this.selectField.down("span").update(perfDate+", "+perfTime);
        this.a_continueBtn.writeAttribute("href",linkURL);
    },
    
    requestCalUpdate: function(event) {
        event.stop();
        var requestParams = event.element().href.parseQuery();
        
        new Ajax.Updater({success: 'perf_calendar', failure: 'cal_failure'}, '/_ajax-calendar.aspx', {
            method: 'get',
            parameters: requestParams,
            onComplete: function(transport) {
                this.registerCalendar();
                this.perfPopup = $("perf_times");
                
                if(this.perfPopup) {
                    this.a_timeSelects = this.perfPopup.select("dl dd a[href]");
                    this.a_soldOut = this.perfPopup.select("dl dd a:not([href])");
                    this.a_perfClose = this.perfPopup.select("a#back_to_cal")[0];
                    
                    for(var i=0; i < this.a_timeSelects.length; i++) {
                        this.a_timeSelects[i].observe("click", this.setDateTime.bindAsEventListener(this));
                    }
                    for(var i=0; i < this.a_soldOut.length; i++) {
                        this.a_soldOut[i].replace("<span>Sold Out</span>");
                    }
                    
                    this.a_perfClose.observe("click", this.hidePerfInfo.bindAsEventListener(this));
                }
            }.bind(this)
        });
    }
});

document.observe("dom:loaded", function() {
    var miniCalWidget = new MiniCal();
});