// floatws.js v2.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Floatws 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("floatws.js requires including script.aculo.us' effects.js library!");

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

	//
	//  Setup the Variables
	//
	showFloatws : null,
	currentFloatws : null,
	duration : null,
	effects : [],
	animating : false,
	
	//  
	//  Initialize the floatwss
	//
	initialize: function(container, options) {
	  if (!$(container)) {
	    throw(container+" doesn't exist!");
	    return false;
	  }
	  
		this.options = Object.extend({
			resizeSpeed : 2,
			classNames : {
				toggle : 'floatws_toggle',
				toggleClose : 'floatws_close',
				toggleActive : 'floatws_toggle_active',
				content : 'floatws_content'
			},
			defaultSize : {
				height : 150,
				width : 502
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		var floatwss = $$('#'+container+' .'+this.options.classNames.toggle);
		floatwss.each(function(floatws) {
			Event.observe(floatws, this.options.onEvent, this.activate.bind(this, floatws), false);
			if (this.options.onEvent == 'click') {
			  floatws.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.currentFloatws = $(floatws.next(0)).setStyle(options);			
		}.bind(this));
		
		var floatwss_close = $$('#'+container+' .'+this.options.classNames.toggleClose);
		floatwss_close.each(function(floatws_close) {
			Event.observe(floatws_close, this.options.onEvent, this.deactivate.bind(this, floatws_close), false);
			if (this.options.onEvent == 'click') {
			  floatws_close.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.currentFloatws = $(floatws_close.next(0)).setStyle(options);			
		}.bind(this));
	},
	
	//
	//  Activate an floatws
	//
	activate : function(floatws) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentFloatws = $(floatws.next(0));
		this.currentFloatws.setStyle({
			display: 'block'
		});		
		
		this.currentFloatws.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: true
			});
		} else {
			this.scaling = $H({
				scaleX: true,
				scaleY: true
			});			
		}
			
		if (this.currentFloatws == this.showFloatws) {
		  this.deactivate();
		} else {
		  this._handleFloatws();
		}
	},
	// 
	// Deactivate an active floatws
	//
	deactivate : function() {
		var options = $H({
		  duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end', 
				scope: 'floatwsAnimation'
			},
			scaleMode: { 
				originalHeight: this.options.defaultSize.height,// ? this.options.defaultSize.height : this.currentFloatws.scrollHeight,
				originalWidth: this.options.defaultSize.width // ? this.options.defaultSize.width : this.currentFloatws.scrollWidth
			},
			afterFinish: function() {
				this.showFloatws.setStyle({
          			display: 'none'
				});				
				this.showFloatws = null;
				this.animating = false;
			}.bind(this)
		});    
    options.merge(this.scaling);

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

  //
  // Handle the open/close actions of the floatws
  //
	_handleFloatws : 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.currentFloatws.scrollHeight,
				originalWidth: this.options.defaultSize.width // ? this.options.defaultSize.width : this.currentFloatws.scrollWidth
			}
		});
		options.merge(this.scaling);
		
		this.effects.push(
			new Effect.Scale(this.currentFloatws, 100, options)
		);

		if (this.showFloatws) {
			this.showFloatws.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.showFloatws, 0, options)
			);				
		}
		
    new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'floatwsAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showFloatws) {
					this.showFloatws.setStyle({
						display: 'none'
					});				
				}
				/*$(this.currentFloatws).setStyle({
				  height: 'auto'
				});*/
				this.showFloatws = this.currentFloatws;
				this.animating = false;
			}.bind(this)
		});
	}
}
	