$(function() {
	// legal popup
	$('a[rel="legal"]').click(function(e) {
		e.preventDefault();
		
		var height	= 500;
		var width	= 500;
		window.open(this.href, 'legal_popup', 'height='+height+', left='+(screen.width-width)/2+', location=no, resizable=no, scrollbars=yes, status=no, toolbar=no, top='+(screen.height-height)/2+', width='+width);
	});
	
	// top winefinder tool
	// open 'select list'
	$('#findStoreForm #wineDropdown').click(function() {
		//if($(this).text() != 'Choose a Wine...') {
		//	$(this).text('Choose a Wine...');
		//}
		if($('#findStoreForm #dropdown').is(':hidden')) {
			$('#findStoreForm #dropdown').slideDown('fast');
		} else { 
			$('#findStoreForm #dropdown').slideUp('fast');
		}
	});
	
	// select new varietal
	$('#findStoreForm #dropdown a').click(function() {
		$('#findStoreForm #txtBrand').attr('value',$(this).attr('href').replace('#',''));
		$('#findStoreForm #wineDropdown').text($(this).text());
		$('#findStoreForm #dropdown').slideUp('fast');
		$('#varietal_code').attr('value',$(this).attr('href').replace('#',''));
	});

	// hide 'select list' on global clicks
	$(document).bind('click', function(e) {
		if($(e.target).attr('class') != 'wineselect' && $(e.target).attr('id') != 'wineDropdown') {
			if($('#findStoreForm #dropdown').is(':visible')) {
				$('#findStoreForm #dropdown').slideUp('fast');
			}
		}
	});
	
	// remove default zip code text
	$('#findStoreForm #txtZip').focus(function() {
		if($(this).val() == 'Enter Your Zip Code') {
			$(this).val('');
		}
	});
	
	// return default zip code text
	$('#findStoreForm #txtZip').blur(function() {
		if($(this).val() == '' || $(this).val().length != 5) {
			$(this).val('Enter Your Zip Code');
		}
	});
	
	// validate then submit
	$('#findStoreForm .submit').click(function(e) {
		varietal_name = $('#findStoreForm #wineDropdown').text();
		varietal_code = $('#findStoreForm #txtBrand').val();
		zip = $('#findStoreForm #txtZip').val();
		miles = $('#findStoreForm #txtRadiusExpand').val();
		
		if(zip == 'Enter Your Zip Code' && varietal_name == 'Choose a Wine...') {
			alert('Please enter you zip code and wine selection.');
		} else if(zip == 'Enter Your Zip Code' && varietal_name != 'Choose a Wine...') {
			alert('Please enter you zip code');
		} else if(zip != 'Enter Your Zip Code' && varietal_name == 'Choose a Wine...') {
			alert('Please select a wine');
		} else {
			var height	= 800;
			var width	= 650;
			
			//window.open($('#findStoreForm').attr('action'), 'winefinder_popup', 'height='+height+', left='+(screen.width-width)/2+', location=no, resizable=no, scrollbars=yes, status=no, toolbar=no, top='+(screen.height-height)/2+', width='+width);
			$('#findStoreForm').submit();
			
			e.preventDefault();
		}
		e.preventDefault();
	});
	
	$('#newsletter_form #error, #contactForm #error').click(function() {
		$(this).css('display','none');
	});
	
	$('a[href=#moredetails]').click(function(e) {
		if($(this).parent().next('.moredetails').is(':visible')) {
			$(this).parent().next('.moredetails').slideUp();
		} else {
			$(this).parent().next('.moredetails').slideDown();
		}
		e.preventDefault();
	});
	
	// wine pairing tool
	// dish selection
	$('a.dish').click(function(e) {
		$('a.dish').parent('li').removeClass('active');
		$(this).parent('li').addClass('active');
		
		var dish = $(this).attr('href');
		dish = dish.replace('#','');
		
		if($('.ingredients').is(':visible')) {
			if($('#pairingMatch').is(':visible')) {
				$('#pairingMatch').hide('slide',{direction:'up'},'fast',function(){
					$('.pairingMatch').css('display','none');
				});
			}
			$('.ingredients:visible').hide('slide',{direction:'left'},'normal',function(){
				$('.ingredients li.active').removeClass('active');
				$('div#'+dish).show('slide',{direction:'left'},'normal');
			});
		} else {
			$('div#'+dish).show('slide',{direction:'left'},'normal');
		}
		
		e.preventDefault();
	});
	
	// ingredient selection
	$('a.ingredient').click(function(e) {
		$('a.ingredient').parent('li').removeClass('active');
		$(this).parent('li').addClass('active');
		
		var dish = $(this).parents('div.column').attr('id');
		
		var ingredient = $(this).attr('href');
		ingredient = ingredient.replace('#','');
		
		switch(dish) {
			case 'beef':
				switch(ingredient) {
					case 'bbq':
						var matches = ['barbecuedsirloinsteakwithconfitgarlicbutter','shiraz','cabernetsauvignon'];
						break;
					case 'cilantro':
						var matches = ['barbecuedbeefsteakstirfry','shiraz','pinotnoir'];
						break;
					case 'garlic':
						var matches = ['barbecuedsirloinsteakwithconfitgarlicbutter','shiraz','cabernetsauvignon','merlot'];
						break;
					case 'mushroom':
						var matches = ['carpetbagsteak','merlot','cabernetsauvignon','pinotnoir'];
						break;
					case 'onion':
						var matches = ['barbecuedbeefsteakstirfry','shiraz','merlot'];
						break;
					case 'pepper':
						var matches = ['barbecuedbeefsteakstirfry','shiraz'];
						break;
				}
				break;
				
			case 'chicken':
				switch(ingredient) {
					case 'cilantro':
						var matches = ['limemarinatedchickenwithpeanutsauce','riesling','pinotnoir'];
						break;
					case 'chili':
						var matches = ['spicychickensamosas','riesling','shiraz'];
						break;
					case 'curry':
						var matches = ['spicychickensamosas','riesling','shiraz'];
						break;
					case 'garlic':
						var matches = ['chickenandhamchoibao','pinotnoir','chardonnay'];
						break;
					case 'lime':
						var matches = ['limemarinatedchickenwithpeanutsauce','riesling','chardonnay','pinotnoir'];
						break;
					case 'onion':
						var matches = ['chickenwiltedspinachcurriedchickpeas','chardonnay','riesling'];
						break;
					case 'pepper':
						var matches = ['spicychickensamosas','riesling','shiraz'];
						break;
				}
				break;
			
			case 'lambandveal':
				switch(ingredient) {
					case 'ham':
						var matches = ['vealhamburgerswithbeetrootrelish','shiraz','merlot'];
						break;
					case 'lemon':
						var matches = ['greenolivelemonparsleylamb','merlot','chardonnay'];
						break;
					case 'mushroom':
						var matches = ['soylambmushroomandbroccolinistirfry','shiraz','cabernetsauvignon','pinotnoir'];
						break;
					case 'onion':
						var matches = ['vealhamburgerswithbeetrootrelish','shiraz','cabernetsauvignon'];
						break;
					case 'parsley':
						var matches = ['greenolivelemonparsleylamb','merlot','pinotnoir'];
						break;
					case 'pepper':
						var matches = ['sauteedchinesepeppercrustedlambwithbokchoy','shiraz','cabernetsauvignon','merlot'];
						break;
					case 'soy':
						var matches = ['soylambmushroomandbroccolinistirfry','shiraz','cabernetsauvignon'];
						break;
				}
				break;
			
			case 'saladsandvegetables':
				switch(ingredient) {
					case 'bacon':
						var matches = ['baconpeachandbluecheesesalad','chardonnay','merlot'];
						break;
					case 'blue-cheese':
						var matches = ['baconpeachandbluecheesesalad','chardonnay','cabernetsauvignon'];
						break;
					case 'corn':
						var matches = ['cornonthecobwithparsleypesto','chardonnay','merlot'];
						break;
					case 'feta-cheese':
						var matches = ['roastedpumpkinfetafrittata','chardonnay','pinotnoir'];
						break;
					case 'fruit':
						var matches = ['barbecuednectarinewedgespancettaandfetasalad','riesling','chardonnay'];
						break;
					case 'pesto':
						var matches = ['cornonthecobwithparsleypesto','chardonnay','merlot'];
						break;
					case 'pumpkin':
						var matches = ['roastedpumpkinfetafrittata','chardonnay','riesling'];
						break;
				}
				break;
			
			case 'fishandseafood':
				switch(ingredient) {
					case 'anchovy':
						var matches = ['scallopswithanchovysauce','riesling','merlot'];
						break;
					case 'butter':
						var matches = ['chardonnaybutteroysters,','chardonnay','riesling'];
						break;
					case 'lemon':
						var matches = ['lemonmyrtlesalmonwithcucumercompote','chardonnay','pinotnoir'];
						break;
					case 'mint':
						var matches = ['mintedshrimporangesalsa','riesling','merlot'];
						break;
					case 'olive':
						var matches = ['smokedrainbowtroutpasta','pinotnoir','cabernetsauvignon'];
						break;
					case 'spicy':
						var matches = ['fishstirfry','riesling','shiraz'];
						break;
					case 'tomato':
						var matches = ['smokedrainbowtroutpasta','pinotnoir','cabernetsauvignon'];
						break;
				}
				break;
		}
		
		if($('#pairingMatch').is(':visible')) {
			$('#pairingMatch').hide('slide',{direction:'up'},'normal',function(){
				$('.pairingMatch').css('display','none');
				showMatches(matches)
			}).show('slide',{direction:'up'},'normal');
		} else {
			showMatches(matches)
			$('#pairingMatch').show('slide',{direction:'up'},'normal');
		}
				
		e.preventDefault();
	});
	
	function showMatches(matches) {
		$.each(matches, function(index) {
			if(index == 0) {
				$('#recipeImg .'+this+',#recipeLink .'+this).css('display','block');
			} else if(index == 1) {
				$('#wineImg .'+this+',#wineLink .'+this).css('display','block');
			} else if(index >= 2) {
				$('#wineLink .'+this).css('display','block');
			}
		});
	}
});
