$(document).ready(function(){
	// Open external link in a new window
	$('a[rel="external"]').click(function(clickEvent){
		clickEvent.preventDefault();
		window.open(this.href);		
	});
	
	// Do some Analytics Tricks
	$('a[rel="analytics"]').click(function(clickEvent){
		clickEvent.preventDefault();
		
		pageTracker._link(this.href);
	});
	
	// Decode base64 "mailto"
	$('a[rel="mailto"]').each(function(){
		var _base64 = $(this).text();
		$(this).attr('href', 'mailto:' + $(this).text($.base64Decode(_base64)).text());
	});	
	
	$('form').each(function() {
		// Fix IE <button> bug
		$(this).ieFixButtons(); 
		
		// Search any birthdate input
		$(this).find('input#d_birthdate').each(function(){
			$(this).datepicker({ yearRange: '-90:+0', maxDate: '-18Y', changeYear: true, changeMonth: true, gotoCurrent: true, dateFormat: 'yy-mm-dd' });			
		});

		// Display sample in forms
		className = 'hasExample';
		
		$(this).find('input[type="text"][title],textarea[title]').each(function(){
			$(this).focus(function(eventFocus){
				if($(this).hasClass(className)) {					
					$(this).val('');
					$(this).removeClass(className);
				}
			});
				
			$(this).blur(function(eventBlur){				
				if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			});
			
			$(this).change(function(eventChange){
				if ($(this).val() != '' && $(this).val() != $(this).attr('title')) {
					$(this).removeClass(className);
				}
			});
					
			if($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				if(!$(this).hasClass(className)) {
					$(this).addClass(className);
					$(this).val($(this).attr('title'));
				}
			}
		});
		
		// "a" html entity to trigger form submission
		$(this).find('div.buttons a:not([rel="external"]), div.buttons button').click(function(e){		
			e.preventDefault();
			
			// Remove any sample values for the form
			$('.' + className, $(this).parents('form')).val('');

			// Set the 'rel' of the link to the hidden 'input[action]'
			$(this).parents('form').first().find('input[name="action"]').attr('value', $(this).attr('rel'));
			
			// Submit the parent form
			$(this).parents('form').trigger('submit');
		});
	});
	
	// Animations
	if ($.support.opacity) {
		$('body#index #header .link-home').fadeTo(2000, 1.0);
	}
});
