/*fadeGallery*/
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		listSelector: '> li',
		navHolder:		false,
		navCreate:		false,
		thumbsSelector: 'li',
		prev:			'a.prev',
		next:			'a.next',
		swichTime:		false,
		delay:			900,
		event: 			'click',
		fadeIEfix:		false,
		onChange:		null,
		onChangeStart:	null
	},_options);
	return this.each(function(){
		var _swichTime = _options.swichTime;
		var _d = (_options.fadeIEfix) ? ($.browser.msie ? 0 : _options.delay) : (_options.delay);
		var _this = $(this);
		var _list = $(_options.listSelector, _this);
		var _linksHold = $(_options.navHolder, _this);

		if(_options.navCreate){
			var _htmlNav ='<ul>';
			for(var i=0; i<_list.length; i++) {
				_htmlNav += '<li><a href="#">'+(i+1)+'</a></li>';
			}
			_htmlNav +='</ul>';
			_linksHold.html(_htmlNav);
		}
		var _links = $(_options.thumbsSelector, _linksHold);
		var _btnPrev = $(_options.prev , _this);
		var _btnNext = $(_options.next , _this);
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		var _t;
		_list.removeClass('active').css({display: 'none', opacity: 0}).eq(_a).addClass('active').css({display: 'block', opacity: 1}).css('opacity', 'auto');
		_links.eq(_a).addClass('active');

		autoSwitch();
		function autoSwitch(){
			if (_swichTime){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, _swichTime);
			}
		}

		if (_btnPrev){
			_btnPrev.click(function(){
				var _prevItem = 0;
				if (_a > 0) _prevItem = _a-1;
				else _prevItem = _list.length-1;
				changeEl(_prevItem);
				return false;
			})
		}
		if (_btnNext){
			_btnNext.click(function(){
				var _nextItem = 0;
				if (_a < _list.length - 1) _nextItem = _a+1;
				else _nextItem = 0;
				changeEl(_nextItem);
				return false;
			})
		}

		if(_links){
			_links.bind(_options.event , function(){
				var _el = $(this);
				if(_options.event == 'mouseenter'){
					_timer = setTimeout(function(){
						var _ind = _links.removeClass('active').index(_el.addClass('active'));
						changeEl(_ind);
					},100);
				}else{
					var _ind = _links.removeClass('active').index(_el.addClass('active'));
					changeEl(_ind);
				};
				return false;
			});
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_list.is(':animated')) _list.stop(true, true);
			if(_ind != _a){
				if (jQuery.isFunction(_options.onChangeStart)) {
					_options.onChangeStart.apply(_this);
				};
				_links.removeClass('active').eq(_ind).addClass('active');
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d});
				_list.eq(_ind).addClass('active').css({opacity: 0, display:'block'}).animate({opacity: 1}, {queue:false, duration:_d,complete:function(){
					$(this).css('opacity', 'auto');
					_a = _ind;
					autoSwitch();
					if (jQuery.isFunction(_options.onChange)) {
						_options.onChange.apply(_this);
					};
				}});
			}
		}
	});
};


jQuery.fn.vvAccordion = function(_options){
	var _options = jQuery.extend({
		el: '> li',
		head: 'a.opener',
		content: 'div.ac-content',
		active: 'selected',
		event: 'click',
		param:'width',
		onBeforeAply: null,
		onChangeStart: null,
		onChange: null,
		duration: 400
	},_options);
	return this.each(function(){
		var _this = this;
		var _t = null;
		_this.duration = _options.duration;
		_this.list = $(_options.el, _this);
		_this.list.each(function(i){
			this.opener = $(_options.head, this);
			this.slider = $(_options.content, this);
			this.width = $(this).width();
			this.height = $(this).height();
			if($(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(){
			if(_options.param=='width'){
				$(this).css({width: 0});
			}else{
				$(this).css({height: 0});
			}
		});
		_this.list.each(function(){
			var next = this;
			this.opener.bind(_options.event, function(){
				if(jQuery.isFunction(_options.onChangeStart)){
					_options.onChangeStart.apply(_this.active);
				};
				if(_t) clearTimeout(_t);
				_t = setTimeout(function(){
					_this.curent = next;
					var _val = _options.param=='width' ? next.width : next.height;
					if(_options.param=='width'){
						$(next).animate({width: _val},{duration: _options.duration,
						step:function(i, curObj){
							var _p = curObj.now/curObj.end;
							if(_this.active !== next){
								var _thisValue = 0;
								_thisValue = _this.active.width - (_this.active.width * _p);
								eval('$(_this.active).css({'+curObj.prop+':'+_thisValue+'})');
							}
						},
						complete: function(){
							$(_this.active).removeClass(_options.active);
							$(_this.active).css({width: 0});
							_this.active = next;
							$(_this.active).addClass(_options.active);
							$(_this.active).css({width: _this.active.width});
							if(jQuery.isFunction(_options.onChange)){
								_options.onChange.apply(_this.active);
							};
							_this.curent = false;
						}});
					}else{
						$(next).animate({height: _val},{duration: _options.duration,
						step:function(i, curObj){
							var _p = curObj.now/curObj.end;
							if(_this.active !== next){
								var _thisValue = 0;
								_thisValue = _this.active.height - (_this.active.height * _p);
								eval('$(_this.active).css({'+curObj.prop+':'+_thisValue+'})');
							}
						},
						complete: function(){
							$(_this.active).removeClass(_options.active);
							$(_this.active).css({height: 0});
							_this.active = next;
							$(_this.active).addClass(_options.active);
							$(_this.active).css({height: _this.active.height});
							if(jQuery.isFunction(_options.onChange)){
								_options.onChange.apply(_this.active);
							};
							_this.curent = false;
						}});
					}
				},150);
				return false;
			});
		});
	});
}


$(window).bind('load',function(){
	$('ul.horizontal-accordion').vvAccordion({
		el: '> li',
		head: 'a.opener',
		content: 'div.holder',
		event: 'mouseenter',
		param:'width',
		activeClass: 'selected',
		duration: 400
	});
	$('ul.vertical-accordion').vvAccordion({
		el: '> li',
		head: 'a.opener',
		content: 'div.holder',
		event: 'mouseenter',
		param:'height',
		activeClass: 'selected',
		duration: 400
	});
});

$(function(){
	$('.slideshow').fadeGallery({
		listSelector: '.f-items img',
		navHolder:		'> ul',
		thumbsSelector: 'li',
		swichTime:		false,
		delay:			900,
		fadeIEfix:		false,
		onChangeStart:		function(){
			var _arrow = this.find('.arrow');
			var _top = this.find('> ul > li.active').position().top+3;
			_arrow.animate({top: _top}, {duration:900, queue: false});
		}
	})
})