jQuery.noConflict;
jQuery(function(){
	initGall();
	jQuery('div.ad-list').crawlLine({
		speed:4,
		crawElement:'ul',
		textElement:'li'
	});
});

jQuery.fn.crawlLine = function(_options){
 // defaults options
	var _options = jQuery.extend({
	speed:2,
	crawElement:'div',
	textElement:'p',
	hoverClass:'viewText'
},_options);

return this.each(function(){
	var _THIS = jQuery(this);
	var _el = jQuery(_options.crawElement, _THIS).css('position','relative');
	var _text = jQuery(_options.textElement, _THIS);
	var _clone = _text.css('whiteSpace','nowrap').clone();
	var _elWidth = 0;
	var _k = 1;

 // set parametrs *******************************************************
	var _textWidth = 0;
	_text.each(function(){
		_textWidth += jQuery(this).outerWidth(true);
	});
	var _duration = _textWidth*50 / _options.speed;
	_el.append(_clone);
	_el.css('width',_textWidth*2);

	var animate = function() {
		_el.animate({left:-_textWidth}, {queue:false, duration:_duration*_k, easing:'linear', complete:function(){
			_el.css('left','0');
			_k=1;
			animate();
		}});
	}
	animate();

	_THIS.hover(function() {
		_el.stop();
		_THIS.addClass(_options.hoverClass);
	}, function(){
		_THIS.removeClass(_options.hoverClass);
		_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
		animate();
	});
	_THIS.bind('wheel',function(event,delta){
		var _marginScroll;
		if (delta<0) {
			_marginScroll = parseInt(_el.css('left')) - 20;
			_el.animate({left:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
				_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
			}});
		} else {
			_marginScroll = parseInt(_el.css('left')) + 20;
			if (_marginScroll > 0) _marginScroll = 0;
				_el.animate({left:_marginScroll}, {queue:false, duration:100, easing:'linear', complete:function(){
					_k = (_textWidth + parseInt(_el.css('left')))/_textWidth;
			}});
		}
		return false;
	});
});
}
jQuery.fn.gallSlide = function(_options){
	// defaults options
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul');
		var _el = _hold.find('ul > li');
		var _count = _el.index(_el.filter(':last'));
		var _t;
		var _active = 0;

		var _next = _hold.find('a.btn-next');
		var _prev = _hold.find('a.btn-prev');
		var _pause = _hold.find('a.btn-pause');
		var _play = _hold.find('a.btn-play');

		var _addAfter = _el.clone();
		var _addBefore = _el.eq(_el.length-1).clone()
		_wrap.append(_addAfter);
		_wrap.prepend(_addBefore);
		_wrap.css('marginLeft', -(_addBefore.outerWidth()));

		function scrollEl(){
			var _ww = 0;
			for (var i=0; i<_active; i++) {
				_ww = _ww + _el.eq(i).outerWidth();
			};
			_wrap.eq(0).animate({marginLeft: -(_ww + _addBefore.outerWidth()) + "px"}, {queue:false, duration: _speed, complete:function(){
				if (_active == _count){
					_wrap.css('marginLeft', 0);
					_active = -1;
				}
			}});
		}

		function scrollElFirst(){
			var _ww = 0;
			for (var i=0; i<_count; i++) {
				_ww = _ww + _el.eq(i).outerWidth();
			};
			_wrap.eq(0).animate({marginLeft: 0}, {queue:false, duration: _speed, complete:function(){
				_wrap.css('marginLeft', -(_ww + _addBefore.outerWidth()));
				_active = _count;
			}});
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				scrollEl();
			}, _timer);
		}

		runTimer();

		_next.click(function(){
			_active++;
			if(_t) clearTimeout(_t);
			scrollEl();
			runTimer();
			_pause.show();
			_play.hide();
			return false;
		});

		_prev.click(function(){
			_active--;
			if (_active < 0)
				scrollElFirst();
			else {
				if(_t) clearTimeout(_t);
				scrollEl();
				runTimer();
				_pause.show();
				_play.hide();
			}
			return false;
		});

		_pause.click(function(){
			_wrap.eq(0).animate().stop();
			if(_t) clearTimeout(_t);
			_pause.hide();
			_play.show();
			return false;
		});
		_play.click(function(_active, _ww){
			scrollEl();
			runTimer();
			_pause.show();
			_play.hide();
			return false;
		});
	});
}
function initGall(){
	gal1 = jQuery('div.gallery-alt').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'div.gallery-holder > ul > li',
		switcher:'div.gallery-control ul.menu > li',
		event:'mouseenter',
		effect:'fade',
		autoHeight: true
	});
	gal2 = jQuery('div.gallery3').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'div.gallery-holder > ul > li',
		switcher:'div.gallery-control ul.menu > li',
		event:'mouseenter',
		effect:'fade'
	});
	gal3 = jQuery('div.gallery2').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'div.gallery-holder > ul > li',
		switcher:'div.gallery-control ul.menu > li',
		event:'mouseenter',
		effect:'fade'
	});
	gal4 = jQuery('div.g1').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'div.gallery-holder > ul > li',
		switcher:'div.gallery-control ul.menu > li',
		event:'mouseenter',
		effect:'fade'
	});
	gal5 = jQuery('div.list-drinks').gallery({
		duration: 500,
		autoRotation: 2000,
		direction: true,
		listOfSlides: 'div.holder > ul.vg1 > li'
	});
	gal6 = jQuery('div.regio').gallery({
		duration: 500,
		autoRotation: 5000,
		direction: true,
		listOfSlides: 'div.map-hold > ul > li'
	});
	gal7 = jQuery('div.photo-gallery').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'ul > li'
	});
	gal8 = jQuery('div.visual').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'ul.visual-g1 > li',
		effect:'fade'
	});
	gal9 = jQuery('div.visual').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'ul.visual-g2 > li',
		effect:'fade'
	});
	/*
	gal10 = jQuery('div.ad-list').gallery({
		duration: 700,
		autoRotation: 5000,
		listOfSlides: 'div.ad-list-holder ul > li'
	});
	*/
}
(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
			}, 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 || 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, 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, 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(jQuery(this)) * $this.options.slideElement;
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn) $this.initDisableBtn();
				if (!$this.options.effect) $this.scrollElement();
				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));
