if(NID == undefined )
{
var NID = {}; 
}

/**
* class NID.MenuAccordion
* 
* Class handling CM type menus as accordion
**/
NID.MenuAccordion = Class.create({
/**
* new NID.MenuAccordion(elem[, options])
* - container (Element): html element or id string
* - options (Object): Configuration for the request. 
* Creates a new `NID.MenuAccordion`.
* 
* <h5>Options</h5>
* 
* * `toggleClass` ([[String]]; default `accordion-toggle`): CSS class triggering accordion toggle
* * `toggleActive`: ([[String]]; default `accordion-toggle-active`): CSS class of active toggled element
* * `contentClass`: ([[String]]; default `accordion-content`): CSS class of content toggled
* * `openFirstIfNotFound`: ([[Boolean]]; default `false`): if true opens first accordion by default
* * `animationTransition`: ([[Object]]; default `Effect.Transitions.sinoidal`): transition effect
* * `animationDuration`: ([[Float]]; default `0.75`): animation duration
* * `animationFPS`: ([[Integer]]; default `35`): animation frame count
**/
	initialize: function (container, options)
	{
		if(!$(container)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");

		this.accordion = $(container);
		this.accordion.identify();

		this.current = null;
		this.toExpand = null;
		this.isAnimating = false;

        this.options = {
            toggleClass: "accordion-toggle",
            toggleActive: "accordion-toggle-active",
            contentClass: "accordion-content",
            openFirstIfNotFound: false,
            animationTransition: Effect.Transitions.sinoidal,
            animationDuration: 0.75,
            animationFPS: 35
        }

        Object.extend(this.options,options || {});

		$$("#"+this.accordion.identify()+" > li > ul").each(function (item)
		{

			item.addClassName(this.options.contentClass);
			item.previous("a").addClassName(this.options.toggleClass);
		}.bind(this)
		);

		var selected = null;

		$$("#"+this.accordion.identify()+" > li > a."+this.options.toggleClass).each(
			function (item, index)
			{
				if(item.up("li").hasClassName("selected"))
				{
					selected = index;

					item.addClassName(this.options.toggleActive);
				}
			}.bind(this)
		);

		this.contents = this.accordion.select("."+this.options.contentClass);

		if(selected != null )
		{

			this.current  = this.contents[selected];

		}else if( this.options.openFirstIfNotFound )
		{
			this.current = this.contents[0];
			
			if(this.current)
			this.current.previous("a."+this.options.toggleClass).addClassName(this.options.toggleActive);
		}

		this.init();

		var clickHandler =  this.clickHandler.bindAsEventListener(this);

		this.accordion.observe("click", clickHandler);
	},
/**
 * NID.MenuAccordion#expand() -> null
 * Expands accordion content next from el
 * - el (Element): html element or id string
 **/	
	expand: function(el)
	{
        this.toExpand = el.next("."+this.options.contentClass);

       	if(this.current != this.toExpand)
        {
			if(this.toExpand != undefined)
			{
				this.toExpand.show();
				this.animate();
			}
        }else
        {
        	this.toExpand = null;
        	this.animate();
        }
    },
/**
* NID.MenuAccordion#init() -> null
* initializes accordion
* hides all but current accordion
**/ 
    init: function()
	{
		for(var i=0; i<this.contents.length; i++)
		{
			this.contents[i].originalHeight = this.contents[i].getHeight();

			if(this.contents[i] != this.current)
			{
				this.contents[i].hide();
				this.contents[i].setStyle({height: 0});
			}

		}
	},
/**
* NID.MenuAccordion#clickHandler(e) -> null
* - e (Event): triggered event
**/ 
	clickHandler: function(e)
	{
        var el = e.element();

        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating)
        {
            this.expand(el);
            e.stop();
        }
    },
/**
* NID.MenuAccordion#animate() -> null
* Animates accordion ( hides and shows elements )
**/    
    animate: function()
    {
        var effects = new Array();

        if(this.toExpand != null)
        {
	        var options = {
	            sync: true,
	            scaleFrom: 0,
	            scaleContent: false,
	            transition: this.options.animationTransition,
	            scaleMode: {
	                originalHeight: this.toExpand.originalHeight,
	                originalWidth: this.accordion.getWidth()
	            },
	            scaleX: false,
	            scaleY: true
	        };


        	effects.push(new Effect.Scale(this.toExpand, 100, options));
        }

        options = {
            sync: true,
            scaleContent: false,
            transition: this.options.animationTransition,
            scaleX: false,
            scaleY: true
        };

        if(this.current != null)
        {
        	effects.push(new Effect.Scale(this.current, 0, options));
        }

        new Effect.Parallel(effects,
        {
            duration: this.options.animationDuration,
            fps: this.options.animationFPS,
            queue: {
                position: "end",
                scope: "accordion"
            },
            beforeStart: function()
            {
                this.isAnimating = true;

                if(this.current != null)
        		{
                	this.current.previous("."+this.options.toggleClass).removeClassName(this.options.toggleActive);
                }

                if(this.toExpand != null)
        		{
                	this.toExpand.previous("."+this.options.toggleClass).addClassName(this.options.toggleActive);
                }

            }.bind(this),
            afterFinish: function()
            {
            	if(this.current != null)
        		{
                	this.current.hide();
                }

                if(this.toExpand != null)
        		{
                	this.toExpand.setStyle({ height: this.toExpand.originalHeight+"px" });
                }

                this.current = this.toExpand;
                this.isAnimating = false;
            }.bind(this)
        });
    }
});