﻿/// <reference path="JQuery.Intellisense.js"/>

$.noConflict();
(function($){

$(document).ready(function(){
	BarillaUS.Init();
//	WTClick:function(site, uri){
//		dcsMultiTrack('DCS.dcssip',site, 'DCS.dcsuri', uri);
//		alert('DCS.dcssip: ' + site + ' - DCS.dcsuri: ' + uri);
//	};
});

_Config = {
	NumOfFilterItemsToShow:7,
	NavCloseDelayInMs:500
};

BarillaUS = {
	Init:function(){
		// can use this as hook for js-dependent styling
		jQuery('html').addClass('js');
		
		BarillaUS.PngFix();
		BarillaUS.ButtonSetup();
		BarillaUS.GlobalSearchSetup();
		BarillaUS.FooterSetup();
		BarillaUS.SearchResultsSetup();
		BarillaUS.DinnerCalendarSetup();
		BarillaUS.CalendarEventSetup();
		BarillaUS.RecipeLanding();
		BarillaUS.RecipeDetail();
		BarillaUS.CookingItalian();
		BarillaUS.Products();
		BarillaUS.MenuDetail();
	},
	PngFix:function(){
		$(document).pngFix({blankgif:'/SiteCollectionImages/global/blank.gif'});
	},
	ButtonSetup:function(){
		// form buttons
		$('div#container input:image,div.modal-container input:image,div#container a img.button')
			.filter(':not(.nohover)')
			.ImageSwap('png');
		
		// "?" icon
		var $helpButton = $('a.help-button');
		$helpButton.PopupWindow();
		$helpButton.children('img').ImageSwap('gif');
	},
	MainNavSetup:function(){
		var $mainNavPoints = $('#mainnav > li');
		
		// add <span>s to main nav items. ensures graphic nav is applied
		$mainNavPoints.children('a').append('<span></span>');
		
		// add "first" css class to the first <li> for design purposes
		$mainNavPoints.find('li:first').addClass('first');
		
		// attach over/out events
		var navTimerHandle = null;
		
		$mainNavPoints.hover(
			function(){
				var $this = $(this);
				$this
					.siblings('.open')
						.removeClass('open')
					.end()
					.addClass('open');
				
				clearTimeout(navTimerHandle);
			},
			function(){
				var $this = $(this);
				navTimerHandle = setTimeout(
					function(){
						$this.removeClass('open');
					},
					_Config.NavCloseDelayInMs
				);
			}

		)
		// add "menu-top" for shadowed top of flyout menu
		.children('ul').prepend('<li class="menu-top"></li>');
	},
	GlobalSearchSetup:function(){
		// field focus/blur
		var searchInput = $('div#globalsearch input:text');
		var defaultValue = searchInput.val();
		searchInput.InputClear(defaultValue);
		
		// "by category" flyouts
		var $catLink = $('div#globalsearch a#category-link');
		
		$catLink.click(function(evt){
			$(this).toggleClass('open').next('div#recipesbycategory').toggleClass('open');
			evt.preventDefault();
		});
		
		$('div#recipesbycategory img.close-button').click(function(){
			$catLink.click();
		});
		
		$('div#recipesbycategory li a').click(function(evt){
			$catLink.text($(this).text()).click();
			evt.preventDefault();
		});
		
	},
	FooterSetup:function(){
		// set css class on column of footer links
		$('div#footer ul#footer-links li').each(
			function(j){
				var $this = $(this);
				if(j % 3 === 0){$this.addClass('col1');}
				if(j % 3 === 1){$this.addClass('col2');}
				if(j % 3 === 2){$this.addClass('col3');}
			}
		);
	},
	HomeCallouts:function(){
		$callouts = $('#homecallouts');
		if(!$callouts.length){
			return;
		}
		
		$callouts.find('a').hover(
			function(){
				$(this).children('img').stop().animate({opacity:0.0},400);
			},
			function(){
				$(this).children('img').stop().animate({opacity:1.0},400);
			}
		);
	},
	SearchResultsSetup:function(){
		$('#pagecontent:not(.recipebox) ul#products-list li:first').addClass('first');
		
		if(!$('div#filternav').length){
			return;
		}
		// expand/collapse filter groups
		var filterGroups = $('div#filternav div.filter-group');
		filterGroups
			.addClass('open')
			.children('p').toggle(
				function(){
					$(this).parent().removeClass('open').addClass('closed');
				},
				function(){
					$(this).parent().removeClass('closed').addClass('open');
				}
			);
		
		// show more
		filterGroups.children('ul')
			.filter(function(){
				return $(this).children('li').length > _Config.NumOfFilterItemsToShow;
			})
			.each(function(){
				$(this).children('li:gt('+(_Config.NumOfFilterItemsToShow-1)+')').addClass('hidden');
			})
			.append('<li><a href="#" class="viewmore">View More...</a></li>');
			
		
		$('a.viewmore').toggle(
			function(){
				$(this).text('View Less...').parent().siblings('li.hidden').show();
			},
			function(){
				$(this).text('View More...').parent().siblings('li.hidden').hide();
			}
		);
	},
	DinnerCalendarSetup:function(){
		if(!$('#weeklyview').length){
			return;
		}
	
		// attach hover and click
		$('#weeklyview table tr.day')
			.hover(
				function(){$(this).toggleClass('over');}
			)
			.click(function(evt){
				document.location.href = $(this).children('td.action').children('a').attr('href');
			});
			
		// add "first" class to popular recipes list
		$('#popular-recipes li:first').addClass('first');
		
		// add hover to calendar
		$('div#monthlyview > table.calendar > tbody > tr:not(:first)').hover(
			function(){$(this).toggleClass('hover');}
		);
		
		// print popups
		var printcalargs = {
			width:540,
			height:500,
			name:'printcalendar',
			scroll:1
		};
		$('a.printcal,a.printlist').PopupWindow(printcalargs);
	},
	CalendarEventSetup:function(){
		if(!$('ul.calendar-event').length){
			return;
		}
		
		var $currRecipeP = $('ul.calendar-event p#current-recipe');
		$currRecipeP
			.show()
			.find('a')
				.click(function(evt){
					$currRecipeP.hide().next('#change-recipe').show();
					evt.preventDefault();
				});
		
		$currRecipeP.next('#change-recipe').hide();
				
		
	},
	RecipeLanding:function(){
		if(!$('#featured-recipe,.carousel').length){
			return;
		}
		var showFeaturedRecipe = function(id){
			jQuery('.recipe-details,.recipe-image').hide();
			jQuery('#'+id+',#'+id+'-image').show();
		};
		
		showFeaturedRecipe(jQuery('.recipe-details').attr('id'));
		
		// insert scrollbars
		$('#featured-recipe .carousel').prepend('<a id="scroll-prev" class="disabled">&lsaquo;</a><a id="scroll-next">&rsaquo;</a>');
		$('#scroll-prev,#scroll-next','#featured-recipe .carousel')
			.hover(function(){
				$(this).toggleClass('hover');
			});
		
		$('.carousel li','#featured-recipe')
			.click(function(){
				var id = jQuery(this)
							.addClass('selected')
								.siblings('.selected')
								.removeClass('selected')
							.end()
								.children('a')
								.attr('href')
								.replace('#','');
				showFeaturedRecipe(id);
				return false;
			})
			.hover(function(){
				jQuery(this).toggleClass('hover');
			});
		
		$('#featured-recipe #jCarouselLite').jCarouselLite(
			{
				btnNext: "#scroll-next",
				btnPrev: "#scroll-prev", 
				speed:600,
				easing:'backout',
				visible:3,
				circular:false
			}
		);
		
		// make up for IE6 calculation issue where the last item drops to next line
		$('#featured-recipe .carousel li').css('paddingLeft','6px');
		
		$('#recipe-sort ul.tabs').Tabs();
		//$('ul.results-list li:first','.tabbed-content').addClass('first');
		
		$('#recipe-sort').CameraThumbnail();
	},
	RecipeDetail:function(){
		if(!$('#recipe-details').length){
			return;
		}
	
		$('#recipe-details ul.tabs').Tabs();
		$('#suggested').CameraThumbnail();
		$('ul.results-list li:first','.tabbed-content').addClass('first');
		
		$('#about-recipe a[href="#reviews"]').click(function(evt){
			$('#recipe-details ul.tabs a[href="#reviews"]').click();
			var targetOffset = $('ul.tabs').offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 300);
			evt.preventDefault();
		});
	},
	MenuDetail:function(){
		$('#centercolumn ul.results-list').CameraThumbnail();
	},
	Menus:function(){
		$menus = $('.menugroup ul.menus');
		if($menus.length){
			// init menus
			$menus
				.hide()
				.before('<p class="openclosemenus"><a href="#">See All Menus</a></p>');
			
			// attach click events
			$menus.siblings('p.openclosemenus').children('a').toggle(
				function(){
					$(this)
						.text('Close Menus')
						.addClass('open')
						.parent().next().show();
					return false;
				},
				function(){
					$(this)
						.text('See All Menus')
						.removeClass('open')
						.parent().next().hide();
					return false;
				}
			);
			$menus.siblings('p.openclosemenus:first').children('a').click();
		}
	},
	Counter:function(mycounter){
		var $mealCount = $(mycounter);
		if(!$mealCount.length){
			return;
		}
		
		// get the value
		var orig = $mealCount[0].innerHTML;
		
		// ensure it's a number
		var noCommas = orig.replace(/,/g,'');
		var match = /^\d+$/.test(noCommas);
		if(!match){
			return;
		}
		
		// write the HTML for the images
		var numImg = '<img src="/SiteCollectionImages/global/number###.gif" width="24" height="31">';
		var commaImg = '<img src="/SiteCollectionImages/global/comma.gif" width="4" height="7" class="comma">';
		var origSplit = orig.split('');
		$mealCount.html('');
		
		for(var i = 0; i < origSplit.length; i++){
			var elToAppend;
			var current = origSplit[i];
			
			if(current === ','){
				elToAppend = commaImg;
			}
			else {
				elToAppend = numImg.replace('###',current);
			}
			$mealCount.append(elToAppend);
		}
	},
	CookingItalian:function(){
		$ovenreadyLasagne = $('div#ovenready-lasagne');
		if($ovenreadyLasagne.length){
			$ovenreadyLasagne.find('ul.results-list').CameraThumbnail();
			$ovenreadyLasagne.find('dl.reveal-list').RevealList();
		}
		
		$wavyLasagne = $('div#wavy-lasagne');
		if($wavyLasagne.length){
			$wavyLasagne.find('ul.results-list').CameraThumbnail();	
			$wavyLasagne.find('dl.reveal-list').RevealList();
		}
		
		$saucePairingGuide = $('#sauce-pairing-guide');
		if($saucePairingGuide.length){
			$saucePairingGuide.find('ul.results-list').CameraThumbnail();
			$saucePairingGuide.find('dl.reveal-list').RevealList();
		}
	},
	VideoCookingTips:function(){
		// video thumbnail hover
		$vidThumbs = $('a.vid:has(img.photo)','#pagecontent.videocookingtips')
		if($vidThumbs.length){
			$vidThumbs.append('<img src="/SiteCollectionImages/global/vidthumb-overlay.gif" width="154" height="112" alt="" class="overlay">');
			$vidThumbs.hover(
				function(){
					$(this).children('img.overlay').stop().animate(
						{opacity:'0.2'},
						250
					);
				},
				function(){
					$(this).children('img.overlay').stop().animate(
						{opacity:'0.01'},
						250
					);
				}
			);
		}
		
		$vidDescrip = $('.videotips p.description','#pagecontent.videocookingtips');
		if($vidDescrip.length){
			$vidDescrip
				.addClass('collapsed')
				.children('span.moretext')
					.before('<span class="ellips">...</span>')
					.after(' <span class="morelink">More</span>');
			;
			
			$vidDescrip.find('span.morelink').toggle(
				function(){
					$this = $(this);
					$this
						.text('Less')
						.parent('.description')
							.removeClass('collapsed')
					;
				},
				function(){
					$this = $(this);
					$this
						.text('More')
						.parent('.description')
							.addClass('collapsed')
					;
				}
			);
		}
	},
	ProductLine:function(scrollBy){
		BarillaUS.Carouselize(scrollBy);
		
		$plusBenefits = $('div#plus-benefits');
		if($plusBenefits.length){
			$plusBenefits.find('dl.reveal-list').RevealList();
		}
		
	},
	Products:function(){
		// classic bluebox recipes tab
		$recipes = $('div#pagecontent.productline div#recipes,div#pagecontent.productdetail div#featured-recipes,div#pagecontent.productline div#best-life-tips');
		if($recipes.length){
			$recipes.find('ul.results-list').CameraThumbnail();
		}
	},
	ProductSorter:function(){
		SelectedIndexByBrand = 2; // which one should show on load
		SelectedIndexByShape = 1; // which one should show on load
		
		var animSpeed = 250;
		
		$sorterByBrandTab = $('#browseByBrand');
		$sorterByPastaTab = $('#browseByPastaShape');
		
		function ShowScroller(){
			// hide all .jCarouselLite
			$('.carousel .jCarouselLite','#browseByBrand,#browseByPastaShape').hide();
			$('a#scroll-prev,a#scroll-next','#browseByBrand,#browseByPastaShape').remove();

			$('.carousel .jCarouselLite:nth('+SelectedIndexByBrand+')','#browseByBrand:visible').show();
			$('.carousel .jCarouselLite:nth('+SelectedIndexByShape+')','#browseByPastaShape:visible').show();
			
			BarillaUS.Carouselize(4);
			
			$sorterByBrandTab.children('.carousel').find('li:visible')
				.unbind('hover')
				.hover(
					function(){
						$this = $(this);
						$this.children('a.quickview').stop().animate(
							{opacity:'0.85'},
							animSpeed
						);
						$this.find('img.icon').stop().animate(
							{opacity:'1.00'},
							animSpeed
						);
					},
					function(){
						$this = $(this);
						$this.children('a.quickview').stop().animate(
							{opacity:'0.01'},
							animSpeed
						);
						$this.find('img.icon').stop().animate(
							{opacity:'0.25'},
							animSpeed
						);
					}
				)
			;
		}
		
		if($sorterByBrandTab.length && $sorterByPastaTab.length){
			// run showscroller whenever tab is clicked.
			$('#pagecontent.ourproducts ul.tabs > li').bind('click',function(){
				ShowScroller();
			});
			
			// hide package list on pasta tab
			$sorterByPastaTab.children('ul.packages').hide();
			
			ShowScroller();
			
			// by brand tab	
			
			$sorterByBrandTab.find('ul.productLines > li')
				.eq(SelectedIndexByBrand).addClass('selected')
				.end()
				.not('.selected').children('strong,img').css('opacity',0.5)
				.end().end()
				.hover(
					function(){
						$this = $(this).not('.selected');
						$this.children('span.description').stop(true,true).fadeIn(animSpeed);
						$this.children('strong,img').stop().animate(
							{
								opacity:1
							},
							animSpeed
						);
					},
					function(){
						//$(this).not('.selected').removeClass('over');
						$this = $(this).not('.selected');
						$this.children('span.description').fadeOut(animSpeed);
						$this.children('strong,img').stop().animate(
							{
								opacity:0.5
							},
							animSpeed
						);
					}
				)
				.click(
					function(){
						$this = $(this);
						$this.not('.selected')
							.siblings('li')
								.removeClass('selected')
								.children('strong,img').css('opacity',0.5)
								.end()
							.end()
							.children('img,span.description')
								.removeAttr('style')
							.end()
							.addClass('selected');
						
						// show respective .jCarouselLite
						SelectedIndexByBrand = $this.prevAll('li').length;					
						ShowScroller();
					}
				)
			;
			
			// by pasta shapes tab
			$sorterByPastaTab
				.find('ul.shapes > li > a')
					.eq(SelectedIndexByShape).addClass('selected')
					.end()
					.click(function(){
						$this = $(this); 
						$this
							.closest('ul')
								.find('a').removeClass('selected')
							.end().end()
							.addClass('selected')	
						;
						
						SelectedIndexByShape = $this.parent('li').prevAll('li').length;
						ShowScroller();
						
//						return false;
					});
			
			$sorterByPastaTab.find('.jCarouselLite li')
				.hover(
					function(){
						$(this).addClass('over');
					},
					function(){
						$(this).removeClass('over');
					}
				)
				.click(
					function(){
						$(this)
							.closest('ul')
								.children('li').removeClass('selected')
							.end()
							.end()
							.addClass('selected')
						;
						// logic to populate the correct list of packages, or use .net ajax?
						$sorterByPastaTab.children('ul.packages').show();
						$('span#step1',$sorterByPastaTab).hide();
						$('span#step2',$sorterByPastaTab).show();
						
//						return false;
					}
				)
			;

			$sorterByPastaTab.find('ul.packages li').live('mouseover mouseout',function(evt){
				$this = $(this);
				
				if(evt.type == 'mouseover'){
					$this.children('a.quickview').stop().animate(
						{opacity:'0.95'},
						animSpeed
					);
					$this.find('img.icon').stop().animate(
						{opacity:'1.00'},
						animSpeed
					);
				}
				else{
					$this.children('a.quickview,,img.icon').stop().animate(
						{opacity:'0.01'},
						animSpeed
					);
					$this.find('img.icon').stop().animate(
						{opacity:'0.25'},
						animSpeed
					);
				}
			});
		}
	},
	Carouselize:function(scrollBy){
		var numberVisible = 4;
		var numberScroll = scrollBy||1;
		
		$jCarouselLite = jQuery('#jCarouselLite:visible,.jCarouselLite:visible');

		if($jCarouselLite.length){
			// shapes carousel
			jQuery('.carousel','.tabbed-content:visible').prepend('<a id="scroll-prev" class="disabled">&lsaquo;</a><a id="scroll-next">&rsaquo;</a>');
			
			// hide prev/next if items less than visible
			$jCarouselLite.each(
				function(){
					$this = $(this);
					if(numberVisible >= $this.find('li').length){
						$(this).siblings('a#scroll-next,a#scroll-prev').hide();
					}
				}
			);

			jQuery('#scroll-prev,#scroll-next','.carousel')
				.hover(function(){
					$(this).toggleClass('hover');
				});
			
			$jCarouselLite.each(
				function(){
					$(this).jCarouselLite(
						{
							btnNext: "#scroll-next",
							btnPrev: "#scroll-prev", 
							speed:600,
							easing:'backout',
							visible:numberVisible,
							circular:false,
							scroll:numberScroll
						}
					);
				}
			);
		}
	}
};

BarillaUS.Flash = {
	Version:'10',
	CommonParams:{
		bgColor:'transparent',
		wmode:'transparent',
		scale:'noscale'
	},
	WriteSWF:function(flashObject,htmlId){
		if(swfobject.hasFlashPlayerVersion(BarillaUS.Flash.Version)){
			swfobject.createSWF(flashObject,BarillaUS.Flash.CommonParams,htmlId);
		}
	},
	HomePage:{
		data:'/Style%20Library/flash/homepage/homepage.swf?mainXml=%2FStyle%20Library%2Fflash%2Fhomepage%2Fhomepage.xml',
		bgColor:'#EFEFEF',
		width:710,
		height:458
	},
	Quiz:{
		data:'/Style%20Library/flash/quiz/quiz.swf?mainXml=%2FStyle%20Library%2Fflash%2Fquiz%2Fquiz.xml',
		width:838,
		height:390
	},
	FamilyCircle:{
		data:'/Style%20Library/flash/familycircle/BarillaFamilyCircle.swf',
		width:481,
		height:220
	},
	PlacematCreator:{
		data:'/Style Library/flash/placematcreator.swf',
	    height:621,
	    width:960
	},
	WriteLasagneVideo:function(pathToFlv,htmlId){
		var lasagneVideo = {
			data:'/Style Library/flash/videoplayer_7.swf?v='+pathToFlv,
			width:328,
			height:300
		};
		BarillaUS.Flash.WriteSWF(lasagneVideo,htmlId);	
	}
};

// override jQuery "show" method to make sure pngFix is applied.
// see http://stackoverflow.com/questions/1890871/jquery-png-fix-on-hidden-div-and-jquerybrowser-question
var _show = jQuery.fn.show; 
jQuery.fn.show = function(){
	// Execute the original method.
	_show.apply( this, arguments );
	// Fix Png
	return $(this).pngFix();
} 

})(jQuery);
