$(function() {
	$('input[type="text"].input, textarea.input').each(function() {
		var $this = $(this);
		$this.data('val', $this.attr('title'));
	});
	
	$('input[type="text"].input, textarea.input').focus(function() {
		var $this = $(this);
		if($this.val() == $this.data('val')) {
			$this.val('');
		}
	});
	
	$('input[type="text"].input, textarea.input').blur(function() {
		var $this = $(this);
		if($this.val() == '') {
			$this.val($this.data('val'));
		}
	});
	
	$('.question .answer').live('click', function() {
		var $this = $(this);
		$this.children(':radio').get(0).click();
		$this.siblings('label').removeClass('active');
		$this.addClass('active');
	});
	
	$('.question .answer :radio:checked').each(function() {
		var $this = $(this);
		$this.parents('.answer').addClass('active');
	});
	
	$(".question a").live('click', function() {
		var $this = $(this);
		var $par = $this.parents('.question');
		
		if(!$par.is('#complete') && !$par.is('#intro') && $(':radio:checked', $par).val() == undefined) {
			//alert('fail');
			return false;
		} else {
			$('.question').hide();
			$($this.attr('href')).show();
		}
		
		if($this.attr('href') == '#complete') {
			var $answers = $('#quiz').serializeArray();
			var $error = false; 
			$.each($answers, function() {
				if(this.value == 'false') $error++;
			});
			if($error) {
				$('#complete .success').hide();
				$('#complete .error').show();
				$('#complete .error .count').text(($answers.length-$error)+' ud af '+$answers.length);
			} else {
				$('#complete .success').show();
				$('#complete .error').hide();
			}
		}
	});
	
	$('select#quick').change(function() {
		var $this = $(this);
		window.location = $this.val();
	});
	
	$('#nav > li > a').bind('mouseenter', function() {
		var $this = $(this);
		$this.addClass('hover');
		$this.next('ul').addClass('hover');
	});
	
	$('#nav > li > a').bind('mouseleave', function() {
		var $this = $(this);
		$this.removeClass('hover');
		$this.next('ul').removeClass('hover');
	});
	
	$('#nav > li > ul').bind('mouseenter', function() {
		var $this = $(this);
		$this.addClass('hover');
		$this.prev('a').addClass('hover');
	});
	
	$('#nav > li > ul').bind('mouseleave', function() {
		var $this = $(this);
		$this.removeClass('hover');
		$this.prev('a').removeClass('hover');
	});
	
	$('#nav li ul').each(function() {
		var $this = $(this);
		var $last = $('a span', $this.children('li:last'));
		if($last.text() != 'Video') {
			$this.css({left: '-190px'});
		}
	});
	
	$('.news a').live('click', function() {
		var $this = $(this);
		$('div.news').hide();
		$($this.attr('href')).show();
	});
	
	$hash = window.location.hash.toString();
	if($hash.indexOf('news') > -1) {
		$('.news a[href='+$hash+']').click();
	}
});