$("document").ready(function() {

	// CUFON - font replacement

	Cufon.replace('.light h3', {
		textShadow: 'rgba(255,255,255,.5) 0px 1px 0px'
	});
	
	Cufon.replace('.glass h3', {
		textShadow: 'rgba(255,255,255,.5) 0px 1px 0px'
	});
	
	Cufon.replace('.dark h3', {
		textShadow: 'rgba(0,0,0,.5) 0px 1px 0px'
	});
	
	Cufon.replace('span.title');
	
	
	// Download vCard bubble

	$(".header a.vcard").hover(function() { //mouseover
		$(".header p.bubble").show();
		$(".header p.bubble").animate({
			top: "8px"
		}, 250 );
	}, function() { // mouseout
		$(".header p.bubble").animate({ 
			top: "0px"
		}, 250 );
		setTimeout("$('.header p.bubble').hide();", 250);
		$(".header p.bubble").animate({ 
			top: "0px"
		}, 250 );
	}).click(function(e){
		captcha = window.open("/vcard.php", "vCard Download", "width=318,height=129");
		captcha.focus();
		
		e.preventDefault();
	});
	
	
	// Tab System

	$('ul.nav').each(function() {
		var $links = $(this).find('a'),
			panelIds = $links.map(function() { return this.hash; }).get().join(","),
			$panels = $(panelIds),
			$panelWrapper = $panels.filter(':first').parent(),
			delay = 500;
			
		$panels.hide();

		$links.click(function() {
			var $link = $(this),
				link = (this);
				
			if ($link.is('.current')) {
				return;
			}
			
			_gaq.push(['_trackPageview', '/'+$link.attr("href")]);
		
			$links.removeClass('current');
			$link.addClass('current');
			
			$panels.animate({ opacity : 0 }, delay);
			$panelWrapper.animate({
				height: 0
			}, delay, function() {
				// var height = $panels.hide().filter(link.hash).show().css('opacity', 1).outerHeight();
				
				var filtered = $panels.hide().filter(link.hash).show().css('opacity', 1);
				if ($.browser.msie)
				  filtered.each(function() { this.style.removeAttribute('filter'); });
				var height = filtered.outerHeight();
							
				$panelWrapper.animate({
					height: height
				}, delay);
			});	
			
			return true;
		});
		
		var showtab = window.location.hash ? '[hash=' + window.location.hash + ']' : ':first';
		
		$links.filter(showtab).click();
	
	});
	
	
	
	// Contact Form
	
	$('#contactform').ajaxForm({
	   	target: '#error',
	   	beforeSubmit: function() {	
	   		$('#error p').remove();
			$('#error').append('<p class="loading">Sende Nachricht...</p>');
		},
		success: function() {
			$('#error p.loading').fadeOut();
			$('#error').fadeIn('slow');
		}
	});
	
	
	// Inline Links
	
	$('.main a[href^="#"]').click(function(){
		$('ul.nav li a[href="'+$(this).attr("href")+'"]').click();
	});
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))){
		$("a").ontouchstart = function(event){
			this.click();
			return false;
		}
	}
	
	// Simple Spam Detection
	$('input[name="spamdetection"]').val("0");
	
	
	/* Background Noise on the fly */
	function generateNoise(opacity) {
		if ( !!!document.createElement('canvas').getContext ) {
			return false;
		}

		var canvas = document.createElement("canvas"), ctx = canvas.getContext('2d'), x, y, r, g, b, opacity = opacity || .2;
		canvas.width = 100;
		canvas.height = 100;

		ctx = canvas.getContext("2d");

		for ( x = 0; x < canvas.width; x++ ) {
			for ( y = 0; y < canvas.height; y++ ) {
				r = Math.floor( Math.random() * 80 );
				g = Math.floor( Math.random() * 80 );
				b = Math.floor( Math.random() * 80 );

				ctx.fillStyle = "rgba(" + r + "," + g + "," + b + "," + opacity + ")";
				ctx.fillRect(x, y, 1, 1);
			}
		}

		document.body.style.backgroundImage = "url(" + canvas.toDataURL("image/png") + ")";
	}

	generateNoise(.25);
	
});
