// akordiyon.js v2.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// akordiyon is freely distributable under the terms of an MIT-style license.
//
// I don't care what you think about the file size...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

/*-----------------------------------------------------------------------------------------------*/

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

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

	//
	//  Setup the Variables
	//
	showakordiyon : null,
	currentakordiyon : null,
	duration : null,
	effects : [],
	animating : false,
	
	//  
	//  Initialize the akordiyonlar
	//
	initialize: function(kaplama, options) {
	  if (!$(kaplama)) {
	    throw(kaplama+" doesn't exist!");
	    return false;
	  }
	  
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				baslik : 'akordiyon_baslik',
				baslikaktif : 'akordiyon_baslik_aktif',
				icerik : 'akordiyon_icerik'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'dikey',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		var akordiyonlar = $$('#'+kaplama+' .'+this.options.classNames.baslik);
		akordiyonlar.each(function(akordiyon) {
			Event.observe(akordiyon, this.options.onEvent, this.activate.bind(this, akordiyon), false);
			if (this.options.onEvent == 'click') {
			  akordiyon.onclick = function() {return false;};
			}
			
			if (this.options.direction == 'yatay') {
				var options = $H({width: '0px'});
			} else {
				var options = $H({height: '0px'});			
			}
			options.merge({display: 'none'});			
			
			this.currentakordiyon = $(akordiyon.next(0)).setStyle(options);			
		}.bind(this));
	},
	
	//
	//  Activate an akordiyon
	//
	activate : function(akordiyon) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentakordiyon = $(akordiyon.next(0));
		this.currentakordiyon.setStyle({
			display: 'block'
		});		
		
		this.currentakordiyon.previous(0).addClassName(this.options.classNames.baslikaktif);

		if (this.options.direction == 'yatay') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
			
		if (this.currentakordiyon == this.showakordiyon) {
		  this.deactivate();
		} else {
		  this._handleakordiyon();
		}
	},
	// 
	// Deactivate an aktif akordiyon
	//
	deactivate : function() {
		var options = $H({
		  duration: this.duration,
			scaleicerik: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end', 
				scope: 'akordiyonAnimation'
			},
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentakordiyon.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentakordiyon.scrollWidth
			},
			afterFinish: function() {
				this.showakordiyon.setStyle({
          height: 'auto',
					display: 'none'
				});				
				this.showakordiyon = null;
				this.animating = false;
			}.bind(this)
		});    
    options.merge(this.scaling);

    this.showakordiyon.previous(0).removeClassName(this.options.classNames.baslikaktif);
    
		new Effect.Scale(this.showakordiyon, 0, options);
	},

  //
  // Handle the open/close actions of the akordiyon
  //
	_handleakordiyon : function() {
		var options = $H({
			sync: true,
			scaleFrom: 0,
			scaleicerik: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentakordiyon.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentakordiyon.scrollWidth
			}
		});
		options.merge(this.scaling);
		
		this.effects.push(
			new Effect.Scale(this.currentakordiyon, 100, options)
		);

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