// ps.accordion.js version .1 alfa // © 2009 jacknife.it
// @todo - fix final slide position on quick hover changes
/*-----------------------------------------------------------------------------------------------*/

if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {

	initialize: function(parent, selector, options) {
		if (!$(parent)) {
			return false;
		}
		
		var istance = this;
		this.options = Object.extend({
			duration 	: .2
		}, options || {});
			
		this.parent 	= $(parent);
		this.selector 	= selector;
		this.childs 	= $$('#'+parent+" "+selector);
		this.selected 	= options.selected || 0;
		this.hover 		= this.selected;
		
		this.parent.makeClipping().relativize().setStyle( { 
			width	: 	options.size.totalWidth+'px', 
			height	: 	options.size.totalHeight+'px'
		});
		
		this.childs.each(function(child, index){
			child.absolutize().setStyle( { 
				width	: 	options.size.maxPanelWidth + 'px', 
				height	: 	options.size.totalHeight + 'px',
				left	:	(index<istance.options.selected)	? 	((options.size.minPanelWidth*(index+1)) - options.size.maxPanelWidth) + 'px'
																:	(options.size.minPanelWidth * index) + 'px',
				zIndex	:	istance.childs.size() - index
			}).writeAttribute('index', index);
			if (index == istance.options.selected) child.addClassName('activeElem');
			Event.observe(child, 'mousedown', istance.handlerOn.bindAsEventListener(istance));
		});
		this.appear.defer(this.parent);
	},
	
	appear : function(o)
	{
		Effect.Appear($(o), { duration: 0.5 });
	},	
	
	
	handlerOn : function(e)
	{
		var current = Event.findElement(e, this.selector);
		Event.stop(e);
		if (current.hasClassName('activeElem')) return;
		
		var istance = this;
		
		this.childs[this.selected].removeClassName('activeElem');
		this.selected = parseInt(current.readAttribute('index'));
		current.addClassName('activeElem');
		
		this.childs.each(function(child, index){
			currentLeft = parseInt(child.style.left);
			afterLeft	= (index<istance.selected)	? 	((istance.options.size.minPanelWidth*(index+1)) - istance.options.size.maxPanelWidth)
													:	(istance.options.size.minPanelWidth * index);
			new Effect.MoveBy(child, 0, -( currentLeft - afterLeft),{ duration : istance.options.duration });
		});
	}	
}
	