$(function(){
		   
	//populate current year (footer - copyright)
	$('.current-year').text((new Date).getFullYear());
	
	//hide noscript (ie)
	$('noscript').hide();
	
	//initiate all new window links
	$('.new-win').click(function(){
		$(this).attr({ 'target' : '_blank' });
	});		   

	//preload images
	var imgDir = 'images/';
	$.preloadImages(
		imgDir + 'bg-index.gif',
		imgDir + 'bg-promotions.png',
		imgDir + 'bg-sorry.png',
		imgDir + 'bg-subpage.gif',
		imgDir + 'card-deck.gif',
		imgDir + 'events-header.gif',
		imgDir + 'logo-pinup-poker.png',
		imgDir + 'logo-schick-hydro-lg.png',
		imgDir + 'pinup-girl.png',
		imgDir + 'prize-hydro-5-razor.png',
		imgDir + 'prize-hydro-5-refill.png',
		imgDir + 'prize-hydro-shave-bag.png',
		imgDir + 'prize-hydro-shave-gel.png',
		imgDir + 'rules-header.gif'

	);
	
	//ie only (all versions)
	if($.browser.msie){
		var settings = { tl: { radius: 18 }, tr: { radius: 18 }, bl: { radius: 18 }, br: { radius: 18 }, antiAlias: true };
		if($('.rounded').length){ curvyCorners(settings, ".rounded") };
	}
	
	//ie6 only
	if($.browser.msie && $.browser.version.substr(0, 1) < 7){
		$(document).pngFix(); //transparent png fix
		$('input:text').attr({ 'class' : 'txt' }); //add a class to text fields since input[type="text"] is not supported by ie6
		//$('input:radio').attr({ 'class' : 'rad' }); //add a class to radio buttons since input[type="radio"] is not supported by ie6
		$('input:radio').css({ 'float' : 'left' });
		$('input:checkbox').css({ 'float' : 'left' });
	}
	
	//registration form nested labels
	if($('label.nested').length){ $('label.nested').inFieldLabels() };
	
	//events shadow box
	if($('.events-link').length){
		$(".events-link").colorbox({
			'innerWidth'	:'520px',
			'innerHeight'	:'380px',
			'width'		:'540px', 
			'height'	:'440px',
			'scrolling'	:'false',
			'inline'	:'true', 
			'href'		:'#events-wrapper'
		});	
	}
	
	//events shadow box
	if($('.btn-rules-regs').length){
		$(".btn-rules-regs").colorbox({
			'innerWidth'	:'520px',
			'innerHeight'	:'640px',
			'width'		:'540px', 
			'height'	:'680px',
			'scrolling'	:'false',
			'inline'	:'true', 
			'href'		:'#rules-wrapper'
		});		
	}
	
	/*****************************************************************************************************************************/
	/* registration form */
	/*****************************************************************************************************************************/
	
	//submit
	$('#btnSubmit').click(function(){ $('#frmReg').submit(); });
	//reset 
	$('#btnReset').click(function(){ window.location = 'registration.shtml'; });
	
	if($('form').length){
	
		$.validator.addMethod("postalcode", function(postalcode, element) { //custom validation method for postal code
			return this.optional(element) || postalcode.match(/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnpstvxy]{1}\d{1}[A-Za-z]{1} ?\d{1}[A-Za-z]{1}\d{1})$/);
		}, "Please specify a valid postal/zip code");		

		jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
			return this.optional(element) || value != $(param).val();
		}, "This has to be different...");


		$(".validate-reg").validate({
			rules: {
				txtFirst: {
					required: true,
					minlength: 2,
					maxlength: 50
				},
				txtLast: {
					required: true,
					minlength: 2,
					maxlength: 50
				},
				txtAddr1: {
					required: true,
					minlength: 2,
					maxlength: 100
				},
				txtAddr2: {
					minlength: 2,
					maxlength: 100
				},
				txtCity: {
					required: true,
					minlength: 2,
					maxlength: 50
				},
				selProvince: 'required',
				txtPostal: {
					required: true,
					minlength: 6,
					maxlength: 7,
					postalcode: true,
					notEqualTo: 'X9X 9X9'
				},
				selAge: 'required',
				txtEmail1: {
					required: true,
					minlength: 2,
					maxlength: 100,
					email: true,
					notEqualTo: 'you@yourdomain.com'
				},
				txtEmail2: {
					required: true,
					minlength: 2,
					maxlength: 100,
					email: true,
					notEqualTo: 'you@yourdomain.com',
					equalTo: '#txtEmail1'
				}
			},
			messages: {
				txtFirst: {
					required: 'Please enter your first name.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.')
				},
				txtLast: {
					required: 'Please enter your last name.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.')
				},
				txtAddr1: {
					required: 'Please enter your mailing address.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.')
				},
				txtAddr2: {
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.')
				},
				txtCity: {
					required: 'Please enter your city.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.')
				},
				selProvince: {
					required: 'Please select your province.'
				},
				txtPostal: {
					required: 'Please enter your postal code.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.'),
					postalcode: 'Please enter a valid postal code (X9X 9X9)',
					notEqualTo: 'Pleae enter your postal code.'
				},
				txtAge: 'Please select your age.',
				txtEmail1: {
					required: 'Please enter your email address.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.'),
					email: 'Please enter a valid email address. (you@yourdomain.com)',
					notEqualTo: 'Please enter a valid email address.'
				},
				txtEmail1: {
					required: 'Please enter your email address.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.'),
					email: 'Please enter a valid email address. (you@yourdomain.com)',
					notEqualTo: 'Please enter a valid email address.',
					equalTo: 'Your email addresses do not match.'
				}			
			},
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					//$(".validation-summary").show();
					//$('.ajax-loader').hide();
				} else {
					$(".error-wrapper").hide();
				}
			},
			submitHandler: function(form) {
				//form.submit();
				var strData = $('#frmReg').serialize();
				$.ajax({
					type: 'POST',
					url: 'register.php',
					data: strData,
					success: showResponse,
					error: showError
				});
				//ajax success
				function showResponse(responseText, StatusText){
					//successful response
					if(responseText === '1'){
						window.location = 'enterpincode.php';	
					}else{
						$('#server-error').show();
						$('#server-error').html(responseText);
					}

				}
				//ajax error
				function showError(XMLHttpRequest, textStatus, errorThrown){ alert('Error: ' + textStatus + '\n' + errorThrown); };				
			},
			errorElement: 'label',
			wrapper: 'div',
			errorPlacement: function(error, element){
				offset = element.offset(),
				error.insertAfter(element),
				error.addClass('error-wrapper')
			}
		});
	}//if form.length	
	
	/*****************************************************************************************************************************/
	/* pin form */
	/*****************************************************************************************************************************/
	
	//submit
	$('#btn-deal').click(function(){ $('#frmPin').submit(); });
	
	$('#txtPin').focus(function(){ $('#server-error').hide() });
	$('#txtSkillQ').focus(function(){ $('#server-error').hide() });
	
	if($('form').length){

		$(".validate-pin").validate({
			rules: {
				txtPin: {
					required: true,
					minlength: 8,
					maxlength: 8
				},
				txtSkillQ: {
					required: true,
					minlength: 1,
					maxlength: 2,
					number: true
				}
			},
			messages: {
				txtPin: {
					required: 'Please enter your PIN.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.')
					
				},
				txtSkillQ: {
					required: 'Please answer the skill testing question.',
					minlength: jQuery.format('Please enter at least {0} characters.'),
					maxlength: jQuery.format('Please enter no more than {0} characters.'),
					number: 'Please enter a numeric value.'
				}
			},
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					//$(".validation-summary").show();
					//$('.ajax-loader').hide();
				} else {
					$(".error-wrapper").hide();
				}
			},
			submitHandler: function(form) {
				//form.submit();
				var strData = $('#frmPin').serialize();
				$.ajax({
					type: 'POST',
					url: 'process.php',
					data: strData,
					success: showResponse,
					error: showError
				});
				//ajax success
				function showResponse(responseText, StatusText){
					//successful response
					switch(responseText){
						case '0': 
							//session expired
							window.location = 'session-exp.shtml';
							break;
						case '1':
							//winning pin
							window.location = 'pgfuncdfl.php';
							break;
						case '2':
							//losing pin
							window.location = 'desdfdiwjes.php';
							break;
						case '3':
							//faild math question
							window.location = 'fsdredaaiuy.php';
							break;
						case '4':
							$('#server-error').show();
							$('#server-error').html('Sorry: That pincode does not exist or has already been redeemed.');
							break;
						default:
							$('#server-error').show();
							$('#server-error').html(responseText);
					}

				}
				//ajax error
				function showError(XMLHttpRequest, textStatus, errorThrown){ alert('Error: ' + textStatus + '\n' + errorThrown); };					
				
			},
			errorElement: 'label',
			wrapper: 'div',
			errorPlacement: function(error, element){
				offset = element.offset(),
				error.insertAfter(element),
				error.addClass('error-wrapper')
			}
		});
	}//if form.length
	
	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* language toggle */
	/*---------------------------------------------------------------------------------------------------------------------------*/	

	$('.lang-toggle').click(function(){
		
		var thePath = window.location.pathname;
		var thePage = thePath.substring(thePath.lastIndexOf('/') + 1);
		var thePagePos = thePath.lastIndexOf(thePage);
		var theDir = thePath.substring(thePagePos - 18, thePagePos);
		
		if(theDir === '/en/hydro_contest/'){
			//window.location = '../fr/' + thePage;
			setTimeout(function(){ window.location = '../../fr/hydro_contest/' + thePage; }, 0);			
		}else if(theDir === '/fr/'){
			//window.location = '../en/' + thePage;
			setTimeout(function(){ window.location = '../../en/hydro_contest/' + thePage; }, 0);	
		}else{
			//window.location = '../en/';
			setTimeout(function(){ window.location = '../../en/hydro_contest/'; }, 0);	
		}
	}); //.lang-toggle click		
	

});//domready		   






/*****************************************************************************************************************************/
/* functions */
/*****************************************************************************************************************************/

var preLoadCache = [];
// Arguments are image paths relative to the current page.
$.preloadImages = function(){
	var args_len = arguments.length;
	for (var i = args_len; i--;){
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		preLoadCache.push(cacheImage);
	}
};//preloadImages


//$.initCorners = function(){
//    var settings = {
//      tl: { radius: 20 },
//      tr: { radius: 20 },
//      bl: { radius: 20 },
//      br: { radius: 20 },
//      antiAlias: true
//    }
//}
