document.observe("dom:loaded", function() {
	$$('.snsaccordion').each(function(el){		
		var rand =  + (new Date()).getTime() + Math.ceil(Math.random() * 10);
		el.id = 'snsaccordion-' + rand;		
		var cnt = 0;
		el.select('.snsaccordioncontent').each(function(section){
			section.id = 'snsaccordioncontent-' + rand + '-' + cnt;
			if(el.select('.snsaccordioncontent').length != 1){
				section.hide();
			}			 
			cnt++;
		}, this);
		
		new Snsaccordion(el.id);
		
	});

});

var Snsaccordion = Class.create();
Snsaccordion.prototype = {
    initialize: function(elem) {
        this.container = $(elem);
        this.sections = $$('#' + elem + ' .snsaccordioncontent');
        this.currentSection = false;
        var headers = $$('#' + elem + ' .snsaccordionheader');
        headers.each(function(header) {
            Event.observe(header,'click',this.sectionClicked.bindAsEventListener(this));
        }.bind(this));
    },

    sectionClicked: function(event) {
		var el = $(Event.element(event));
        this.openSection(el.next('.snsaccordioncontent'));
//		el.scrollTo();
        Event.stop(event);
    },

    openSection: function(section) {
        var section = $(section);
        if(section.id != this.currentSection) {
            this.closeExistingSection();
            this.currentSection = section.id;
            $(this.currentSection).addClassName('active');
			$(this.currentSection).show();
            //Effect.SlideDown(contents[0], {duration:.2});
            
        }
		else if(this.currentSection){
			this.closeSection(this.currentSection);
			this.currentSection = false;
		}
    },

    closeSection: function(section) {
        $(section).removeClassName('active');
        $(section).hide();
        //Effect.SlideUp(contents[0]);
    },


    closeExistingSection: function() {
        if(this.currentSection) {
            this.closeSection(this.currentSection);
        }
    }
}

