$(document).ready(function() {

	$('#slider').nivoSlider({
	        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
	        slices:15, // For slice animations
	        boxCols: 8, // For box animations
	        boxRows: 4, // For box animations
	        animSpeed:500, // Slide transition speed
	        pauseTime:15000, // How long each slide will show
	        startSlide:0, // Set starting Slide (0 index)
	        directionNav:true, // Next & Prev navigation
	        directionNavHide:true, // Only show on hover
	        controlNav:false, // 1,2,3... navigation
	        keyboardNav:true, // Use left & right arrows
	        pauseOnHover:true, // Stop animation while hovering
	        manualAdvance:false, // Force manual transitions
	        prevText: 'Prev', // Prev directionNav text
	        nextText: 'Next', // Next directionNav text
		});
		
	$("a.fancybox").fancybox();
	$("a.fancybox-iframe").fancybox({ 'width': 800, 'height': 400, 'type': 'iframe'});

	$("#icpsignup input").focus( function() {
		$("#icpsignup .hidden:hidden").slideDown("normal");
		})
	$("#subscribe input").focus( function() {
		$("#subscribe .hidden:hidden").slideDown("normal");
		})
	$("#unsubscribe input").focus( function() {
		$("#unsubscribe .hidden:hidden").slideDown("normal");
		});

	$(".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();
		});
		
	var currentProducer = 0;
	var totalProducers = $(".producer").length;
	$("#nextProducer").click( function() {
		$(".producer:eq("+ currentProducer +")").fadeOut(function() {
			currentProducer = currentProducer + 1;
			if (currentProducer == totalProducers) currentProducer = 0;
			$(".producer:eq("+ currentProducer +")").fadeIn();
			return false;
			});
		});
	$("#prevProducer").click( function() {
		$(".producer:eq("+ currentProducer +")").fadeOut(function() {
			currentProducer = currentProducer - 1;
			if (currentProducer < 0) currentProducer = totalProducers-1;
			$(".producer:eq("+ currentProducer +")").fadeIn();
			return false;
			});
		});

	$("#producer-lookup").flash({
		src: "/swf/map.swf",
		update: false,
		wmode: "opaque",
		width: 630,
		height: 630
		});
	$('#shop_header').flash({
		src: '/swf/shop_header.swf',
		expressInstall: true,
		wmode: 'opaque',
		width: 631,
		height: 127
		});
		
	$("#producerSearch").keyup(function() {
		showRegion(currentRegion,$(this).val());
		});
		
	$("#technical").click(function() {
		$(this).toggleClass("on");
		$("#technical-data").slideToggle("normal");
		return false;
		});
		
	var currentImage = 0;
	var active = false;
	var totalImages = $(".galleryImage").length;
	$("#nextImage").click( function() {
		if (active) return false;
		active = true;
		var $img = $(".galleryImage:eq("+ currentImage +")");
		$img.fadeOut(function() {
			currentImage = currentImage + 1;
			if (currentImage == totalImages) currentImage = 0;
			$(".galleryImage:eq("+ currentImage +")").fadeIn();
			active = false;
			return false;
			});
		});
	$("#prevImage").click( function() {
		if (active) return false;
		active = true;
		var $img = $(".galleryImage:eq("+ currentImage +")");
		$img.fadeOut(function() {
			currentImage = currentImage - 1;
			if (currentImage < 0) currentImage = totalImages-1;
			$(".galleryImage:eq("+ currentImage +")").fadeIn();
			active = false;
			return false;
			});
		});
		
	$("#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 isNumeric(sText) {
	var ValidChars = "0123456789.";
	var Char;
	for (i = 0; i < sText.length; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			return false;
		}
	return true;
	}

function showRegion(region,search) {
	//alert(region);
	var params = "";

	if (region == "all") region = 0;
	if (isNumeric(region)) {
		params = "region="+region;
		} else {
		params = "country="+region;
		}
	if (search) params += "&search="+search;
	currentRegion = region;
	
	$.ajax({
	    type: "GET",
	    url: "/producer-list.php",
		data: params,
	    dataType: "xml",
		error: function() {
			alert("There was a problem!");
			},
	    success: function(data) {
			var totalProducers = $(data).find("producer").length;
			var totalProducersRow = Math.ceil(totalProducers/2);
			$("#producerList div").empty();
			$(data).find("producer").each(function(i) {
				var row = (i < totalProducersRow) ? 0:1;
				$("#producerList div:eq("+ row +")").append('<a href="/our_wines/'+$(this).find("url").text()+'/">'+$(this).find("name").text()+'</a><br/>');
				if (i == totalProducers-1) {
					return false;
					}
				});
			}
		});
	}

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;
		}
	if (document.reqLogin["city"].value == "") {
		document.reqLogin["city"].focus();
		alert("The city field is required.");
		return false;
		}
	if (document.reqLogin["state"].value == "") {
		document.reqLogin["state"].focus();
		alert("The state field is required.");
		return false;
		}
	if (document.reqLogin["wholesale_security_code"].value == "") {
		document.reqLogin["wholesale_security_code"].focus();
		alert("The security code field is required.");
		return false;
		}
	//alert("GO");
	$.ajax({
		type : "POST",
		cache : false,
		url : "/wholesale-process.php",
		data : $("#reqLogin").serializeArray(),
		success: function(data) {
			if (!$("#dialog").length) {
				$("body").append('<div id="dialog"></div>');
				}
			$("#dialog").html(data).dialog({ modal: true, width: 400, height: "auto", resizable: false, position: ["center", "center"] });
			$(".ui-widget-overlay").click(function() {
					$("#dialog").dialog('close');
					});
			}
		});

	
	}
	
function validateUnsubscribe() {
	if (document.unsubscribe["name"].value == "") {
		document.unsubscribe["name"].focus();
		alert("The name field is required.");
		return false;
		}
	if (document.unsubscribe["address"].value == "") {
		document.unsubscribe["address"].focus();
		alert("The street address field is required.");
		return false;
		}
	if (document.unsubscribe["city"].value == "") {
		document.unsubscribe["city"].focus();
		alert("The city field is required.");
		return false;
		}
	if (document.unsubscribe["state"].value == "") {
		document.unsubscribe["state"].focus();
		alert("The state field is required.");
		return false;
		}
	if (document.unsubscribe["zip"].value == "") {
		document.unsubscribe["zip"].focus();
		alert("The zip field is required.");
		return false;
		}
	if (document.unsubscribe["security_code"].value == "") {
		document.unsubscribe["security_code"].focus();
		alert("The security code field is required.");
		return false;
		}
	return true;
	}
function validateSubscribe() {
	if (document.subscribe["name"].value == "") {
		document.subscribe["name"].focus();
		alert("The name field is required.");
		return false;
		}
	if (document.subscribe["address"].value == "") {
		document.subscribe["address"].focus();
		alert("The street address field is required.");
		return false;
		}
	if (document.subscribe["city"].value == "") {
		document.subscribe["city"].focus();
		alert("The city field is required.");
		return false;
		}
	if (document.subscribe["state"].value == "") {
		document.subscribe["state"].focus();
		alert("The state field is required.");
		return false;
		}
	if (document.subscribe["zip"].value == "") {
		document.subscribe["zip"].focus();
		alert("The zip field is required.");
		return false;
		}
	if (document.subscribe["security_code"].value == "") {
		document.subscribe["security_code"].focus();
		alert("The security code field is required.");
		return false;
		}
	return true;
	}

function createDialog(title,url) {
	if (!$("#dialog").length) {
		$("body").append('<div id="dialog"></div>');
		}
	$("#dialog").empty().load(url, function() {
		$("#dialog").dialog({ modal: true, width: 800, height: "auto", resizable: false, position: ["center", "top"] });
		$(".ui-widget-overlay").click(function() {
			$("#dialog").dialog('close');
			});
		})
	
	
	}
