jQuery.fn.vvAccordion = function(_options){
	var _options = jQuery.extend({
		el: '> li',
		head: 'a.opener',
		content: 'div.ac-content',
		active: 'selected',
		event: 'click',
		collapsible: false,
		onBeforeAply: null,
		onChangeStart: null,
		onChange: null,
		onMade: null,
		duration: 400
	},_options);

	return this.each(function(){
		var _this = this;
		var _t = null;
		_this.duration = _options.duration;
		_this.list = jQuery(_options.el, _this);
		_this.list.each(function(i){
			this.opener = jQuery(_options.head, this);
			jQuery(_options.content, this).css({display: 'block'});
			this.slider = jQuery(this);
			this.width = jQuery(this).width();
			this.height = jQuery(this).height();
			if(jQuery(this).hasClass(_options.active)){
				_this.active = this;
			};
			this.index = i;
		});
		if(!_this.active) _this.active = _this.list[0];
		if(jQuery.isFunction(_options.onBeforeAply)){
			_options.onBeforeAply.apply(_this);
		};
		_this.list.not(_this.active).each(function(){
			this.slider.css({
				width: '0px',
				overflow: 'hidden'
			});
		});
		_this.list.each(function(){
			var next = this;
			this.opener.bind(_options.event, function(){
					if(jQuery(this).parent().hasClass(_options.active) && _options.collapsible){
						next.slider.animate({width: 0},{duration: _options.duration,
							complete: function(){
								jQuery(_this.active).removeClass(_options.active);
								_this.active.slider.css({width: 0});
								_this.active = null;
								if(jQuery.isFunction(_options.onChange)){
									_options.onChange.apply(_this.active);
								};
								_this.curent = false;
							}});
					}else{
						if(jQuery.isFunction(_options.onChangeStart)){
							_options.onChangeStart.apply(_this.active);
						};
						if(_t) clearTimeout(_t);
						_t = setTimeout(function(){
							_this.curent = next;
							next.slider.animate({width: next.width},{duration: _options.duration,
							step:function(i, curObj){
								if(_this.active){
									var _p = curObj.now/curObj.end;
									if(_this.active !== next){
										var _thisValue = 0;
										if (curObj.prop == 'width') {
											_thisValue = _this.active.width - (_this.active.width * _p);
										}else{
											_thisValue = curObj.end - curObj.now;
										}
										eval('_this.active.slider.css({'+curObj.prop+':'+_thisValue+'})');
									}
								}
							},
							complete: function(){
								if(_this.active){
									jQuery(_this.active).removeClass(_options.active);
									_this.active.slider.css({
										width: 0,
										overflow: 'hidden'
									});
								}
								_this.active = next;
								jQuery(_this.active).addClass(_options.active);
								_this.active.slider.css({width: _this.active.width});
								if(jQuery.isFunction(_options.onChange)){
									_options.onChange.apply(_this.active);
								};
								_this.curent = false;
							}});
						},150);
					}
					return false;
			});
		});
		if(jQuery.isFunction(_options.onMade)){
			_options.onMade.apply(_this);
		};
	});
}
jQuery(function(){
	jQuery('ul.accordion').vvAccordion({
		el: '> li',
		head: 'a.opener',
		content: 'div.block',
		active: 'active',
		event: 'click',
		duration: 400
	});
})

