$(function(){
	$('div.gallery').galleryV({
		galleryList:'div.slider div.holder ul a',
		slideEl: 'div.image > ul.g1-list > li',
		loadIMG:'.load',
		btNext: 'a.link-next',
		btPrev:'a.link-prev',
		play: false,
		holderEl:'div.slider div.holder',
		moveEl:'ul',
		simpleEl:'li',
		slideTime : 7000,
		duration: 400,
		switcherOther:'ul.swicher > li'
	});
	$('div.slider').gallery({
		listOfSlides:'div.slide > ul > li span',
		switcher:'div.heading > ul.swicher > li',
		autoRotation:5000
	});
});

jQuery.fn.galleryV = function(_options){
	var _options = jQuery.extend({
		galleryList: 'ul li a span',
		slideEl: 'div.img-hold img',
		btNext: 'a.next-photo',
		btPrev:'a.prev-photo',
		play:'a.play',
		loadIMG:'div.img-hold img.load',
		holderEl:'div',
		moveEl:'ul',
		simpleEl:'li',
		duration: 400,
		slideTime : 4000,
		autoscroll: 700 ,
		switcherOther:false
	},_options);
	
	return this.each(function(){
		var _THIS = jQuery(this);
		var _galleryList = jQuery(_options.galleryList, _THIS);
		var _btNext = jQuery(_options.btNext, _THIS);
		var _btPrev = jQuery(_options.btPrev, _THIS);
		var _btFirst = _options.btFirst ? jQuery(_options.btFirst, _THIS) : false;
		var _btLast = _options.btLast ? jQuery(_options.btLast, _THIS) : false;
		var _btPlay = _options.play ? jQuery(_options.play, _THIS) : false;
		var _slideEl = jQuery(_options.slideEl, _THIS);
		var _loadIMG = jQuery(_options.loadIMG, _THIS);
		var _duration = _options.duration;
		var _img = new Image();
		var _holderEl = jQuery(_options.holderEl, _THIS);
		var _moveEl = jQuery(_options.moveEl, _holderEl);
		var _simpleEl = jQuery(_options.simpleEl, _moveEl);
		var _holderWidth = _holderEl.outerHeight();
		var _simpleElWidth = _simpleEl.outerHeight(true);
		var _moveElwidth = _simpleElWidth*_simpleEl.length;
		var _margin = 0, _current, _next = 0;
		if(switcherOther) var othSwitch = jQuery(_options.switcherOther, _THIS);;
		
		var _step = _simpleElWidth;
		
		if (!_slideEl.filter('.active').length) {
			_slideEl.eq(0).addClass('active');
			_current = 0;
		} else {
			_current = _slideEl.index(_slideEl.filter('.active'));
			_next = _current;
		}
		_slideEl.not(".active").hide();
		
		_galleryList.eq(0).parent().addClass('active');
		// click gallery
		_galleryList.click(function(){
			if (!jQuery(this).parent().is('.active')) {
				_current = _galleryList.index(this);
				showImg();
				addActiveClass();
				if(switcherOther) switcherOther();
			}
			//return false;
		});
		function switcherOther(){
			othSwitch.removeClass('active').eq(_current).addClass('active');
		}
		function showImg(href) {
			if (_current != _next) {
				_slideEl.removeClass('active');
				_hide = _slideEl.filter(':visible');
				_slideEl.eq(_current).fadeIn(_duration, function(){
					_hide.hide().removeClass('hide');
				}).addClass('active');
				_next = _current;
			}
		}
		var _timerPlay = false;
		if (_btPlay) {
			_btPlay.click(function(){
				if (jQuery(this).is('.pause')) {
					jQuery(this).removeClass('pause');
					if (_timerPlay) clearTimeout(_timerPlay);
				} else {
					jQuery(this).addClass('pause');
					_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
				}
				return false;
			})
		}
		_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
		// Next/Prev
		if (_btNext) {
			_btNext.click(function(){
				if (!_slideEl.is(':animated'))
					nextSlide();
				return false;	
			});
		}
		othSwitch.click(function(){
			var ind = othSwitch.index($(this));
			//_galleryList.eq(ind).trigger('click');
			
			if (!_galleryList.eq(ind).parents('li').is('.active')) {
				_current = ind;
				showImg();
				addActiveClass();
				if(switcherOther) switcherOther();
			}
			
			othSwitch.removeClass('active').eq(_current).addClass('active');
			return false;
		});

		function nextSlide(){
			_current++;
			if (_current > _galleryList.length-1) 
				_current = 0;
			showImg();
			moveNext();
			addActiveClass();
			if(switcherOther) switcherOther();
		}
		if (_btPrev) {
			_btPrev.click(function(){
				if (!_slideEl.is(':animated')) {
					_current--;
					if (_current < 0) 
						_current = _galleryList.length-1;
					
					showImg();
					movePrev();
					addActiveClass();
				}
				return false;
			});
		}
		function addActiveClass() {
			_galleryList.parents('li').removeClass('active');
			_galleryList.eq(_current).parents('li').addClass('active');
		}
				
		function moveNext() {
			if(_timerPlay) clearTimeout(_timerPlay);
			if (_margin+_step > _moveElwidth-_holderWidth) {
				_margin = _moveElwidth-_holderWidth;
			}
			else _margin += _step;
			if (_current == 0) {
				_margin = 0;
			}
			_moveEl.animate({marginTop:-_margin},{queue:false,duration:_duration,complete: function(){
				_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
			}});
		}
		function movePrev(){
			if(_timerPlay) clearTimeout(_timerPlay);
			if (_margin-_step < 0) {
				_margin = 0;
			}
			else _margin -= _step;
			if (_current == _galleryList.length - 1) _margin = _moveElwidth-_holderWidth;
			_moveEl.animate({marginTop:-_margin},{queue:false,duration:_duration,complete:function(){
				_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
			}});
			if(switcherOther) switcherOther();
		}
	});
}

$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
function Gallery(context, options) { this.init(context, options); };
Gallery.prototype = {
	options:{},
	init: function (context, options){
		this.options = $.extend({
			infinite: false,								//true = infinite gallery
			duration: 700,									//duration of effect it 1000 = 1sec
			slideElement: 1,								//number of elements for a slide
			autoRotation: false,							//false = option is disabled; 1000 = 1sec
			effect: false,									//false = slide; true = fade
			listOfSlides: 'ul > li',						//elements galleries
			switcher: false,								//false = option is disabled; 'ul > li' = elements switcher
			disableBtn: false,								//false = option is disabled; 'hidden' = class adds an buttons "prev" and "next"
			nextBtn: 'a.link-next, a.btn-next, a.next',		//button "next"
			prevBtn: 'a.link-prev, a.btn-prev, a.prev',		//button "prev"
			circle: true,									//true = cyclic gallery; false = not cyclic gallery
			direction: false,								//false = horizontal; true = vertical
			event: 'click',									//event for the buttons and switcher
			IE: false,										//forced off effect it "fade" in IE
			autoHeight: false								//auto height on fade
		}, options || {});
		var _el = $(context).find(this.options.listOfSlides);
		if (this.options.effect) this.list = _el;
		else this.list = _el.parent();
		this.switcher = $(context).find(this.options.switcher);
		this.nextBtn = $(context).find(this.options.nextBtn);
		this.prevBtn = $(context).find(this.options.prevBtn);
		this.count = _el.index(_el.filter(':last'));
		
		if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
		else this.active = _el.index(_el.filter('.active:eq(0)'));
		if (this.active < 0) this.active = 0;
		this.last = this.active;
		
		this.woh = _el.outerWidth(true);
		if (!this.options.direction) this.installDirections(this.list.parent().width());
		else {
			this.woh = _el.outerHeight(true);
			this.installDirections(this.list.parent().height());
		}
		
		if (!this.options.effect) {
			this.rew = this.count - this.wrapHolderW + 1;
			if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
			else this.anim = '{marginTop: -(this.woh * this.active)}';
			eval('this.list.css('+this.anim+')');
		}
		else {
			this.rew = this.count;
			this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
			this.switcher.removeClass('active').eq(this.active).addClass('active');
			if(this.options.autoHeight) this.list.parent().css({height: this.list.eq(this.active).outerHeight()});
		}
		this.flag = true;
		if (this.options.infinite){
			this.count++;
			this.active += this.count;
			this.list.append(_el.clone());
			this.list.append(_el.clone());
			eval('this.list.css('+this.anim+')');
		}
		
		this.initEvent(this, this.nextBtn, true);
		this.initEvent(this, this.prevBtn, false);
		if (this.options.disableBtn) this.initDisableBtn();
		if (this.options.autoRotation) this.runTimer(this);
		if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
	},
	initDisableBtn: function(){
		this.prevBtn.removeClass('prev-'+this.options.disableBtn);
		this.nextBtn.removeClass('next-'+this.options.disableBtn);
		if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);
		if (this.active == 0 && this.count == 1 || this.count+1 <= this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);
		if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);
	},
	installDirections: function(temp){
		this.wrapHolderW = Math.ceil(temp / this.woh);
		if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;
	},
	fadeElement: function(){
		if ($.browser.msie && this.options.IE){
			this.list.eq(this.last).css({opacity:0});
			this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
		}
		else{
			this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
			this.list.removeClass('active').eq(this.active).addClass('active').animate({
				opacity:1
			}, {queue:false, duration: this.options.duration, complete: function(){
				$(this).css('opacity','auto');
			}});
		}
		if(this.options.autoHeight) this.list.parent().animate({height: this.list.eq(this.active).outerHeight()}, {queue:false, duration: this.options.duration});
		if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		this.last = this.active;
	},
	scrollElement: function($this){
		if (!$this.options.infinite) eval('$this.list.animate('+$this.anim+', {queue:false, duration: $this.options.duration});');
		else eval('$this.list.animate('+$this.anim+', $this.options.duration, function(){ $this.flag = true });');
		if ($this.options.switcher) $this.switcher.removeClass('active').eq($this.active / $this.options.slideElement).addClass('active');
	},
	runTimer: function($this){
		if($this._t) clearTimeout($this._t);
		$this._t = setInterval(function(){
			if ($this.options.infinite) $this.flag = false;
			$this.toPrepare($this, true);
		}, this.options.autoRotation);
	},
	initEventSwitcher: function($this, el){
		el.bind($this.options.event, function(){
			$this.active = $this.switcher.index($(this)) * $this.options.slideElement;
			if($this._t) clearTimeout($this._t);
			if ($this.options.disableBtn) $this.initDisableBtn();
			if (!$this.options.effect) $this.scrollElement($this);
			else $this.fadeElement();
			if ($this.options.autoRotation) $this.runTimer($this);
			//return false;
		});
	},
	initEvent: function($this, addEventEl, dir){
		addEventEl.bind($this.options.event, function(){
			if ($this.flag){
				if ($this.options.infinite) $this.flag = false;
				if($this._t) clearTimeout($this._t);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation) $this.runTimer($this);
			}
			return false;
		});
	},
	toPrepare: function($this, side){
		if (!$this.options.infinite){
			if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
			if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
			for (var i = 0; i < $this.options.slideElement; i++){
				if (side) { if ($this.active + 1 <= $this.rew) $this.active++; }
				else { if ($this.active - 1 >= 0) $this.active--; }
			};
		}
		else{
			if ($this.active >= $this.count + $this.count && side) $this.active -= $this.count;
			if ($this.active <= $this.count-1 && !side) $this.active += $this.count;
			eval('$this.list.css('+$this.anim+')');
			if (side) $this.active += $this.options.slideElement;
			else $this.active -= $this.options.slideElement;
		}
		if (this.options.disableBtn) this.initDisableBtn();
		if (!$this.options.effect) $this.scrollElement($this);
		else $this.fadeElement();
	},
	stop: function(){
		if (this._t) clearTimeout(this._t);
	},
	play: function(){
		if (this._t) clearTimeout(this._t);
		if (this.options.autoRotation) this.runTimer(this);
	}
}
