$(document).ready(function() {
	
	$("#events .more").click( function() {
		if (!$(this).hasClass("on")) {
			$("#events .hidden").slideDown("normal");
			$("#events .more").text("read less");
			$("#events .more").addClass("on");
			} else {
			$("#events .hidden").slideUp("normal");
			$("#events .more").text("read more");
			$("#events .more").removeClass("on");
			}
		});
	
	$("#icpsignup input").focus( function() {
		$("#icpsignup .hidden:hidden").slideDown("normal");
		});
		
	$("#post_email input").focus( function() {
		$("#post_email .hidden:hidden").slideDown("normal");
		});
		
	$("#unsubscribe input").focus( function() {
		$("#unsubscribe .hidden:hidden").slideDown("normal");
		});
		
	var currentProducer = 0;
	var totalProducers = $(".producer").length;
	$("#nextProducer").click( function() {
		var $div = $(".producer:eq("+ currentProducer +")");
		var h = $div.height();
		$("#loading").css("height",h+"px");
		$div.hide();
		$("#loading").show();
		currentProducer = currentProducer + 1;
		if (currentProducer == totalProducers) currentProducer = 0;
		$("#loading").hide();
		$(".producer:eq("+ currentProducer +")").show();
		return false;
		});
	$("#prevProducer").click( function() {
		var $div = $(".producer:eq("+ currentProducer +")");
		var h = $div.height();
		$("#loading").css("height",h+"px");
		$div.hide();
		$("#loading").show();
		currentProducer = currentProducer - 1;
		if (currentProducer < 0) currentProducer = totalProducers-1;
		$("#loading").hide();
		$(".producer:eq("+ currentProducer +")").show();
		return false;
		});
		
	$("#technical").click( function() {
		$("#technical_data").slideToggle("normal");
		$("#technical").toggleClass("on");
		return false;
		});
	var currentImage = 0;
	var totalImages = $(".galleryImage").length;
	$("#nextImage").click( function() {
		var $img = $(".galleryImage:eq("+ currentImage +")");
		$img.hide();
		currentImage = currentImage + 1;
		if (currentImage == totalImages) currentImage = 0;
		$(".galleryImage:eq("+ currentImage +")").show();
		return false;
		});
	$("#prevImage").click( function() {
		var $img = $(".galleryImage:eq("+ currentImage +")");
		$img.hide();
		currentImage = currentImage - 1;
		if (currentImage < 0) currentImage = totalImages-1;
		$(".galleryImage:eq("+ currentImage +")").show();
		return false;
		});
		
	$('#shop_header').flash({
		src: '/shop_header.swf',
		expressInstall: true,
		wmode: 'opaque',
		width: 631,
		height: 127
		});
	
	$('#producer_lookup').flash({
		src: '/map.swf',
		expressInstall: true,
		wmode: 'opaque',
		width: 630,
		height: 630
		});
	
	$(".help").hover(function(){
		var p = $(this).position();
		var x = p.left - 136;
		var y = p.top - $(this).next(".popup").height() - 10;
		$(this).next(".popup").show()
            .css({ left: x+'px', top: y+'px' })
		},
		function() {
			$(this).next(".popup").hide();
		});
		
	$("#newsletterArchives .archiveList").click(function(){
		if ($(this).css("font-weight") == "bold") {
			$("#newsletterArchivesList:visible").slideUp();
			$("#newsletterArchives .archiveList").css("font-weight","normal");
			return false;
			}
		$("#newsletterArchives .archiveList").css("font-weight","normal");
		$(this).css("font-weight","bold");
		$("#newsletterArchivesList").load('/tools.php?getNewsletterArchive='+$(this).text(), function()
			{
			$("#newsletterArchivesList:hidden").slideDown();
			});
		return false;
		});
	$("#alertArchives .archiveList").click(function(){
		if ($(this).css("font-weight") == "bold") {
			$("#alertArchivesList:visible").slideUp();
			$("#alertArchives .archiveList").css("font-weight","normal");
			return false;
			}
		$("#alertArchives .archiveList").css("font-weight","normal");
		$(this).css("font-weight","bold");
		$("#alertArchivesList").load('/tools.php?getAlertArchive='+$(this).text(), function()
			{
			$("#alertArchivesList:hidden").slideDown();
			});
		return false;
		});
	
});

var reEmail = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;

function isEmail(str) {
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
		}
	if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
		else		
		return (reEmail.test(str));
	}

function verifyRequired() {
	if (document.icpsignup["fields_fname"].value == "") {
		document.icpsignup["fields_fname"].focus();
		alert("The First Name field is required.");
		return false;
		}
	if (document.icpsignup["fields_lname"].value == "") {
		document.icpsignup["fields_lname"].focus();
		alert("The Last Name field is required.");
		return false;
		}
	if (document.icpsignup["fields_email"].value == "") {
		document.icpsignup["fields_email"].focus();
		alert("The Email field is required.");
		return false;
		}
	var estr=document.icpsignup["fields_email"].value.toLowerCase();
	if(!estr) return false;
	if (!isEmail(estr)) {
		alert("Please enter a valid email address in this field."); 
		document.icpsignup["fields_email"].focus();
		return false;
		}
	if (
		!document.icpsignup["listid:176290"].checked &&
		!document.icpsignup["listid:177835"].checked &&
		true)  {
			alert("The Lists field is required.");
			return false;
		} 
	return true;
	}

function showRegion(region) {
	if (region == "all") {
		for (i = 1; i <= 17; i++) {
			$("#producerList div.r"+i+":hidden").slideDown("normal");
			}
		} else if (region == "france") {
		for (i = 1; i <= 12; i++) {
			$("#producerList div.r"+i+":hidden").slideDown("normal");
			}
		for (i = 13; i <= 17; i++) {
			$("#producerList div.r"+i+":visible").slideUp("normal");
			}
		} else if (region == "italy") {
		for (i = 13; i <= 17; i++) {
			$("#producerList div.r"+i+":hidden").slideDown("normal");
			}
		for (i = 1; i <= 12; i++) {
			$("#producerList div.r"+i+":visible").slideUp("normal");
			}
		} else {
		for (i = 1; i <= 17; i++) {
			if (i != region) $("#producerList div.r"+i+":visible").slideUp("normal");
			}
		$("#producerList div.r"+region+":hidden").slideDown("normal");
		}
	}
	
function validateLoginReq() {
	if (document.reqLogin["name"].value == "") {
		document.reqLogin["name"].focus();
		alert("The name field is required.");
		return false;
		}
	if (document.reqLogin["email"].value == "") {
		document.reqLogin["email"].focus();
		alert("The email field is required.");
		return false;
		}
	var estr=document.reqLogin["email"].value.toLowerCase();
	if(!estr) return false;
	if (!isEmail(estr)) {
		alert("Please enter a valid email address in this field."); 
		document.reqLogin["email"].focus();
		return false;
		}
	if (document.reqLogin["phone"].value == "") {
		document.reqLogin["phone"].focus();
		alert("The phone field is required.");
		return false;
		}
	if (document.reqLogin["business"].value == "") {
		document.reqLogin["business"].focus();
		alert("The business name field is required.");
		return false;
		}
	return true;
	}
	
function validateUnsubscribe() {
	if (document.unsubscribe["unsub_name"].value == "") {
		document.unsubscribe["unsub_name"].focus();
		alert("The name field is required.");
		return false;
		}
	if (document.unsubscribe["unsub_email"].value == "") {
		document.unsubscribe["unsub_email"].focus();
		alert("The email field is required.");
		return false;
		}
	var estr=document.unsubscribe["unsub_email"].value.toLowerCase();
	if(!estr) return false;
	if (!isEmail(estr)) {
		alert("Please enter a valid email address in this field."); 
		document.unsubscribe["unsub_email"].focus();
		return false;
		}
	if (document.unsubscribe["security_code"].value == "") {
		document.unsubscribe["security_code"].focus();
		alert("The security code field is required.");
		return false;
		}
	return true;
	}
