/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
Accordion = Class.create();
Accordion.prototype = {
    initialize: function(elem, clickableEntity, expandableEntity, checkAllow, applyEffects, checkout) {
        this.checkout = checkout || false;
        this.container = $(elem);
        this.expandableEntity = expandableEntity;
        this.clickableEntity = clickableEntity;
        this.checkAllow = checkAllow || false;
        this.applyEffects = applyEffects || false;
        this.disallowAccessToNextSections = false;
        this.sections = $$('#' + elem + ' ' + this.expandableEntity);
        this.currentSection = false;
        var headers = $$('#' + elem + ' ' + this.clickableEntity);
        var header_spans = $$('#' + elem + ' ' + this.clickableEntity + ' span');
        headers.each(function(header) {
            Event.observe(header, 'click', this.sectionClicked.bindAsEventListener(this));
        }.bind(this));
        header_spans.each(function(header) {
            Event.observe(header, 'click', this.spanSectionClicked.bindAsEventListener(this));
        }.bind(this));
    },

    sectionClicked: function(event) {
        var section;
        if (!this.checkout) {
            section = $(Event.element(event)).next(this.expandableEntity);
        } else {
            section = Event.element(event).id.gsub('^tab-', 'opc-');
        }
        this.openSection(section);
        Event.stop(event);
    },

    spanSectionClicked: function(event) {
        var section = $(Event.element(event)).up().next(this.expandableEntity);
        this.openSection(section);
        Event.stop(event);
    },

    openSection: function(section) {
        var section = $(section);

        // Check allow
        if (this.checkAllow && !Element.hasClassName(section, 'allow')) {
            return;
        }

        if (!this.checkout) {
            $$('#' + this.container.id + ' ' + section.up(1).id + ' ' + this.clickableEntity).invoke('removeClassName', 'expanded');
            $$('#' + this.container.id + ' ' + section.up(1).id + ' ' + this.expandableEntity).each(function(entity) {
                if (entity.id != section.id && entity.visible()) {
                    entity.hide();
                }
            });

            if (section.visible()) {
                section.previous(this.clickableEntity).removeClassName('expanded');
                if (this.applyEffects) {
                    Effect.SlideUp(section, {duration:.2});
                } else {
                    section.hide();
                }
            } else {
                section.previous(this.clickableEntity).addClassName('expanded');
                if (this.applyEffects) {
                    Effect.SlideDown(section, {duration:.2});
                } else {
                    section.show();
                }
            }
        } else {
            $$('#' + this.container.id + ' ' + this.clickableEntity).invoke('removeClassName', 'active');
            $(section.id.gsub('^opc-', 'tab-').gsub('shipping$', 'billing')).addClassName('active');
            if (!section.visible()) {
                if (section.id != this.currentSection) {
                    this.closeExistingSection();
                    this.currentSection = section.id;
                }

                if (this.applyEffects) {
                    Effect.SlideDown($(this.currentSection), {duration:.2});
                } else {
                    $(this.currentSection).show();
                }

                if (this.disallowAccessToNextSections) {
                    var pastCurrentSection = false;
                    for (var i=0; i<this.sections.length; i++) {
                        if (pastCurrentSection) {
                            Element.removeClassName(this.sections[i], 'allow')
                        }
                        if (this.sections[i].id==section.id) {
                            pastCurrentSection = true;
                        }
                    }
                }
            }
        }

    },

    closeSection: function(section) {
        if (this.applyEffects) {
            Effect.SlideUp($(section), {duration:.2});
        } else {
            $(section).hide();
        }
    },

    openNextSection: function(setAllow){
        for (section in this.sections) {
            var nextIndex = parseInt(section)+1;
            if (this.sections[section].id == this.currentSection && this.sections[nextIndex]){
                if (setAllow) {
                    Element.addClassName(this.sections[nextIndex], 'allow')
                }
                var section_to_open = this.sections[nextIndex].id.gsub('shipping_method$', 'payment')
                this.openSection(section_to_open);
                return;
            }
        }
    },

    openPrevSection: function(setAllow){
        for (section in this.sections) {
            var prevIndex = parseInt(section)-1;
            if (this.sections[section].id == this.currentSection && this.sections[prevIndex]){
                if (setAllow) {
                    Element.addClassName(this.sections[prevIndex], 'allow')
                }
                var section_to_open = this.sections[prevIndex].id.gsub('shipping_method$', 'shipping')
                this.openSection(section_to_open);
                return;
            }
        }
    },

    closeExistingSection: function() {
        if (this.currentSection) {
            this.closeSection(this.currentSection);
        }
    }
}
