// Initialisierung
var width = 980;
var firstloading = true;
var statusLoading = 0;
var languageID = 0;

if(window.location.href.indexOf("language=german")!=-1){
	languageID = 0;
}else if(window.location.href.indexOf("language=english")!=-1){
	languageID = 1;
}else{
	languageID = 0;
}

function init(){
	bodyFrame = $(".bodyFrame");
	wrapper = $(".wrapper");
	body_bg = $(".body_bg");
	logo = $(".logo");
	language = $(".language");
	content = $(".content");
	content_bg = $(".content_bg");
	newsticker = $(".newsticker");
	stage = $(".stage");
	background = $(".body_bg img");
	if(navigator.userAgent.indexOf("Navigator")!=-1){
		$("h1").css("font-size","12px");
		$("h2").css("font-size","11px");
		$("p").css("font-size","11px");
		$(".news").find("p").css("font-size","10px");
	}
	// Start
	loadLanguage(languageID);
	body_bg.css("opacity",0.7);
	content_bg.css("opacity",0.4);
}

// Sprachauswahl
function loadLanguage(pId){	
	if(languageID!=pId || firstloading==true){
		switch(pId){
			case 0:
				if(!firstloading){
					animateLoading();
				}else{
					body_bg.load("html/background.html"); // Startanimation: s. background.html
				}
				content.load("html/content_german.html", function(){
					spinning = $(".spinning");
					nonwovens = $(".nonwovens");
					cardclothing = $(".cardclothing");
					hoverBranches(spinning);
					hoverBranches(nonwovens);
					hoverBranches(cardclothing);
				});
				language.find("a").eq(pId).css("cursor","default");
				language.find("a").eq(1).css("cursor","pointer");
			break;
			case 1:
				if(!firstloading){
					animateLoading();
				}else{
					body_bg.load("html/background.html"); // Startanimation: s. background.html
				}
				content.load("html/content_english.html", function(){
					spinning = $(".spinning");
					nonwovens = $(".nonwovens");
					cardclothing = $(".cardclothing");
					hoverBranches(spinning);
					hoverBranches(nonwovens);
					hoverBranches(cardclothing);
				});
				language.find("a").eq(pId).css("cursor","default");
				language.find("a").eq(0).css("cursor","pointer");
			break;
		}
		languageID = pId;
	}
}

// Animation bei Sprachwechsel
function animateLoading(){
	if(statusLoading==0){
		statusLoading=1;
		logo.hide();
		content.hide();
		content_bg.hide();
		newsticker.hide();
		logo.fadeIn(1000, function(){
			content_bg.fadeIn(700);
			content.fadeIn(700, function(){
				switch(languageID){
					case 0:
						stage.load("html/news_german.html");
					break;
					case 1:
						stage.load("html/news_english.html");
					break;	
				}
				newsticker.fadeIn(700, function(){statusLoading=0});
			});
		});
	}
}

// Overlay
function hoverBranches(pObj){
	pObj.hover(
		function(){
			pObj.find(".overlay_bottom").first().show();
			if(languageID==0 && pObj.hasClass("nonwovens")){
				pObj.find(".overlay_top").first().stop(true, true).css("height", 0).show().animate({"height":220},700);
			}else if(languageID==1 && pObj.hasClass("nonwovens")){
				pObj.find(".overlay_top").first().stop(true, true).css("height", 0).show().animate({"height":195},700);
			}else{
				pObj.find(".overlay_top").first().stop(true, true).css("height", 0).show().animate({"height":180},700);
			}
			
		},
		function(){
			pObj.find(".overlay_bottom").first().hide();
			pObj.find(".overlay_top").first().hide().css("height", 0);
		}
	);
}

// Newsticker
var userNavi = true;
var autoAni = true;
var intro = true;
var display = 2;
var title = true;
var speed = 1000;
var timeout = 12000;
var clear = 0;
var status = 0;

function initTicker(){
	if(clear!=0){clearTimeout(clear)};
	clear = 0;
	newsticker = $(".newsticker");
	stage = $(".stage");
	news = $(".news");
	news.last().prependTo(stage);
	news = $(".news");
	caption = $(".caption");
	arrowLeft = $(".arrowLeft");
	arrowRight = $(".arrowRight");
	newsWidth = news.width();
	newsHeight = news.height();
	newsPaddingW = parseInt(news.css("padding-left")) + parseInt(news.css("padding-right"));
	newsPaddingH = parseInt(news.css("padding-top")) + parseInt(news.css("padding-bottom"));
	totalW = newsWidth + newsPaddingW;
	totalH = newsHeight + newsPaddingH;
	if(display<1){
		display=1;
	}else if(display>news.length-2){
		display=news.length-2;
	}
	stageW = display * totalW;
	stageH = totalH;
	if(newsticker.width()<stageW){
		display = Math.round((newsticker.width())/newsWidth);
		stageW = display * totalW;
	}
	//stage.css({"width":stageW, "height":stageH});
	if(userNavi){
		arrowLeft.show();
		arrowRight.show();
	}
	if(title){
		caption.show();
	}
	setStart();
}

function setStart(){
	news.each(function(i){
		posL = i*totalW-totalW;
		$(this).css({"left":posL});
	});
	if(intro){
		animateStart(0);
	}else{
		news.css({"display":"block"});
		prevNews();
		nextNews();
	}
}

function animateStart(id){
	status=1;
	if(id!=0 && id<=display){
		news.eq(id).fadeIn(1500, function(){
			if(id<news.length){
				id+=1;
				animateStart(id);
			}
		});
	}else{
		news.eq(id).css({"display":"block"});
		if(id<news.length){
			id+=1;
			animateStart(id);
		}
		if(id==news.length-1){
			status=0;
			prevNews();
			nextNews();
			if(autoAni){
				clear = setTimeout("animateLeft()",timeout);
			}
		}
	}
}

function animateLeft(){
	if(status==0){
		if(clear!=0){clearTimeout(clear)};
		clear=0;
		status=1;
		news.each(function(i){
			newPos = parseInt($(this).css("left"))-totalW;
			$(this).animate({left:newPos}, speed, function(){
				if(parseInt($(this).css("left"))<=-totalW*2){
					endPos = (totalW * news.length) - totalW*2;
					$(this).css("left",endPos);
				}
				if(i==news.length-1){
					status=0;
					nextNews();
					prevNews();
				}
			});
		});
	}
	if(autoAni){
		clear = setTimeout("animateLeft()",timeout);
	}
}

function animateRight(){
	if(status==0){
		if(clear!=0){clearTimeout(clear)};
		clear=0;
		status=1;
		news.each(function(i){
			newPos = parseInt($(this).css("left"))+totalW;
			$(this).animate({left:newPos}, speed, function(){
				if(parseInt($(this).css("left"))>=totalW*news.length-totalW){
					endPos = -totalW;
					$(this).css("left",endPos);
				}
				if(i==news.length-1){
					status=0;
					nextNews();
					prevNews();
				}
			});
		});
	}
	if(autoAni){
		clear = setTimeout("animateRight()",timeout);
	}
}

function prevNews(){
	arrowLeft.one("click", function(){
		if(autoAni){
			clearTimeout(clear);
		}
		animateRight();
	});
}

function nextNews(){
	arrowRight.one("click", function(){
		if(autoAni){
			clearTimeout(clear);
		}
		animateLeft();
	});
}

// Layoutanpassung
function setWidth(){
	if(navigator.userAgent.indexOf("iPhone")==-1 && navigator.userAgent.indexOf("iPad")==-1){
		if($("body").width()<width){
			wrapper.css({"width":width});
		}else{
			wrapper.css({"width":"100%"});
		}
	}
}

$(document).ready(function(){
	init();
	setWidth();
	$(window).resize(function(){
		setWidth();
	});
	/*$.supersized({
		slides:[{image:'img/body_bg.jpg', title:'Trützschler GmbH & Co. KG'}],
		//fit_always:1,
		vertical_center:0,
		//horizontal_center:1,
		//fit_landscape:1,
		//fit_portrait:0
		min_width:1024,
		min_height:768
	});
	if(navigator.userAgent.indexOf("iPad")!=-1 || navigator.userAgent.indexOf("iPhone")!=-1){
		$(window).bind("resize", function(){
			if(window.orientation==0){
				$.supersized({
					vertical_center:0,
					fit_portrait:0
				});
			}else if(window.orientation==90){
				$("#supersized").css({"overflow":"visible"});
			}else if(window.orientation==-90){
				$("#supersized").css({"overflow":"visible"});
			}
		});
		
	}*/
});
