if (typeof Effect == 'undefined') 
	throw("tamayuz.js requires including script.aculo.us' effects.js library!");

var tamayuz = Class.create();
tamayuz.prototype = {

	showtamayuz : null,
	currenttamayuz : null,
	duration : null,
	effects : [],
	animating : false,
	initialize: function(container, options) {
	  if (!$(container)) {
	    throw(container+" doesn't exist!");
	    return false;
	  }
	  
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'tamayuz_toggle',
				toggleActive : 'tamayuz_toggle_active',
				content : 'tamayuz_content'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		var tamayuzs = $$('#'+container+' .'+this.options.classNames.toggle);
		tamayuzs.each(function(tamayuz) {
			Event.observe(tamayuz, this.options.onEvent, this.activate.bind(this, tamayuz), false);
			if (this.options.onEvent == 'click') {
			  tamayuz.onclick = function() {return false;};
			}
			
			if (this.options.direction == 'horizontal') {
				var options = $H({width: '0px'});
			} else {
				var options = $H({height: '0px'});			
			}
			options.merge({display: 'none'});			
			
			this.currenttamayuz = $(tamayuz.next(0)).setStyle(options);			
		}.bind(this));
	},
	
		activate : function(tamayuz) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currenttamayuz = $(tamayuz.next(0));
		this.currenttamayuz.setStyle({
			display: 'block'
		});		
		
		this.currenttamayuz.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
			
		if (this.currenttamayuz == this.showtamayuz) {
		  this.deactivate();
		} else {
		  this._handletamayuz();
		}
	},
	deactivate : function() {
		var options = $H({
		  duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end', 
				scope: 'tamayuzAnimation'
			},
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currenttamayuz.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currenttamayuz.scrollWidth
			},
			afterFinish: function() {
				this.showtamayuz.setStyle({
          height: 'auto',
					display: 'none'
				});				
				this.showtamayuz = null;
				this.animating = false;
			}.bind(this)
		});    
    options.merge(this.scaling);

    this.showtamayuz.previous(0).removeClassName(this.options.classNames.toggleActive);
    
		new Effect.Scale(this.showtamayuz, 0, options);
	},

	_handletamayuz : function() {
		var options = $H({
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currenttamayuz.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currenttamayuz.scrollWidth
			}
		});
		options.merge(this.scaling);
		
		this.effects.push(
			new Effect.Scale(this.currenttamayuz, 100, options)
		);

		if (this.showtamayuz) {
			this.showtamayuz.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			});
			options.merge(this.scaling);
			
			this.effects.push(
				new Effect.Scale(this.showtamayuz, 0, options)
			);				
		}
		
    new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'tamayuzAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showtamayuz) {
					this.showtamayuz.setStyle({
						display: 'none'
					});				
				}
				$(this.currenttamayuz).setStyle({
				  height: 'auto'
				});
				this.showtamayuz = this.currenttamayuz;
				this.animating = false;
			}.bind(this)
		});
	}
}
	