jQuery(document).ready(function(){
	initHeight();
	initGallery();
});

function initHeight(){
	var _hardcode = 0;
	var _hold = jQuery('div#wrapper');
	var _holdH = _hold.height(); 
	if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		}
	else {
		_height = document.documentElement.clientHeight;
		_width = document.documentElement.clientWidth;
	}
	if(_holdH + _hardcode < _height + 5){
		_hold.css('height', (_height - _hardcode));
	}
	
	jQuery(window).resize(function(){
		if (window.innerHeight) {
				_height = window.innerHeight;
				_width = window.innerWidth;
			}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		if(_holdH + _hardcode < _height + 5){
			_hold.css('height', (_height - _hardcode));
		}
	});
};

// gallery init
function initGallery() {
	// settings
	var _waitAnimation = false;
	var _autoSlide = true;
	var _easing = 'linear';
	var _activeClass = 'active';
	var _switchTime = 10000;
	var _speed = 700;

	jQuery('div.gallery-holder').each(function(){
		// gallery options
		var _holder = jQuery(this);
		var _pagerLinks = _holder.find('div.switcher-holder > ul > li');
		var _slidesHolder = _holder.find('div.g1');
		var _slider = _slidesHolder.find('> ul.gallery');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = _slides.eq(0).outerWidth(true);
		var _visibleCount = Math.round(_slidesHolder.width() / _slideWidth);
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _animating = false;
		var _direction;
		var _timer;

		// slider height
		_holder.css({position:'relative'});
		_slider.css({height:_slides.eq(0).outerHeight(true)});
		_slides.show().css({position:'absolute',top:0,left:_slideWidth});
		_slides.eq(_currentIndex).css({left:0});

		// gallery control
		_pagerLinks.each(function(_ind){
			jQuery(this).click(function(){
				if(_animating) return;
				if(_ind != _currentIndex) {
					_oldIndex = _currentIndex;
					_currentIndex = _ind;
					_direction = (_oldIndex < _currentIndex);
					switchSlide();
				}
				return false;
			});
		});
		function prevSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slidesCount-1;
			_direction = false;
			switchSlide();
		}
		function nextSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex < _slidesCount-1) _currentIndex++;
			else _currentIndex = 0;
			_direction = true;
			switchSlide();
		}

		// gallery animation
		function refreshStatus() {
			_pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
		}
		function switchSlide() {
			if(_waitAnimation) _animating = true;
			_slides.eq(_currentIndex).css({left:(_direction ? _slideWidth : -_slideWidth)}).animate({left:0},{duration:_speed, queue:false,easing:_easing,complete:function(){
				_animating = false;
			}});
			_slides.eq(_oldIndex).animate({left:(_direction ? -_slideWidth : _slideWidth)},{duration:_speed, queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		refreshStatus();
		autoSlide();
	});
}

(function($) {
	jQuery.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 = jQuery.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
				easing: 'swing'
			}, options || {});
			var _el = jQuery(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			this.switcher = jQuery(context).find(this.options.switcher);
			this.nextBtn = jQuery(context).find(this.options.nextBtn);
			this.prevBtn = jQuery(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 == 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 (jQuery.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, easing: this.options.easing, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					jQuery(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, easing: "'+$this.options.easing+'", duration: $this.options.duration});');
			else eval('$this.list.animate('+$this.anim+', $this.options.duration, "'+$this.options.easing+'", 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(jQuery(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);
		}
	}
}(jQuery));

