jQuery(document).ready(function(){
	jQuery('div.f-gall').fadeGallery({
		listSelector: 'div.img-hold > img',
		addListSelector: 'ul.description-f-gal > li',
		prev: 'a.btn-next',
		next: 'a.btn-prev',
		navHolder: 'div.sub-nav',
		navCreate: true,
		galInThumb: true,
		swichTime: 7000
	});
	jQuery('div.second-f-gal').fadeGallery({
		listSelector: 'div.f-item',
		navHolder: 'div.caption-els ul.dish-list',
		swichTime:false
	});
	jQuery('div.photo-list a[rel="kpicasa_gallery"]').lightBox({
		imageLoading: 'wp-content/themes/ember/images/lightbox-ico-loading.gif',
		imageBtnPrev: 'wp-content/themes/ember/images/lightbox-btn-prev.gif',
		imageBtnNext: 'wp-content/themes/ember/images/lightbox-btn-next.gif',
		imageBtnClose: 'wp-content/themes/ember/images/lightbox-btn-close.gif',
		imageBlank: 'wp-content/themes/ember/images/lightbox-blank.gif',
		fixedNavigation:true
	});
});
/*fadeGallery*/
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		listSelector: ' > li',
		addListSelector: false,
		navHolder:		false,
		navCreate:		false,
		thumbsSelector: 'li',
		galInThumb: 	false,
		prev:			'a.prev',
		next:			'a.next',
		swichTime:		false,
		delay:			900,
		fadeIEfix:		false,
		onChange:		null
	},_options);
	return this.each(function(){
		var _swichTime = _options.swichTime;
		var _d = (_options.fadeIEfix) ? (jQuery.browser.msie ? 0 : _options.delay) : (_options.delay);
		var _d2 = jQuery.browser.msie ? 0 : _options.delay;
		var _this = jQuery(this);
		var _list = jQuery(_options.listSelector, _this);
		var _addList = jQuery(_options.addListSelector, _this);

		var _linksHold = jQuery(_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);
		}
		if(_options.navHolder) var _links = jQuery(_options.thumbsSelector, _linksHold);
		else var _links = jQuery(_options.thumbsSelector, _this);
		var _btnPrev = jQuery(_options.prev , _this);
		var _btnNext = jQuery(_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');
		if(_addList) _addList.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;
				if(_options.galInThumb) scrollThumbs(_prevItem);
				changeEl(_prevItem);
				return false;
			});
		}
		if (_btnNext){
			_btnNext.click(function(){
				var _nextItem = 0;
				if (_a < _list.length - 1) _nextItem = _a+1;
				else _nextItem = 0;
				if(_options.galInThumb)  scrollThumbs(_nextItem);
				changeEl(_nextItem);
				return false;
			})
		}


		if(_links){
			_links.click(function(){
				var _ind = _links.index(jQuery(this));
				changeEl(_ind);
				return false;
			})
		};

		function scrollThumbs(next){
			if(_options.galInThumb){
				_links.removeClass('active').eq(next).addClass('active');
				var pos = _links.eq(next).position().top + parseInt(_links.eq(next).parent().css('top'),10);
				var _pH = _links.eq(next).parent().parent().height();
				var _hH = _links.eq(next).parent().height();
				var _maxOffset = _hH - _pH;

			}else _links.removeClass('active').eq(next).addClass('active');
		}
//get Active position
		var _linksParent = _links.parent().parent();
		var _maxMargin = _links.outerHeight(true)*_links.length - _linksParent.height();
		function getActivePosition(_next){
			var _curentPosition = _links.eq(_next).offset().top;
			var _ThumbsWindowPosition = _linksParent.offset().top;
			if(_curentPosition > _ThumbsWindowPosition && _curentPosition < (_ThumbsWindowPosition+_linksParent.height())){
				//we can see active thumbs now
				return false;
			}else{
				//need scroll thumbs
				return true;
			}
		}
//move thumbs
		function moveGallery(_next){
			if(_a > _next){
				_mM = _links.outerHeight(true)*_next - _linksParent.height()+_links.outerHeight(true);
				if(_mM < 0) _mM = 0;
			}else{
				_mM = _links.outerHeight(true)*_next;
				if(_mM > _maxMargin) _mM = _maxMargin;
			}
			_links.eq(_next).parent().animate({top: -_mM},{duration: _d, queue: false});
			_direction = false;
		}

		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_list.is(':animated')) _list.stop(true, true);
			if(_ind != _a){
				if(_options.galInThumb){
					var _direction = getActivePosition(_ind);
					if(_direction){
						moveGallery(_ind);
					}
				}
				if(_links) _links.removeClass('active').eq(_ind).addClass('active');
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d, complete: function(){jQuery(this).css({display:'none'})}});
				_list.eq(_ind).addClass('active').css({opacity: 0, display:'block'}).animate({opacity: 1}, {queue:false, duration:_d,complete:function(){
					jQuery(this).css('opacity', 'auto');
					_a = _ind;
					autoSwitch();
					if (jQuery.isFunction(_options.onChange)) {
						_options.onChange.apply(_this);
					};
				}});
				if(_addList){
					_addList.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:_d2, complete: function(){jQuery(this).css({display:'none'})}});
					_addList.eq(_ind).addClass('active').css({opacity: 0, display:'block'}).animate({opacity: 1}, {queue:false, duration:_d2,complete:function(){
						jQuery(this).css('opacity', 'auto');
					}});
				}
			}
		}
	});
};
