/**
 * 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, checkAllow) {
        this.container = $(elem);
        this.checkAllow = checkAllow || false;
        this.disallowAccessToNextSections = false;
        this.sections = $$('#' + elem + ' .section');
        this.currentSection = false;
        var headers = $$('#' + elem + ' .section ' + clickableEntity);
        
        headers.each(function(header) {
            Event.observe(header,'click',this.sectionClicked.bindAsEventListener(this));
        }.bind(this));
        
        var headers2 = $$('#vithoCheckoutSteps .smile-section ' + clickableEntity);
        headers2.each(function(header) {
        	Event.observe(header, 'click', this.sectionClickedSmile.bindAsEventListener(this));
        }.bind(this));
        
        
    },
    sectionClickedSmile: function(event) {
    	var elem = $(Event.element(event));
    	var idOfSmileSection = jQuery(elem).parent('li').attr('id');
    	var idOfOther   = idOfSmileSection.replace('smile-', '');
    	this.openSection($(idOfOther));

        Event.stop(event);
    },
    sectionClicked: function(event) {
    	var elem = $(Event.element(event));
    	
    	/*if(elem.id == 'smile-opc-billing') {
    		this.openSection('checkout-step-billing');
    	} else { */
    		this.openSection($(Event.element(event)).up('.section'));
    	//}
    	
        Event.stop(event);
    },

    openSection: function(sectionName) {
    	
        var section = $(sectionName);
        
        // Check allow
        if (this.checkAllow && !Element.hasClassName(section, 'allow')){
            return;
        }

        if ( sectionName == 'billing' ) {
        	renewCaptcha();
        }
        
        if(section.id != this.currentSection) {
            this.closeExistingSection();
            this.currentSection = section.id;
            $(this.currentSection).addClassName('active');
            $("smile-"+this.currentSection).addClassName('on');
            var contents = Element.select(section, '.a-item');
            contents[0].show();
            //Effect.SlideDown(contents[0], {duration:.2});
            
            this.manageBreadcrumbs(this.currentSection);
            
            if (this.disallowAccessToNextSections) {
                var pastCurrentSection = false;
                for (var i=0; i<this.sections.length; i++) {
                    if (pastCurrentSection) {
                        Element.removeClassName(this.sections[i], 'allow');
                        jQuery("#smile-"+jQuery(this.sections[i]).attr('id')).removeClass('allow');
                    }
                    if (this.sections[i].id==section.id) {
                        pastCurrentSection = true;
                    }
                }
            }
        }
    },

    closeSection: function(section) {
        $(section).removeClassName('active');
        jQuery("#smile-"+section).removeClass('on');
        //jQuery("#smile-"+section).removeClass('allow');
        
        var contents = $(section).select('.a-item');
        if(contents.length == 0) {
        	//alert('Il aurai du exister une section ID ' + section + ' avec la class a-item');
        } else {
        	contents[0].hide();
        }
        //Effect.SlideUp(contents[0]);
    },

    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')
                }
                this.openSection(this.sections[nextIndex]);
                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')
                }
                this.openSection(this.sections[prevIndex]);
                return;
            }
        }
    },

    closeExistingSection: function() {
        if(this.currentSection) {
            this.closeSection(this.currentSection);
        }
    },
    
    manageBreadcrumbs: function(sectionId){
    	if( sectionId == "opc-billing")
    	{
    		jQuery("a#crumbCoordonnees").show();
    		jQuery("a#crumbCoordonnees").addClass('on');
    		if(jQuery('span.crumbCoordonnees').length > 0)
    		{
    			jQuery("span.crumbCoordonnees").hide();
    		}
    		jQuery("span#crumbPaiement").hide();
    	}
    	if( sectionId == "opc-shipping")
    	{
    		jQuery("a#crumbCoordonnees").show();
    		jQuery("span#crumbPaiement").hide();
    	}
    	if( sectionId == "opc-smilereview")
    	{
    		jQuery("span#crumbPaiement").show();
    		jQuery("a#crumbCoordonnees").removeClass('on');
    		jQuery("a#crumbCoordonnees").after("<span class=\"crumbCoordonnees\"> > </span>")
    	}
    }
}
