$(document).ready(function(){

	// makes all ul.column li.block.down a.header toggle their container
	$("ul.column li.block.down a.header").click(function() {
		$(this).parent().toggleClass("open");
	});

});

var current_part_number = '';
function slideDrawerZip(part_number) {
	if (part_number == null) {
		$('#ciSlide').fadeTo(400, 0.0, function() { $(this).hide(); });
		current_part_number = '';
	}
	else if (current_part_number != part_number) {
		if (current_part_number == '') {
			var zipFormHtml = 'Enter your zip code (US only) to search nearby retailers for <b>'+ part_number +':</b>'+cii_ppp_api(0,part_number,'',-1,1815,-1,-1,-1,-1,'US','','');
			$('#zipForm').html(zipFormHtml);
			$('#ciSlide').show().fadeTo(400, 1.0);
		}
		else {
			$('#ciSlide').show().fadeTo(400, 0.0, function() {
				var zipFormHtml = 'Enter your zip code (US only) to search nearby retailers for <b>'+ part_number +':</b>'+cii_ppp_api(0,part_number,'',-1,1815,-1,-1,-1,-1,'US','','');
				$('#zipForm').html(zipFormHtml);
				$('#ciSlide').show().fadeTo(400, 1.0);
			});
		}
		current_part_number = part_number;
	}
}

function slideDrawer() {
	if ($('#drawer').is(':visible')) {
		$('#drawer').animate({'top': '-600px'}).hide('fast', function() {
			$('#drawerFlood').fadeTo('fast', 0).animate({ 'width': '0px', 'height': '0px' });
		});		
	} else {
		$('#drawerFlood').css({ 'width': $(document).width(), 'height': $(document).height() } ).fadeTo('fast', 0.7, function() {
			$('#drawer').show().animate({'top': '-6px'});
		});
	}
}

function switchImg(filename) {
	$('#stageImg').attr('src', filename);
}

function swapState(sel) {
  if ($(sel).val() == 'United States') {
    $('#state_select').removeClass('HIDE');
    $('#state_select_ca').addClass('HIDE');
    $('#state_box').addClass('HIDE');
  } else if ($(sel).val() == 'Canada') {
    $('#state_select').addClass('HIDE');
    $('#state_select_ca').removeClass('HIDE');
    $('#state_box').addClass('HIDE');      
  }  else {
     $('#state_box').removeClass('HIDE');
     $('#state_select').addClass('HIDE');
     $('#state_select_ca').addClass('HIDE');
  }
}


function checkForm(form) {
	email = form.elements['contact[email]'].value;
	if (!validEmail(email)) {
		alert('Please enter a valid email address');
		return false
	}
	if (form.elements['contact[policy]'].checked) {
		for (var i in form.elements) {
			if (form.elements[i].value == '') {
				alert('All blanks are required - make sure to fill out the whole form.\n\n Write "N/A" if field does not apply.');
				return false
			}
		}
	} else {
		alert('Make sure to read and agree with the Review Policy.');
		return false
	}
	return true
}

function slideSku(divId, ypos) {
	if (!ypos) { ypos = '24px'};
	var sliderdiv = $('#slide'+divId);
	var invisiblediv = $('#i'+divId);
	if (sliderdiv.is(':visible')) {
		invisiblediv.css('z-index', '103');
		sliderdiv.animate({ 'top': '54px'}, 200, '', function() { $(this).hide(); });
	}	else {
		invisiblediv.css('z-index', '98');
		sliderdiv.show().animate({ 'top': ypos}, 200);
	}
}

function checkEmail(email) {
	if (!validEmail(email)) {
		alert('Please enter a valid email address');
		return false
	}
	return true
}

function validEmail(email) {
  return email.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)
}

function toggleAll(control, el) {
	if (control.checked) {
		checkAll(el)
	} else {
		uncheckAll(el)
	}
}