$(document).ready(function(){
	// HOMEPAGE SLIDESHOW
	$('#slide-list').after('<ol class="paginate"></ol>').cycle({
		fx: 'fade',
		speed: 700,
		timeout: 6000,
		pager: '.paginate',
		pagerAnchorBuilder: function(idx, img){
			return '<li><a href="#">' + (+idx+1) + '</a></li>';
		}
	});
	
	// SUBNAV MENUS
	if ($.browser.msie) {
		$('#primary-nav li').mouseenter(function(){
			$(this).children('.subnav').show().addClass('open');
			window.clearTimeout();
		}).mouseleave(function(){
			$('.open').removeClass('open');
		});
	} else {
		$('#primary-nav li').mouseenter(function(){
			$(this).children('.subnav').addClass('open');
			window.clearTimeout();
		}).mouseleave(function(){
			window.setTimeout(function(){
				$('.open').removeClass('open');
			}, 200);
		});
	}

	$(window).click(function(){
		$('.open').removeClass('open');
	});

	$('#primary-nav span').bind('touchstart, click', function(){
		parent = $(this).parent();
		$(this).siblings('.subnav').addClass('open').remove().appendTo(parent);
	});

	// CUSTOM FORM STYLES
	$('select.styled').uniform({
		hoverClass: 'hovered'
	});
	
	$('.check-list input[type="checkbox"]').uniform({
		hoverClass: 'hovered',
		checkedClass: 'checked'
	});
	
	// VIDEO LIGHTBOX
	$('#video-list.thumbs a, #video-btn, .video-btn').fancybox({
		width: '75%',
		height: '75%',
		autoScale: false,
		type: 'iframe',
		overlayColor: '#000'
	});
	
	// FORM VALIDATION
	var returnVal = $("input[name='return']").val();
	$("input[name='return']").val(returnVal + '/ajax');
	siteID = $('html').attr('data-id');
	message = "";
	
	if(siteID == 1) {
		message = "Please complete the required fields."
	} else if(siteID == 2) {
		message = "Por favor, rellene todos los campos requeridos."
	} else if(siteID == 3) {
		message = "Pros&iacute;m vypl&#328;te po&#382;adovan&aacute; pole."
	} else if(siteID == 4) {
		message = "&#1055;&#1086;&#1078;&#1072;&#1083;&#1091;&#1081;&#1089;&#1090;&#1072;, &#1079;&#1072;&#1087;&#1086;&#1083;&#1085;&#1080;&#1090;&#1077; &#1085;&#1077;&#1086;&#1073;&#1093;&#1086;&#1076;&#1080;&#1084;&#1099;&#1077; &#1087;&#1086;&#1083;&#1103;."
	} else if(siteID == 5) {
		message = "S'il vous pla&icirc;t remplir les champs requis."
	} else if(siteID == 6) {
		message = "Prosz&#281; wype&#322;ni&#263; wymagane pola."
	} else if(siteID == 7) {
		message = "Por favor, preencha os campos obrigat&oacute;rios."
	} else if(siteID == 8) {
		message = "Bitte f&uuml;llen Sie die erforderlichen Felder aus."
	} else if(siteID == 9) {
		message = "&#1610;&#1585;&#1580;&#1609; &#1605;&#1604;&#1569; &#1575;&#1604;&#1581;&#1602;&#1608;&#1604; &#1575;&#1604;&#1605;&#1591;&#1604;&#1608;&#1576;&#1577;."
	} else if(siteID == 10) {
		message = "&#35531;&#22635;&#23531;&#25152;&#38656;&#30340;&#23383;&#27573;&#12290;"
	} else if(siteID == 11) {
		message = "Gerekli alanlar&#305; doldurun."
	} else if(siteID == 12) {
		message = "Pros&iacute;m vypl&#328;te po&#382;adovan&eacute; polia."
	}
	
	$("#contact-form, #demo-form, #info-form").validate({
		rules: {
			email: {email: true}
		},
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				$('em.error-msg').remove();
				$(this).append('<em class="error-msg">'+message+'</em>');
				$('em.error-msg').fadeIn('fast').css('display','block');
			} else {
				$('em.error-msg').remove();
			}
		}, submitHandler: function(form) {
			$('#content form').append('<strong id="busy">Your message is sending...</strong>');
			$('#content form').after('<div id="secondary"></div>');
			$('#secondary').hide();
		
		   	$(form).ajaxSubmit({
				target: '#secondary',
				success: function() {
					$('#content form').hide();
					$('#secondary').fadeIn();
					$('#busy').fadeOut();
				}
			});
		}
	});
	
	// DISTRIBUTOR FORM
	$('#find-country').change(function(){
		country = $(this).val();
		
		if(country == 'US'){
			$('#uniform-find-state-US').show();
			$('#uniform-find-state-CA').hide();
			$('#find-form li + li').fadeIn();
		} else if (country == 'CA'){
			$('#uniform-find-state-CA').show();
			$('#uniform-find-state-US').hide();
			$('#find-form li + li').fadeIn();
		} else {
			if(country != ""){
				window.location.replace('/support/find-a-distributor/' + country);
			}
		}
		
		$('#find-state-US, #find-state-CA').val('');
	});
	
	$('.state-select').change(function(){
		country = $('#find-country').val();
		state = $(this).val();
		window.location.replace('/support/find-a-distributor/' + country + '/' + state);
	});
	
	// ZEBRA STRIPING
	$('#find table tbody tr:nth-child(odd)').addClass('odd');
	$('#find table tbody tr:nth-child(even)').addClass('even');	
	
	// TRANSLATE FORM
	$('#language-select').change(function(){
		newSite = $(this).val();
		window.location.replace(newSite);
	});
});
