/*********************************************************
* Variablen
*********************************************************/

// var xyz = 'xyz';


/*********************************************************
* Scrolllinks
*********************************************************/

function scrollinterna() {
	$('a[href*=#]').click(function() {
		if( location.pathname.replace( /^\//,'' ) == this.pathname.replace( /^\//,'' ) && location.hostname == this.hostname && $(this).attr('href').replace( /#/,'' ) != '' )
		{
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length)
			{
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 800, 'swing');
				return false;
			}
		}
	});
};


/*********************************************************
* eMail-Verschlüsselung
*********************************************************/

function maildecoding() {
	$('.email').each(function(){
		var mailArr = $(this).html();
		mailArr = mailArr.split(' ');
		var mailTxt = mailArr[0] + "@" + mailArr[2] + "." + mailArr[4];
		var mailOut = "<a href=\"mailto:" + mailTxt + "\" title=\"" + mailTxt + "\">" + mailTxt + "</a>";
		$(this).html(mailOut);
	});
}


/*********************************************************
* Galerie-Plugin
*********************************************************/

(function($) {
	
	var options, curPos = parseInt('0'), parentHeight = parseInt('0');
	
	$.fn.gallery = function( parameter )
	{
		parameter = $.extend({}, $.fn.gallery.defaults, parameter); // Parameter um Standardwerte erweitern, die nicht angegeben wurden
		options = parameter;
		
		return this.each(function() { // Objektliste durchlaufen
			_start($(this));
		});
		
		function _start( el ) { // Funktionsaufruf
			_preparation( el );
			options.el = el;
			if( options.autoAnimation && $(el).children().size() > 1 ) {
				$(document).ready(function() {
					_autoAnimate();
				});
			}
		}
		
		function _autoAnimate() { // Automatisierte Animationen
			_autoNext( options.el );
		}
		
		function _autoNext() {
			$.fn.gallery.changeItem('+', options.stopTime, _autoNext)
		}
		
		function _preparation( el ) { // Vorbereitungen
			$.fn.gallery.getItems( el ); // Item-Array aufbauen + weitere Informationen über Galerie sammeln
			
			$(el).children().css({
				position: 'absolute', 'display': 'none'}); // Alle Galerie-Elemente absolut positionieren und ausblenden
			$(el).children(':first-child').show(); // Erstes Galerie-Element einblenden
			
			if( $.browser.msie && $.browser.version == '7.0' ) { // maximale Höhe
				$(el).height(parentHeight - 2);
			}
			else {
				$(el).height(parentHeight);
			}
			
			_navigation( el );
		}
		
		function _navigation( el ) { // Navigationselemente initialisieren
			_navigationPrev( el ); 
			_navigationNext( el );
		}
		
		function _navigationPrev( el ) { // Navigationselement zurück
			$(options.controller + ' ' + options.controllerPrev).click(function() {
				$.fn.gallery.changeItem('-')
				return false;
			});
		}
		
		function _navigationNext( el ) { // Navigationselement vor
			$(options.controller + ' ' + options.controllerNext).click(function() {
				$.fn.gallery.changeItem('+')
				return false;
			});
		}
	}
	
	$.fn.gallery.getItems = function( el ) { // Item-Array erstellen + weitere Informationen
		if( !options.items.length ) {
			var curChildren = $(el).children()
			curChildren.show();
			curChildren.each(function(i) {
				if( $(this).height() > parentHeight ) {
					parentHeight = $(this).height();
				}
				options.items.push($(this));
			});
			curChildren.hide();
		}
	}
	
	$.fn.gallery.getChangeItem = function( newPos ) { // Itemnummer ändern
		var newCurPos = parseInt('0');
		
		switch( newPos ) {
			case '+':
				if( curPos + 1 > (options.items.length - 1) ) {
					newCurPos = 0;
				}
				else {
					newCurPos = curPos + 1;
				}
				break;
			case '-':
				if( curPos - 1 < 0 ) {
					newCurPos = options.items.length - 1;
				}
				else {
					newCurPos = curPos - 1;
				}
				break;
			default:
				var tempCurPos = curPos + parseInt(newPos);
				if( tempCurPos > (options.items.length - 1) ) {
					newCurPos = tempCurPos - options.items.length;
				}
				else if( tempCurPos < 0 ) {
					newCurPos = options.items.length - tempCurPos;
				}
				else {
					newCurPos = tempCurPos;
				}
		}
		
		return newCurPos;
	}
	
	$.fn.gallery.changeItem = function( newPos, delay, callBack ) { // Änderung animiert ausgeben
		var curItem = options.items[curPos]; // aktuelles Item
		delay = ( !delay ) ? 0 : delay;
		curPos = $.fn.gallery.getChangeItem(newPos); // neue Position bestimmen
		var newItem = options.items[curPos]; // neues Item
		
		switch(options.transition) { // für Transparenz-Übergang
			case 'transparency':
				$(curItem).css({
					zIndex: '2'
				});
				$(newItem).css({
					zIndex: '1'
				}).show();
				$(curItem).delay(delay).fadeOut(options.transitionTime, callBack);
				break;
		}
	}
	
	$.fn.gallery.defaults = { // Standardwerte festlegen
		el: false,
		autoAnimation: false,
		transition: 'transparency',
		stopTime: 5000,
		transitionTime: 100,
		controller: '.gallery-controller',
		controllerPrev: '.left',
		controllerNext: '.right',
		items: []
	};
})(jQuery);


/*********************************************************
* Galerie-Plugin ausführen
*********************************************************/

function galleryStart() {
	$('#teaser ul').gallery({
		autoAnimation: true,
		stopTime: 5000,
		transitionTime: 2000
	});
}


/*********************************************************
* Document-Ready
*********************************************************/

$(document).ready(function() { // Wenn DOM geladen, dann ...
	scrollinterna(); // Scrollinks
	maildecoding(); // eMail-Verschlüsselung
	galleryStart(); // Galerie-Plugin ausführen
});
