function iniSlide(){
	var _speed = 200;
	$('ul.items-list .overflow').each(function(){
		var _this = $(this);
		var _slide = _this.find('.description');
		var _slide_b = parseInt(_slide.css('bottom'));
		var _cont = _slide.find('p');
		_slide.css('bottom', -_cont.outerHeight(true));
		_this.mouseenter(function(){
			_slide.animate({
				bottom: -_slide_b
			}, {queue:false, duration: _speed});
		}).mouseleave(function(){
			_slide.animate({
				bottom: -_cont.outerHeight(true)
			}, {queue:false, duration: _speed});
		});
	});
}

function gallery(_obj) {
	
	// defaults options
	this.options = {
		hold: 'div.gallery-slide',
		duration: 700,
		slideElement: 1,
		autoRotation: false,
		effect: false,
		fadeEl: 'ul',
		switcher: 'ul > li',
		disableBtn: false,
		nextBtn: 'a.link-next, a.btn-next, a.next',
		prevBtn: 'a.link-prev, a.btn-prev, a.prev',
		circle: true,
		direction: false
	};
	for(key in _obj) this.options[key] = _obj[key];
	
	var _this = this;
	var _hold = $(_this.options.hold);
	var _speed = $.browser.msie ? 0 : _this.options.duration;
	var _timer = _this.options.autoRotation;
	var _wrap = _hold.find(_this.options.fadeEl);
	var _el = _hold.find(_this.options.switcher);
	var _next = _hold.find(_this.options.nextBtn);
	var _prev = _hold.find(_this.options.prevBtn);
	var _count = _el.index(_el.filter(':last'));
	var _w = _el.outerWidth(true);
	var _h = _el.outerHeight(true);
	if (!_this.options.direction) {
		var _wrapHolderW = Math.ceil(_wrap.parent().width() / _w);
		if (((_wrapHolderW - 1) * _w + _w / 2) > _wrap.parent().width()) _wrapHolderW--;
	}
	else{
		var _wrapHolderW = Math.ceil(_wrap.parent().height()/_h);
		if (((_wrapHolderW-1)*_h + _h/2) > _wrap.parent().height()) _wrapHolderW--;
	}
	if (_timer) var _t;
	var _active = _el.index(_el.filter('.active:eq(0)'));
	if (_active < 0) _active = 0;
	var _last = _active;
	if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
	else var rew = _count;
	
	if (!_this.options.effect) {
		if (!_this.options.direction) _wrap.css({marginLeft: -(_w * _active)})
		else _wrap.css({marginTop: -(_h * _active)})
	}
	else {
		_wrap.css({
			opacity: 0
		}).removeClass('active').eq(_active).addClass('active').css({
			opacity: 1
		}).css('opacity', 'auto');
		_el.removeClass('active').eq(_active).addClass('active');
	}
	if (_this.options.disableBtn) {
		if (_count < _wrapHolderW) _next.addClass(_this.options.disableBtn);
		_prev.addClass(_this.options.disableBtn);
	}
	
	function fadeElement(){
		_wrap.eq(_last).animate({opacity:0}, {queue:false, duration: _speed});
		_wrap.removeClass('active').eq(_active).addClass('active').animate({
			opacity:1
		}, {queue:false, duration: _speed, complete: function(){
			$(this).css('opacity','auto');
		}});
		_el.removeClass('active').eq(_active).addClass('active');
		_last = _active;
	}
	function scrollEl(){
		if (!_this.options.direction) _wrap.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed})
		else _wrap.animate({marginTop: -(_h * _active)}, {queue:false, duration: _speed})
	}
	function toPrepare(){
		if ((_active == rew) && _this.options.circle) _active = -_this.options.slideElement;
		for (var i = 0; i < _this.options.slideElement; i++){
			_active++;
			if (_active > rew) {
				_active--;
				if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
			}
		};
		if (_active == rew) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.addClass(_this.options.disableBtn);
		if (!_this.options.effect) scrollEl();
		else fadeElement();
	}
	function runTimer(){
		_t = setInterval(function(){
			toPrepare();
		}, _timer);
	}
	_next.click(function(){
		flag=true;
		if(_t) clearTimeout(_t);
		if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
		toPrepare();
		if (_timer) runTimer();
		return false;
	});
	_prev.click(function(){
		flag=true;
		if(_t) clearTimeout(_t);
		if (_this.options.disableBtn &&(_count > _wrapHolderW)) _next.removeClass(_this.options.disableBtn);
		if ((_active == 0) && _this.options.circle) _active = rew + _this.options.slideElement;
		for (var i = 0; i < _this.options.slideElement; i++){
			_active--;
			if (_active < 0) {
				_active++;
				if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
			}
		};
		if (_active == 0) if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.addClass(_this.options.disableBtn);
		if (!_this.options.effect) scrollEl();
		else fadeElement();
		if (_timer) runTimer();
		return false;
	});
	if (_this.options.effect) _el.click(function(){
		_active = _el.index($(this));
		if(_t) clearTimeout(_t);
		fadeElement();
		if (_timer) runTimer();
		if(_this.options.fadeEl != _this.options.switcher ){
			return false;
		}
	});
	if (_timer) runTimer();
	
	this.stop = function(){
		if (_t) clearTimeout(_t);
	}
	this.play = function(){
		if (_t) clearTimeout(_t);
		if (_timer) runTimer();
	}
}

$(function(){
	iniSlide();
	var gal = new gallery({
		hold: '.gallery',
		duration: 1000,
		autoRotation: 5000,
		fadeEl: '.visual ul > li',
		switcher: '.services-block .area',
		effect: 'fade'
	});
	$('.gallery .visual ul').mouseenter(function(){
		gal.stop();
	}).mouseleave(function(){
		gal.play();
	});
});

