function biggerLink(element) {
	if (strpos(window.location.pathname,'weblication')>0) { // wir sind im backend - lass es
	} else {
		if (element.length>0) {
			jQuery(element).each(function() {
				var linkto = jQuery(this).find("a:first").attr('href');
				jQuery(this).css({cursor:'pointer'});
				jQuery(this).click(function(){
					document.location = linkto;
				});
			});
		}
	}
}

function Navigation() {
	jQuery('li.active').parents('li').addClass('active');
	jQuery("ul.menulist ul").hover(
		function(){
			$mainliclass = jQ(this).parents('li:eq(0)').attr('class');
			$topli = jQ(this).parents('li:eq(0)');
			$topa = jQ(this).parents('li:eq(0)').find('a:eq(0)');
			if(false === strpos($mainliclass,'active')) {
				$topli.addClass('active');
				$topa.addClass('active');
			} else {
				$topli.addClass('hover')
				$topa.addClass('hover');
			}
			// alert($topli);
		},
		function(){
			$mainliclass = jQ(this).parents('li:eq(0)').attr('class');
			$topli = jQ(this).parents('li:eq(0)');
			$topa = jQ(this).parents('li:eq(0)').find('a:eq(0)');
			if(false === strpos($mainliclass,'hover')) {
				$topli.removeClass('active');
				$topa.removeClass('active');
			} else {
				$topli.removeClass('hover');
				$topa.removeClass('hover');
			}
		}
	);
}

function strpos (haystack, needle, offset) {
	var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
	return i === -1 ? false : i;
}


function count (mixed_var, mode) {
	var key, cnt = 0;
	if (mixed_var === null){
		return 0;
	} else if (mixed_var.constructor !== Array && mixed_var.constructor !== Object){
		return 1;
	}
	if (mode === 'COUNT_RECURSIVE') {
		mode = 1;
	}
	if (mode != 1) {
		mode = 0;
	}
	for (key in mixed_var){
		cnt++;
		if ( mode==1 && mixed_var[key] && (mixed_var[key].constructor === Array || mixed_var[key].constructor === Object) ){
			cnt += this.count(mixed_var[key], 1);
		}
	}
	return cnt;
}


function validateEmail() {
	jQ("#subscribe2newsletter-form :submit").click(function(){
		var eml = jQ("#subscribe2newsletter-form input[name=eml]").getValue();
		if (eml == 'E-Mail Adresse') {
			// default Vorgabe
			jQ("#subscribe2newsletter-form input[name=eml]").setValue("").focus();
		} else {
			if (isemail(eml)) {
				// console.log(eml);
				jQ("#subscribe2newsletter-form").submit();
			} else {
				jQ("#subscribe2newsletter-form input[name=eml]").focus();
				return false;
			}
		}
	});
}

function isemail(email) {
	// from Validate Plugin for jQuery
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
}

function textareaPopulate() {
	var jQ = jQuery;
	var txtarea = jQ("textarea[name=Mitteilung]");
	txtarea.focus(function(){
		var txtcontent = jQ.trim(txtarea.getValue());
		if ('' == txtcontent) {
			jQ(this).setValue('');
		}
	});
}

function lightboxCloseDiv() {
	var jQ = jQuery;
	var boxwidth = jQ("#outerImageContainer").width()-30;
	var boxheight = jQ("#outerImageContainer").height()-10;
	jQ("#lightboxIframe").css({width:(boxwidth-20)});
	jQ("#lightboxIframe").css({height:(boxheight-40)});
}

function topLink() {
	var jQ = jQuery;
	// Scroll internal Anchors - requires ScrollTo.js
	jQ('a[href*="#"]:not(a[href="#"])').click(function() {
		var parts        = this.href.split('#');
		var scrolltarget = '#' + parts[1];
		jQ(scrolltarget).ScrollTo(700);
		return false;
	});
}
function printLink () {
	var jQ = jQuery;
	jQ("#toprinter").click(function(){
		window.print();
		return false;
	});
}
function imageCaptions() {
	var jQ = jQuery;
	var captions = jQ("div.image-with-caption");
	
	// only if there are images with captions
	if (captions.length>0) {
		captions.each(function(){
			// important grab img dimensions after it's loaded
			the_image = jQ(this).find("img");
			the_image.load(function(){
				the_width = the_image.width();
				the_height = the_image.height();
				jQ(this).attr({
					width:the_width,
					height:the_height
				}).parents("div:eq(0)").css({
					width:the_width+'px',
					'margin-left':'auto',
					'margin-right':'auto'
				});
			});
		});
	}
}

jQuery.getPos = function (e) {
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e) {
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) {
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};
jQuery.intval = function (v) {
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};
jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};
jQuery.fx.ScrollTo = function (e, o) {
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};

var jQ = jQuery;
jQ(document).ready(function(){
	jQuery("a[rel=lightbox]").click(function(){
		return false;
	});
	imageCaptions();
	printLink();
	topLink();
	lightboxCloseDiv();
	textareaPopulate();
	validateEmail();
	Navigation();
	biggerLink('div.linkteaser');
	// jQuery('textarea').autogrow({min_height:'150'});
});