(function ($) {
	$.fn.wxaccordion = function (options) {
		options = $.extend({
			height: 'auto',
			orient: 'horizontal',
			panelWidthCollapse: 100,
			panelWidthExpand: 'auto',
			hidePanelDescription: true,
			panelDescription: '.wx-accordion-panel-description'
		}, options);
		
		var widget = this;
		
		widget.addClass('wx-accordion').addClass('wx-accordion-orient-horizontal');
		widget.options = options;
		
		$('> *', widget).addClass('wx-accordion-panel').each(function (i) {
			var accordionPanel = $(this), 
				maxWidth = 0,
				panelDescription = $(widget.options.panelDescription, accordionPanel);
			
			panelDescription
				.addClass('wx-accordion-panel-description')
				.wrap('<div class="wx-accordion-panel-description-container">');
				
			panelDescription = panelDescription.parent();
			
			if (widget.options.height !== 'auto') {
				accordionPanel.height(widget.options.height);
			}
			
			if (widget.options.hidePanelDescription) {
				panelDescription.each(function () {
					$(this).data('height', $(this).outerHeight())
						.css({
							height:0,
							overflow:'hidden'
						});
				});
			}
			
			if (widget.options.panelWidthExpand == 'auto') {
				maxWidth = Math.max.apply(null, $.makeArray($('> *', accordionPanel).map(function () {
					return $(this).width();
				})));
				widget.data('panelWidthExpand', maxWidth);
			} else {
				widget.data('panelWidthExpand', widget.options.panelWidthExpand);
			}
			
			widget.data('panelWidthCollapse', widget.options.panelWidthCollapse);
			accordionPanel.width(widget.options.panelWidthCollapse);
			accordionPanel.hover(function (event) {
				var self = $(this)
					panelDescription = $('.wx-accordion-panel-description-container', self);
					
				self.addClass('wx-accordion-panel-active').stop();
				
				if (widget.options.hidePanelDescription) {
					$({
						height:0,
						width:self.width()
					}).animate({
						height:panelDescription.data('height'),
						width:widget.data('panelWidthExpand')
					}, {
						step: function () {
							panelDescription.height(this.height);
							self.width(this.width);
						}
					});
				} else {
					$({
						width:self.width()
					}).animate({
						width:widget.data('panelWidthExpand')
					}, {
						step: function () {
							self.width(this.width);
						}
					});
				}
			}, function (event) {
				var self = $(this);
				self.removeClass('wx-accordion-panel-active').stop();
				if (widget.options.hidePanelDescription) {
					$({
						height:panelDescription.data('height'),
						width:self.width()
					}).animate({
						height:0,
						width:widget.data('panelWidthCollapse')
					}, {
						step: function () {
							panelDescription.height(this.height);
							self.width(this.width);
						}
					});
				} else {
					$({width:self.width()}).animate({
						width:widget.data('panelWidthCollapse')
					}, {
						step: function () {
							self.width(this.width);
						}
					});
				}
			});
		});
	};
})(jQuery);
