

$(function(){
		   
	//initiate PNG fix for ie6
	if($.browser.msie && $.browser.version.substr(0, 1) < 7){
		$(document).pngFix();
		//fake :first-child psuedo class for main nav
		$('.nav-wrapper ul:first-child').css({ 'padding-left' : '12px' });
		$('.nav-wrapper ul').css({ 'width' : '938px' });
	}		
	
	//populate current year (footer - copyright)
	$('.current-year').text((new Date).getFullYear());
	
	//initiate all new window links
	$('.new-win').click(function(){
		var x = $(this).attr('href');
		$(this).attr({ 'href' : 'javascript:void(0);' });
		window.open(x, 'hihatwin');	
	});	
	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* products browser */
	/*---------------------------------------------------------------------------------------------------------------------------*/
	
	var fadeSpeed = 600;
	
	//all products are hidden via css by default, show the first one
	//$('.col1 > div:first').fadeIn(fadeSpeed);
	
	//add click event for thumbs
	$('.prod-thumb').click(function(n){
		var theLink = $(this);
		//get the last char (digit) from the link rel attribute to referrence the arrow icon
		var lastChar = $(this).attr('rel').substr($(this).attr('rel').length - 1, 1);
		
		if($('#arrow' + lastChar).is(':visible')){
			//hide arrow for current product
			$('#arrow' + lastChar).fadeOut(fadeSpeed);
			//show arrow for hidden product
			$('img[alt="more"]:hidden').fadeIn(fadeSpeed);
			//hide visible product and show user selected product
			$('.col1 > div:visible').fadeOut(fadeSpeed, function(){ $('#' + $(theLink).attr('rel')).fadeIn(fadeSpeed); });
		}

		
	});
	
	//set product to query string variable if provided
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == 'id') {
			//return pair[1];
			var prodToLoad = pair[1];
		}
	} 	
	if(prodToLoad){
		//query string variable for 'id' found, load that product
		$('#product-container' + prodToLoad).fadeIn(fadeSpeed);
		$('#arrow' + prodToLoad).css({ 'display' : 'none' });
	}else{
		//no query string variable found, load the first product
		$('.col1 > div:first').fadeIn(fadeSpeed);
		$('#arrow1').css({ 'display' : 'none' });
	}
	
	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* contact form */
	/*---------------------------------------------------------------------------------------------------------------------------*/	

	if($('form').length){
		$.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");
	
		$(".validate-me").validate({
			
			errorLabelContainer: $('.errors'),
	
			rules: {
				fname: {
					required: true,
					minlength: 2
				},
				lname: {
					required: true,
					minlength: 2
				},
				addr1: {
					required: true,
					minlength: 5
				},
				city: {
					required: true,
					minlength: 2
				},	
				prov: 'required',
				postc: {
					required: true,
					minlength: 2,
					postalcode: true
				},
				dphone: {
					required: true,
					minlength: 10
				},
				age: 'required',
				email: {
					required: true,
					minlength: 5,
					email: true
				},
				lang: 'required',
				terms: 'required',
				privacy: 'required'
			},
			
			messages: {
				fname: {
					required: 'Please enter your first name.',
					minlength: jQuery.format("Please enter at least {0} characters for your first name. ")
				},
				lname: {
					required: 'Please enter your last name.',
					minlength: jQuery.format("Please enter at least {0} characters for your last name. ")
				},
				addr1: {
					required: 'Please enter your street address.',
					minlength: jQuery.format("Please enter at least {0} characters for your address. ")
				},
				city: {
					required: 'Please enter your city.',
					minlength: jQuery.format("Please enter at least {0} characters for your city. ")
				},
				prov: {
					required: 'Please select your province'
				},			
				postc: {
					required: 'Please enter your postal code.',
					minlength: jQuery.format("Please enter at least {0} characters for your first name. "),
					postalcode: 'Please enter a valid postal code (X9X9X9).'
				},
				dphone: {
					required: 'Please enter your phone number.',
					minlength: jQuery.format("Please enter at least {0} characters for your phone number. ")
				},
				age: {
					required: 'Please enter your age.'
				},
				email: {
					required: 'Please enter your email address.',
					minlength: jQuery.format("Please enter at least {0} characters for your email address. "),
					email: 'Please enter a valid email address (yourname@yourdomain.com).'
				},
				lang: {
					required: 'Please select your language.'
				},
				terms: {
					required: 'Please agree to the terms to continue.'
				},
				privacy: {
					required: 'Please agree to the privacy policy to continue.'
				}				
			},
			
			invalidHandler: function(e, validator) {
				
				var errors = validator.numberOfInvalids();
				
				if (errors) {
	//				var message = errors == 1
	//					? 'Vous avez omis 1 champ. Veuillez remplir le champ suivant :'
	//					: 'Vous avez omis ' + errors + ' champs. Veuillez remplir les champs suivants :';
	//				$("div.error-wrapper span").html(message);
					$("div.error-wrapper").show();
					$('.loader').hide();
				} else {
					$("div.error-wrapper").hide();
				}
			},
			
			submitHandler: function(form) {
				$("div.error-wrapper").hide();
				
				var strData = $('#entryform').serialize();
				
				
				$.ajax({
					type: 'POST',
					url: 'contest.pl',
					data: strData,
					success: showResponse,
					error: showError
				});
	
				//ajax success
				function showResponse(responseText, StatusText){
					//successful response
					if(responseText === 1 || responseText === '1'){
						//$('.ajax-loader').hide();
						$('#entryform').fadeOut(500, function(){ 
							$('.thankyou-wrapper').fadeIn(500);
						});
					//validation failed, display errors
					}else{
						$('.loader').hide();
						$('div.error-wrapper').show();
						$("div.error-wrapper span").html('Veuillez corriger les renseignements contenus dans le(s) champ(s) suivant(s):');
						$('div.errors').show().append('<p>' + responseText + '</p>');
					}
				}
				
				//ajax error
				function showError(XMLHttpRequest, textStatus, errorThrown){ alert('Error: ' + textStatus + '\n' + errorThrown); };
	
			}		
	
		});
	}//if form.length
	
	
	//submit button click
	$('#go').click(function(){ 
		$('.loader').show();
		$('#entryform').submit(); 
	});

	//reset button click
	$('#res').click(function(){ 
		var v = $('#entryform').validate();							  
		v.resetForm();
		$('#entryform').each(function(){ this.reset(); });
		$('.error-wrapper').hide();
	});
	
	
	
	
	
	
	
	/*---------------------------------------------------------------------------------------------------------------------------*/
	/* 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 = '../fr/' + thePage;
			setTimeout(function(){ window.location = '../fr/' + thePage; }, 0);			
		}else if(theDir === '/fr/'){
			//window.location = '../en/' + thePage;
			setTimeout(function(){ window.location = '../en/' + thePage; }, 0);	
		}else{
			//window.location = '../en/';
			setTimeout(function(){ window.location = '../en/'; }, 0);	
		}
	}); //.lang-toggle click	
	
})//domready