var animationFlag = true;
var anzahlTeaser;
var anzahlComingTeaser;
var sizeProElement = 104;
var numberOfElements = 4;

        window.onload = function () {
			 var tabs = getElementsByClass('teaser_box_slider');
			 anzahlTeaser = tabs.length;
			 anzahlComingTeaser = tabs.length;
        };
function showInline(sElementID) {
	var theElement = document.getElementById(sElementID);
	if (theElement) {
		theElement.style.display ='inline';
	}
}

function hideInline(sElementID) {
	var theElement = document.getElementById(sElementID);
	if (theElement) {
		theElement.style.display ='none';
	}
}

function activateTab(sElementID) {
	var theElement = document.getElementById(sElementID);
	if (theElement) {
		showInline(sElementID);
		theElement.style.backgroundColor = 'transparent';

	}
}

function deactivateTab(sElementID) {
	var theElement = document.getElementById(sElementID);
	if (theElement) {
		hideInline(sElementID);
	}
}

function changeFontColor(sElementID, color){
	var theElement = document.getElementById(sElementID);
	if (theElement) {
		theElement.style.color = color;
	}
}

function toggleTab(sTabID){
	aTabs = new Array('fuss_punkt1','fuss_punkt2', 'fuss_punkt3', 'fuss_punkt4', 'fuss_punkt5', 'fuss_punkt6', 'fuss_punkt7');

	for (var i = 0; i < aTabs.length; ++i){

		if(aTabs[i] == sTabID){
			activateTab(sTabID+'_inhalt');
			changeFontColor(sTabID, '#ff660a');
		}
		else {
			deactivateTab(aTabs[i]+'_inhalt');
			changeFontColor(aTabs[i], '#000000');
		}
	}

}



function toggleTabBilder(sTabID){
	aTabs = new Array('1','2');

	for (var i = 0; i < aTabs.length; ++i){

		if(aTabs[i] == sTabID){
			activateTab('topthema_'+ sTabID);
		}
		else {
			deactivateTab('topthema_'+ aTabs[i]);
		}
	}

}
function RotateImages(Start)
  {
      var max=2;
      if(Start>=max){
          Start=0;
      }
      var id = Start+1;
      toggleTabBilder(id);
      window.setTimeout("RotateImages(" + (Start+1) + ")",8000);
  }

 function animation(xpos, pace, start)
{
	var container = $('div.sliderGallery');
	var div = $('div.teaser_box_slider', container);
	if(start){
		div.css('left', xpos * -2.33);
	}else
	if(xpos <= 940)
	{
		div.animate({'left' : xpos * -2.33}, pace);
		div.css('left', xpos * -2.33);
	}
	if(xpos == 0){
		var theElement = document.getElementById('slidernavi1');
					if (theElement) {
						theElement.className="aktiv";
			}
		var theElement2 = document.getElementById('slidernavi2');
							if (theElement2) {
								theElement2.className="inaktiv";
			}
		var theElement3 = document.getElementById('slidernavi3');
							if (theElement3) {
								theElement3.className="inaktiv";
			}
	}
	if(xpos == 416){
		var theElement = document.getElementById('slidernavi1');
					if (theElement) {
						theElement.className="inaktiv";
			}
		var theElement2 = document.getElementById('slidernavi2');
							if (theElement2) {
								theElement2.className="aktiv";
			}
		var theElement3 = document.getElementById('slidernavi3');
							if (theElement3) {
								theElement3.className="inaktiv";
			}
	}
	if(xpos == 834){
		var theElement = document.getElementById('slidernavi1');
					if (theElement) {
						theElement.className="inaktiv";
			}
		var theElement2 = document.getElementById('slidernavi2');
							if (theElement2) {
								theElement2.className="inaktiv";
			}
		var theElement3 = document.getElementById('slidernavi3');
							if (theElement3) {
								theElement3.className="aktiv";
			}
	}
}

function setAnimationFlag(FlagValue)
{
	animationFlag = FlagValue;
}

function animationOnClick(xpos, sElementID, className)
{
	var container = $('div.sliderGallery');
	var div = $('div.teaser_box_slider', container);
	if(xpos <= 940)
	{
		div.css('left', xpos * -2.33);
	}

	aTabs = new Array('1', '2', '3');

	for (var i = 0; i < aTabs.length; ++i){
		if(aTabs[i] == sElementID){
			var theElement = document.getElementById('slidernavi'+ sElementID);
			if (theElement) {
				theElement.className="aktiv";
			}
		}
		else
		{
			var theElement = document.getElementById('slidernavi'+ aTabs[i]);
			if (theElement) {
			theElement.className="inaktiv";
			}
		}
	}
	animationFlag = false;
	window.setTimeout("setAnimationFlag(true)", 8000);
}

function setSlider(pos)
  {
	if(animationFlag)
	{
	 	anzahlComingTeaser = anzahlComingTeaser - numberOfElements;
		if(anzahlComingTeaser > 0){
	  		animation(pos, 2500, false);
	 	}else{
	 		pos = 0;//x Koordinate auf die zurück gesprungen werden soll
	 		animation(pos, 0, true);
	 		anzahlComingTeaser = anzahlTeaser;
	 	}
	 }

	 var run = numberOfElements * 2;
	 if(anzahlComingTeaser < run){
		var diff = run - anzahlComingTeaser;
	 	var tmp = numberOfElements-diff;
	 	moveX = sizeProElement*tmp;
	 }else{
	 	moveX = sizeProElement*numberOfElements;
	 }
     window.setTimeout("setSlider(" + (pos+moveX) + ")",9000);
  }

 function getElementsByClass( searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}
  RotateImages(0);
  setSlider(0);

function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul#elements li').css({opacity: 0.0});

	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul#elements li:first').css({opacity: 1.0});

	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',12000);

}

function rotate() {
	//Get the first image
	var current = ($('div#rotator ul#elements li.show')?  $('div#rotator ul#elements li.show') : $('div#rotator ul#elements li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul#elements li:first') :current.next()) : $('div#rotator ul#elements li:first'));

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');

};

function rotateOnDemand(elementID) {
	//Get the first image
	var current = ($('div#rotator ul#elements li.show')?  $('div#rotator ul#elements li.show') : $('div#rotator ul#elements li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = $('div#rotator ul#elements li#'+elementID);
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');

};



$(document).ready(function() {
	//Load the slideshow
	theRotator();
});

