/* Simple Accordion Script 
 * Requires Prototype and Script.aculo.us Libraries
 * By: Brian Crescimanno <brian.crescimanno@gmail.com>
 * http://briancrescimanno.com
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 */
goAccordion =function(divAccId, accNum){ new Accordion(divAccId, accNum);}
var allHTMLTags = new Array();
var allHTMLTags=document.getElementsByTagName("*");
var idToFind = new String();
allExceptAccord = function(){
		$(idToFind).removeClassName('hide');
		ek=idToFind;
}
var specDiv='';

if (typeof Effect == 'undefined')
  throw("You must have the script.aculo.us library to use this accordion");
var eachHeight = new Array;
var Accordion = Class.create({

    initialize: function(id, defaultExpandedCount) {
        if(!$(id)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");
        this.accordion = $(id);
        this.options = {
            toggleClass: "accordion-toggle",
            toggleActive: "accordion-toggle-active",
            contentClass: "accordion-content"
        }
        this.contents = this.accordion.select('div.'+this.options.contentClass);
        this.isAnimating = false;
        this.maxHeight = 0;
        this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
        this.toExpand = null;

        this.checkMaxHeight();
        this.initialHide();
        this.attachInitialMaxHeight();

        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);
    },

    expand: function(el) {
        this.toExpand = el.next('div.'+this.options.contentClass);
        if(this.current != this.toExpand){
			this.toExpand.show();
            this.animate();
        }
    },

    checkMaxHeight: function() {
        for(var i=0; i<this.contents.length; i++) {
            this.contents[i].index=Number(i);
			eachHeight[i] = this.contents[i].getHeight();
			if(eachHeight[i] == 30){eachHeight[i] =0;}
			if(this.contents[i].getHeight() > this.maxHeight) {
                this.maxHeight = this.contents[i].getHeight();
            }
        }
    },

    attachInitialMaxHeight: function() {
		this.current.previous('h3.'+this.options.toggleClass).addClassName(this.options.toggleActive);
        this.current.setStyle({height: this.current.getHeight()+"px"});
    },
	
    clickHandler: function(e) {
        var el = e.element();
        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
            this.expand(el);
        }
		this.toExpand= el.next('div.'+this.options.contentClass);
		idToFind = toplevelcat[this.toExpand.index];
		if(idToFind=='minibooks'){
			$(document.getElementById('bigwrapper')).addClassName('minibooks');
		}else{
			$(document.getElementById('bigwrapper')).removeClassName('minibooks');
		}
		this.hidePanels('active-tab',idToFind);
		window.location = '#'+idToFind+'\/';
		specDiv = '_'+idToFind;

		document.getElementById(specDiv).addClassName('active-tab');
		//if(idToFind != ek){
//			document.getElementById(ek).addClassName('hide');
//			Effect.Fade('bkgd_rightcorner', { duration:.75, from:2.0, to:0.1 });
//			Effect.Fade($(idToFind), { duration:.75, from:-1.0, to:1.0 });
//			rightCornerOver(this.toExpand.index);
//			setTimeout('allExceptAccord()',100);
//
//		}
		window.title =pageTitleMain+maincatTitles[this.toExpand.index];

	},
	hidePanels:function (theClass) {
		for (i=0; i<allHTMLTags.length; i++) {
			if (allHTMLTags[i].className==theClass) {
				allHTMLTags[i].removeClassName('active-tab');
			}
		}
	},
	
    initialHide: function(){
        for(var i=0; i<this.contents.length; i++){
            if(this.contents[i] != this.current) {
                this.contents[i].hide();
                this.contents[i].setStyle({height: 0});
            }
        }
    },

    animate: function() {
        var effects = new Array();
        var options = {
            sync: true,
            scaleFrom: 0,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleMode: {
                //originalHeight: this.maxHeight,
                originalHeight: eachHeight[this.toExpand.index],
                originalWidth: this.accordion.getWidth()
            },
            scaleX: false,
            scaleY: true
        };

        effects.push(new Effect.Scale(this.toExpand, 100, options));

        options = {
            sync: true,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleX: false,
            scaleY: true
        };

        effects.push(new Effect.Scale(this.current, 0, options));

        var myDuration = 0.5;

        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'accordion'
            },
            beforeStart: function() {
				document.getElementById(ek).addClassName('hide');
				Effect.Fade('bkgd_rightcorner', { duration:.1, from:0.1, to:2.0 });
                this.isAnimating = true;
                this.current.previous('h3.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
                this.toExpand.previous('h3.'+this.options.toggleClass).addClassName(this.options.toggleActive);
            }.bind(this),
            afterFinish: function() {
                this.current.hide();
                this.toExpand.setStyle({ height: this.toExpand.getHeight()+"px" });
                this.current = this.toExpand;
                this.isAnimating = false;
				document.getElementById(ek).addClassName('hide');
				Effect.Fade('bkgd_rightcorner', { duration:.75, from:2.0, to:0.1 });
				Effect.Fade($(idToFind), { duration:.75, from:-1.0, to:1.0 });
				rightCornerOver(this.toExpand.index);
				setTimeout('allExceptAccord()',100);
            }.bind(this)
        });
    }

});
