// Typo3 Stuff
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-1);}


function messages() {
	this.lang = null;
	var meta = document.getElementsByTagName("meta");
	for (i = 0; i < meta.length; i++) {
		if (meta[i].getAttribute("name") == "language") {
			this.lang = meta[i].getAttribute("content");
		}
	}

	this.error = new Object();

	switch (this.lang) {
		default:
		case "de":
			this.error["required"] = "Bitte füllen Sie alle Pflichtfelder aus.";
			break;
	}
}

var messages = new messages();

function trackEvent(category, action, label, value) {
	//console.log("trackEvent(category,action,label,value): \n" + category + ", " + action + ", " + label + ", " + value);
	if(_gaq) {
		_gaq.push(['_trackEvent', category, action, label, value]);
	}
}
function trackPageview(category, section, path) {
	var completePath = category + ':' + window.location.pathname + '#' + section + ' > ' + path;
	//console.log(completePath);
	if(_gaq) {
		_gaq.push(['_trackPageview', completePath]);
	}
}
function trackLink(link) {
	var internal = (link.host == window.location.host);
	var path = internal ? link.pathname : link.href;
	var parentId = $(link).parents('[id]').first().attr('id');
	if (!parentId) {
		parentId = 'unknown';
	}
	trackPageview(internal ? 'internal' : 'external', parentId, path);
}

// jQuery Init
jQuery(document).ready(function() {

	$("img:not(#logo img)").lazyload({
		threshold : 100,
		failurelimit : 20,
		event : "scroll",
		effect : "fadeIn",
		container : window
	});

	$("#head:has(#scrollableimages .items div+div)").append('<div class="navi"></div>');
	$("body:has(#scrollableimages .items div+div)").append('<a class="prev browse left"></a><a class="next browse right"></a>');
	$(".next, .prev").css( {
		cursor : 'pointer'
	})
	$(".scrollable").scrollable( {
		activeClass: 'active',
		circular: true,
		clonedClass: 'cloned',
		disabledClass: 'disabled',
		easing: 'swing',
		initialIndex: 0,
		item: null,
		items: '.items',
		keyboard: false,
		mousewheel: false,
		next: '.next',
		prev: '.prev',
		speed: 400,
		vertical: false,
		wheelSpeed: 0,
		//lazyload: true,
		onSeek: function(event,id) {
			elem = event.target.getRoot();
			if(elem.attr('id') == 'scrollableimages') {
				trackEvent('Stage seek','Stage: ' + id,window.location.pathname.pathname,0);
			}
			$(window).scroll();
		}
	}).navigator({
		navi: '.navi',
		naviItem: 'a',
		activeClass: 'active',
		indexed: false,
		idPrefix: null,
		history: false
	}).autoscroll({
		autoplay: false,
		interval: 3000,
		autopause: true
	});

	$("div.postit:has(a),div.displayswitch ul li:has(a),div.layout10 div.csc-textpic-imagewrap").click(function(o) {
		var link = null;
		var href = null;
		var target = null;

		try {
			link = $("a:first",this).get(0);
			href = link.href;
			target = $(link).attr("target")
		}
		catch (e) {}

		if (href) {
			if (target) {
				window.open(href);
			}
			else {
				window.location.href = href;
			}
			trackLink(link);
			return false;
		}
	}).hover(function() {
			$(this).toggleClass("hover");
	});

	$("a.switch").click(function() {
		if($(this).hasClass("swap")) {
			$(this).removeClass("swap");
			$("div.displayswitch ul").fadeOut("fast", function() {
					$(this).fadeIn("fast").removeClass("thumbs");
					$(window).scroll();
			});
			trackEvent('List switch','List expanded',window.location.pathname,0);
		}
		else {
			$(this).addClass("swap");
			$("div.displayswitch ul").fadeOut("fast", function() {
					$(this).fadeIn("fast").addClass("thumbs");
					$(window).scroll();
			});
			trackEvent('List switch','List collapsed',window.location.pathname,0);
		}
		return false;
	});

	$("a[target=popup]").fancybox({
		padding				:	10,
		imageScale			:	true,
		zoomOpacity			:	true,
		zoomSpeedIn			:	0,
		zoomSpeedOut		:	0,
		zoomSpeedChange		:	300,
		easingIn			:	'swing',
		easingOut			:	'swing',
		easingChange		:	'swing',
		frameWidth			:	560,
		frameHeight			:	340,
		overlayShow			:	true,
		overlayOpacity		:	0.8,
		overlayColor		:	'#000',
		enableEscapeButton	:	true,
		showCloseButton		:	true,
		hideOnOverlayClick	:	true,
		hideOnContentClick	:	true,
		centerOnScroll		:	true,
		itemArray			:	[],
		callbackOnStart		:	null,
		callbackOnShow		:	function() {
			trackEvent('Fancybox','Show Fancybox',window.location.pathname,0);
		},
		callbackOnClose		:	null
	});

	/* FORMS */
	$("#page form").submit(function() {
		var errors = 0;
		$("input.required, textarea.required, checkbox.required").each(function() {
			if(!$(this).val()) {
				showFormError(messages.error["required"]);
				$(this).addClass("requiredEmpty").one('focus', function() {
					$(this).removeClass("requiredEmpty");
				});
				errors++;
				trackEvent('Form error','Field empty: ' + $('label[for=' + this.id + ']').text(),window.location.pathname,0);
			}
		})
		if(errors > 0) return false;
	});
	function showFormError(text) {
		if($("p.error").size() == 0) {
			$("fieldset div:last").before('<p class="error">' + text + '</p>');
			$("p.error").slideDown();
		}
	}

	$('a').click(function() {
		if(this.host && this.pathname) {
			trackLink(this);
			return true;
		}
	})

});

