//init global page
jQuery(function(){
	initPlugins();
});

//init plugins
function initPlugins(){
	//init sidebar gallery
	jQuery('.gallery').gallery({
		duration: 700,
		slideElement:1,
		autoRotation: 5000,
		effect: 'fade',
		listOfSlides: '.slider > li',
		circle: true,
		pauseOnHover: true
	});
}

//gallery plugin
(function(jQuery) {
	jQuery.fn.gallery = function(options) {
		var args = Array.prototype.slice.call(arguments);
		args.shift();
		this.each(function(){
			if(this.galControl && typeof options === 'string') {
				if(typeof this.galControl[options] === 'function') {
					this.galControl[options].apply(this.galControl, args);
				}
			} else {
				this.galControl = new Gallery(this, options);
			}
		});
		return this;
	};
	function Gallery(context, options) { this.init(context, options); };
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = jQuery.extend({
				duration: 700,
				slideElement:1,
				autoRotation: false,
				effect: false,
				listOfSlides: '.list > li',
				switcher: false,
				autoSwitcher: false,
				disableBtn: false,
				title: false,
				titleAutoDimensions: false,
				titleEffect: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				clone: false,
				direction: false,
				pauseOnHover: false,
				event: 'click'
			}, options || {});
			var self = this;
			this.context = jQuery(context);
			this.els = this.context.find(this.options.listOfSlides);
			this.list = this.els.parent();
			this.count = this.els.length;
			this.title = jQuery(this.options.title, this.context);
			this.autoRotation = this.options.autoRotation;
			this.direction = this.options.direction;
			this.duration = this.options.duration;
			if (this.options.clone) {
				this.list.append(this.els.clone());
				this.list.prepend(this.els.clone());
				this.els = this.context.find(this.options.listOfSlides);
			}
			this.wrap = this.list.parent();
			if (this.options.nextBtn) this.nextBtn = this.context.find(this.options.nextBtn);
			if (this.options.prevBtn) this.prevBtn = this.context.find(this.options.prevBtn);

			this.calcParams(this);
			
			if (this.options.autoSwitcher) {
				this.switcherHolder = this.context.find(this.options.switcher).empty();
				this.switchPattern = jQuery('<ul class="'+ (this.options.autoSwitcher == true ? '' : this.options.autoSwitcher) +'"></ul>');
				for (var i=0;i<this.max+1;i++){
					jQuery('<li><a href="#">'+i+'</a></li>').appendTo(this.switchPattern);
				}
				this.switchPattern.appendTo(this.switcherHolder);
				this.switcher = this.context.find(this.options.switcher).find('li');
				this.active = 0;
				this.index = 0;
			} else {
				if (this.options.switcher) {
					this.switcher = this.context.find(this.options.switcher);
					this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
					this.index = this.active;
				}else{
					this.active = this.els.index(this.els.filter('.active:eq(0)'));
					this.index = this.active;
				}
			}
			if (this.active < 0){
				this.active = 0;
				this.index = this.active;
			}
			this.last = this.active;
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			if (this.options.clone) this.active += this.count;
			
			if (this.options.effect) this.els.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
			else {
				if (this.direction) this.list.css({marginTop: -(this.mas[this.active])});
				else this.list.css({marginLeft: -(this.mas[this.active])});
			}
			if(this.options.titleEffect){
				this.title.hide().css({opacity: 0}).eq(this.index).show().css({opacity: 1});
				if(this.options.titleAutoDimensions){
					this.title.parent().css({
						width: this.title.eq(this.index).width(),
						height: this.title.eq(this.index).height()
					});
				}
			}
			this.lastIndex = this.index;
			if (this.options.nextBtn) this.initEvent(this, this.nextBtn,true);
			if (this.options.prevBtn) this.initEvent(this, this.prevBtn,false);
			
			this.initWindow(this,jQuery(window));
			
			if (this.autoRotation) this.runTimer(this);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
			if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
			if (this.options.pauseOnHover){
				this.context.bind('mouseenter', jQuery.proxy(this.stop, this));
				this.context.bind('mouseleave', jQuery.proxy(this.play, this));
			}
		},
		calcParams: function(self){
			this.mas = [];
			this.sum = 0;
			this.max = this.count-1;
			this.width = 0;
			if (!this.options.effect) {
				this.els.each(function(){self.mas.push(self.width);self.width += self.direction?jQuery(this).outerHeight(true):jQuery(this).outerWidth(true);self.sum+=self.direction?jQuery(this).outerHeight(true):jQuery(this).outerWidth(true);});
				this.finish = this.direction?this.sum-this.wrap.outerHeight():this.sum-this.wrap.outerWidth();
				for (var i=0;i<this.count;i++){
					if (this.mas[i]>=this.finish) {
						this.max = i;
						break;
					}
				}
			}
		},
		changeSettings: function(set,val){
			this[set] = val;
		},
		fadeElement: function(){
			this.els.eq(this.last).animate({opacity:0}, {queue:false, duration: this.duration});
			this.els.removeClass('active').eq(this.active).addClass('active').animate({
				opacity:1
			}, {queue:false, duration: this.duration, complete: function(){
				jQuery(this).css('opacity','auto');
			}});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			if(this.options.title){
				if(this.options.title.length){
					if(this.options.titleEffect){
						this.title.eq(this.lastIndex).animate({opacity: 0}, this.duration, function(){
							jQuery(this).hide();
						});
						this.title.eq(this.index).show().animate({opacity: 1}, this.duration);
						if(this.options.titleAutoDimensions){
							this.title.parent().stop().animate({
								width: this.title.eq(this.index).width(),
								height: this.title.eq(this.index).height()
							}, this.duration);
						}
					}else{
						if(this.options.titleDirection){
							this.title.parent().animate({marginTop: -this.title.eq(this.index)}, this.duration);
						}else{
							this.title.parent().animate({marginLeft: -this.title.eq(this.index)}, this.duration);
						}
					}
				}
			}
			this.lastIndex = this.index;
			this.last = this.active;
		},
		scrollElement: function(f){
			if (this.direction) this.list.animate({marginTop: f ? -this.finish : -(this.mas[this.active])}, {queue:false, duration: this.duration});
			else this.list.animate({marginLeft: f ? -this.finish : -(this.mas[this.active])}, {queue:false, duration: this.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.options.clone ? this.active < this.count ? this.active/this.options.slideElement : this.active >= this.count*2 ? (this.active - this.count*2)/this.options.slideElement : (this.active - this.count)/this.options.slideElement : this.active/this.options.slideElement).addClass('active');
			if(this.options.title){
				if(this.options.title.length){
					if(this.options.titleEffect){
						this.title.eq(this.lastIndex).animate({opacity: 0}, this.duration, function(){
							jQuery(this).hide();
						});
						this.title.eq(this.index).show().animate({opacity: 1}, this.duration);
						if(this.options.titleAutoDimensions){
							this.title.parent().stop().animate({
								width: this.title.eq(this.index).width(),
								height: this.title.eq(this.index).height()
							}, this.duration);
						}
					}else{
						if(this.options.titleDirection){
							this.title.parent().animate({marginTop: -this.title.eq(this.index)}, this.duration);
						}else{
							this.title.parent().animate({marginLeft: -this.title.eq(this.index)}, this.duration);
						}
					}
				}
			}
			this.lastIndex = this.index;
			this.last = this.active;
		},
		runTimer: function(jQuerythis){
			if(jQuerythis._t) clearTimeout(jQuerythis._t);
			jQuerythis._t = setInterval(function(){
				jQuerythis.nextStep();
			}, this.autoRotation);
		},
		initEventSwitcher: function(jQuerythis, el){
			el.bind(jQuerythis.options.event, function(){
				if (!jQuery(this).hasClass('active')){
					jQuerythis.active = jQuerythis.switcher.index(jQuery(this)) * jQuerythis.options.slideElement;
					if (jQuerythis.options.clone) jQuerythis.active += jQuerythis.count;
					jQuerythis.initMove();
				}
				return false;
			});
		},
		initEvent: function(jQuerythis, addEventEl, dir){
			addEventEl.bind(jQuerythis.options.event, function(){
				if (dir) jQuerythis.nextStep();
				else jQuerythis.prevStep();
				if(jQuerythis._t) clearTimeout(jQuerythis._t);
				if (jQuerythis.autoRotation) jQuerythis.runTimer(jQuerythis);
				return false;
			});
		},
		disableControls: function(){
			this.prevBtn.removeClass(this.options.disableBtn);
			this.nextBtn.removeClass(this.options.disableBtn);
			if (this.active>=this.max) this.nextBtn.addClass(this.options.disableBtn);
			if (this.active<=0) this.prevBtn.addClass(this.options.disableBtn);
		},
		initMove: function(){
			var f = false;
			if (this.active >= this.max && !this.options.clone) {
				f = true;
				this.active = this.max;
			}
			if(this._t) clearTimeout(this._t);
			if (!this.options.effect) this.scrollElement(f);
			else this.fadeElement();
			if (this.autoRotation) this.runTimer(this);
			this.index = this.switcher.filter('.active').index();
			if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
		},
		nextStep:function(){
			var f = false;
			this.active = this.active + this.options.slideElement;
			if(this.index < this.count-1) this.index += this.options.slideElement;
			else if(this.options.circle) this.index = 0;
			if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
			if (this.options.clone){
				if (this.active > this.count*2) {
					if (this.direction) this.list.css({marginTop:-this.mas[this.count]});
					else this.list.css({marginLeft:-this.mas[this.count]});
					this.active = this.count+this.options.slideElement;
				}
			} else {
				if (this.active >= this.max) {
					if (this.options.circle) {
						if (this.active > this.max) this.active = 0;
						else {
							this.active = this.max;
							f = true
						}
					}
					else {
						this.active = this.max;
						f = true;
					}
				}
			}
			if (!this.options.effect) this.scrollElement(f);
			else this.fadeElement();
		},
		prevStep: function(){
			var f = false;
			this.active = this.active - this.options.slideElement;
			if(this.index > 0) this.index -= this.options.slideElement;
			else if(this.options.circle) this.index = this.count-1;
			if (this.options.disableBtn && !this.options.circle && !this.options.clone) this.disableControls();
			if (this.options.clone){
				if (this.active < 0) {
					if (this.direction) this.list.css({marginTop:-this.mas[this.count]});
					else this.list.css({marginLeft:-this.mas[this.count]});
					this.active = this.count-1;
				}
			} else {
				if (this.active < 0) {
					if (this.options.circle) {
						this.active = this.max;
						f = true;
					}
					else this.active = 0;
				}
			}
			if (!this.options.effect) this.scrollElement(f);
			else this.fadeElement();
		},
		initWindow: function(jQuerythis,jQuerywindow){
			jQuery(window).focus(jQuery.proxy(this.play,this));
			jQuery(window).blur(jQuery.proxy(this.stop,this));
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));
