$(document).ready(function(){

//open link in new window	
	$("a[rel='external']").click(function(){
		window.open($(this).attr('href'));
		return false;
	});

//tooltips	
	$('.info_tooltip').tooltip({
			delay:0,
			showURL: false,
			fade:250,
			top:-5,
			left:7
	});
	
	initStartDateStatistics();	
	
//statistics	
	$(".form_input input, .result_input input").blur(function(){
		if(statistics_id!=''){
			if($(this).attr('value')!=''){				
				postparams = $(this).attr('name')+'=1&data[Registration][id]='+statistics_id;

				$.ajax({
					url: webpath+"statistics/update",
					type: "POST",
					data: postparams
				});		

			}
			else if($(this).attr('value')==''){
				//if value is empty, change value in database?
			}
		}
	});

//back button	
	$(".step_back_button").click(function(){
		history.go(-1);
	});
	

//flash header embed	
	$('#header_animation').flash({ 
		src:webpath+'files/animation.swf',
		width:732,
		height:294
	 });


//datepicker	
	$(".datepicker").datepicker({
		dateFormat: "dd-mm-yy",
		buttonText: "Kies een datum",
		showOn: "button",
		buttonImage: webpath+"img/button_small_calendar.gif",
		buttonImageOnly: true,
		onSelect: updateStartdate,
		beforeShow: readStartdate,
		minDate:'+1d'
	}); 
});


//function to read the current date from 3 seperate fields (for datepicker)
function readStartdate(){
	$(".datepicker").val($('.startdate_day').val()+'-'+$('.startdate_month').val()+'-'+$('.startdate_year').val());
	return {};
}

//function to update 3 seperate fields with the value generated by datepicker
function updateStartdate(date) {
  $('.startdate_day').val(date.substring(0, 2));  
  $('.startdate_month').val(date.substring(3, 5));  
  $('.startdate_year').val(date.substring(6, 10));  
}


//function to initialize startdate statistics (because it has the date of tomorrow as standard)
function initStartDateStatistics(){
	if($("input[name='data[Registration][startdate_day]']").attr('value')!='' && statistics_id!=''){
		postparams = 'data[Registration][startdate_day]=1&data[Registration][id]='+statistics_id;
		$.ajax({
			url: webpath+"statistics/update",
			type: "POST",
			data: postparams
		});
	}
	if($("input[name='data[Registration][startdate_month]']").attr('value')!=''  && statistics_id!=''){
		postparams = 'data[Registration][startdate_month]=1&data[Registration][id]='+statistics_id;
		$.ajax({
			url: webpath+"statistics/update",
			type: "POST",
			data: postparams
		});
	}
	if($("input[name='data[Registration][startdate_year]']").attr('value')!=''  && statistics_id!=''){
		postparams = 'data[Registration][startdate_year]=1&data[Registration][id]='+statistics_id;
		$.ajax({
			url: webpath+"statistics/update",
			type: "POST",
			data: postparams
		});
	}
}