// Globale Parameter definieren
var globalVars=new Array();

$(document).ready(function() {
	
	// HTML: Seitenleiste: Hintergrund-Container
	if($('body').height()>1200) sidebarBackgroundContainerClass='big';
	else sidebarBackgroundContainerClass='small';
	$('div.sidebar').append('<div class="bottom_background '+sidebarBackgroundContainerClass+'"></div>');
	
	
	// Lightbox Initialisierung
	if(lang=='de') { var txtImageString='Bild'; var txtOfString='von'; }
	else { txtImageString='Image'; txtOfString='of'; }
	$('a[rel*=lightbox]').lightBox({
		fixedNavigation: true,
		overlayBgColor: '#000',
		overlayOpacity: 0.8,
		imageLoading: '/assets/js/jquery-lightbox/images/loading.gif',
		imageBtnClose: '/assets/js/jquery-lightbox/images/closelabel.gif',
		imageBtnPrev: '/assets/js/jquery-lightbox/images/prevlabel.gif',
		imageBtnNext: '/assets/js/jquery-lightbox/images/nextlabel.gif',
		containerResizeSpeed: 350,
		txtImage: txtImageString,
		txtOf: txtOfString,
		currentLocation: currentLocation
	});
	
	
	//
	// Bilderwechsel-Parameter prüfen und Bilderwechsel starten: headImages
	//
		
	if(typeof(headImages)!='undefined') startImageChange('6000');
	
	
	//
	// E-Mails entschlüsseln
	//
	
	// Expression erweitern
	$.extend($.expr[':'], {
		mailToLink: function (obj) {
			return obj.href.match(/^mailto\:/);
		}
	});
	
	// Mailto-Links anpassen
	$('a:mailToLink').each(function() {
		this.href=this.href.split("?")[0].replace("ANTI[at]SPAMBOT", "@");
		$(this).html($(this).html().replace("TOBMAPS]ta[ITNA", "@"));
	});
	
	
	//
	// SmoothScroll
	//
	
	$('a[href*=#]').click(function() {

		// skip SmoothScroll on links inside sliders or scroll boxes also using anchors or if there is a javascript call
		if($(this).parent().attr('class')=='scrollable_navigation' || $(this).attr('href').indexOf('javascript')>-1) return;

		// duration in ms
		var duration=1000;

		// easing values: swing | linear
		var easing='swing';

		// get / set parameters
		var newHash=this.hash;
		var oldLocation=window.location.href.replace(window.location.hash, '');
		var newLocation=this;
		
		// make sure it's the same location		
		if(oldLocation+newHash==newLocation)
		{
			// get target
			var target=$(this.hash+', a[name='+this.hash.slice(1)+']').offset().top;

			// adjust target for anchors near the bottom of the page
			if(target > $(document).height()-$(window).height()) target=$(document).height()-$(window).height();			
			
			// set selector
			if($.browser.safari) var animationSelector='body:not(:animated)';
			else var animationSelector='html:not(:animated)';
			
			// animate to target and set the hash to the window.location after the animation
			$(animationSelector).animate({ scrollTop: target }, duration, easing, function() {

				// add new hash to the browser location
				window.location.href=newLocation;
			});

			// cancel default click action
			return false;
		}
	});

});

//
// Funktionen Bildwechsel
//

// Funktion zum Start des Bildwechsels
function startImageChange(intervalTime)
{
	// Parameter bestimmen
	var container='div.head h2';
	var type='head';

	// Anzahl der Bilder feststellen
	var headImagesTotal=0;
	$.each(headImages, function() { headImagesTotal++; });

	// Bildwechselfunktion mit Intervall aufrufen
	setInterval('imageChange("'+type+'", "'+container+'", "'+headImagesTotal+'")', intervalTime);
}

// Funktion für den Bildwechsel
function imageChange(type, container, imagesTotal)
{
	// Parameter bestimmen
	if(type=='head')
	{
		var imagesArray=headImages;
		var imagePath=headImagePath;
	}
	
	// Neues Bild bestimmen
	var imageID=globalVars['imageChange'];		
	while(imageID==globalVars['imageChange'])
	{
		var imageRand=rnd(imagesTotal);
		imageID='gallery_image_'+imageRand;
	}

	// Neues Bild einfügen
	var newImage='<img src="'+imagePath+imagesArray[imageRand-1]+'" alt="" class="fade" id="'+imageID+'">';
	$(container).append(newImage);
	
	// Neues Bild einblenden	
	$('#'+imageID).load(function () {
		$('#'+imageID).fadeIn(2000, function() {
			
			// Altes Bild löschen und neues Bild merken
			$('#'+globalVars['imageChange']).remove();
			globalVars['imageChange']=imageID;				
		});
	});
}

// Funktion um einen Zufallswert zu ermitteln
function rnd(max)
{
	var rndNum = max * Math.random();
	rndNum = Math.ceil (rndNum);
	return rndNum;
}
