$(document).ready( function() {
	
	// cooper
	Cufon.replace('h1, h2, h3:not(.boxes), h4, h5, h6, span.heading, #stats');
	Cufon.replace('#navigation a', { hover: true } );
	
	$('a.mailto').click( function() {
		$(this).attr('href', 'mailto:' + $(this).attr('title').replace(/\[at\]/, '@'));
	} );
	
	$('a.external').click( function() {
		$(this).attr('target', 'external');
	} );
	
	$('a.button').wrapInner('<span>');
	
	$('input').customInput();
	
	if($('.login #email').length) 
		$('.login #email').focus();
	
	// --------------------
	
	$('form a').live('click', function() {
		
		if($(this).attr('rel')) {
			$(this).parents('form').append('<input type="hidden" name="' + $(this).attr('rel') + '" value="1" />');
			$(this).parents('form').submit();
		}
		
	} );
	
	// --------------------
	
	$('td.add span, td.add a').click( function() {
		
		ScrollPosition.setTop();
		
		var mode = ($(this).hasClass('remove')) ? 'remove' : 'add';
		
		$('#lijst form').append('<input type="hidden" name="' + mode + '" value="' + $(this).parents('td').attr('rel') + '" />');
		$('#lijst form').submit();
		
		$('td.add span, td.add a').remove();
		
		return false;
		
	} );
	
	// --------------------
	
	$('#lijst tr:nth-child(odd)').addClass('odd');
	$('#lijst tr:nth-child(even)').addClass('even');
	
	$('table tr').each( function() {
		
		$(this).children().each( function() {
			
			var index = $(this).parent().children().index(this);
			
			if(index > 2) 
				$(this).addClass('opaque');
			
		} );
		
	} );
	
	// --------------------
	
	$('#content th.tip').tooltip( {
		top: 20,
		left: 10,
		track: true, 
		delay: 250, 
		bodyHandler: function() {
			return $(this).find('div.tip-content').html();
		}
	} );
	
	// --------------------
	
	$('textarea[maxlength]').bind('keydown keyup', function() {
		
		var max = parseInt($(this).attr('maxlength'));
		
		if($(this).val().length > max) 
			$(this).val($(this).val().substr(0, $(this).attr('maxlength')));
		
	} );
	
	// --------------------
	
	/*
	$('a.conditions').click( function() {
		
		alert('Bierlijst.nl voorwaarden en privacy:\n\n- Deelnemers aan Bierlijst.nl moeten 18 jaar of ouder zijn\n- Aan het gebruik van Bierlijst.nl kunnen geen rechten worden ontleend\n- Bierlijst.nl privacy volgens geldende privacywetgeving\n- E-mailadressen worden nooit aan derden verstrekt!\n- Fair Use Policy! ;-)');
		
		return false;
		
	} );
	*/
	
	// --------------------
	
	$('.calculate').click( function() {
		
		if($('#amount').val().match(/^[1-9]([0-9]+)?$/) && $('#price_unit').val().match(/^[0-9]+(\.[0-9]{2})?$/)) {
			
			var total = $('#amount').val() * $('#price_unit').val();
				total = total.toFixed(2);
			
			$('.price').html(total);
			$('#price').val(total);
			
		}
		else {
			
			$('.price').html('0.00');
			$('#price').val('');
			
		}
		
	} );
	
	// --------------------
	
	$('.popup').live('click', function() {
		
		var page = ($(this).hasClass('conditions')) ? 'voorwaarden_privacy.htm' : 'welkom.htm'; // tmp
		
		$('#dimmed_overlay').show();
		
		$('#popup_overlay').fadeIn('slow', function() {
			
			$('#popup .body').addClass('loading');
			
			$.ajax( {
				url: '/ajax/' + page,
				cache: false,
				success: function(html) {
					
					if(page == 'welkom.htm')  // tmp
						$('#popup a.close').hide();
					
					$('#popup .body').removeClass('loading').append(html);
					
					$('#popup .body a.button').wrapInner('<span>');
					$('#popup .body input').customInput();
					
					Cufon.refresh();
					
				}
			} );
			
		} ).css( { top: $(window).scrollTop() + 80 } );
		
		return false;
		
	} );
	
	$('#popup .close').live('click', function() {
		
		$('#popup_overlay').fadeOut('slow', function() {
			
			$('#popup .body > *').remove();
			
			$('#dimmed_overlay').hide();
			
		} );
		
	} );
	
	// --------------------
	
	if($('#lijst .sponsors')[0] && !$.cookie('sponsors')) {
		
		$('#lijst .sponsors').show('slow');
		
	}
	
	$('#lijst .sponsors .close').click( function() {
		
		$('#lijst .sponsors').hide('slow');
		
		$.cookie('sponsors', 1, { expires: 0.5 } );
		
	} );
	
	// --------------------
	
	var ScrollPosition = {
		
		init: function() {
			
			if($('#scroll_position').length && $('#scroll_position').val()) {
				$(window).scrollTop(
					$('#scroll_position').val()
				);
			}
			
		},
		
		setTop: function() {
			
			$('#scroll_position').val(
				$(window).scrollTop()
			);
			
		}
		
	};
	
	ScrollPosition.init();
	
} );
