/**
 * Copyright (c) 2007 Alexander Interactive, Inc
 *                    http://www.alexanderinteractive.com
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
 
var Slider = new Class({
	initialize: function(slider,options) {
		options = options ? options : {};
		
		var self = this;
		this.onShow = options.onShow ? options.onShow : null;
		this.onHide = options.onHide ? options.onHide : null;
				
		var width = slider.getStyle('width').toInt();
		var kids = slider.getChildren()
		var numKids = kids.length
		var normalWidth = Math.floor(width/numKids)
		var bigWidth = options.bigWidth ? options.bigWidth : Math.floor(normalWidth * 1.5);
		var littleWidth = Math.floor((width - bigWidth)/(numKids-1));

		this.bigWidth = bigWidth;
		this.normalWidth = normalWidth;
		this.littleWidth = littleWidth;


		// set up inner box:
		slider.setStyle('overflow','hidden');
		var container = new Element('div');
		var innerWidth = width+10;
		container.setStyles({width:innerWidth+'px'});
		slider.adopt(container);
		
		// set up kid wrappers:
		kids = kids.map(function(kid,i) {
			var wrapper = new Element('div');
			if (options.wrapperClass) wrapper.addClass(options.wrapperClass);
			if (options.wrapperStyles)
				wrapper.setStyles(options.wrapperStyles);
			wrapper.setStyles({width:normalWidth,
							   overflow:'hidden',
							   'float':'left'});
			kid.setStyle('width',bigWidth);
			wrapper.adopt(kid);
			container.adopt(wrapper);
			return wrapper;
		});
		
		this.kids = kids;
		
		// set up effects:
		var duration = options.duration ? options.duration : 200;
		var transition = options.transition ? options.transition : Fx.Transitions.quadOut;
		var fx = new Fx.Elements(kids, {wait: false, duration: duration, transition: transition,
										onComplete:function(){self.fxCompleted();}});
		kids.each(function(kid, i){
			kid.addEvent('click', function(e){
				var obj = {};
				obj[i] = {'width': [kid.getStyle('width').toInt(), bigWidth]};
				
				kids.each(function(other, j){
					if (other != kid){
						var w = other.getStyle('width').toInt();
						if (w != littleWidth) obj[j] = {'width': [w, littleWidth]};
					}
				});
				fx.start(obj);	
			});	
		});
		$$(['formz1', 'formz2', 'formz3', 'formz4', 'formz5', 'formz6', 'formz7']).addEvent('reset', function(e){
			var obj = {}
			kids.each(function(other, j){
				obj[j] = {'width': [other.getStyle('width').toInt(), normalWidth]};
			});
			fx.start(obj);
		});
	},
	fxCompleted: function() {
		var self = this;
		if (self.onShow)
			self.kids.each(function(kid, i) {
				if (kid.getStyle('width').toInt() == self.bigWidth)
					typeof(self.onShow) == "function" ? self.onShow(kid) : self.onShow[i](kid);
			});
		if (self.onHide)
			self.kids.each(function(kid, i) {
				if (kid.getStyle('width').toInt() != self.bigWidth)
					typeof(self.onHide) == "function" ? self.onHide(kid) : self.onHide[i](kid);
			});
	}
})

function createSliders(sliders,options) {
	sliders.each(function(slider) {
		new Slider(slider,options);
	});
}