if (typeof(WWDC) == 'undefined') WWDC = {};




WWDC.myagenda = [];




WWDC.MyAgenda = Class.create({

    requestUrl: '/wwdc/attendee/scripts/services.php?',
    query: {},
    requestOptions: {},

    initialize: function(quick) {
        this.query.type = 'list';
        this.quick = quick;
        if (WWDC.myagenda.length < 1) {
            this.sendRequest();
        } else {
            this.handleCached(WWDC.myagenda);
        }
    },

    sendRequest: function() {
        if (this.query.session) {
            this.query.status = !this.button.hasClassName('remove');
            this.query.type = 'update';
        }
        var url = this.requestUrl+'&x='+Math.random()+'&'+Object.toQueryString(this.query);
        // alert (url);
        this.requestOptions.onSuccess = this.handleResponse.bind(this);
        this.requestOptions.onFailure = this.handleFailure.bind(this);
        if (!this.requestOptions['method']) this.requestOptions['method'] = 'get';
        if (!this.requestOptions.requestHeaders) {
            this.requestOptions.requestHeaders = { 'Cache-Control':'no-cache', 'Cache-Control':'must-revalidate' };
        } else if (!this.requestOptions.requestHeaders['Cache-Control']) {
            this.requestOptions.requestHeaders['Cache-Control'] = 'no-cache';
            this.requestOptions.requestHeaders['Cache-Control'] = 'must-revalidate';
        }
        this.request = new Ajax.Request(url, this.requestOptions);
    },

    handleResponse: function(response) {
        var items = response.responseText.split(';');
        items = items.without('');

        items.each(function(item) {
            var value = item.split(',');
            var id = value[0];
            var value = value[1];
            if (value === '1') {
                this.handleItem(id);
            }
        }.bind(this));

        // lauren added callback
        if (typeof(WWDC.Init) == 'function') {
            WWDC.Init();
        }
    },

    handleCached: function(items) {
        // try { console.log('cached items = ', items); } catch(e) { alert(items); }
        items.each(this.handleItem.bind(this));
    },

    handleItem: function(id) {
       //try { console.log(id); } catch(e) { alert(id); }

        var button = $('myagenda-'+id);
        if (button) {
            button.addClassName('remove');
            button.insert('remove')
        }

        var id = parseInt(id);
        WWDC.myagenda.push(id);
    },

    handleFailure: function(r,e) {
        throw(e);
    }

});




WWDC.MyAgendaButton = Class.create(WWDC.MyAgenda, {

    initialize: function(button) {
        this.button = $(button);
        this.id = parseInt(button.id.replace(/.*-/, ''));
        this.query = {
            session: this.button.identify().replace(/.*-/, '')
        }
        this.setUpButton();
    },

    setUpButton: function() {
        if (WWDC.myagenda.indexOf(this.id) > -1) {
            this.button.addClassName('remove');
            this.button.insert('remove');
        }
        this.button.observe('click',this.sendRequest.bind(this));
    },

    handleResponse: function(response) {
        this.button.toggleClassName('remove');
        this.button.innerHTML = (this.button.innerHTML == 'remove') ? 'add' : 'remove';

        var other = $$('.myagenda-'+this.id);
        other.each(function(wrapper) {
            if(wrapper.id != this.button.id) {
                wrapper.toggleClassName('remove');
            }
        }.bind(this));
        WWDC.myagenda.push(this.id);
    }

});