$(function(){
	$('html').addClass('screen');
	initGall();
	initFadeHover();
	scrollPageToTop();
	jQuery('ul.accordion').acc({
		speed: 400,
		active: 'active',
		list: '.children()',
		opener: 'a.opener',
		slide: '.slide'
		
	});
});
function initFadeHover(){
	$('strong.bg-nav').each(function(){
		if($(this).parents('li').hasClass('active')) $(this).css({'opacity': 0.4});
		else $(this).css({'opacity': 0, 'display':'none'});
	});
	$('span.bg').each(function(){
		if($(this).parents('li').hasClass('active')) $(this).css({'opacity': 0.4});
		else $(this).css({'opacity': 0});
	});

	$('div.boxes-holder li, ul.items-list > li, #nav > li').each(function(){
		$(this).mouseenter(function(){
			if(!$(this).hasClass('active')){
				$(this).find('span.bg').animate({'opacity': 0.4}, 200);
				$(this).find('strong.bg-nav').show().animate({'opacity': 1}, 300);
				$(this).find('span.bg-evenemang').fadeIn(400);
			}
		}).mouseleave(function(){
			if(!$(this).hasClass('active')){
				$(this).find('span.bg').animate({'opacity': 0}, 200);
				$(this).find('strong.bg-nav').animate({'opacity': 0}, {duration:300, complete: function(){$(this).hide()}});
				$(this).find('span.bg-evenemang').fadeOut(400);
			}
		});
	});
}
function initGall(){
	$('div.gallery').gallery({
		autoRotation:5000,
		effect:'fade',
		listOfSlides:'ul.carusel > li',
		switcher:'div.swicher ul > li'
	});
}
function scrollPageToTop(){
	$('a.topscroll').click(function(){
		if ($.browser.safari) {
			$('body').animate({
				'scrollTop':0
			}, 750)
		}
		else $('html').animate({
			'scrollTop':0
		}, 750)
		return false;
	})
}
(function($) {
	$.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);
		}
	}
	jQuery.fn.acc = function(_options){
		var _options = jQuery.extend({
			speed: 400,
			active: 'active',
			list: '.children()',
			opener: 'a.opener',
			slide: 'div.slide'
		}, _options);
		
		return this.each(function(){
			var _list = eval('jQuery(this)' + _options.list);
			var _active = _options.active;
			var _speed = _options.speed;
			var _a = _list.index(_list.filter('.' + _active + ':eq(0)'));
			if(_a != -1) _list.removeClass(_active).eq(_a).addClass(_active);
			for(var i = 0; i < _list.length; i++){
				_list.eq(i).data('btn', _list.eq(i).children(_options.opener).eq(0));
				_list.eq(i).data('box', _list.eq(i).children(_options.slide).eq(0));
				if(i == _a) _list.eq(i).data('box').css('display', 'block');
				else _list.eq(i).data('box').css('display', 'none');
				_list.eq(i).data('btn').data('ind', i);
				_list.eq(i).data('btn').click(function(){
					if (_list.eq(jQuery(this).data('ind')).data('box').length != 0) {
						changeEl(jQuery(this).data('ind'));
						return false;
					}
				});
			}
			var anim_f = true;
			var a_h, ind_h, _k;
			function changeEl(_ind){
				if(anim_f){
					anim_f = false;
					if(_a == _ind){
						_list.eq(_a).removeClass(_active).data('box').animate({height: 0}, {
							duration: _speed,
							complete: function(){
								jQuery(this).css({display:'none', height:'auto'});
								_a = -1;
								anim_f = true;
							}
						});
					}
					else{
						_list.eq(_ind).data('box').css('display', 'block');
						ind_h = _list.eq(_ind).data('box').outerHeight();
						_list.eq(_ind).data('box').height(0);
						if(_a != -1){
							a_h = _list.eq(_a).removeClass(_active).data('box').outerHeight();
							_k = a_h/ind_h;
						}
						_list.eq(_ind).addClass(_active).data('box').animate({height: ind_h}, {
							duration: _speed,
							step: function(t_h){
								if(_a != -1) _list.eq(_a).data('box').height(a_h - t_h*_k);
							},
							complete: function(){
								_list.eq(_ind).data('box').height('auto');
								if(_a != -1) _list.eq(_a).data('box').css({display:'none', height: 'auto'});
								_a = _ind;
								anim_f = true;
							}
						});
					}
				}
			}
		});
	}
}(jQuery));
