// css for mac
if(navigator.userAgent.indexOf('Mac') != -1){
	document.write('<link rel="stylesheet" type="text/css" media="screen" href="css/mac.css" />');
}

// js for ie 6
if(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1){
	document.write('<script type="text/javascript" src="iepngfix/iepngfix_tilebg.js"></script>'+
		'<link rel="stylesheet" type="text/css" href="iepngfix/iepngfix.css" />');
}

var currentSectionIndex;
var global_slideSection;

function leftSwitch_setMouseOver(state) {
	if (state) {
		$("#switch_left a").addClass("active");
	} else {
		$("#switch_left a").removeClass("active");
	}
}

function rightSwitch_setMouseOver(state) {
	if (state) {
		$("#switch_right a").addClass("active");
	} else {
		$("#switch_right a").removeClass("active");
	}
}

$(document).ready(function(){
	global_slideSection = slideSection;
	
	
	///////////////////////////// Folge //////////////////////////////
	function setSwitch() {
		var currentLeft = parseInt($('#section_container').css("margin-left"));
		if(maxLeftPos >= currentLeft) {
			$("#switch_right").addClass("inactive");
		} else {
			$("#switch_right").removeClass("inactive");
		}
		if(currentLeft >= 0) {
			$("#switch_left").addClass("inactive");
		} else {
			$("#switch_left").removeClass("inactive");
		}
	}
	function slideSection(direction) {
		if($('#section_container').is(':animated')==true) return;
		
		try {
			document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_unloadVideo();
			document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_prevButton_onMouseOut();
			document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_nextButton_onMouseOut();
		} catch (error) {}

		var directionSign;
		if(direction == 'right') {
			directionSign = "-";
			currentSectionIndex++;
		} else {
			directionSign = "+";
			currentSectionIndex--;
		}
		
		var currentLeft = parseInt($('#section_container').css("margin-left"));
		var newPos = eval(currentLeft + directionSign + sectionWidth);
		
		if(newPos <= maxLeftPos) {
			newPos = maxLeftPos;
		} else if(newPos > 0) {
			newPos = 0;
		}
		$('.text_box').css('overflow','hidden');
		$('#section_container').animate({
			marginLeft: newPos+'px'
		}, 1500, function() { 
			setSwitch(); 
			$('.text_box').css('overflow','auto');
			try {
				document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_loadVideo();
			} catch (error) {}
		});
	}
	function stepInitialize() {
		var sections = $(".section");
		sections.each(function() {
			var firstStep =  $(this).find('.step');
			if($(firstStep[0]).hasClass('active') == false)	{
				$(firstStep[0]).addClass('active');
			}	   
		});
	}

	//movie scrolling
	currentSectionIndex = 0;
	if($('.section').length > 0) {
		var sectionWidth = parseInt($('.section').css("width"));
		var sectionSpace = parseInt($('.section').css("width")) - parseInt($(".movie").css("width"));
		var sectionLength = $(".section").length;
		var getMovieScreenWidth = sectionWidth * sectionLength;
		var maxLeftPos = (sectionWidth) - getMovieScreenWidth;
		var currentSection;

		//set width for #section_container
		$("#section_container").css("width",getMovieScreenWidth+'px');
		var sections = $(".section");
		if(sections.hasClass("active") == false){
			$(sections[0]).addClass("active");
		} else {
			sections.each(function (index) {
				if($(this).hasClass("active")) {
					currentSectionIndex = index;
					var initialPos = '-' + sectionWidth * index + 'px';
					$('#section_container').css("marginLeft",initialPos);
				} 
			});
		}

		loadFlashVideos();
		setSwitch();
		
		//set sliding arrow
	
		$("#switch_left").click(function() {
			slideSection('left');
		});
		
		$("#switch_right").click(function() {
			slideSection('right');
		});
	
		$("#switch_left").hover(
			function () {
				try {
					document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_prevButton_onMouseOver();
				} catch (error) {}
				try {
					$(".movie_text_nav.left").css("color", "#ffffff");
				} catch (error) {}
		  	},
		  	function () {
				try {
					document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_prevButton_onMouseOut();
				} catch (error) {}
				try {
					$(".movie_text_nav.left").css("color", "#6E6E6E");
				} catch (error) {}
		  	}
		);
		$("#switch_right").hover(
			function () {
				try {
					document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_nextButton_onMouseOver();
				} catch (error) {}
		  	},
		  	function () {
				try {
					document.getElementById("flashMovie_"+(currentSectionIndex+1)).as_nextButton_onMouseOut();
				} catch (error) {}
		  	}
		);
		
		// start -  Html Text Switch in Movie Area -------
		$(".movie_text_nav.left").mouseover(function(){
			leftSwitch_setMouseOver(true);
			$(".movie_text_nav.left").css("color", "#ffffff");
		}).mouseout(function(){
			leftSwitch_setMouseOver(false);
			$(".movie_text_nav.left").css("color", "#6E6E6E");
		});
		$(".movie_text_nav.left").click(function() {
			slideSection('left');
		});
		// end -  Html Text Switch in Movie Area -------
		
		// link to section back-------
		$(".section_back").click(function() {
			slideSection('left');
		});
		


		///////////////////////////// Steps //////////////////////////////
	
		stepInitialize();
	
		$("a[class*=step_next]").click(function() {
			parentDiv = $(this).parents().filter('.step');
			nextDiv = parentDiv.next();
			if(nextDiv.hasClass('step')) {
				parentDiv.removeClass("active");
				nextDiv.addClass("active");
			};
		});
		$("a[class*=step_back]").click(function() {
			parentDiv = $(this).parents().filter('.step');
			nextDiv = parentDiv.prev();
			if(nextDiv.hasClass('step')) {
				parentDiv.removeClass("active");
				nextDiv.addClass("active");
			};
		});
		
		$("#submit_nextfolge").click(function() {
			$("#form_inform_next").css("display","none");
			$(".form_success").css("display","block");
			
		});
	} // end of if($('.section').length > 0) {
		
	
	///////////////////////////// tafel highlight //////////////////////////////

	var browserIE = 0;
	if(navigator.userAgent.toLowerCase().indexOf('msie') != -1){
		browserIE = 1;
	}
	function pulsTafel() {
		if (isMouseOver) return;
		
		count = count + 1;
		if(count > roundMax) {
			clearInterval(timer);
			return;
		}
		
		tafelIndex++;
		if (tafelIndex >= length) {
			tafelIndex = 0;
		}
		
		if(browserIE == 1) {
			elements.eq(tafelIndex).show();
			setTimeout(function() {
				elements.eq(tafelIndex).hide();
			},2000);
		} else {
			elements.eq(tafelIndex).fadeIn(2000);
			elements.eq(tafelIndex).fadeOut(1000);
		}
	}
	if($('.highlight').length > 0) {

		var isMouseOver = false;
		var speed = 3000;
		
		var tafelIndex;
		
		var elements = $('.highlight');
		
		elements.css('display','none');
		var length = elements.length;
	
		 $('.tafel').mouseover(function(){
			isMouseOver = true;
			elements.eq(tafelIndex).stop();
			if(browserIE != 1) {
				elements.eq(tafelIndex).css('opacity','1');
			}
			elements.eq(tafelIndex).hide();
			$(this).find('.highlight').show();
		});
		 
		 $('.tafel').mouseout(function(){
			isMouseOver = false;
			$(this).find('.highlight').hide();
		});
		 
		elements.each(function (index) {
			$(this).hide();
			if($(this).parent().hasClass("active")==1) {
				 tafelIndex = index-1;
			} 
		});
		
		var roundMax = length * 3;
		var count = 0;
		var timer = setInterval(pulsTafel, speed);
		pulsTafel();
	}	
	
});
