$(function(){
		   
	if($.browser.msie && $.browser.version.substr(0, 1) < 7){ $.initIE6(); }		//all IE6 related fixes
	
	$.initLightBoxes(['.btn-rules']);												//initialize all colorboxes
	
	$.initNewWindowLinks(['.new-win']);												//initialize all new window links
	
	$.initFormValidation();															//initialize form validation
	   
		   
});//domready		   



	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* 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 - 4, thePagePos);
		if(theDir === '/en/'){
			window.location = '../french/' + thePage;		
		}else if(theDir === '/french/'){
			window.location = '../english/' + thePage;
		}else{
			window.location = '../english/';	
		}
	}); //.lang-toggle click	




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



$.initIE6 = function(){																//this function will correct any IE6 issues
	$('form :text').addClass('txt');												//add class name to text fields
	$('form :radio').addClass('rad');												//add class name to radio buttons
	$('form :checkbox').addClass('chk');											//add class name to checkboxes
	DD_belatedPNG.fix('div, span, a, img');											//png fix
};//initIE6


$.initNewWindowLinks = function(param){												//this function will open links in a new window
	$.each(param, function(i, e){
		$(e).click(function(){													 	//click event
			var x = $(this).attr('href');											//save href in variable
			$(this).attr({ 'href' : 'javascript:void(0);' });						//reset the links href
			window.open(x, 'hihatwin');												//open new window and direct to href
		});	
	});//each
};//initNewWindowLinks


$.initLightBoxes = function(param){													//this function will initiate each color box
	if(typeof($.colorbox) != 'undefined'){											//if colorbox is available
		$.each(param, function(i, item){											//loop through each selector passed as a parameter					   
			$(param[i]).colorbox({									
				'innerWidth'	:'535px',
				'innerHeight'	:'575px',
				'width'		: '547px', 
				'height'	: '647px',
				'scrolling'	:'false',
				'inline'	:'true', 
				'href'		:$(param[i]).attr('href')
			});//colorbox		
		});//each
	}//if
};//initLightBoxes


$.initFormValidation = function(){													//this function will initialize the form validation
	
	if($('form').length){
		
		$('.btn-submit').click(function(){ 
			$('form').submit();
			$('.btn-submit, .ajax-loader').toggle();
		});
		
		$.validator.addMethod('phonenumber', function(phone_number, element) { //custom validation method for phone number
			phone_number = phone_number.replace(/\s+/g, ''); 
			return this.optional(element) || phone_number.length > 9 &&
				phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
		}, 'Please enter a valid telephone number.');
		
		$.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");
	
		$('form').validate({
			
			rules: {
				txtFirst: {
					required: true,
					minlength: 2,
					maxlength: 50
				},
				txtLast: {
					required: true,
					minlength: 2,
					maxlength: 50
				},
				txtStreet1: {
					required: true,
					minlength: 5,
					maxlength: 100
				},
				txtStreet2: {
					minlength: 5,
					maxlength: 100
				},
				txtCity: {
					required: true,
					minlength: 2,
					maxlength: 100
				},
				selProvince: 'required',
				txtPostal: {
					required: true,
					minlength: 6,
					maxlength: 7,
					postalcode: true
				},
				txtPhone: {
					required: true,
					minlength: 10,
					maxlength: 18,
					phonenumber: true
				},
				selAge: 'required',
				txtEmail1: {
					required: true,
					minlength: 5,
					maxlength: 100,
					email: true
				},
				txtEmail2: {
					required: true,
					minlength: 5,
					maxlength: 100,
					email: true,
					equalTo: '#txtEmail1'
				},
				selLanguage:'required',
				radGender: 'required',
				radQ1: 'required',
				chkQ2: {
					required: true,
					minlength: 2
				},
				radQ3: 'required',
				chkQ4: 'required',
				radQ5: 'required',
				radQ6: 'required',
				radQ7: 'required',
				radQ8: 'required',
				radQ9: 'required',
				radQ10: 'required',
				chkOptRules: 'required'
			},
			
			messages: {
	
				txtFirst: {
					required: 'Please enter your first name.',
					minlength: jQuery.format('Please enter at least {0} characters for your first name.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your first name.')
				},
				txtLast: {
					required: 'Please enter your last name.',
					minlength: jQuery.format('Please enter at least {0} characters for your last name.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your last name.')
				},
				txtStreet1: {
					required: 'Please enter your address.',
					minlength: jQuery.format('Please enter at least {0} characters for your address.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your address.')
				},
				txtStreet2: {
					minlength: jQuery.format('Please enter at least {0} characters for your address.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your address.')
				},
				txtCity: {
					required: 'Please enter your city.',
					minlength: jQuery.format('Please enter at least {0} characters for your city.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your city.')
				},
				selProvince: 'Please select your province.',
				txtPostal: {
					required: 'Please enter your postal code.',
					minlength: jQuery.format('Please enter at least {0} characters for your postal code.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your postal code.'),
					postalcode: 'Please enter a valid postal code. (X9X 9X9)'
				},
				txtPhone: {
					required: 'Please enter your phone number.',
					minlength: jQuery.format('Please enter at least {0} characters for your phone number.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your phone number.'),
					postalcode: 'Please enter a valid phone number. (999-999-9999)'
				},
				selAge: 'Please select your age range.',
				txtEmail1: {
					required: 'Please enter your email address.',
					minlength: jQuery.format('Please enter at least {0} characters for your email address.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your email address.'),
					email: 'Please enter a valid email address. (yourname@yourdomainname.com)'
				},
				txtEmail2: {
					required: 'Please confirm your email address.',
					minlength: jQuery.format('Please enter at least {0} characters for your email address.'),
					maxlength: jQuery.format('Please enter no more than {0} characters for your email address.'),
					email: 'Please enter a valid email address. (yourname@yourdomainname.com)',
					equalTo: 'Your email addresses do not match.'
				},
				selLanguage: 'Please select your language.',
				radGender: 'Please select your gender.',
				radQ1: 'Please answer question 1.',
				chkQ2: {
					required: 'Please select at least one answer for question 2.',
					minlength: jQuery.format('Please select at least {0} options for question 2.')
				},
				radQ3: 'Please select at least one answer for question 3.',
				chkQ4: 'Please select at least one answer for question 4.',
				radQ5: 'Please select at least one answer for question 5.',
				radQ6: 'Please answer question 6.',
				radQ7: 'Please answer question 7.',
				radQ8: 'Please answer question 8.',
				radQ9: 'Please answer question 9.',
				radQ10: 'Please answer question 10.',
				chkOptRules: 'Please agree to the rules and regulations.'
			},
			
			errorPlacement: function(error, element) {

				error.appendTo('.error-wrapper');
				$('.error-wrapper').append('<div class="clear"></div>');

			},

			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					$('div.error-wrapper').show();
					$('.btn-submit, .ajax-loader').toggle();
					
				} else {					
					$('div.error-wrapper').hide();
				}
				
			},
			
			submitHandler: function(form) {
				var strData = $(form).serialize();
				$.ajax({
					type: 'POST',
					url: 'register.php',
					data: strData,
					success: showResponse,
					error: showError
				});
				//ajax success
				function showResponse(responseText, StatusText){
					//successful response
					if(responseText === 1 || responseText === '1'){
						$('.btn-submit, .ajax-loader').toggle();					
						$('#divContestForm').fadeOut(500, function(){ $('#divConfirmation').fadeIn(500) });
						
					//validation failed, display errors
					}else{
						$('#btn-submit').toggle();
						$('.loader').toggle();						
						$('.error-wrapper').show();
						$(".error-wrapper").html('<span>Please correct the following:</span>');
						$('.error-wrapper').show().append('<p>' + responseText + '</p>');
						$('.btn-submit, .ajax-loader').toggle();
					}
				}
				//ajax error
				function showError(XMLHttpRequest, textStatus, errorThrown){ alert('Error: ' + textStatus + '\n' + errorThrown); };
				
			} 
			
		});//validate
	}//if
	
	
	
}


