/* Event listener for the document */
$(document).ready(function() {
	// Encrypt the emails span
	encrypt_email();
	
	// Load the twitter feed
	$("#twitterlist").getTwitter({
		userName: "aquasonic",
		numTweets: 5,
		loaderText: "Loading tweets...",
		slideIn: true,
		slideDuration: 750,
		showHeading: false,
		headingText: '',
		showProfileLink: false,
		showTimestamp: true
	});
	
	// Change the content
	$('a.nav').click(function(event) {
		// Change title
		try {
			$('title').html($(this).attr('title') + ' &laquo; aquasonic.ch');
		} catch (e) {}
		
		// Toggle the navigation
		$('a.active').removeClass('active');
		$(this).addClass('active');
						
		// Get current vars
		var oActive = $('div.active');
		var oNew = $('div#' + this.id.substring(0, this.id.indexOf('_')));
		
		// Animation
		oActive.animate({height: 'toggle', opacity: 0}, 500, '', function(){ // offene Items schließen, danach ...
			oNew.animate({height: 'toggle', opacity: 1}, 500);
		});
		
		// Set new classes
		oActive.removeClass('active');
		oNew.addClass('active');
		
		// Stop the link event
		event.preventDefault();
	});
});

/* Helper function to replace the email address in the span */
function encrypt_email() {
	var oCrypt = $('#email_crypt');
	var oPlain = $('#email_plain');

	if (oCrypt.length && oPlain.length) {
		var sEmail = encrypt('znvy@ndhnfbavp.pu');
		oCrypt.html(oCrypt.html().replace(/znvy@ndhnfbavp.pu/gi, sEmail));
		
		oCrypt.show();
		oPlain.hide();
	}
}

/* Helper method to encrypt a string */
function encrypt(sString) {
	return sString.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c<='Z'?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);
	});
}
