// page init
$(function(){
	initCurrentDate();
	initSlideShow();
	initNavigation();
	initFontResize();
	hoverForIE6('#nav li');
	fixLayout();
	fixLayout2();
})
$(window).load(function(){
	fixLayout3();
})

// font resize event
$(function(){
	var _refresh = 200;
	var _lastSize;
	var _body = $('body');
	var _text = $('<div class="font-resize-event">a</div>');
	_text.css({
		fontSize: '100em',
		position:'absolute',
		display:'block',
		top:-9999,
		left:-9999
	});

	_body.append(_text);
	_lastSize = _text.height();

	setInterval(function(){
		if(_text.height() != _lastSize) {
			_lastSize = _text.height();
			_body.trigger('fontResized');
		}
	},_refresh)
})

// fix layout
function fixLayout() {
	var _header = $('#header');
	var _logo = $('#header .logo');

	function fix() {
		_logo.css('paddingTop','0');
		_logo.css('paddingTop',parseInt(_header.height()/2 - _logo.height()/2))-parseInt(_header.css('paddingBottom'));
	}
	fix();
	$(window).resize(fix)
}
// fix layout
function fixLayout2() {
	var _header = $('#header');
	var _logo = $('#header .info-box');

	function fix() {
		_logo.css('marginTop','0');
		_logo.css('marginTop',parseInt(_header.height()/2 - _logo.height()/2))-parseInt(_header.css('marginBottom'));
	}
	fix();
	$(window).resize(fix)
}

// fix layout
function fixLayout3() {
	var _bigImage = $('img.big-img')
	var _header = $('#header');
	var _logo = _header.find('.logo');
	var _nav = _header.find('#nav');
	var _content = $('#main > div.hold, .text-hold')
	var _footer = $('#footer')
	var _sidebar = $('.sidebar')
	var _footerList = _footer.find('ul.slide-list');
	var _footerListPrev = _footer.find('ul.list');

_sidebar.css({marginLeft:0})

	function fix() {
		// reset layout
		_bigImage.css({width:'auto',height:'auto'});
		_logo.css({marginLeft:0});
		_nav.css({marginLeft:0});
		_content.css({paddingLeft:0});
		_footerList.css({left:0});

		// stretch image
		_bigImage.css({
			height: _footerList.parent().offset().top-_header.height()
		})

		// move elements
		var _imageWidth = _bigImage.width();
		_logo.css({marginLeft:_imageWidth});
		_nav.css({marginLeft:_imageWidth-3});
		_content.css({paddingLeft:_imageWidth});
		_header.css({backgroundPosition:_imageWidth+'px 0'});
		_footerList.css({left:_imageWidth-9});

	}
	fix();
	$(window).resize(fix);
	$(window).load(fix);
	$('body').bind('fontResized',fix);
}

// hover for IE
function hoverForIE6(_list, _class) {
	var _hoverClass = 'hover';
	if(_class) _hoverClass = _class;
	if ($.browser.msie && $.browser.version < 7) {
		$(_list).hover(function() {
			$(this).addClass(_hoverClass);
		}, function() {
			$(this).removeClass(_hoverClass);
		});
	}
}

// current date show
function initCurrentDate() {
	$('span.current-date').each(function(){
		var _holder = $(this);
		var _date = new Date();
		_holder.text(
			(_date.getDate()<10 ? '0'+_date.getDate() : _date.getDate()) + '.' +
			(_date.getMonth()+1<10 ? '0'+(_date.getMonth()+1) : (_date.getMonth()+1)) + '.' +
			_date.getFullYear());
	})
}

// fade dropdown function
function initNavigation() {
	var _stayTime = 200;
	var _fadeSpeed = ($.browser.msie ? 0 : 250);
	var _hoverClass = 'hover';
	var _items = $('ul#nav li');
	_items.each(function(){
		var _item = $(this);
		var _drop = _item.find('>div').hide();
		var _timer;
		_item.hover(function(){
			if(_timer) clearTimeout(_timer);
			_item.siblings().removeClass(_hoverClass).find('>div').hide();
			_item.addClass(_hoverClass);
			_timer = setTimeout(function(){
				_drop.fadeIn(_fadeSpeed);
			},_stayTime);
		},function(){
			if(_timer) clearTimeout(_timer);
			if(_drop.is(':visible')) {
				_timer = setTimeout(function(){
					_drop.fadeOut(_fadeSpeed,function(){
						_item.removeClass(_hoverClass);
					});
				},_stayTime);
			} else {
				_item.removeClass(_hoverClass);
			}
		});
	})
}

// slideshow init
function initSlideShow() {
	$('div.slideshow').fadeGallery({
		slideElements:'ul.slide > li',
		pagerLinks:'ul.swicher a',
		pauseOnHover:true,
		autoRotation:true,
		autoHeight:true,
		switchTime:5000, // Info-Feld
		duration:500
	})
	$('ul.slideset').fadeGallery({
		slideElements:' > li',
		pauseOnHover:false,
		autoRotation:true,
		autoHeight:false,
		switchTime:7000, // Slogan Footer
		duration:500
	})
}

// font-resize
function initFontResize() {
	var textChanger = {
		cpanel : 'textchanger',  //set here the id of the element (div, p) within you want to insert the control panel
		element : 'wrapper',   	 //set here the id of the element (div, p) within you want to change the text
		defaultFS : 1,         //set here the default font size in 'em'
		init: function() {
			var cpel = document.getElementById(textChanger.cpanel);
			var el = document.getElementById(textChanger.element);
			if (cpel == null || el == null) {alert('The elements with the \"'+textChanger.cpanel+'\" and/or \"'+textChanger.element+'\" ID do not exist in HTML source.');} else {
			var u = document.createElement('ul');
			cpel.appendChild(u);
			u.innerHTML =
			'<li id="reset"><a href="#" title="Default font size">Schriftgr&#246;&#223;e</a></li>'+
			'<li id="decrease"><a href="#" title="Decrease font size">A</a></li>'+
			'<li id="increase"><a href="#" title="Increase font size">A</a></li>'
			var sz = textChanger.getCookie();
			el.style.fontSize = sz ? sz + 'em' : textChanger.defaultFS + 'em';
			var incr = document.getElementById('increase');
			incr.onclick = function(){textChanger.changeSize(1); $('body').trigger('fontResized'); return false;};
			var decr = document.getElementById('decrease');
			decr.onclick = function(){textChanger.changeSize(-1); $('body').trigger('fontResized'); return false;};
			var reset= document.getElementById('reset');
			reset.onclick = function(){textChanger.changeSize(0); $('body').trigger('fontResized'); return false;};
			}
		} ,

		changeSize: function(val) {
			var el = document.getElementById(textChanger.element);
			var size = el.style.fontSize.substring(0,3);
			var fSize = parseFloat(size,10);
			if (val == 1) {
				fSize += 0.11;
				if (fSize > 2.0) fSize = 2.0;
			}
			if (val == -1) {
				fSize -= 0.11;
				if (fSize < 0.5) fSize = 0.5;
			}
			if (val == 0) {
				fSize = textChanger.defaultFS;
			}
			el.style.fontSize = fSize + 'em';
			getHeight();
			textChanger.updateCookie(fSize);
			} ,

		updateCookie: function(vl) {
			var today = new Date();
			var exp = new Date(today.getTime() + (365*24*60*60*1000)); //the cookie will expire in one year
			document.cookie = 'textChangerL=size=' + vl + ';' +'expires=' + exp.toGMTString() + ';' +'path=/';
		} ,

		getCookie: function() {
			var cname = 'textChangerL=size=';
			var start = document.cookie.indexOf(cname);
			var len = start + cname.length;
			if ((!start) && (cname != document.cookie.substring(0,cname.length))) {return null;}
			if (start == -1) return null;
			var end = document.cookie.indexOf(";",len);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(len, end));
		}
	}
	textChanger.init();
}

function getHeight(){
	var _liH = $('ul.slide > li.active').outerHeight();
	$('ul.slide').css('height', _liH);
};


// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > div',
		pagerLinks:'div.pager a',
		btnNext:'a.next',
		btnPrev:'a.prev',
		btnPlayPause:'a.play-pause',
		btnPlay:'a.play',
		btnPause:'a.pause',
		pausedClass:'paused',
		disabledClass: 'disabled',
		playClass:'playing',
		activeClass:'active',
		currentNum:false,
		allNum:false,
		startSlide:null,
		noCircle:false,
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		onChange:false,
		switchTime:3000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _btnPause = jQuery(_options.btnPause, _this);
		var _btnPlay = jQuery(_options.btnPlay, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _disabledClass = _options.disabledClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _currentNum = (_options.currentNum ? jQuery(_options.currentNum, _this) : false);
		var _allNum = (_options.allNum ? jQuery(_options.allNum, _this) : false);
		var _startSlide = _options.startSlide;
		var _noCycle = _options.noCircle;
		var _onChange = _options.onChange;

		// gallery init
		var _hover = false;
		var _prevIndex = 0;
		var _currentIndex = 0;
		var _slideCount = _slides.length;
		var _timer;
		if(!_slideCount) return;

		_prevIndex = _slides.index(_slides.filter('.'+_activeClass));
		if(_prevIndex < 0) _prevIndex = _currentIndex = 0;
		else _currentIndex = _prevIndex;
		if(_startSlide != null) {
			if(_startSlide == 'random') _prevIndex = _currentIndex = Math.floor(Math.random()*_slideCount);
			else _prevIndex = _currentIndex = parseInt(_startSlide);
		}
		_slides.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// play pause section
		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					_autoRotation = false;
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}
		if(_btnPlay.length) {
			_btnPlay.bind(_controlEvent,function(){
				_this.removeClass(_pausedClass).addClass(_playClass);
				_autoRotation = true;
				autoSlide();
				return false;
			});
		}
		if(_btnPause.length) {
			_btnPause.bind(_controlEvent,function(){
				_autoRotation = false;
				if(_timer) clearTimeout(_timer);
				_this.removeClass(_playClass).addClass(_pausedClass);
				return false;
			});
		}

		// gallery animation
		function prevSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else {
				if(_noCycle) return;
				else _currentIndex = _slideCount-1;
			}
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else {
				if(_noCycle) return;
				else _currentIndex = 0;
			}
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			if(_currentNum) _currentNum.text(_currentIndex+1);
			if(_allNum) _allNum.text(_slideCount);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
			if(_noCycle) {
				if(_btnPrev.length) {
					if(_currentIndex == 0) _btnPrev.addClass(_disabledClass);
					else _btnPrev.removeClass(_disabledClass);
				}
				if(_btnNext.length) {
					if(_currentIndex == _slideCount-1) _btnNext.addClass(_disabledClass);
					else _btnNext.removeClass(_disabledClass);
				}
			}
			if(typeof _onChange === 'function') {
				_onChange(_this, _currentIndex);
			}
		}
		function switchSlide() {
			_slides.eq(_prevIndex).fadeOut(_duration);
			_slides.eq(_currentIndex).fadeIn(_duration);
			if(_autoHeight) _slides.eq(_currentIndex).parent().animate({height:_slides.eq(_currentIndex).outerHeight(true)},{duration:_duration,queue:false});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}