//onerror = function() {};

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


function setNaviboxWidth(el) {
	parentWidth = 0;
	$(el).children("ul.level2").each(function() {		
		$(this).parent().css("display","block");		
		parentWidth += ($(this).width() + 35);
		$(this).parent().css("display","none");
	});
	
	$(el).css("width", parentWidth+"px");
}

var blockArray = new Array("[url","<a href");

function submitCustDataNew(theAction,theForm,lang) {
	$(".formfield").css("backgroundColor","#f3f3f3");
	
	formError = 0;	

	if(document.forms[theForm].Anrede.value && document.forms[theForm].Anrede.value.search(reg_name)) {
		document.forms[theForm].Anrede.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(document.forms[theForm].Vorname.value.search(reg_name)) {
		document.forms[theForm].Vorname.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(document.forms[theForm].Nachname.value.search(reg_name)) {
		document.forms[theForm].Nachname.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(document.forms[theForm].Firma.value && document.forms[theForm].Firma.value.search(reg_name2)) {
		document.forms[theForm].Firma.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(document.forms[theForm].Telefon.value && document.forms[theForm].Telefon.value.search(reg_telvw)) {
		document.forms[theForm].Telefon.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(document.forms[theForm].Strasse_Nr.value && document.forms[theForm].Strasse_Nr.value.search(reg_name2)) {
		document.forms[theForm].Strasse_Nr.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(document.forms[theForm].PLZ_Ort.value && document.forms[theForm].PLZ_Ort.value.search(reg_name2)) {
		document.forms[theForm].PLZ_Ort.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(document.forms[theForm].E_Mail.value.search(reg_email)) {
		document.forms[theForm].E_Mail.style.backgroundColor = "#f33";
		formError++;
	}
	
	AnfText = document.forms[theForm].Anfragetext.value;
	
	if(AnfText.indexOf(blockArray[0])>-1||AnfText.indexOf(blockArray[1])>-1||(AnfText=="Ihre Anfrage*")) {
		document.forms[theForm].Anfragetext.style.backgroundColor = "#f33";
		formError++;
	}
	
	if(lang == "en") {
		errorMsg = "Please fill out the fields marked in red correctly.";
	}
	else if(lang == "fr") {
		errorMsg = "Please fill out the fields marked in red correctly.";
	}
	else {
		errorMsg = unescape("Bitte füllen Sie die rot hinterlegten Felder korrekt aus.");
	}
	
	if(formError > 0) {
		alert(errorMsg);
	}
	else {
		updatePage(theAction,theForm);
	}		
}

function updatePage(theAction,theForm) {
	document.forms[theForm].action = theAction;
	document.forms[theForm].submit();
}

function popNewsletter(inout) {
    address = document.forms["optinout"].elements[0].value;
    if(address && (address!="Ihre eMail-Adresse")) {
        var url = "http://letters.popletter.com/synedra/optinout.php?inOut="+inout+"&address="+address;
        window.open(url,"optWin","width=300,height=180,top=50,left=100,scrollbars=no");
    }
    else {
        alert("Bitte geben Sie eine Adresse ein.");
    }
}

var hoverintentConfig = {
	sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
	interval: 100, // number = milliseconds for onMouseOver polling interval    
	over: hoverIn, // function = onMouseOver callback (REQUIRED)    
	timeout: 250, // number = milliseconds delay before onMouseOut    
	out: hoverOut // function = onMouseOut callback (REQUIRED)
};

function hoverIn() {
	$(this).children("div").fadeIn('30');
	$(this).children("a").css("color","#dc5034");
	
	if($.browser.msie && (parseInt($.browser.version, 10) < 8)) {	// z-index fix fuer fb likebox und cycle plugin
		$(this).parents().css("z-index","2000");
	}
}

function hoverOut() {
	$(this).children("div").fadeOut('40');
	$(this).children("a").not(".act").css("color","#fff");
}


$(document).ready(function(){

	$("#terminToggler").click(function() {
		$("li.hiddentermin").toggle();
		if($(this).html() == weitereTermine) {
			$(this).html(wenigerTermine);
		} else {
			$(this).html(weitereTermine);
		}
	});

	// navi hover

	if(!is_touch_device()) {		
		$('#navi1>li').hoverIntent(hoverintentConfig);
	}
	else {	
		$("#navi1>li").each(function() {
			if($(this).has("div").length) {		// nur jene mit subnavi
				$(this).children("a").attr("href","JavaScript:void(0);");	// link 1. ebene wegnehmen
			}
		});		
		
	
		$('#navi1>li>a').click(function() {
			$('#navi1>li>a').not(this).next().css("display","none");
			$(this).next().toggle();
		});
	}

	$(".level2box").each(function () {	
		setNaviboxWidth($(this));
	});

	$("div.leftmatrixdiv table:not(.dltable) tr:odd td").addClass("evenrow");


	// navi act:

	l1links = $("ul#navi1>li").children("a");		// nur level1

	for(n=0; n<l1links.length; n++) {
		if(($(l1links[n]).attr("href") == window.location.pathname) || (window.location.pathname == "/de/" && $(l1links[n]).attr("href") == "/de/index.php") || (window.location.pathname == "/en/" && $(l1links[n]).attr("href") == "/en/index.php") || (window.location.pathname == "/fr/" && $(l1links[n]).attr("href") == "/fr/index.php")) {
			$(l1links[n]).addClass("act");				
		}
	}
	
	l2links = $("ul.level2>li>a");			// von level2 aus
	
	for(p=0; p<l2links.length; p++) {
		if(($(l2links[p]).attr("href") == window.location.pathname)||($(l2links[p]).attr("href").indexOf("/kundenbereich/")>-1 && window.location.pathname.indexOf("/kundenbereich/")>-1)) {
			$(l2links[p]).addClass("activeclass");
			
			level1link = $(l2links[p]).parents("ul#navi1>li").children("a");
			$(level1link).addClass("act");				
			breadcrumb = '<a href='+$(level1link).attr("href")+'>'+$(level1link).html()+'</a>';
			
			breadcrumb += ' &gt; <a href='+$(l2links[p]).attr("href")+'>'+$(l2links[p]).html()+'</a>';			
			$("#breadcrumbbar").html(breadcrumb);		
		}	
	}

	l3links = $("ul.level2 ul a");			// von level3 aus
	
	for(q=0; q<l3links.length; q++) {
		if($(l3links[q]).attr("href") == window.location.pathname) {
			$(l3links[q]).addClass("activeclass");
			
			level1link = $(l3links[q]).parents("ul#navi1>li").children("a");	// level1
			$(level1link).addClass("act");	
			breadcrumb = '<a href='+$(level1link).attr("href")+'>'+$(level1link).html()+'</a>';
			
			level2link = $(l3links[q]).parent().parent().parent().children("a");	// level2
			$(level2link).addClass("activeclass");	
			
			breadcrumb += ' &gt; <a href='+$(level2link).attr("href")+'>'+$(level2link).html()+'</a>';
			breadcrumb += ' &gt; <a href='+$(l3links[q]).attr("href")+'>'+$(l3links[q]).html()+'</a>';
			
			$("#breadcrumbbar").html(breadcrumb);
		}
	}
	
	if(!is_touch_device()) {		
		$("#aimbutton").hover(
			function() {
				$(this).attr("src","/img/synedra_aim_button-"+jsLang+"-hover.png")
			},
			function() {
				$(this).attr("src","/img/synedra_aim_button-"+jsLang+".png")		
			}
		);
		
		$("#viewbutton").hover(
			function() {
				$(this).attr("src","/img/synedra_view_button-"+jsLang+"-hover.png")
			},
			function() {
				$(this).attr("src","/img/synedra_view_button-"+jsLang+".png")		
			}
		);		
	}
	
	$("#termine:has(li)").css("display","block");		// termine nur einblenden, wenn welche da sind
	$("#termine:has(li)").nextAll("hr.terminline").css("display","block");	// trennlinie einblenden
	$("#termine:not(:has(li))").nextUntil("div.newsinnerbox").next().css("padding-top","0px");		//
	
	
	$("a.faqquestion").click(function() {
		$(this).parent().children(".faqanswer").toggle();
	})

});
