/*
---
description:     Kwicks

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires:
  core/1.2.1:   '*'

provides:
  - Kwicks
...
*/
var Kwicks = new Class({

	Implements: [Options],

	options: {
		squeezeWidth: 100,
		maxWidth: 285,
		offset: 192
	},

	initialize: function(list,options) {
		this.setOptions(options);
		this.list = document.id(list);
		this.parse();
	},

	parse: function() {
		var items = this.list.getElements('li'),
			 fx = new Fx.Elements(items, {wait: false, duration: 500, transition:Fx.Transitions.Cubic.easeOut}),
			 fxh = new Fx.Elements(items.getElements('h2'), {wait: false, duration: 500}),
			 fxc = new Fx.Elements(items.getElements('.c'), {wait: false, duration: 500}),
			 startWidths = [],
			 options = this.options;
		items.each(function(item,i) {
			startWidths.push(item.getStyle('width').toInt());
			if(item.getElement('.c').getStyle('display') == 'none') {
				item.getElement('.c').setStyles({ 'opacity': '0', 'display': 'block' });
				item.getElement('h2').setStyle('background-position', '0 0');
			} else {
				item.getElement('h2').setStyle('background-position', '20px 0');
			}
			if(item.getElement('a')) {
				item.setStyle('cursor', 'pointer');
				item.addEvent('click', function() {
					window.location = item.getElement('a').get('href');
				});
			}
			item.addEvent('mouseenter',function(){
				var fxSettings = {}, fxhSettings = {}, fxcSettings = {};
				fxSettings[i] = {
					'width': [item.getStyle('width').toInt(),options.maxWidth],
					'background-position' : '0 0'
				};
				fxhSettings[i] = {
					'background-position' : '20px 0'
				};
				fxcSettings[i] = {
					'opacity' : '1',
					'paddingLeft': '30px'
				};
				items.each(function(sibling,ii) {
					if(sibling != item) {
						var w = sibling.getStyle('width').toInt();
						if (w != options.squeezeWidth) {
							fxSettings[ii] = {
								'width': [w,options.squeezeWidth],
								'background-position': '-' + options.offset + 'px 0'
							};
							fxhSettings[ii] = {
								'background-position': '0 0'
							};
							fxcSettings[ii] = {
								'opacity': '0',
								'paddingLeft': '10px'
							};
						}
					}
				},this);
				fx.start(fxSettings);
				fxh.start(fxhSettings);
				fxc.start(fxcSettings);
			},this);
		},this);
	}
});
