var thisCat = 0;
var stellenId = 0;

function footerPos() {
	footerBottomCoord = $("#outerfooter").offset().top + $("#outerfooter").height();
	//alert(footerBottomCoord);
	//$("#debugoutput").attr("value",$("#outerfooter").offset().top+"/"+$("#contentbox #inner").height());
	
	if($(window).height() > footerBottomCoord) {
		//alert("pos");
		posMargin = $(window).height() - footerBottomCoord;
		$("#outerfooter").css("padding-top", posMargin+"px");
	}
	
	$("#outerfooter").css("visibility", "visible");		
}

function ieNaviWidth(theLi) {
	theUl =  $(theLi).children("ul.level2");
	theWidth = $(theUl[0]).width();
	theWidth -= 42;		
	alert(theWidth);
	$(theUl).children("li").css("width", theWidth+"px");
}

function supportsSVG() {
	return !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', "svg").createSVGRect;
}

var teaserMarginStep = 958;
var biersliderMarginStep = 308;
var marginStep = 853;

if(typeof(isSmartphone) != undefined && window.isSmartphone) {
	marginStep = 379;
	teaserMarginStep = 490;		
}

function checkScroll() {
	theMargin = $("#pagernav").css("margin-left").replace("px", "");
	
	if(theMargin < 0) {
		$("#productsleft, #geschichteleft").css("visibility", "visible");
	}
	else {
		$("#productsleft, #geschichteleft").css("visibility", "hidden");	
	}
	
	if($("#pagernav").width() <= marginStep + (theMargin*-1)) {
		$("#productsright, #geschichteright").css("visibility", "hidden");	
	}
	else {
		$("#productsright, #geschichteright").css("visibility", "visible");	
	}
	
	activatePagerbutton(theMargin,marginStep);
}

function checkScrollGeschichtecontent() {
	theMargin = $("#geschichte-slider").css("margin-left").replace("px", "");
	
	geschichtePanels = $(".geschichtepanel");
	maxMargin = teaserMarginStep * (geschichtePanels.length - 1);
	
	if(theMargin < 0) {
		$("#geschichteblaetter-li").css("visibility", "visible");
	}
	else {
		$("#geschichteblaetter-li").css("visibility", "hidden");	
	}

	if((theMargin * -1) >= maxMargin) {
		$("#geschichteblaetter-re").css("visibility", "hidden");
	}
	else {
		$("#geschichteblaetter-re").css("visibility", "visible");	
	}
	
	panelIndex = -1 * theMargin / teaserMarginStep;
	
	if(window.isSmartphone) {
		pagernavElements = 4;	
		marginStep = 384;
	}
	else {
		pagernavElements = 9;
	}
	
	marginRangeBottom = marginStep * Math.floor(panelIndex / pagernavElements);
	marginRangeTop = marginRangeBottom + marginStep;
		
	pagernavMargin = ($("#pagernav").css("margin-left").replace("px", "")) * -1;
	
	if((pagernavMargin < marginRangeBottom)||(pagernavMargin >= marginRangeTop)) {
		productsScrollTo(1+Math.floor(panelIndex / pagernavElements));
	}	
	
	activateJz(panelIndex);
}

function activateJz(index) {
	pagernavLinks = $("#pagernav li a");		// aktuelles pagernav item aktivieren
	$("#pagernav li a").removeClass("active");
	$(pagernavLinks[index]).addClass("active");
}

function productsScrollTo(page,theElement) {
	scrollMargin = -marginStep * (page-1);
	$("#pagernav").animate(
		{"margin-left": scrollMargin+"px"}, 
		250, 
		function() { checkScroll() }
	);
}

function teasersScrollTo(page,theElement) {
	scrollMargin = -teaserMarginStep * (page-1);
	$("#coda-slider-1").animate(
		{"margin-left": scrollMargin+"px"}, 
		350,
		function() { activatePagerbutton(scrollMargin,teaserMarginStep) }
	);
}

function biersliderScrollTo(page,theElement) {
	scrollMargin = -biersliderMarginStep * (page-1);
	$("#coda-slider-2").animate(
		{"margin-left": scrollMargin+"px"}, 
		200,
		function() { activatePagerbutton(scrollMargin,biersliderMarginStep) }
	);
}


function geschichteScrollTo(page,theElement) {
	scrollMargin = -teaserMarginStep * (page-1);
	$("#geschichte-slider").animate(
		{"margin-left": scrollMargin+"px"}, 
		350,
		function() { checkScrollGeschichtecontent() }
	);
}

function activatePagerbutton(theMargin,theStep) {
	$("#pagernavbuttons a").removeClass("current");
	marginIndex = (theMargin*-1)/theStep;
	pagerbuttonsArray = $("#pagernavbuttons a");
	$(pagerbuttonsArray[marginIndex]).addClass("current");
}

var teaserIndex = 1;
var elementsLength = 0;
var cycleInterval;
var cycleInterval2;

function doCycle() {
	teaserIndex++;
	if(teaserIndex > elementsLength) teaserIndex = 1;
	teasersScrollTo(teaserIndex,0);
}

function autoCycle() {
	theElements = $("#coda-slider-1 .panel");	
	elementsLength = theElements.length;
	if(elementsLength) {		
		cycleInterval = setInterval("doCycle()", 11500); // startseite
	}
}

function doCycle2() {
	teaserIndex++;
	if(teaserIndex > elementsLength) teaserIndex = 1;
	biersliderScrollTo(teaserIndex,0);
}

function autoCycle2() {
	theElements = $("#coda-slider-2 img");	
	elementsLength = theElements.length;
	if(elementsLength) {		
		cycleInterval2 = setInterval("doCycle2()", 3500); // startseite - bier slider unten
	}
}

function blockImages(selector) {
   $(selector).each(function () {
	   $(this).data("src", this.src);
	   this.src = "";
   });
}

function loadImages(selector) {
	var img = $(selector);
	for(m=0; m<img.length; m++) {
		img[m].src = $(img[m]).data("src");
	   //alert(img[m].src);
	   
	   if($.browser.msie && $.browser.version < 9 && !geschichte) {			// unitpngfix fuer nachgeladene produktbilder anwenden
		   filterClass = /noload/;
		   var els=document.getElementsByTagName('*'),ip=/\.png/i,al="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='",i=els.length,uels=new Array(),c=0;while(i-->0){if(els[i].className.match(filterClass)){uels[c]=els[i];c++;}}if(uels.length==0)pfx(els);else pfx(uels);function pfx(els){i=els.length;while(i-->0){var el=els[i],es=el.style,elc=el.currentStyle,elb=elc.backgroundImage;if(el.src&&el.src.match(ip)&&!es.filter){es.height=el.height;es.width=el.width;es.filter=al+el.src+"',sizingMethod='crop')";el.src=clear;}else{if(elb.match(ip)){var path=elb.split('"'),rep=(elc.backgroundRepeat=='no-repeat')?'crop':'scale',elkids=el.getElementsByTagName('*'),j=elkids.length;es.filter=al+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage='none';if(j!=0){if(elc.position!="absolute")es.position='static';while(j-->0)if(!elkids[j].style.position)elkids[j].style.position="relative";}}}}};
		}
	}
}

function is_touch_device() { 
	if(navigator.appVersion.indexOf('IEMobile') > -1) {
		return true;
	}
	else {
		try {  
			document.createEvent("TouchEvent");  
			return true;  
		} catch (e) {  
			return false;  
		}  
	}
}


function openNews(id) {
	window.location.href="news.php?eId="+id;
}

function openEvent(id) {
	window.location.href="events.php?eId="+id;
}

function activateLevel1mobile(k) {
	li1items = $('li.n1item');	
	$(li1items[k-1]).children("a").addClass("activeclass");					
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

$(document).ready(function(){

	if(!($.browser.msie && $.browser.version < 7)) {
		blockImages(".noload");
	}
	autoCycle();
	autoCycle2();
	
	$('#pagernavbuttons ul#index').hover(
		function() {
			clearInterval(cycleInterval);
		}
	);

	$('#pagernavbuttons ul#index-2').hover(
		function() {
			clearInterval(cycleInterval2);
		}
	);
	
	$('#startnews').cycle(
		{
			timeout:       7500,  // milliseconds between slide transitions (0 to disable auto advance) 
			speed:         1800,  // speed of the transition (any valid fx speed value) 
			height:       '51px', // container height 
			sync:          0,     // true if in/out transitions should occur simultaneously 
			pause:         0     // true to enable "pause on hover" 
		}
	);
	
	$('a.teaserlink, a.mehrlink, #innerfooter #navi a').append(" &rsaquo;");		// pfeile zu links hinzufuegen
	
	$("ul#social li:last-child").addClass('last-child');
	
	// navi hover
	
	if(!is_touch_device()) {
	
		$('li.n1item').hover(
			function() {
				$(this).children("ul").stop(true,true);			
				$(this).children("ul").fadeIn('30');
				$(this).children("a").css("color","#004628");
				$(this).addClass("hoverclass");
				//ieNaviWidth(this);
			},
			function() {
				$(this).children("ul").fadeOut('40');
				//$(this).children("ul").css("display","none");		
				$(this).children("a").not(".activeclass").css("color","#fff");
				$(this).children("a.activeclass").css("color","#E0B241");
				$(this).removeClass("hoverclass");
			}
		);
	
	}
	else if(!window.isSmartphone) {
		$('li.n1item').each(function() {
			if($(this).has("ul.level2").length) {		// nur jene mit subnavi
				$(this).children("a").attr("href","JavaScript:void(0);");	// link 1. ebene wegnehmen
			}
		});	

		$('li.n1item').hover(
			function() {
				$(this).children("a").css("color","#004628");
				$(this).addClass("hoverclass");
			},
			function() {
				$(this).children("a").not(".activeclass").css("color","#fff");
				$(this).children("a.activeclass").css("color","#E0B241");
				$(this).removeClass("hoverclass");
			}
		);	
	
		$('li.n1item').click(function() {
			$(this).children("ul").toggle();
		});
	}
	
	
	$('ul.level2 li').hover(
		function() {
			$(this).children("a").css("color","#fff");
		},
		function() {		
			$(this).children("a").css("color","#004628");
		}
	);	
	
	// navi activate
		
	// a) nur bis navi2 vorhanden
		
	li1items = $('li.n1item');
	
	for(p=0; p<li1items.length; p++) {
		if($(li1items[p]).children("a").attr("href") == window.location.pathname || ($(li1items[p]).children("a").attr("href") == "/"+jsLang+"/index.php" &&  window.location.pathname == "/"+jsLang+"/")) {
			$(li1items[p]).children("a").addClass("activeclass");					
		}
	}	
	
	if(!window.isSmartphone) {
		l2links = $("li.n1item ul a");
		
		for(q=0; q<l2links.length; q++) {
			if($(l2links[q]).attr("href") == window.location.pathname) {
				$(l2links[q]).addClass("activeclass");
				$(l2links[q]).parent().parent().parent("li.n1item").children("a").addClass("activeclass");
			}
		}
	}
	
	// b) ab navi3:
	
	contentnavilinks = $("#contentnavi a");
	
	for(r=0; r<contentnavilinks.length; r++) {
		if(($(contentnavilinks[r]).attr("href") == window.location.pathname) || ($(contentnavilinks[r]).attr("href") == "/de/shop/kategorie_inhalt.php?catId="+thisCat) || ($(contentnavilinks[r]).attr("href") == "/de/unternehmen/offene_stellen/index.php?id="+stellenId)) {
			$(contentnavilinks[r]).addClass("active");
			$(contentnavilinks[r]).parents("li").children("a").addClass("active");
			
			// einblenden
			$(contentnavilinks[r]).parents("ul").css("display","block");			
			$(contentnavilinks[r]).parent().children("ul").css("display","block");			
	
			// href vom "obersten" element nehmen
			// damit n2 und n1 aktivieren
			theParents = $(contentnavilinks[r]).parents("li").children("a");
			l2href = $(theParents[theParents.length-1]).attr("href");

			for(q=0; q<l2links.length; q++) {
				if($(l2links[q]).attr("href") == l2href) {
					$(l2links[q]).addClass("activeclass");
					$(l2links[q]).parent().parent().parent("li.n1item").children("a").addClass("activeclass");
				}
			}
		}
	}
	
	/* smartphone scrollboxes fuer geschichte & produkte text: */

	if(window.isSmartphone && !$.browser.msie) {
		$('.scrollbox').jScrollPane({ verticalGutter: 10 });
	}
	
	/* * * * * * * * * * * * produkt-blaetter * * * * * * * * * * * * */
	
	if(window.isSmartphone) {
		$('#main2 #images, #main2 #descriptions').cycle({ 
			fx:			'scrollHorz',
			delay:    -1000,
			before: function(curr, next, opts) {
				opts.animOut.opacity = 0;
			},			
			speed:		250,  
			prev: 		'#blaetter_li',
			next:		'#blaetter_re',
			sync:		0,
			easing:		'easeout',			
			timeout:	0,
			rev:		0,
			pager:  '#pagernav',
			pagerAnchorBuilder: function(idx, slide) {
				// return sel string for existing anchor
				return '#pagernav li:eq(' + (idx) + ') a';
			}		
		});
	
		$("#fadecontainer").wipetouch( { 
			preventDefault:	false,
			wipeLeft: function() { 
				$('#blaetter_re').trigger('click');							
			}, 
			wipeRight: function() { 
				$('#blaetter_li').trigger('click');			
			}
		});		
	}
	else {
		$('#main2 #images, #main2 #descriptions').cycle({ 
			fx:			'fade',
			speed:		1000,  
			prev: 		'#blaetter_li',
			next:		'#blaetter_re',
			sync:		0,	
			easing:		'easeout',
			timeout:	0,
			pager:  '#pagernav',
			pagerAnchorBuilder: function(idx, slide) {
				// return sel string for existing anchor
				return '#pagernav li:eq(' + (idx) + ') a';
			}		
		});
	}
		
	$("#blaetter_li").hover(
		function() {
			$(this).attr('src','/images/produkt-blaetter-hover_li.png');
		},
		function() {
			$(this).attr('src','/images/produkt-blaetter_li.png');
		}
	);

	$("#blaetter_re").hover(
		function() {
			$(this).attr('src','/images/produkt-blaetter-hover_re.png');
		},
		function() {
			$(this).attr('src','/images/produkt-blaetter_re.png');
		}
	);
	
	if($.browser.msie && $.browser.version < 8) {			// produktnavi hover 
		$('#pagerbox #pagernav a').hover(
			function() {
				$(this).addClass('iehoverclass');
			},
			function() {
				$(this).removeClass('iehoverclass');
			}
		);
	}
	else {
		$('#pagerbox #pagernav li a').before('<div class="hoverbg">&nbsp;</div>');
		$('#pagerbox #pagernav a img').css('position','relative')
		
		$("#pagerbox #pagernav li").hover(
			function() {				
				$(this).children('div.hoverbg').fadeIn('50');
			},
			function() {
				$(this).children('div.hoverbg').fadeOut('70');
			}
		);
	}
	
	numProducts = $('#pagerbox #pagernav li');
	if(numProducts.length < 2 ) {
		$("#blaetter_li, #blaetter_re").css("visibility","hidden");
	}
	
	/* * * * * * * * * * * * geschichte-blaetter * * * * * * * * * * * * */
		
	$("#geschichteleft img").hover(									// zeitleiste blaetter hover
		function() {			
			$(this).attr('src','/images/zeitleiste-blaetter-hover_li.png');
		},
		function() {
			$(this).attr('src','/images/zeitleiste-blaetter_li.png');
		}
	);

	$("#geschichteright img").hover(
		function() {
			$(this).attr('src','/images/zeitleiste-blaetter-hover_re.png');
		},
		function() {
			$(this).attr('src','/images/zeitleiste-blaetter_re.png');
		}
	);

	$("#geschichteblaetter-li").hover(									// geschichte content blaetter hover
		function() {			
			$(this).attr('src','/images/geschichte_blaetter-hover_li.png');
		},
		function() {
			$(this).attr('src','/images/geschichte_blaetter_li.png');
		}
	);

	$("#geschichteblaetter-re").hover(
		function() {
			$(this).attr('src','/images/geschichte_blaetter-hover_re.png');
		},
		function() {
			$(this).attr('src','/images/geschichte_blaetter_re.png');
		}
	);
												
	// geschichte content blaetter
	if(window.isSmartphone)	{
		$("#geschichte-slider .teaserimg").wipetouch( { 
			preventDefault:	false,
			wipeLeft: function() { 
				theMargin = $("#geschichte-slider").css("margin-left").replace("px", "");	
				geschichtePanels = $(".geschichtepanel");
				maxMargin = teaserMarginStep * (geschichtePanels.length - 1);
	
				if((theMargin * -1) < maxMargin) {						
					$("#geschichte-slider").animate(
						{"margin-left": "-="+teaserMarginStep+"px"}, 
						250, 
						function() { checkScrollGeschichtecontent() }
					);
				}
			}, 
			wipeRight: function() { 
				if($("#geschichte-slider").css("margin-left").replace("px", "") < 0) {
					$("#geschichte-slider").animate(
						{"margin-left": "+="+teaserMarginStep+"px"}, 
						250, 
						function() { checkScrollGeschichtecontent() }
					);
				}
			}
		});
	}
	
	$("#geschichteblaetter-re").click(function(){		
		if (!$("#geschichte-slider").is(":animated")) {
			$("#geschichte-slider").animate(
				{"margin-left": "-="+teaserMarginStep+"px"}, 
				250, 
				function() { checkScrollGeschichtecontent() }
			);
		}
	});
	
	$("#geschichteblaetter-li").click(function(){		
		if (!$("#geschichte-slider").is(":animated")) {		
			$("#geschichte-slider").animate(
				{"margin-left": "+="+teaserMarginStep+"px"}, 
				250, 
				function() { checkScrollGeschichtecontent() }
			);
		}
	});
	
	
	$("#geschichtepagerbox li a").first().addClass("active");		// erste jz bei laden aktivieren


	// geschichte - mehr content - verti scrollen
		
	var step = 128;
		
	$("img.down").click(function() {
		if(geschichte) {			theElement = $(this).parent().parent().parent(".geschichtepanel").children(".right").children(".scrollbox").children(".contents");
			//alert(theElement[0].tagName);
		} 
		else {
			theElement = $(this).parent().parent(".description").children(".scrollbox").children(".contents");
		}
				
		theMargin = -1 * $(theElement).css("margin-top").replace("px", "");
		
		if(($(theElement).height() > $(theElement).parent().height()) && ($(theElement).height() > (theMargin+step)) && !$(theElement).is(":animated")) {
			$(theElement).animate(
				{"margin-top": "-="+step+"px"}, 
				250, null			
			);
		}
		
	});
	
	$("img.up").click(function() {
		if(geschichte) {
			theElement = $(this).parent().parent().parent(".geschichtepanel").children(".right").children(".scrollbox").children(".contents");
		}
		else {
			theElement = $(this).parent().parent(".description").children(".scrollbox").children(".contents");
		}
		
		if (!$(theElement).is(":animated")) {	
			theMargin = -1 * $(theElement).css("margin-top").replace("px", "");
			if(theMargin >= step) {
				$(theElement).animate(
					{"margin-top": "+="+step+"px"}, 
					250, null			
				);
			}
		}
	});	

	$("img.up").hover(
		function() {
			$(this).attr('src','/images/scrollbutton-hover_01.png');
		},
		function() {
			$(this).attr('src','/images/scrollbutton_01.png');
		}
	);	
	
	$("img.down").hover(
		function() {
			$(this).attr('src','/images/scrollbutton-hover_02.png');
		},
		function() {
			$(this).attr('src','/images/scrollbutton_02.png');
		}
	);
	
	$("a.thumbimg").attr("rel","gallery");
	$("a.thumbimg").addClass("fancygallery");	
	
	$("a.zip-icon").addClass("downloadlink zip");
	
	/* * * * mobile: * * * */
	
	$("#selectsrow select").change(function() {
		//alert($(this).val());
		window.location.href = $(this).val();
	});
				
	// jobs:	
	$("#selectsrow").append($("#addtoselectsrow").html());
	
	$(".jobselect option").each(function() {
		if(window.location.href.indexOf($(this).attr("value")) > -1) {
			$(this).attr("selected","selected");
		}
	});	
	
	if(window.isSmartphone) {
		if($('p.teasertext').length) {
			teaserText = $('p.teasertext').html();
			teaserText = str_replace('<br>','',teaserText);
			teaserText = str_replace('<br />','',teaserText);		
			$('p.teasertext').html(teaserText);
		}
		
		$('option.hidemobile').remove();
	}
	
	if(supportsSVG()) {
		$("#logo").attr("src","/images/logo-vektor.svg");
	}
	
	/* * * * product deep links: * * * */
	if(window.geschichte === 0) {
		var hash = window.location.hash;
		if(hash) {
			if(window.isSmartphone) {
				pagernavElements = 4;	
			}
			else {
				pagernavElements = 9;
			}
		
			hash = str_replace('#', '', hash);
			$('#pagernav li:nth-child('+hash+') a').trigger('click');
			page = Math.ceil(hash / pagernavElements);
			
			productsScrollTo(page,0);
		}
	}

	
});

function urlSelect(theEl) {
	window.location.href = $(theEl).val();
}

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

$(window).load(function(){
	//setTimeout("footerPos()",100);
	footerPos();
	
	if($("#pagernav").width() > marginStep) {	
		$("#productsright, #geschichteright").click(function(){
		    if (!$("#pagernav").is(":animated")) {		
				$("#pagernav").animate(
					{"margin-left": "-="+marginStep+"px"}, 
					250, 
					function() { checkScroll() }
				);
			}			
		});
		
		$("#productsleft, #geschichteleft").click(function(){
		    if (!$("#pagernav").is(":animated")) {	
				if($("#pagernav").css("margin-left").replace("px", "") < 0) {
					$("#pagernav").animate(
						{"margin-left": "+="+marginStep+"px"},
						250, 
						function() { checkScroll() }
					);
				}
			}
		});	
		
		if(window.isSmartphone) {
			$("#pagernav").wipetouch( { 
				preventDefault:	false,
				wipeLeft: function() { 
					$("#pagernav").animate(
						{"margin-left": "-="+marginStep+"px"}, 
						250, 
						function() { checkScroll() }
					);
				}, 
				wipeRight: function() { 
					$("#pagernav").animate(
						{"margin-left": "+="+marginStep+"px"},
						250, 
						function() { checkScroll() }
					);
				}
			});
		}
	}
	else {
		$("#productsright, #productsleft").css("visibility", "hidden");
	}
	
	if($("#pagerbox").length) {
		checkScroll();
	}

	if(!window.isSmartphone) {
		// checken, ob scrollbuttons noetig:
		geschichtepanels = $("div.geschichtepanel");
		for(n=0; n<geschichtepanels.length; n++) {
			theScrollbox = $(geschichtepanels[n]).children("div.right").children("div.scrollbox");		
			if($(theScrollbox).height() < $(theScrollbox).children("div.contents").height()) {
				$(geschichtepanels[n]).children(".right").children("div.scrollbuttons").css("visibility","visible");
			}
		}
		
		// checken, ob scrollbuttons noetig:
		productdescriptions = $("#descriptions div.description");
		for(n=0; n<productdescriptions.length; n++) {
			theScrollbox = $(productdescriptions[n]).children("div.scrollbox");	
			if($(theScrollbox).height() < $(theScrollbox).children("div.contents").height()) {
				$(productdescriptions[n]).children("div.scrollbuttons").css("visibility","visible");
			}
		}
	}
	
	if(!($.browser.msie && $.browser.version < 7)) {
		loadImages("img.noload");
	}
	
	// startseite teaser div klickbar:
	$(".teaserlink").parents(".panel").click(function() {
		window.location.href = $(this).find(".teaserlink").attr("href");
	});
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

$(window).resize(function() {
	setTimeout("footerPos()",750);
});

