$(document).ready(function() 
{
 	//animateBackground();

	$('.nav-bar a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function()
		{
			$(this).stop().animate({backgroundPosition:"(0 -250px)"}, {duration:500})
		})
		.mouseout(function()
		{
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:500})
		})
	
	var current_sub;
	var index_sub;
	$('.sub').each(function(index) {
		if ($(this).css("display")=='block')
		{
			current_sub = this
			index_sub = index;
		}
		
	});
	
	

	
	
	
	$('ul.nav-bar li.nav-parent').each(function(index2) {
		//alert(index_sub +"="+index);
		if(index_sub!=index2)
		{
			if ($(current_sub))
			{
				//alert($(this).html());
				
				$(this).mouseout(function()
				{
					$(current_sub).css("display","block");
				});
				
				
				$(this).mouseover(function()
				{
					$(current_sub).css("display","none");
					//alert(index);
				});
			}
		}
	});

	
	if (document.getElementById("calendar"))
	{
		//alert('test');
		displayCalendar();
	}
	$('ul#filter a').click(function() {
		$(this).css('outline','none');
		$('ul#filter li a.anchor').removeClass('anchor');
		$(this).parent().addClass('anchor');
		
		var filterVal = $(this).text().toLowerCase().replace(' ','-');
				
		if(filterVal == 'all') {
			$('ul#product-list li.hidden').fadeIn('slow').removeClass('hidden');
		} else {
			
			$('ul#product-list li').each(function() {
				if(!$(this).hasClass(filterVal)) {
					$(this).fadeOut('normal').addClass('hidden');
				} else {
					$(this).fadeIn('slow').removeClass('hidden');
				}
			});
		}
		
		return false;
	});	
	
	
	$("#join_list").click (function()
	{
		$("#join_list").val("");
	});	
		
});


function showNewsLetterBox()
{
	$("#newsletter-box").animate({
		top: '250px'
	}, 1500, 'easeInOutBounce',function()
	{});
}

function closeNewsletterBox()
{
	$("#newsletter-box").animate({
		top: '-250px'
	}, 1500, 'easeOutSine',function()
	{});
}

function joinEmailList()
{

	var err_count = 0;
	var err_message = "";
	
	var e = $("#join_list").val();
	if(e=="" || e.indexOf("@")==-1 || e.indexOf(".")==-1)
	{
		err_count++;
	}
	
	if(err_count>0)
	{
		$("#join_list").val("Please enter a valid email address.").css("color","#990000");
		$("#error_container").fadeIn("fast");
		
	} else
	{
		$("#error_container").fadeOut("fast");
		
		$.ajax({
			url: '/assets/ajax/join_newsletter.php',
			data: "e="+e,
			success: function(data) {
				
				$("#join_list").val("");
				$("#message").html("Thank you for joining our email list!");
				$("#message").fadeIn("fast");
				
				
				setTimeout(closeNewsletterBox,2500);
			}
		});

	}
	
	return false;
}

function animateAboutImage()
{
	$('ul#about-images').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '220px'
	});
}

function animateTastingImages()
{
	$('ul#tasting-images').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '220px'
	});
}


function animateBackground()
{	
	$(document).bgStretcher({
		images: ['/assets/img/backgrounds/malibu_bg_1.jpg','/assets/img/backgrounds/homepage_bg.jpg','/assets/img/backgrounds/homepage_bg_3.jpg','/assets/img/backgrounds/weddings_bg.jpg','/assets/img/backgrounds/homepage_bg_4.jpg','/assets/img/backgrounds/homepage_bg_2.jpg'],
		imageWidth: 1885, imageHeight: 1800, nextSlideDelay:8000, slideShowSpeed: 1000
	});	
}
function showLarge(id)
{
	$('#large-display').fadeOut('slow', function() {
	    fadeInNewImage(id)
  });

}
function fadeInNewImage(id)
{
	$('#large-display').html('<img src="/assets/img/gallery/'+id+'.jpg">');
	$('#large-display').fadeIn("slow");	
}


function viewEventInfo(i)
{
	$.ajax({
   type: "POST",
   url: "/assets/ajax/event_info.php",
   data: "id="+i,
   success: function(msg){
   	$("#event_text").html(msg);
	 $("#overlay-bg").fadeIn("slow");
   }
 });
 
 

	
}

function closeEventInfo()
{
	$("#overlay-bg").fadeOut("slow");
	 	$("#event_text").html("");
}


function CheckAddToCart()
{

	if ($("form :radio").length>0)
	{
		if ($('form :radio').is(':checked'))
		{
		} else 
		{
			alert("Please select Case or Bottle");
			return false;
		}
		
	}
	
	if ($("#quantity").val()!="" && isNumeric($("#quantity").val()) )
	{
		return true;
	} else 
	{
		alert("Please enter a valid quantity");
		return false;
	}
}

function isNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;

}

 
   
function quickBag_close()
{
	$("#quick-shopping-bag").fadeOut("slow");
}



function anchorWineTab(ele,cat_id)
{
	$(".wine-cat-tabs").css("background","#4b253e");
	$(".wine-cat-tabs").css("color","#FFFFFF");
	$('#'+ele).css("background","#DFD5DE");
	$('#'+ele).css("color","#4b253e");
	
	$.ajax({
	   type: "POST",
	   url: "/assets/ajax/filter.php",
	   data: "category_id="+cat_id,
	   success: function(msg){
	    // alert( "Data Saved: " + msg );
		$("#product-view-list").css("display","none");
		$("#product-view-list").html(msg);
		$("#product-view-list").fadeIn("slow");
	   }
	 });
	
}


function productTab(l)
{
	$(".review-tabs li a").removeClass("selected");
	$("#review-content p").css("display","none");
	
	$("#link_"+l).addClass("selected");
	
	$("#tab_"+l).fadeIn("slow");
}


function checkContact()
{
	var e = 0;
	
	if ($("#inquiry_type").val()=="")
	{
		$("#label_inquiry-type").css("color","#EF0E17");
		$("#commuity").css("border","1px solid #EF0E17");
		e++;
		
	} else 
	{
		$("#label_inquiry-type").css("color","#99C090");
		$("#commuity").css("border","0px solid #EF0E17");
	}
	
	if ($("#first-name").val()=="")
	{
		$("#label_first-name").css("color","#EF0E17");
		$("#first-name").css("border","1px solid #EF0E17");
		e++;
		
	} else 
	{
		$("#label_first-name").css("color","#99C090");
		$("#first-name").css("border","0px solid #EF0E17");
	}
	
	if ($("#last-name").val()=="")
	{
		$("#label_last-name").css("color","#EF0E17");
		$("#last-name").css("border","1px solid #EF0E17");
		e++;
		
	} else 
	{
		$("#label_last-name").css("color","#99C090");
		$("#last-name").css("border","0px solid #EF0E17");
	}
	
	
	
	if ($("#email").val()=="")
	{
		$("#label_email").css("color","#EF0E17");
		$("#email").css("border","1px solid #EF0E17");
		e++;
		
	} else 
	{
		$("#label_email").css("color","#99C090");
		$("#email").css("border","0px solid #EF0E17");
	}
	
	
	
	if ($("#phone").val()=="")
	{
		$("#label_phone").css("color","#EF0E17");
		$("#phone").css("border","1px solid #EF0E17");
		e++;
		
	} else 
	{
		$("#label_phone").css("color","#99C090");
		$("#phone").css("border","0px solid #EF0E17");
	}
	
	
	if ($("#comments").val()=="")
	{
		$("#label_comments").css("color","#EF0E17");
		$("#comments").css("border","1px solid #EF0E17");
		e++;
		
	} else 
	{
		$("#label_comments").css("color","#99C090");
		$("#comments").css("border","0px solid #EF0E17");
	}
	
	
	
	if (e>0)
	{
		return false;
	} else 
	{
		return true;
	}
}
