(function($) {
	$(document).ready(function(){
		initSlide();
		gall1 = $('.gallery-slide-wrap').gallery({
			duration: 600,
			autoRotation: 8000,
			listOfSlides: '.gallery-slide>li'
		});
		$('.gallery-slide-wrap .gallery-slide').mouseenter(function(){
			gall1.stop();
		}).mouseleave(function(){
			gall1.play();
		});
		if(typeof($.fn.lightBox)=='function'){
			$('a[rel^="lightbox"]').lightBox();
		}
	});
	
	/* Gallery */
	function Gallery(el, options) {
		this._hold = $(el);
		this.initOptions(options);
		this._timer = options.autoRotation;
		this._t;
		this.initialize();
	}
	
	$.fn.gallery = function(options) {
		return new Gallery(this.get(0), options);
	};
	
	Gallery.prototype = {
		initOptions: function(_obj){
			this.options = {
				randomSlide: false,
				changeHeight: false,
				duration: 700,
				slideElement: 1,
				event: 'click',
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				disableBtn: false,
				nextBtn: 'a.next-link, a.btn-next, a.next',
				prevBtn: 'a.prev-link, a.btn-prev, a.prev',
				circle: true,
				direction: false,
				IE: false,
				swicherReplace: false,
				titleReplace: false
			};
			for(key in _obj) this.options[key] = _obj[key];
		},
		initialize: function(){
			var _this = this;
			var _hold = _this._hold;
			var _speed = _this.options.duration;
			var _timer = _this.options.autoRotation;
			var _el = _hold.find(_this.options.listOfSlides);
			if(_el.length){
				_el.css('display','block');
				if (_this.options.effect){
					_el.css('position','absolute');
					var _list = _el;
				}
				else var _list = _el.parent();
				var _switcher = _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.switcher) var _active = _switcher.index(_switcher.filter('.active:eq(0)'));
				else var _active = _el.index(_el.filter('.active:eq(0)'));
				if (_active < 0) _active = 0;
				var _last = _active;
				if(_this.options.swicherReplace){
					var switcherReplace = _hold.find(_this.options.switcher).parent();
					switcherReplace.html('');
					for (var i = 0; i < _el.length; i++){
						switcherReplace.append('<li><a href="#">'+ (i+1) +'</a></li>');
						if(i==_active){
							switcherReplace.find('li').eq(_active).addClass('active');
						}
					};
					_switcher = switcherReplace.find('li');
				}
				if(_this.options.titleReplace){
					var titleReplace = _hold.find(_this.options.titleReplace);
					if(titleReplace.length){
						_el.each(function(i){
							var this_el = $(this);
							var this_title = this_el.attr('title');
							if(this_title.length){
								_el.eq(i).data("title", this_title);
							}else{
								_el.eq(i).data("title", 'No title');
							}
							this_el.removeAttr('title');
						});
						titleReplace.html(_el.eq(_active).data("title"));
					}
				}
				
				// Installation directions
				if (!_this.options.direction) {
					var _wrapHolderW = Math.ceil(_list.parent().width() / _w);
					if (((_wrapHolderW - 1) * _w + _w / 2) > _list.parent().width()) _wrapHolderW--;
				}
				else{
					var _wrapHolderW = Math.ceil(_list.parent().height()/_h);
					if (((_wrapHolderW-1)*_h + _h/2) > _list.parent().height()) _wrapHolderW--;
				}
				
				// Setting "fade" or "slide" effect
				if (!_this.options.effect) var rew = _count - _wrapHolderW + 1;
				else var rew = _count;
				if (!_this.options.effect) {
					if (!_this.options.direction) _list.css({marginLeft: -(_w * _active)})
					else _list.css({marginTop: -(_h * _active)})
				}
				else {
					_list.css({
						opacity: 0
					}).removeClass('active').eq(_active).addClass('active').css({
						opacity: 1
					}).css('opacity', 'auto');
					_switcher.removeClass('active').eq(_active).addClass('active');
				}
				// Function for "slide"
				function scrollEl(){
					if (!_this.options.direction) _list.animate({marginLeft: -(_w * _active)}, {queue:false, duration: _speed})
					else _list.animate({marginTop: -(_h * _active)}, {queue:false, duration: _speed})
					if (_this.options.switcher && _this.options.slideElement) _switcher.removeClass('active').eq(_active/_this.options.slideElement).addClass('active');
					else{
						if (_this.options.switcher) _switcher.removeClass('active').eq(_active).addClass('active');
					}
					if(_this.options.changeHeight){
						if(_this.options.slideElement>1){
							var active_H = _list.children().eq(_active).outerHeight(true);
							for (var i = 1; i < _this.options.slideElement; i++){
								if(active_H<_list.children().eq(_active+i).outerHeight(true)){
									active_H=_list.children().eq(_active+i).outerHeight(true)
								}
							};
							_el.parent().animate({
								height: active_H
							}, {queue:false, duration:_speed});
						}else{
							_el.parent().animate({
								height: _list.children().eq(_active).outerHeight(true)
							}, {queue:false, duration:_speed});
						}
					}
					if(titleReplace){
						titleReplace.html(_el.eq(_active).data("title"));
					}
				}
				function toPrepare(){
					if ((_active == rew) && _this.options.circle) _active = -_this.options.slideElement;
					for (var i = 0; i < _this.options.slideElement; i++){
						if(_this.options.randomSlide){
							var pre_active = _active;
							if(_this.options.listOfSlides.length){
								while(pre_active == _active){
									_active = parseInt(Math.random()*(_count+1));
								}
							}
						}else{
							_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();
				}
				if(_this._timer){
					_this._hold.bind('runTimer', function(){
						if(_this._t) clearTimeout(_this._t);
						_this._t = setInterval(function(){
							toPrepare();
						}, _this._timer);
					});
				}
				_next.click(function(){
					if(_this._t) clearTimeout(_this._t);
					if (_this.options.disableBtn &&(_count > _wrapHolderW)) _prev.removeClass(_this.options.disableBtn);
					toPrepare();
					if (_this._timer) _this._hold.trigger('runTimer');
					return false;
				});
				_prev.click(function(){
					if(_this._t) clearTimeout(_this._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 (_this._timer) _this._hold.trigger('runTimer');
					return false;
				});
				if (_this.options.switcher) _switcher.click(function(){
					if (_this.options.event=='click'){
						if (_this.options.slideElement){
							_active = _switcher.index($(this))*_this.options.slideElement;
						}else{
							_active = _switcher.index($(this));
						}
						if(_this._t) clearTimeout(_this._t);
						if (!_this.options.effect) scrollEl();
						else fadeElement();
						if (_this._timer) _this._hold.trigger('runTimer');
					}
					return false;
				}).mouseenter(function(){
					if (_this.options.event=='hover'){
						if (_this.options.slideElement){
							_active = _switcher.index($(this))*_this.options.slideElement;
						}else{
							_active = _switcher.index($(this));
						}
						if(_this._t) clearTimeout(_this._t);
						if (!_this.options.effect) scrollEl();
						else fadeElement();
						if (_this._timer) _this._hold.trigger('runTimer');
					}
				});
				if (_this._timer) _this._hold.trigger('runTimer');
			}
		},
		stop: function(){
			var _this = this;
			if (_this._t) clearTimeout(_this._t);
		},
		play: function(){
			var _this = this;
			if (_this._t) clearTimeout(_this._t);
			if (_this._timer) _this._hold.trigger('runTimer');
		}
	}
	
	/* Slide effect */
	function initSlide(){
		var open_class = 'sb-active';
		var slide_blocks_all = $('.slide-block');
		var speed = 500;
		var _H = [];
		var open_all = $('.open-all');
		var open_all_class = 'all-is-open';
		slide_blocks_all.each(function(i){
			var holder = $(this);
			var links = holder.find('.open-close');
			var slide_blocks = holder.find('.block');
			slide_blocks.show();
			var slide_h = slide_blocks.outerHeight();
			_H.push(slide_h);
			if(!holder.hasClass(open_class)){
				slide_blocks.css({
					'height': 0,
					display: 'none'
				});
			}
			links.click(function(){
				var this_link = $(this);
				if(!holder.hasClass(open_class)){
					slide_blocks.show();
					holder.addClass(open_class);
					slide_blocks.stop().animate({
						height: slide_h
					}, {queue:false, duration: speed});
				}else{
					holder.removeClass(open_class);
					open_all.removeClass(open_all_class);
					slide_blocks.stop().animate({
						height: 0
					}, {queue:false, duration: speed, complete:function(){
						slide_blocks.hide();
					}});
				}
				return false;
			});
		});
		open_all.click(function(){
			if(!open_all.hasClass(open_all_class)){
				slide_blocks_all.each(function(i){
					var holder = $(this);
					var slide_blocks = holder.find('.block');
					if (!holder.is('.'+ open_class)) {
							holder.removeClass(open_class);
							slide_blocks.show();
							holder.addClass(open_class);
							open_all.addClass(open_all_class);
							slide_blocks.stop().animate({
								height: _H[i]
							}, {queue:false, duration: speed});
					}
				});
			}else{
				slide_blocks_all.each(function(i){
					var holder = $(this);
					var slide_blocks = holder.find('.block');
					if (holder.is('.'+ open_class)) {
							holder.removeClass(open_class);
							open_all.removeClass(open_all_class);
							slide_blocks.stop().animate({
								height: 0
							}, {queue:false, duration: speed, complete:function(){
								slide_blocks.hide();
							}});
					}
				});
			}
			return false;
		});
	}
}(jQuery));
