var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

function decode(encStr) {
	var bits;
	var decOut = '';
	var i = 0;
	for(; i<encStr.length; i += 4) {
		bits = (base64s.indexOf(encStr.charAt(i))    & 0xff) <<18 |
			 (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 |
			 (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
			  base64s.indexOf(encStr.charAt(i +3)) & 0xff;
		decOut += String.fromCharCode((bits & 0xff0000) >>16, 
		(bits & 0xff00) >>8, bits & 0xff);
		}
	if(encStr.charCodeAt(i -2) == 61) { return(decOut.substring(0, decOut.length -2)); }
	else if(encStr.charCodeAt(i -1) == 61) { return(decOut.substring(0, decOut.length -1));	}
	else {return(decOut)};
	}


/*function tiodtsuj(a,text) { var text=text || 'NULL'; var x=decode(a);temp=x.split('###');document.write('<a href="mai');document.write('lto:'+temp[1]);document.write('@');document.write(temp[0]+'">'); if (text=="NULL") { document.write(temp[1]);document.write('@');document.write(temp[0]); } else document.write(text); document.write('</a>');} 	*/
function tiodtsuj(a) { var x=decode(a);temp=x.split('###');document.write('<a href="mai');document.write('lto:'+temp[1]);document.write('@');document.write(temp[0]+'">');document.write(temp[1]);document.write('@');document.write(temp[0]);document.write('</a>');} 	

function tiodtsuj2(a,b) { var x=decode(a);temp=x.split('###');document.write('<a href="mai');document.write('lto:'+temp[1]);document.write('@');document.write(temp[0]+'">');document.write(b);document.write('</a>');} 

function tiodtsuj3(a) { var x=decode(a);temp=x.split('###');document.write(temp[1]);document.write('@');document.write(temp[0]);} 	

function trimString( s)
{
	var i = 0;
	var l = s.length;
	while
	(
		i < l
		&&
		s.charAt( i) == ' '
		||
		s.charAt( i) == '\n'
		||
		s.charAt( i) == '\r'
		||
		s.charAt( i) == '\t'
		||
		s.charAt( i) == '\f'
	)
	{
		i++;
	}
	if (i >= l)
	{
		return ("");
	}
	do
	{
		l--;
	}
	while
	(
		s.charAt( l) == ' '
		||
		s.charAt( l) == '\n'
		||
		s.charAt( l) == '\r'
		||
		s.charAt( l) == '\t'
		||
		s.charAt( l) == '\f'
	);
	return (s.substring( i, l + 1));
}

function ismail( addr, txto) {

	var chkEmail = true;
	var i, len, atpos, dotpos;

	// trim
	addr = trimString( addr.toLowerCase());
	len = addr.length;
	
	// strip out any blank
	i = 0;
	while (i < len) {
		if (addr.charAt( i) == ' ') {
			addr = addr.substring( 0, i) + addr.substring( i + 1, addr.length);
			--len;
		} else {
			++i;
		}
	}

	if (
		// a@bcd.ef should be the shortest an address could be
		len < 8 ||
		// exactly one "@" after at least one user name characters
		((atpos = addr.indexOf( "@")) < 1 || addr.indexOf( "@", atpos + 1) > atpos) ||
		// no "_" after the "@"
		addr.indexOf( "_") > atpos ||
		// at least one "." after at least three characters
		(dotpos = addr.lastIndexOf( ".")) < 3 ||
		// at least 2 but no more than 3 characters for tld
		((len - (dotpos + 1)) < 2 || (len - (dotpos + 1)) > 6) ||
		// no two dots in succession
		addr.indexOf( "..") >= 0
	) {
		chkEmail = false;
	} else {
		// check if domain name is at least 2 characters long
//		var username = addr.substring( 0, atpos);
		var host_domain = addr.substring( atpos + 1, len).split( /\.+/);
		if (host_domain[host_domain.length - 2].length < 2) {
			chkEmail = false;
		} else {
			// check each character for validity
			var _a = "az09".charCodeAt( 0);
			var _z = "az09".charCodeAt( 1);
			var _0 = "az09".charCodeAt( 2);
			var _9 = "az09".charCodeAt( 3);
			for (i = 0; i < len; i++) {
				var c = addr.charAt( i);
				var cc = addr.charCodeAt( i);
				if (
					(cc < _a || cc > _z) &&
					c != '_' &&
					c != '.' &&
					c != '@' &&
					c != '-'
				) {
					if (i < dotpos && cc >= 0 && cc <= _9) {
						continue;
					}
					chkEmail = false;
					break;
				}
			}
		}
	}
	
	if (chkEmail && txto != null) {
		txto.value = addr;
	}

	return (chkEmail);
}

function fade(step,which) {
	var myimg = document.getElementById(which);

	step = step || 0;

	myimg.style.opacity = step/100;
	myimg.style.filter = "alpha(opacity=" + step + ")"; // IE

	step = step + 2;

	if (step <= 100) {
		window.setTimeout(function () { fade(step, which); }, 50);
		}
	}

jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};

$(document).ready(function(){ 	

	$('a[rel=openfirstgallery]').click(function() {
		$("a[name=firstgalleryfirstimage]").click();
		return false;
		});

	window.setTimeout(function () { $("#second").fadeIn(4000); },4000);

	/*$("#hauptmenu li, #submenu li, #subsubmenu li, #stage-welcome .item, #welcome-quicklinks .quick-item, .button-link, #cats .category, #years li, #info .item").hover(function() {
		 $(this).addClass("hover");
	   },function(){
		 $(this).removeClass("hover");
	   });


	$("form.suchformular input").focus(function() { this.select(); })


	$("#projektliste .item").click(function() {
		$("#projektliste .file-container").slideUp('fast', function() {
			});
		$("#projektliste .item").removeClass('open');
		$(this).next(".file-container").slideDown('fast');
		$(this).addClass('open');
		});



	$('.button-link.external').click(function(){
		window.open( $(this).attr('href') , "Berthold" );
		return false;
		});
	
	$("#years li").click(function() {
		$("#years li").removeClass("active");
		$(this).addClass("active");
		$("#refyearfilter").val($(this).attr('id'));
		getRefs();
		});
		
	$(".category input").click(function() {
		$(".category").removeClass("active");
		$(this).closest(".category").addClass("active");
		$("#refkatfilter").val($(this).val());
		getRefs();
		});
	
	$('input[title], textarea[title]').each(function() {
		if ($(this).attr("id")!="email"	) {												 
			if($(this).val() === '') {
				$(this).val($(this).attr('title')); 
				}
			$(this).focus(function() {
				if($(this).val() === $(this).attr('title')) {
					$(this).val('').addClass('focused'); 
					}
				});
			$(this).blur(function() {
				if($(this).val() === '') {
					$(this).val($(this).attr('title')); 
					// $(this).val($(this).attr('title')).removeClass('focused'); 
					}
				});
			}
		});
	*/
	
	
    }); 




