var isScrolling;

$(document).ready(function() {	
		// ---------------------------------------------------------------------------------------------WINE SECTION

		var bottlePosition = $('#navBottle').css("margin-left");
		$('#wineNavigation ul a').hover(
			function(){
				$('#navBottle').stop();
				var thisItem = $(this).parent().attr("class");
				var newBottlePos;
				if (thisItem == "houseWines"){newBottlePos = 40}
				if (thisItem == "wineGuide"){newBottlePos = 146}
				if (thisItem == "wineTastings"){newBottlePos = 255}
				if (thisItem == "pairings"){newBottlePos = 372}	
				$('#navBottle').animate({marginLeft: newBottlePos},"slow","swing");
			},
			function(){
				$('#navBottle').stop();
				$('#navBottle').animate({marginLeft:bottlePosition},"slow","swing")
			}
		);				
		$('.pairings .landing li').hover(
			function(){// ------------------------------------------------------ hover state event
				var thisID = $(this).attr("class");
				$("#foodGroups ."+thisID+" h4").animate({marginTop: 0},400, "swing");//end animate top-margin
				$(this).addClass("open1");
			},			
			function(){// --------------------------------------------------------over state event
				var thisID = $(this).attr("class").replace(" ","").replace("open1","");
				if (thisID == "pairCheeses"){returnMargin = 91}
				if (thisID == "pairMeat"){returnMargin = 78}
				if (thisID == "pairIntl"){returnMargin = 61}
				if (thisID == "pairFruits"){returnMargin = 84}
				$(this).removeClass("open1");						
				$("#foodGroups li."+thisID+" h4").animate({marginTop:returnMargin},400, "swing");
			}/*end out state */
		);		

		$('.pairings dt').click(function(){
				$('.pairings dt.selected').removeClass("selected");
				$(this).addClass("selected");	
				var varietal = $(this).parent().attr("class").replace("oddDL","");
				var foodGroupID = $(this).parent().parent().parent().attr("class").replace(" ","").replace("open1","");//gets the class of the parent lialert(foodGroupID);
				var foodName = $(this).html();
				$("#foodGroups").fadeTo(200, 0, //slideUp
					function(){						
						if ($('#foodGroups').attr("class") == "landing"){$('#foodGroups h4').stop().attr("style","")}//stops and resets the sliding food landing page behavior
						var matchingWines = $('#foodGroups .'+varietal+' dd').html().replace(/\s+/g,"").split(",");//gets text - conerts to array
						var numberOfMatches = (matchingWines.length);
						$('ul.varietals').css("width",(numberOfMatches * 138));
						//$('#varietalViewer h3').html($('li.'+foodGroupID+' h4').html());//put the foodGroup header in the stage
						$('#varietalViewer h5').html(foodName+" Goes Well With:");//put the food Name in the sub title spot
						$('.varietals li.selected').removeClass('selected');
						var matchCounter;
						for (matchCounter=0; matchCounter<numberOfMatches; matchCounter++){
							var currentItem = matchingWines[matchCounter];
							$('.varietals li#'+matchingWines[matchCounter]).addClass("selected");
						}
						if (numberOfMatches > 3){$('.wineScrollControls').addClass('activated'); $('.varietals').css("left", "0");}
						if (numberOfMatches <= 3){$('.wineScrollControls').removeClass("activated");$('.varietals').css("left", "0");}
						$('.pairings .landing li').unbind();//UNBIND original hover behavior
						$('.stage').addClass("groupDisplay");
						$('#foodGroups li.open1').removeClass("open1");
						$("#foodGroups h4").attr("style","");
						if ($('#foodGroups').attr("class") == "landing"){
							$('#foodGroups li span dl:nth-child(even)').addClass('oddDL');
							$('.landing').removeClass("landing");							
							$('.initial').removeClass('initial').addClass("navOnBottom");
						}						
						$('#foodGroups').attr("class",foodGroupID).fadeTo(200,1);
					}
				)/*end #foodGroups animation*/;
		});// ------------------------------------------------end ('.pairings dt').click event

		$('.pairings .wineScrollControls a').click(function(){
			var listingWidth = $('.varietals').css("width");
			var listingWidth = listingWidth.substring(0, listingWidth.length-2);
			var currentOffset = $('.varietals').css("left");//get current "left" value
			var currentOffset = currentOffset.substring(0, currentOffset.length-2);
			var scrollDistance = 414;	
			if ($(this).attr("id") == "rightWines"){
				if((listingWidth - (-currentOffset)) < scrollDistance){return false;}
				var leftValue = (currentOffset - scrollDistance);
			}
			if ($(this).attr("id") == "leftWines"){
				if (currentOffset >= 0){return false;/* don't scroll left if left=0 */}
				var leftValue = (currentOffset - (-scrollDistance));
				}
			$('.varietals').animate({left: leftValue},500,"swing");
			return false;
		});
						
		$('.pairings dt').hover(
		function(){$(this).addClass('dtHover')},
		function(){$(this).removeClass('dtHover')}
		);		
		
		// ------------------------------------------ TASTINGS

		
		$('#tastings li h4').click(function(){			
			$(this).next().slideToggle(400);
			$(this).parent().toggleClass('open');
			return false;
		});
		
		$('#tastings li h4').hover(
			function(){$(this).children('a').toggleClass('headerHover');},
			function(){$(this).children('a').toggleClass('headerHover');}		
		);
		
		$('#tastings li dl:nth-child(even)').addClass('oddTastingsDL');
		
		
		$('body#wineGuide ul#landingWineGuideNav li').hover(
			function(){
				var wineClass = $(this).attr("class");				
				$('li.'+ wineClass +' span').fadeTo("200",0);
				$('li.'+ wineClass +' pre').animate({top: 0},400,"swing",function(){
					$('li.'+ wineClass +' a').attr("style","display: block;").fadeIn("slow");
				})//.effect("bounce", {times: 3, distance: 12,direction: "down"},300);							
			},
			function(){//out state
				var wineClass = $(this).attr("class");
				$('li.'+ wineClass +' a').fadeTo("fast",0,function(){$(this).attr("style","");});
				$('li.'+ wineClass +' pre').stop().animate({top: 113},300,"swing", function(){
					$("li."+wineClass+" span").fadeTo("300",1);
					$('li.'+ wineClass +' pre').attr("style","");
				});
			}
		);		
		
		var wineGuide_red = $('body#wineGuide #grapes li.red li').length;
		var wineGuide_white = $('body#wineGuide #grapes li.white li').length;
		var wineGuide_rose = $('body#wineGuide #grapes li.rose li').length;
		var wineGuide_sparkling = $('body#wineGuide #grapes li.sparkling li').length;		
		var wineGuideScroll = 469;// pixels to scroll by in wine guide viewer
				
		$('body#wineGuide ul#landingWineGuideNav li').click(function(){
				var wineType = $(this).attr('class');		
				$('body#wineGuide ul#landingWineGuideNav').fadeTo(300,0, function(){
				$('#wineGuide #grapeNavigator li.red li:nth-child(even), #wineGuide #grapeNavigator li.white li:nth-child(even)').addClass('oddDL');//sets up the 2-column footer nav layout				
				$('.landingContent').fadeTo(300,0, function(){
					$('#grapes li.red ul').width((wineGuideScroll*(wineGuide_red)));// these 4 lines establish the width of the wineGuide scrollable lists
					$('#grapes li.white ul').width((wineGuideScroll*(wineGuide_white)));
					$('#grapes li.rose ul').width((wineGuideScroll*(wineGuide_rose)));
					$('#grapes li.sparkling ul').width((wineGuideScroll*(wineGuide_sparkling)));
					$('.wineScrollControls').fadeTo(0,0,function(){$('.wineScrollControls').css({'display':'block'}).fadeTo(1000,1);});
					$('#grapes li.'+wineType).addClass('onDisplay').fadeIn();											
					$('.initial').removeClass('initial');
					$('#grapeNavigator li.'+wineType+' li:eq(0) a').addClass('selected');
										
				});
				$(this).attr("style","display: none;");							
			});
			return false;
		});

		$('body#wineGuide .wineScrollControls a').click(function(){ // wine guide side-scroller
			if (isScrolling) {return false;}
			if ($(this).attr("id")=="rightActive" || $(this).attr("id")=="leftActive"){			
				isScrolling = true;
				var currentListWidth = $('li.onDisplay ul').width();
				var currentListOffset = $('li.onDisplay ul').css('left');	
				currentListOffset = currentListOffset.substring(0, currentListOffset.length-2);
				var itemInView = (-(currentListOffset/wineGuideScroll));
				var goingLeft = (($(this).attr("class")).match("Left"));// going left?  (boolean)
				 if (goingLeft == 'Left'){
				 	if (currentListOffset == 0){return false;}
					else {					
						var scrollTo = (currentListOffset - (-wineGuideScroll));	
						var remainder = (scrollTo -(-currentListWidth) - wineGuideScroll);
						$('#grapes li.onDisplay ul').animate({left:scrollTo},500,"swing", function(){isScrolling = false;});
						$('#grapeNavigator .selected').removeClass('selected').parent().prev().children('a').addClass('selected');			
						if (scrollTo == 0){$(this).removeAttr("id");};					
						$('.wineScrollControls a.seeRightWines').attr("id","rightActive");	
						return false;
					}
				}
				else {				
					if (currentListWidth - (-currentListOffset) - wineGuideScroll == 0) {return false;}
					else {	
						$('.wineScrollControls a.seeLeftWines').attr("id","leftActive");				
						var scrollTo = (currentListOffset -wineGuideScroll);	
						$('#grapeNavigator .selected').removeClass('selected').parent().next().children('a').addClass('selected');					
						$('#grapes li.onDisplay ul').animate({left:scrollTo},500,"swing", function(){isScrolling = false;});	
						var remainder = (scrollTo -(-currentListWidth) - wineGuideScroll);
						if (remainder == 0){$(this).removeAttr("id");}
						if (scrollTo == 0){$('.wineScrollControls a#leftActive').removeAttr("id");};
						return false;
					}
				}	
			}
			else{return false;}				
		});
		
		$('#grapeNavigator li a').click(function(){
			$('#grapeNavigator li a.selected').removeAttr('class');
			$(this).addClass('selected');
			var thisTitle = $(this).html();
			var thisWineFamily = $(this).parent().parent().parent().attr('class');
			var thisIndex = $('#grapeNavigator li.'+thisWineFamily+' a').index($(this));
			var matchedClass = $('#grapes li.onDisplay').attr("class").match(thisWineFamily);// boolean (returns thisWineFamily or 'null')
			var scrollTo = (wineGuideScroll*(-thisIndex));
			var displayedListWidth = $('#grapes li.onDisplay .varietals').width();
			if (matchedClass == thisWineFamily){//this type of wine is already on display
				$('#grapes li.onDisplay ul').animate({left:scrollTo},600,"swing", function(){
					var displayedListWidth = $('#grapes li.onDisplay .varietals').width();
					if (scrollTo == (-displayedListWidth)+wineGuideScroll) {$('#rightActive').removeAttr('id');$('.seeLeftWines').attr('id','leftActive');}
					else if (thisIndex == 0){$('a#leftActive').removeAttr('id');$('.seeRightWines').attr('id','rightActive');}	
					else {$('.seeLeftWines').attr('id','leftActive');$('.seeRightWines').attr('id','rightActive');}
				});
										
			}// end "if scrolling within same class
			else {//if wine type onDisplay does not match selected wine
				//$('#grapes li.onDisplay ul').css({left:0});
				$('#grapes li.onDisplay').fadeTo(500,0,function(){
					$('#grapes li.onDisplay').removeClass('onDisplay');
				 	$('#grapes li.'+thisWineFamily).addClass('onDisplay');
				 	//$('#grapes li.onDisplay .varietals').css({left:0});
				 	$('#grapes li.onDisplay ul')
				 	$('#grapes li.onDisplay').css({'display':'list-item'}).fadeTo(200,1);
				 	$('#grapes li.onDisplay ul').animate({left:scrollTo},600,"swing", function(){					 	
					 	var displayedListWidth = $('#grapes li.onDisplay .varietals').width();
					 	if (scrollTo == (-displayedListWidth)+wineGuideScroll) {$('#rightActive').removeAttr('id');$('.seeLeftWines').attr('id','leftActive');}
						else if (thisIndex == 0){$('a#leftActive').removeAttr('id');$('.seeRightWines').attr('id','rightActive');}	
						else {$('.seeLeftWines').attr('id','leftActive');$('.seeRightWines').attr('id','rightActive');}	
				 	});				 	
				 	
				 });
			};
			return false;
		});
		
		$('#houseWines .wine a.learn').click(function(){
			var wineList = $(this).parent().parent().html();			
			$('#uncorked').html(wineList);
			var wineFamily = $(this).parent().parent().parent().attr("class");
			$('#boxOfWine').addClass(wineFamily);
			$('#content .screen').fadeTo(10,0,function(){
				$('#content .screen').css({'display':'block'}).fadeTo(300,.7,function(){
					$('#content #boxOfWine').animate({top: 125},500,"swing",function(){
						
					}).effect("bounce", {times: 1, distance: 10}, 200);
				});
				
								
			});
			return false;
		});
		$('#recork,#houseWines .screen').click(function(){			
			$('#content #boxOfWine').animate({top: -800},500,"swing",function(){
				$('#content .screen').fadeTo(300,0, function(){
					$('#content .screen').removeAttr("style");
					$('#boxOfWine').removeAttr("class");
				});
			});
			return false;
		});
		
		
		
});