	$(document).ready(function(){
		// Change form skin	
		$('form').jqTransform({imgPath:'/agescreenerjqtransformplugin/img/'});	

		$("#birth-year, #birth-month, #birth-day").change(function(){
			$("#agescreener_dob").val($("#birth-year").val() + "-" + $("#birth-month").val() + "-" + $("#birth-day").val());
		});
		$("#birth-year, #birth-month, #birth-day").each(function() {
			var value = $(this).val();
			$(this).attr('rel', value);
		});
		$("#birth-year, #birth-month, #birth-day").focus(function() {
			
			var value = $(this).attr('rel');
			//if we focus, clear val if we have a rel attrib
			if(value) {
				$(this).val('');
			}
			
		
		}).blur(function() {
			
			var value = $(this).attr('rel');
		
			//if we do have a rel attribute then we need to return it.
			if(value) {
				 $(this).val(value);				
			}			
			
		}).keypress(function() {
			//clear rel attribute
			$(this).attr('rel', '');
		});
		
		$("#birth-year").keyup(function() {
			
			var count = $(this).val().length;			
			if(count == 4)
			{
				$("#birth-month").trigger('focus');
			}
		});
		$("#birth-month").keyup(function() {
			var count = $(this).val().length;
			if(count == 2)
			{
				$("#birth-day").trigger('focus');
			}
		});
		
		// Switch background image
		$('#birth-year').blur(function() {
				var d=new Date();
				var birthDate = parseInt($("#birth-year").val());
				var currentYear = d.getFullYear();
				var age = currentYear - birthDate;
				
				if (age >=18 && age < 25) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-18y.png)");
				}
				else if(age >=25 && age < 31) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-25y.png)");
				}
				else if(age >=31 && age < 35) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-31y.png)");
				}
				else if(age >=35 && age < 41) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-35y.png)");
				}
				else if(age >=41 && age < 45) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-41y.png)");
				}
				else if(age >=45 && age < 51) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-45y.png)");
				}
				else if(age >=51 && age < 55) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-51y.png)");
				}
				else if(age >=55 && age < 60) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-55y.png)");
				}
				else if(age >=60) {
					$(".elephant").css("background-image", "url(agescreener/images/bg-elephant-60y.png)");
				}
		});
		
		
		if (navigator.geolocation) {
			 navigator.geolocation.getCurrentPosition(function (position) {
				 var geocoder = new google.maps.Geocoder();
				 if (geocoder) {
				      geocoder.geocode({ 'location':new google.maps.LatLng(position.coords.latitude, position.coords.longitude)}, function (results, status) {
				         if (status == google.maps.GeocoderStatus.OK) {
				           $.each(results[0].address_components, function(id, part) {
				        	  
				        	   if($.inArray('country',part.types) != -1) {				        		   
				        		   var option = $('#country_transform ul a[rel="' + part.short_name + '"]');				        		   
									if(option.length > 0) {				
										option.trigger('click');
									}
				        	   }
				        	   
				        	   
				           });
				         }
				      });
				   } 				
					
				}, {				
				timeout: (5 * 1000),
				maximumAge: (1000 * 60 * 15),
				enableHighAccuracy: true
			});
		}
});
