

if (typeof Effect == 'undefined')
	throw("openbox.js requires including script.aculo.us' effects.js library!");

var openbox = Class.create();
openbox.prototype = {
	//
	//  Setup the Variables
	//
	currentOpenbox : null,
	duration : null,
	effects : [],
	animating : false,
	//  
	//  Initialize the openboxs
	//
	initialize: function(container, options) {
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'openbox_toggle',
				toggleActive : 'openbox_toggle_active',
				content : 'openbox_content'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		var openboxs = $$(container+' .'+this.options.classNames.toggle);
		openboxs.each(function(openbox) {
			Event.observe(openbox, this.options.onEvent, this.activate.bind(this, openbox), false);
			openbox.onclick = function() {return false;};
			
			if (this.options.direction == 'horizontal') {
				var options = $H({width: '0px'});
			} else {
				var options = $H({height: '0px'});			
			}			
			this.currentOpenbox = $(openbox.next(0)).setStyle(options);			
			
		}.bind(this));
	},
	//
	//  Activate an openbox
	//
	activate : function(openbox) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentOpenbox = $(openbox.next(0));
		
		
	
		if (this.options.direction == 'horizontal') {
			var adjustments = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			var adjustments = $H({
				scaleX: false,
				scaleY: true
			});			
		}
		
		//alert(this.currentOpenbox.hasClassName(this.options.classNames.toggleActive));
		
		var isopen = this.currentOpenbox.previous(0).hasClassName(this.options.classNames.toggleActive)
		
		if (isopen) {
			this.currentOpenbox.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			
			var options = $H({
				sync: true,
				scaleFrom: (100*this.currentOpenbox.up(0).scrollHeight/(this.currentOpenbox.up(0).scrollHeight - this.currentOpenbox.scrollHeight)),
				scaleContent: false,
				transition: Effect.Transitions.sinoidal,
				scaleMode: { 
					originalHeight: this.currentOpenbox.up(0).scrollHeight - this.currentOpenbox.scrollHeight,
					originalWidth: this.currentOpenbox.up(0).scrollWidth - this.currentOpenbox.scrollHeight
				}
			});
			options.merge(adjustments);
			
			this.effects.push(
				new Effect.Scale(this.currentOpenbox.up(0), 100, options)
			);
		} else {
			this.currentOpenbox.previous(0).addClassName(this.options.classNames.toggleActive);
			
			var options = $H({
				sync: true,
				scaleFrom: (100*this.currentOpenbox.up(0).scrollHeight/(this.currentOpenbox.up(0).scrollHeight + this.currentOpenbox.scrollHeight)),
				scaleContent: false,
				transition: Effect.Transitions.sinoidal,
				scaleMode: { 
					originalHeight: this.currentOpenbox.up(0).scrollHeight + this.currentOpenbox.scrollHeight,
					originalWidth: this.currentOpenbox.up(0).scrollHeight + this.currentOpenbox.scrollWidth
				}
			});
						
			options.merge(adjustments);
			
			this.currentOpenbox.style["height"] = this.currentOpenbox.scrollHeight + "px";
			//this.currentOpenbox.style["width"] = this.currentOpenbox.scrollWidth + "px";
			this.effects.push(
				new Effect.Scale(this.currentOpenbox.up(0), 100, options)
			);				
		}
		
		new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'openboxAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (isopen) { 
					this.currentOpenbox.style["height"] = 0;
					//this.currentOpenbox.style["width"] = 0;
				}
				this.animating = false;
			}.bind(this)
		});
	}
}
	