Cufon.replace('body.page-template-page-kids-php .main-cnt h2, body.page-template-page-kids-php .gform_wrapper .gform_body .gform_fields li.gfield > label');

jQuery(function($) {
	$('#footer .footer-navigation li:last-child').addClass('last');
	
	// Sidebar Gravity Form Update Elements
	(function() {
		var labels = $('#sidebar .promo-form label').hide();
		var inputs = $('input[type="text"], input[type="email"]', '#sidebar .promo-form .gfield');
		
		inputs.each(function() {
			var label = labels.filter('[for="' + $(this).attr('id') + '"]').text().replace(/\*$/i, '');
			$(this).val(label).attr('title', label).addClass('blink');
		});
	})();
	
	$('.product-view .product-info .thumbs ul li a').click(function() {
		$('.product-view .big-img img').hide();
		$($(this).attr('rel')).show();
		return false;
	});
	$('.product-view .product-info .thumbs ul li a:first').click();
	
	$('p').each(function() {
		if ($(this).text() == '') {
			$(this).remove();
		}
	});
	
	$('.content-heading h2').each(function() {
		if ($(this).height() > 50) {
			$(this).css('marginTop', '0px');
		}
	});
	
	// Homepage Carousel
	(function() {
		var slider = $('#mycarousel .slider-holder');
		var ul = slider.find('> ul');
		var lis = ul.find('> li');
		var width = lis.outerWidth();
		var currentLi = 0;
		var autoSlide = false;
		var animationSpeed = 300;
		var pause = 4000;
		
		function setAutoslide() {
			clearInterval(autoSlide);
			autoSlide = false;
			autoSlide = setInterval(function() {
				slideTo(currentLi+1);
			}, pause);
		}
		
		function slideTo(index) {
			clearInterval(autoSlide);
			autoSlide = false;
			
			if (index >= lis.length) {
				index = 0;
			} else if (index < 0) {
				index = lis.length - 1;
			}
			
			$(nav[index]).addClass('active').siblings().removeClass('active');
			ul.animate({left: '-' + (index*width) + 'px'}, animationSpeed, function() {
				currentLi = index;
			});
			
			setAutoslide();
		}
		
		var nav = $('#mycarousel .slider-navigation a').click(function() {
			slideTo($(this).index());
			return false;
		});
		
		slider.hover(function() {
			clearInterval(autoSlide);
			autoSlide = false;
		}, function() {
			setAutoslide();
		});
		
		slideTo(currentLi);
	})();
	
	$('.blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('.blink').blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	$('.blink input').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('').removeClass('light');
		}
	});
	
	$('.blink input').blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
		
		if ($(this).val() == $(this).attr('title')) {
			$(this).addClass('light');
		}
	});
	
	$('.blink input').blur();
	
	$('ul.gfield_radio li img').click(function(){
		$(this).parent('li').find('input').click();
	});
	
	$('ul.gfield_radio li input[type="radio"]').click(function() {
		$('a.vc_preview').show().attr('href', $(this).attr('data-preview'));
	});
	
	$('.product').hover(function(){
		$(this).addClass('hovered');
	},function(){
		$(this).removeClass('hovered');
	});
	
	$('a.pop-loader').facebox();
	
	var now_font_size = $('body').css('font-size');
	now_font_size.substr(0,2);
	now_font_size = parseInt(now_font_size);
	
	$('a.minimize-txt').click(function(){
		now_font_size--;
		$('body').css('font-size', now_font_size);
		return false;
	});
	
	$('a.maximize-txt').click(function(){
		now_font_size++;
		$('body').css('font-size', now_font_size);
		return false;
	});
	
	var faq_contents = $('body.page-template-page-faq-php .main-cnt .entry').hide();
	
	$('.faq-list a').click(function() {
		faq_contents.hide();
		$($(this).attr('href')).fadeIn(200);
	});
	
	if($.browser.msie && parseInt($.browser.version) == 6)
	{
		DD_belatedPNG.fix('.wrapper, #sidebar ul li.sidebar-link-holder a, .galerie .product .img a img, .pop-up-t, .pop-up-c, .pop-up-b, .banner a.grey-btn, .abs-img-holder img, .span.red-label');
	}
	
	$(window).load(function() {
		$('.abs-img-holder img').each(function() {
			$(this).css({ left: '-' + $(this).width() + 'px' });
			$(this).animate({ left: $(window).width() + $(this).width() + 'px' }, 7000);
		});
	});
	
	$(window).load(function() {
		$('.abs-img-holder').each(function() {
			$(this).animate({ top: '-=50' }, 1900);
			$(this).animate({ top: '+=100' }, 2000);
			$(this).animate({ top: '-=100' }, 2400, function() {
				$(this).css({ top: '50px', height: '186px' });
				$('#les-dompteurs-de-souris').css('bottom', '30px');
			});
		});
	});
	
	$('.gform_wrapper form').submit(function() {
		var valid = true;
		var errors = '';
		var lang = $('#promotion_subscribe_form_language').text();
		var i18n = {
			'en': { 'the': 'The', 'is_required': 'field is required',     'invalid_email': 'The email address you\'ve entered is not valid' },
			'fr': { 'the': 'Le champ',  'is_required': 'est obligatoire', 'invalid_email': 'L\'adresse de contact que vous avez entré n\'est pas valide' }
		};
		
		var requiredFields = [];
		$(this).find('label:has(.gfield_required)').each(function() {
			requiredFields.push($('#' + $(this).attr('for'))[0]);
		});
		
		$(requiredFields).each(function() {
			console.log($(this).val());
			console.log($(this).attr('title'));
			if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				$(this).addClass('field-error');
				errors += i18n[lang]['the'] + ' "' + $(this).attr('title') + '" ' + i18n[lang]['is_required'] + '\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		$(requiredFields).filter('[type="email"]').each(function() {
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			
			if (!pattern.test($(this).val())) {
				$(this).addClass('field-error');
				errors += i18n[lang]['invalid_email'] + '\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		if (!valid) {
			alert(errors);
		}
		
		return valid;
	});
	
	// Rating stars
	(function() {
		var rating = $('.product-view .rating, .pop-up .rating');
		var score = rating.find('.score');
		
		rating.bind('hover', toggle_score);
		rating.find('li').bind('hover', toggle_hover_class);
		
		rating.find('a').click(function() {
			rating.unbind('hover');
			rating.find('li').unbind('hover');
			
			score.hide();
			rating.find('li').removeClass('hover');
			$(this).parents('.rating li').addClass('hover');
			
			$.get($(this).attr('href'), function(data, textStatus, XHR) {
			});
			
			return false;
		});
		
		function toggle_score() {
			score.toggle();
		}
		
		function toggle_hover_class() {
			$(this).toggleClass('hover');
		}
	})();
	
	$('.validate').submit(function() {
		var valid = true;
		var errors = '';
		
		$(this).find('.required').each(function() {
			if ($(this).val() == '') {
				$(this).addClass('field-error');
				errors += 'The "' + $(this).attr('title') + '" field is required\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		$(this).find('.email').each(function() {
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			
			if (!pattern.test($(this).val())) {
				$(this).addClass('field-error');
				errors += 'The email address you entered is not valid\n';
				if (valid) {
					$(this).focus();
				}
				valid = false;
			} else {
				$(this).removeClass('field-error');
			}
		});
		
		if (!valid) {
			alert(errors);
		}
		
		return valid;
	});
	
	$('form#find_retail_form').submit(function() {
		var form_data = $(this).serialize();
		var field = $(this).find('#find_retail_address');
		
		var details = $('.main-cnt .details').html('');
		
		$.post($(this).attr('action'), form_data, function(data, status, XHR) {
			details.html(data);
		});
		
		return false;
	});
	
	$(document).bind('reveal.facebox', function() {
		
		if ($('#facebox .map').length==0) {
			return; // nothing to do if there is no map div in the window
		}
		var container = $('#facebox .map').get(0);
		
		map = new GMap2(container, {
				size: new GSize($(container).width(), $(container).height())
			});
		
		var geocoder = new GClientGeocoder();
		
		geocoder.getLatLng($(container).data('geocoding-addr'), function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.setCenter(point, 16);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				var control = new GLargeMapControl3D();
				map.addControl(control);
			}
		})
	}).bind('close.facebox', function() {
		if ($('#facebox .map').length) {
			GUnload();
		}
	});;
});
