/**
 * Accordion Effect for Script.aculo.us
 * Created by Lucas van Dijk
 * http://www.return1.net
 *
 * Copyright 2007 by Lucas van Dijk
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 */

function calcSelfHeight()
{
  var obj = $(window.frameElement);
  var the_height= document.body.scrollHeight;
  parent.document.getElementById('ctl00_ContentPlaceHolderContent_ifMain').height = the_height + 'px';
}

var Accordion = Class.create();

Accordion.prototype =
{
    initialize: function(handles, bodys, options) {
        this.options = this._set_options(options);
        this.handles = $$(handles);
        this.bodys = $$(bodys);

        if (this.bodys.length != this.handles.length)
        { throw Error('Number of handles/bodys does not match!'); }
        else
        { this.length = this.bodys.length; }

        for (var i = 0; i < this.handles.length; i++) {
            Event.observe(this.handles[i], this.options.trigger, this.toogle.bind(this, i));
            this.handles[i].style.cursor = 'pointer';
            this.bodys[i].style.display = 'none';
            this.bodys[i].index = i;
            this.bodys[i].opened = false;
        }
    },

    toogle: function(index) {
        if (this.bodys[index].opened == false)
        { this.show(index); }
        else
        { this.hide(index); }
    },

    show: function(index) {
        new Effect.BlindDown(this.bodys[index], { afterUpdate: calcSelfHeight });
        this.bodys[index].opened = true;
        this.handles[index].className = 'accordionTooglerOff';

    },

    hide: function(index) {

        new Effect.BlindUp(this.bodys[index], { afterUpdate: calcSelfHeight });
        this.bodys[index].opened = false;
        this.handles[index].className = 'accordionTooglerOn';
    },

    _default_options:
	{
	    duration: 0.5,
	    trigger: 'click',
	    show: 0
	},

    _set_options: function(options) {
        if (typeof options != 'undefined') {
            var result = [];
            for (option in this._default_options) {
                if (typeof options[option] == 'undefined')
                { result[option] = this._default_options[option]; }
                else
                { result[option] = options[option]; }
            }
            return result;
        }
        else
        { return this._default_options; }
    }
};

Effect.Accordion = Accordion;