(function($){

$(document).ready(function(){


	//
	$('input[name="dob-year"]').css({
			width:'190px',
			marginLeft:'214px'
	});
	
	$('input[name="dob-day"], input[name="dob-month"]').css({
		position : 'absolute', 
		top:'0', 
		left:'0'});
	$('input[name="dob-month"]').css({left:'107px'});


	
	//disable submit until filled
	$('#submit').css({opacity: '0.4'});  // .attr('disabled', 'disabled')
	
	//add the error messages & hide
	if ( /\/us\//.test(window.location) ) {
	
	} else if ( /\/ru\//.test(window.location) ) {
		$('<span id="country-error"><span />Please select<br /> your country</span><span id="year-error"><span />Please enter the<br /> year you were born</span>').appendTo('#age-entry-form').hide();
	} else if ( /\/fr\//.test(window.location) ) {
		$('<span id="country-error"><span />Veuillez sélectionner<br /> votre pays de résidence</span><span id="year-error"><span />Veuillez indiquer votre<br /> année de naissance</span>').appendTo('#age-entry-form').hide();
	} else {
		$('<span id="country-error"><span />Please select<br /> your country</span><span id="year-error"><span />Please enter the<br /> year you were born</span>').appendTo('#age-entry-form').hide();
	}//end if
	
	// VALIDATE : : only submit if fields are completed else prompt - aka validation ;/
	$('#submit').click(function(){
		// country and year not entered
		if ($('input#year').val() == '' && $('.select-clickr').text() == 'Select your Country') {
			//alert('Please enter your country and year of birth');
			$('#country-error, #year-error').fadeIn('fast', function(){
				$('#country-error span, #year-error span').animate({
					left: -16
				}, 100, function(){
					$('#country-error span, #year-error span').animate({
						left: -10
					}, 200);
				});
			});
			return false;
			
		// year not entered
		} else if ($('input#year').val() == '') {
			//alert('Please enter your year of birth');
			$('#year-error').fadeIn('fast', function(){
				$('#year-error span').animate({
					left: -16
				}, 100, function(){
					$('#year-error span').animate({
						left: -10
					}, 200);
				});
			});
			return false;
			
		//country not entered
		} else if ($('.select-clickr').text() == 'Select your Country') {
			//alert('Please select your country');
			$('#country-error').fadeIn('fast', function(){
				$('#country-error span').animate({
					left: -16
				}, 100, function(){
					$('#country-error span').animate({
						left: -10
					}, 200);
				});
			});
			return false;
			
		// all GOOD!
		}else {
			//go!
		}
	});//end of validate

		



	if (/\/us\//.test(window.location)) {
	
		$('input[name="dob-month"]').insertBefore('input[name="dob-day"]');
		$('input#year').css('width', '194px');
		
	} else {
	
		//hide day & month
	//	$('input[name="dob-day"], input[name="dob-month"]').hide();
	
	}
	
	
	
	//Blur remove background image if not empty 
	$('input[name="dob-day"], input[name="dob-month"]').blur(function(){
		if ($(this).val() !=='') {
			$(this).css({'background-image' : 'none'});
		} else {
		}
	});
	
	
	$('input#year').blur(function(){
		if ($(this).val() !=='') {
			$(this).css({'background-image' : 'none'});
		} else {
			$('#submit').css({opacity: '0.4'});
			$('#year-error').fadeIn('fast', function(){
				$('#year-error span').animate({
					left: -16
				}, 100, function(){
					$('#year-error span').animate({
						left: -10
					}, 200);
				});
			});
		}
		
		if ($('input#year').val() !== '' && $('.select-clickr').text() !== 'Select your Country') {
			$('#submit').attr('disabled', '').css({opacity: '1'});
			
			$('#country-error, #year-error').fadeOut('fast');
		}
	});	
	
	
	

	
	
		
	//add the select container 
	if ( /\/fr\//.test(window.location) ) {
		$('<div class="select-container"><div class="select-clickr" tabIndex="0">Sélectionnez</div><div id="country-wrap" class="jazzy-scrollr"></div></div>').insertBefore('select[name="country"]');	
	} else 	if ( /\/ru\//.test(window.location) ) {
		$('<div class="select-container"><div class="select-clickr" tabIndex="0">Выберите страну</div><div id="country-wrap" class="jazzy-scrollr"></div></div>').insertBefore('select[name="country"]');	
	} else {
		$('<div class="select-container"><div class="select-clickr" tabIndex="0">Select your Country</div><div id="country-wrap" class="jazzy-scrollr"></div></div>').insertBefore('select[name="country"]');
	}


    /*=== Custom Select Box ===*/
	$('select').each(function(){
			
		var name = $(this).attr('name');
		var selectId = $(this).attr('id');
		var selectClass = $(this).attr('class');
			
		// create a ul to place content in and wrap in containing div
		$('<ul id="'+name+'-replace" class="'+name+'"></ul>').appendTo('#country-wrap');

		//loop through option list and recreate as li's within the ul we created
		$('#country option').each(function(i){
			var text = $(this).text();
			var value = $(this).attr('value');
			// make the class represent the option value and replicated the text - then append to our select box
			$('<li tabIndex="'+i+'"><a href="#" class="'+value+'">'+text+'</a></li>').appendTo('#'+name+'-replace');	
						
		});
	});
	
	//add gradient spans
	$('<span class="grad top" /><span class="grad btm" />').appendTo('.select-container');
	
	
	/* keyboard navigation FUNK */
	/*
	$('.select-clickr')
	.focus(function(){
		$('#country-wrap').show();
		$('#country-replace li:first').trigger('focus');
	})
	.blur(function(){
		$('#country-wrap').hide();
	});
	$('#country-replace li').blur(function(){
		$('#country-wrap').hide();
	});
	*/
	
	
			
	$('#country-wrap, select[name="country"]').hide();
	
	$('.select-clickr').click(function(){
		$('#country-error').fadeOut('fast');
		$('#country-wrap, span.grad').show();
	});
	
	$('#country-wrap').mouseleave(function(){
		$('span.grad').fadeOut('fast');
		$(this).fadeOut('fast');
		if ($('.select-clickr').text() == 'Select your Country') {
			$('#country-error').fadeIn('fast', function(){
				$('#country-error span').animate({
					left: -16
				}, 100, function(){
					$('#country-error span').animate({
						left: -10
					}, 200);
				});
			});
		}
	});
	
	//change select / clickr and hide list on click 
	$('ul[id$=-replace] li a').click(function(){
			
		var value = $(this).attr('class');
		var optionText = $(this).text();
			
		$('select#country').attr('value', value);
 		$('.select-clickr').text(optionText);
		$('#country-wrap, span.grad').hide();
		
		if ($('input#year').val() !== '' && $('.select-clickr').text() !== 'Select your Country') {
			$('#submit').attr('disabled', '').css({opacity: '1'});
			$('#country-error, #year-error').fadeOut('fast');
			
		} else if ($('.select-clickr').text() !== 'Select your Country') {
			$('#country-error').fadeOut('fast');
		} else if ($('.select-clickr').text() == 'Select your Country') {
			$('#country-error').fadeIn('fast', function(){
				$('#country-error span').animate({
					left: -16
				}, 100, function(){
					$('#country-error span').animate({
						left: -10
					}, 200);
				});
			});
		}
		return false;
	});
	
	
		//Trigger country based on IP
		var inCountry = geoplugin_countryName();
		//alert(inCountry);	
		$('ul[id$=-replace] li a:contains('+inCountry+')').trigger('click');	

	

	/* === animate to show day and month fields if needed === */
    
    //when focused
    if (/\/us\//.test(window.location) == false) {
    
	$('input[name="dob-year"]').focus(function() {
	
		$('#year-error').fadeOut('fast');
	
		//check for characters & date every .5 seconds
		/*
		setInterval(function(){
				//if the value is greater than 1992 show day/month
				if ($('input[name="dob-year"]').val() == 1992)  {
					$('input[name="dob-year"]').animate({
						width:'190px',
						marginLeft:'214px'
						}, 400, function(){
							$('input[name="dob-day"], input[name="dob-month"]').css({position : 'absolute', top:'0', left:'0'});
							$('input[name="dob-month"]').css({left:'107px'});
							$('input[name="dob-day"], input[name="dob-month"]').fadeIn('fast', function(){
							//	j$('input[name="dob-day"]').focus();
							});
						});
				}
				//if the value is 1992 hide the day/month
				else {
						$('input[name="dob-day"], input[name="dob-month"]').delay(0).fadeOut('fast', function(){
							$('input[name="dob-year"]').stop().animate({
								width:'248px',
								marginLeft:'0px'
							});
						});
				}
				
				//if fields filled activate submit
				if ($('input#year').val() !== '' && $('.select-clickr').text() !== 'Select your Country') {
					$('#submit').attr('disabled', '').css({opacity: '1'});
			
					$('#country-error, #year-error').fadeOut('fast');
				}else {
					$('#submit').css({opacity: '0.4'});
				}		
				
		},600);//end setInterval
		*/
	});
	
	
	} //ends if
	
	// checkbox
	$('#remember').wrap('<div class="check" />').hide();
    $('div.check, label[for="remember"]').click(function(){
    	$(this).toggleClass('checked');
    	if ($('input[name="remember"]').is(':checked')){
    		$('input[name="remember"]').attr('checked', '');
    	} else {
    		$('input[name="remember"]').attr('checked', 'checked');
    	}
    });
    
    //jscrollpane
    $(function(){
	var api = $('.jazzy-scrollr').jScrollPane({
		showArrows: true,	
		autoReinitialise: true,
		autoReinitialiseDelay: 600,
		verticalDragMinHeight : 12,
		verticalDragMaxHeight : 12
	}).data('jsp');
	
	});
    
    
  
    
}); // doc rdy


$(window).load(function(){
		if ($('input#year').val() !=='') {
			$('input#year').css({'background-image' : 'none'});
		}
		

		
});
	
	

})(jQuery)

