// 
// Show/Hide Descriptions
// 
// initialize variants:
// new Descriptions(formContainerId, descriptionClass);
// new Descriptions(formContainer, descriptionElementsArray);
// new Expandable(labelClass, labelClass, {
//     initialState:'off'
// });
// new Expandable(labelClass, labelClass, {
//     initialize: function(label, content, options) {}
// });
// 

Descriptions = Class.create({
    initialize: function(wrapper, content, options) {
        // get the wrapper and elements
        this.wrapper = $(wrapper);

        this.contents = content;
        if (Object.isString(content)) this.contents = $$('.'+content);

        // set the options if we have them
        this.options = options || {};
        this.initialState = this.options.initialState || 'on';

        // if we have a callback
        if (Object.isFunction(this.options.initialize)) this.options.initialize(label, content, options);

        this.createForm();
        this.getDefault();
    },

    createForm: function() {
        this.form = new Element('form', { method:'post', id:this.wrapper.identify()+'form' });

        this.on = new Element('label');
        this.on.innerHTML = '<input type="radio" id="'+this.wrapper.identify()+'On" name="'+this.wrapper.identify()+'Radio" value="on"> On';
        this.on.observe('click', this.toggle.bind(this));
        this.form.appendChild(this.on);

        this.form.appendChild(document.createTextNode('   '));

        this.off = new Element('label');
        this.off.innerHTML = '<input type="radio" id="'+this.wrapper.identify()+'Off" name="'+this.wrapper.identify()+'Radio" value="off"> Off';
        this.off.observe('click', this.toggle.bind(this));
        this.form.appendChild(this.off);

        this.wrapper.appendChild(this.form);
    },

    cookies: function(value) {
        var match = document.cookie.toString().match(window.location.href + /RefLib_Descriptions=([^;]*)/);
        if (match) {
            match = match[1];
            this.initialState = match;
        }

        if (value) {
            // set cookie to expire
            var date = new Date();
            date.setTime(date.setTime()+1000*60*60*24*365*20);
            date.toGMTString();

            // set the cookie
            document.cookie = 'RefLib_Descriptions='+value+'; expires='+date+'; path=/';
        }
    },

    getDefault: function() {
        this.cookies();

        var input = this[this.initialState].down('input');
        input.checked = true;

        this.toggle();
    },

    toggle: function(evt) {
        // stop the default action if it's not a hash event
        ADC.cancelEvent(evt);

        var query = this.form.serialize().toQueryParams();
        var value = query[this.wrapper.identify()+'Radio'];
        if (value && value == 'on') {
            this.cookies('on');
            this.contents.each(Element.show);
        } else if (value && value == 'off') {
            this.cookies('off');
            this.contents.each(Element.hide);
        }
    }

});
