
/***********************************************
***  FACEBOOK LOCALSTORAGE HACK  ***************
************************************************/

if (typeof(localStorage) === "undefined") {
  var localStorage = {};
}

/***********************************************
***  BUTTON ROLLOVER & PRELOAD  ****************
************************************************/

$(function(){
	// button rollover
	if ($('.imgBtn') != null) {
		$('.imgBtn').hover(function(){
				if(!$(this).is('.disabled'))
					$(this).attr("src", $(this).attr("src").replace(/^(.*?)(?:-hover)?(\..*?)$/i,'$1-hover$2'));
			},
			function(){
					if(!$(this).is('.disabled'))
						$(this).attr("src", $(this).attr("src").replace(/^(.*)-hover(\..*?)$/i, '$1$2'));
				});
	}
	// preload image rollovers
	if ($("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']") != null) {
	    $("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']").each(function(){
			    $("<img>").attr("src", $(this).attr("src").replace(/(btn-.*)(\..*?)/i, '$1-hover$2'));
		    });
	}
});

/***********************************************
***  EXTERNAL LINKS  ***************************
************************************************/

$(document).ready(function() {
	$("a[rel=external]").each(function() {
			$(this).addClass('external').attr({
					title: "Open this page in a new window",
					target: "_blank"
				});
		});
	$('a[href^="http://www.facebook.com/"]').click(function(){
			if(typeof _gaq !== 'undefined')
				_gaq.push(['_trackEvent', 'Navigation', 'Click', 'Facebook']);
		});
	$('a[href^="http://itunes.apple.com/"]').click(function(){
			if(typeof _gaq !== 'undefined')
				_gaq.push(['_trackEvent', 'WhereRU', 'Click', 'AppStore']);
		});
});

/***********************************************
***  JQUERY MASONRY  ***************************
************************************************/

$(window).load(function() {
	$('#boxSet').masonry({
		columnWidth: 175, 
		itemSelector: '.box',
		animate: true
	});
});


/***********************************************
***  FOOTER MENU  ******************************
************************************************/

// Initialize

$('html').addClass('jsEnabled');

$(document).ready(function() {

		// no footer menu on age check page
		if(! $('.ageCheckForm').length){
			// measuring
			var fmv = $('.footerMenuViewport');
			fmv.css({
					width: fmv.width() + 'px'
				});

			// Menu popup

			var slideSpeed = 500;
			var growSpeed = 250;

			function menu_expand(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'Navigation', 'Roll On', 'NavBar']);

				$('#footerContainer').addClass('expanded');
				$('.footerMenuWrapper').stop().animate({
						height: 130
					}, growSpeed, 'easeOutCubic');
				$('.productRangeWrapper').stop().animate({
						bottom: -10
					}, growSpeed, 'easeOutCubic');
			}
			function menu_shrink(){
				$('#footerContainer').removeClass('expanded');
				$('.footerMenuWrapper').stop().animate({
						height: 0
					}, growSpeed, 'easeOutCubic');
				$('.productRangeWrapper').stop().animate({
						bottom: 70
					}, growSpeed, 'easeOutCubic');
			}
			$('#footerContainer').hover(menu_expand, menu_shrink);
			$('.footerMenuBtn').click(function(){
					($('#footerContainer').is('.expanded') ? menu_shrink : menu_expand)();					
				});
		}

		// Menu carousel

		var fm_li_w = 114;
		var fm_ul = $('.footerMenu ul');
		var fm_ul_li = fm_ul.find('li');

		var target_index = Math.floor(fm_ul_li.length /2);
		fm_ul.prepend(fm_ul_li.slice($('.footerMenuViewport li.current').index('.footerMenuViewport li') +2 -target_index));
		fm_ul_li = fm_ul.find('li');
		var fm_ul_l = -(fm_ul_li.length *fm_li_w);
		fm_ul.css({
				left: fm_ul_l +'px',
				width: (fm_ul_li.length *fm_li_w *3) +'px'
			});

		fm_ul.prepend(fm_ul_li.clone()).append(fm_ul_li.clone());

		fm_ul.find('.current').each(function(){
				var thumbOver = $(this).find('img').attr('src');

				$(this).find('a').css({
						background : 'url(' + thumbOver + ') no-repeat center bottom'
					});
				$(this).find('.thumb').css({
						display: 'none'
					});
			});
	
		function shiftMenuLeft(){
			if(typeof _gaq !== 'undefined')
				_gaq.push(['_trackEvent', 'Navigation', 'Slide', 'Right']);
			var fm_ul = $('.footerMenuViewport > ul');

			fm_ul.css({
					left: (fm_ul.stop().position().left +fm_li_w) +'px'
				}).append(fm_ul.find('li:first')).animate({left: fm_ul_l +'px'}, slideSpeed, 'easeOutQuint');
		}
		function shiftMenuRight(){
			if(typeof _gaq !== 'undefined')
				_gaq.push(['_trackEvent', 'Navigation', 'Slide', 'Left']);
			var fm_ul = $('.footerMenuViewport > ul');

			fm_ul.css({
					left: (fm_ul.stop().position().left -fm_li_w) +'px'
				}).prepend(fm_ul.find('li:last')).animate({left: fm_ul_l +'px'}, slideSpeed, 'easeOutQuint');
		}
		$('.menuBtnLeft').hover(function(){
				$(this).stop().animate({
						paddingRight: 7
					}, 200);
			},
			function(){
					$(this).stop().animate({
							paddingRight: 0
						}, 200);
				}).click(shiftMenuRight);
		$('.menuBtnRight').hover(function(){
				$(this).stop().animate({
						paddingLeft: 7
					}, 200);
			},
			function(){
					$(this).stop().animate({
							paddingLeft: 0
						}, 200);
				}).click(shiftMenuLeft);


		// Menu item hovers

		$('.footerMenu li:not(.current)').hover(function() {
				var thumbOver = $(this).find('img').attr('src');
				$(this).find('a').css({
						background : 'url(' + thumbOver + ') no-repeat center bottom'
					});
				$(this).find('.thumb').stop().fadeTo('fast', 0 , function() {
						$(this).hide();
					});
			}, function() {
					$(this).find('.thumb').stop().fadeTo('fast', 1).show();
				}).click(function(){
						if(typeof _gaq !== 'undefined')
							_gaq.push(['_trackEvent', 'Navigation', 'Click', $(this).attr('title')]);
					});
	});


/***********************************************
***  MAIN CAROUSEL  ****************************
************************************************/

$(document).ready(function(){

		// arrows

		$('.carouselContainer .carouselBtnLeft, .productContent .prodPrev').hover(function(){
				$(this).stop().animate({
						paddingRight: 7
					}, 200);
			},
			function(){
					$(this).stop().animate({
							paddingRight: 0
						}, 200);
				});
		$('.carouselContainer .carouselBtnRight, .productContent .prodNext').hover(function(){
				$(this).stop().animate({
						paddingLeft: 7
					}, 200);
			},
			function(){
					$(this).stop().animate({
							paddingLeft: 0
						}, 200);
				});

		// sliding

		var speed = 500;

		var qli_w = 173;
		var mcli_w = 550;

		var lq_ul = $('.carouselQueueLeft ul');
		var rq_ul = $('.carouselQueueRight ul');
		var mc_ul = $('.mainCarousel ul');

		var lq_li = lq_ul.find('li');
		var rq_li = rq_ul.find('li');
		var mc_ul_li = mc_ul.find('li');

		var lq_l = -77;
		lq_l = -rq_li.length *qli_w -77;
		var rq_l = -lq_li.length *qli_w -((lq_li.length -1) *3);
		var q_w = (lq_li.length +rq_li.length) *(qli_w +3) *2;

		lq_ul.prepend(rq_li.clone()).css({
				left: lq_l +'px',
				width: q_w +'px'
			});
		rq_ul.prepend(lq_li.clone()).css({
				left: rq_l +'px',
				width: q_w +'px'
			}).append(lq_li.clone()).append(rq_li.clone());

		var mc_l = -(mc_ul_li.length *mcli_w);

		mc_ul.prepend(mc_ul_li.clone()).append(mc_ul_li.clone()).css({
				left: mc_l +'px',
				width: (mc_ul_li.length *mcli_w *3) +'px'
			});

		Cufon.replace('.mainCarousel .slideContent h1 .line1', { fontFamily: 'shardee' });
		Cufon.replace('.mainCarousel .slideContent h1 .line2', { fontFamily: 'agenda-medium' });

		$('.carouselBtnLeft').click(function(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'Carousel', 'Slide', 'Left']);
				var lq_cl = (lq_ul.position().left -qli_w);
				if(lq_l -lq_cl > 300)
					return;
				lq_ul.stop().css({
						left: lq_cl +'px'
					});
				rq_ul.stop().css({
						left: (rq_ul.position().left -qli_w) +'px'
					});

				lq_ul.prepend(lq_ul.find('li:last'));
				rq_ul.prepend(rq_ul.find('li:last'));

				lq_ul.animate({left: lq_l +'px'}, speed, 'easeInOutCirc');
				rq_ul.animate({left: rq_l +'px'}, speed, 'easeInOutCirc');

				mc_ul.css({
						left: (mc_ul.stop().position().left -mcli_w) + 'px'
					}).prepend(mc_ul.find('li:last'));
				mc_ul.animate({left: mc_l +'px'}, speed, 'easeInOutCirc');
			});
		$('.carouselBtnRight').click(function(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'Carousel', 'Slide', 'Right']);
				var lq_cl = (lq_ul.position().left +qli_w);
				if(lq_cl -lq_l > 300)
					return;
				lq_ul.stop().css({
						left: lq_cl +'px'
					});
				rq_ul.stop().css({
						left: (rq_ul.position().left +qli_w) +'px'
					});

				lq_ul.append(lq_ul.find('li:first'));
				rq_ul.append(rq_ul.find('li:first'));

				lq_ul.animate({left: lq_l +'px'}, speed, 'easeInOutCirc');
				rq_ul.animate({left: rq_l +'px'}, speed, 'easeInOutCirc');

				mc_ul.css({
						left: (mc_ul.stop().position().left +mcli_w) + 'px'
					}).append(mc_ul.find('li:first'));
				mc_ul.animate({left: mc_l +'px'}, speed, 'easeInOutCirc');
			});
		$('.mainCarousel a').click(function(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'Carousel', 'Click', $(this).attr('title')]);
			});
	});
	
/***********************************************
***  PROGRESSIVE ENHANCEMENT  ******************
************************************************/



$(function() {
	// Set .first / .last class;
	//$(".feedList li:last").addClass("last");
});

// Events page hovers

$(document).ready(function(){  
		$('.vidThumbList img, .youTubeLarge img, .fbPhotoGallery img, .magList img, .box .mapLink img').fadeTo(0, .8)
		$('.vidThumbList li, .youTubeLarge, .fbPhotoGallery li, .magList li, .box .mapLink').each(function(){
				var ele = $(this);
				var ele_fc = $(this).find('img');
				ele.hover(function(){
						ele_fc.stop(true, true).fadeTo('fast', 1);
					}, function(){
							ele_fc.stop(true, true).fadeTo('fast', .8);
						});
			});
	});
	
	
// Racing Frames hovers

$(document).ready(function(){  
		$('.racingFrames li').each(function(){
				var ele = $(this);
				var ele_fc = $(this).find('.frameContent');
				ele.hover(function(){
						ele_fc.stop(true, true).fadeIn('fast');
					}, function(){
							ele_fc.stop(true, true).fadeOut('fast');
						});
			});
	});

/***********************************************
***  FACEBOOK SLIDER  **************************
************************************************/

$(document).ready(function(){

		var speed = 500;
		var ps_h = 142;

		var ps_vp = $('.col .photoScroller .scrollerViewport');
		ps_vp.css({
				height: ps_vp.height() +'px',
				overflow: 'hidden'
			});
		var ps_ul = ps_vp.find('ul');
		var ps_li = ps_ul.find('li');
		var ps_uh = ps_li.length *(ps_h +8);

		var ps_ul_t = -ps_li.length *ps_h -(ps_li.length -1) *8;

		ps_ul.css({
				position: 'absolute',
				height: (ps_li.length *3 *ps_h +(ps_li.length *3 -1) *8) *3,
				top: ps_ul_t +'px'
			});
		ps_ul.prepend(ps_li.clone()).append(ps_li.clone());

		$('.photoScroller .buttonLeft').click(function(){
				ps_ul.prepend(ps_ul.find('li:last')).css({
						top: (ps_ul.stop().position().top -ps_h -8) +'px'
					});
				ps_ul.animate({
						top: ps_ul_t
					}, speed, 'easeOutQuint');
			});
		$('.photoScroller .buttonRight').click(function(){
				ps_ul.append(ps_ul.find('li:first')).css({
						top: (ps_ul.stop().position().top +ps_h) +'px'
					});
				ps_ul.animate({
						top: ps_ul_t
					}, speed, 'easeOutQuint');
			});
	});
	
/***********************************************
***  ANIMATED SCROLLING  ***********************
************************************************/

$(document).ready(function() {
		$('.linkIvy, .dreamgirlsHeader .girlsNav .tagLine, .faqLink, .winemakersNav .charles a').click(function(){
				var elementClicked = $(this).attr("href");
				var destination = $(elementClicked).offset().top;
				$.scrollTo( destination, 500, {
						easing: 'easeOutQuint',
						onAfter: function() {
								window.location = elementClicked;
							}
					} );
				return false;
			});
		$('.linkKelly, .winemakersNav .trina a').click(function(){
				var elementClicked = $(this).attr("href");
				var destination = $(elementClicked).offset().top;
				$.scrollTo( destination, 800, {
						easing: 'easeOutQuint',
						onAfter: function() {
								window.location = elementClicked;
							}
					} );
				return false;
			});
		$('.linkHeather, .kelly a').click(function(){
				var elementClicked = $(this).attr("href");
				var destination = $(elementClicked).offset().top;
				$.scrollTo( destination, 1100, {
						easing: 'easeOutQuint',
						onAfter: function() {
								window.location = elementClicked;
							}
					} );
				return false;
			});
		$('.carrie a').click(function(){
				var elementClicked = $(this).attr("href");
				var destination = $(elementClicked).offset().top;
				$.scrollTo( destination, 1400, {
						easing: 'easeOutQuint',
						onAfter: function() {
								window.location = elementClicked;
							}
					} );
				return false;
			});
		$('.backToTop').click(function(){
				var elementClicked = $(this).attr("href");
				var destination = $(elementClicked).offset().top;
				$.scrollTo( destination, 500, {
						easing: 'easeOutQuint',
						onAfter: function() {
								window.location = elementClicked;
							}
					} );
				return false;
			});
	});

	
/***********************************************
***  DREAMGIRLS HOVER  *************************
************************************************/

$(document).ready(function() {
		$('.linkIvy').hover(function(){
				var div = $('#thumbIvy');
				div.css({
						'background' : 'url(' + div.find('img').attr('src') + ') no-repeat center bottom'
					});
				div.find('span').stop(true, true).fadeOut('fast');
			}, function(){
					var div = $('#thumbIvy');
					div.find('span').stop(true, true).fadeIn('fast');
				});
		$('.linkKelly').hover(function(){
				var div = $('#thumbKelly');
				div.css({
						'background' : 'url(' + div.find('img').attr('src') + ') no-repeat center bottom'
					});
				div.find('span').stop(true, true).fadeOut('fast');
			}, function(){
					var div = $('#thumbKelly');
					div.find('span').stop(true, true).fadeIn('fast');
				});
		$('.linkHeather').hover(function(){
				var div = $('#thumbHeather');
				div.css({
						'background' : 'url(' + div.find('img').attr('src') + ') no-repeat center bottom'
					});
				div.find('span').stop(true, true).fadeOut('fast');
			}, function(){
					var div = $('#thumbHeather');
					div.find('span').stop(true, true).fadeIn('fast');
				});

		$('.mainCarousel a[href="#dreamgirlIvy"]').click(function(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'DreamGirls', 'Click', 'Heather']);
			});
		$('.mainCarousel a[href="#dreamgirlKelly"]').click(function(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'DreamGirls', 'Click', 'Kelly']);
			});
		$('.mainCarousel a[href="#dreamgirlHeather"]').click(function(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'DreamGirls', 'Click', 'Heather']);
			});
	});

/***********************************************
***  HISTORY FRAMES HOVER  *********************
************************************************/

$(document).ready(function(){
		$('.historyFrames .winery a').hover(function(){
				$(this).siblings('.frameCover').stop().animate({
						bottom: '27px'
					}, 200);
			}, function(){
					$(this).siblings('.frameCover').stop().animate({
							bottom: '8px'
						}, 200);
			});
		$('.historyFrames .products a').hover(function(){
				$(this).siblings('.frameCover').stop().animate({
						bottom: '27px'
					}, 200);
			}, function(){
					$(this).siblings('.frameCover').stop().animate({
							bottom: '11px'
						}, 200);
			});
		$('.historyFrames .bubblyGirls a').hover(function(){
				$(this).siblings('.frameCover').stop().animate({
						bottom: '27px'
					}, 200);
			}, function(){
					$(this).siblings('.frameCover').stop().animate({
							bottom: '10px'
						}, 200);
			});
	});

	
/***********************************************
***  FAQ  **************************************
************************************************/

$(document).ready(function(){

		// arrows

		$('.appFAQContent h3').hover(function(){
				$(this).stop().animate({
						backgroundPosition: '7px 7px'
					}, 200);
			}, function(){
					$(this).stop().animate({
							backgroundPosition: '0px 7px'
						}, 200);
				}).click(function(){
						var li = $(this).closest('li');
						$('.faqList li.expanded').not(li).stop().removeClass('expanded').find('div').animate({
								height: '0px'
							});
						var div = $(this).closest('li').find('div');
						div.stop().animate({
								height: $(li).is('.expanded') ? '0px' : $(div).data('originalHeight') +'px'
							});
						li.toggleClass('expanded');
					});
		$('.faqList > li > div').each(function(){
				var height = $(this).height();
				$(this).css({
						height: '0px'
					}).data('originalHeight', height);
			});
	});

/***********************************************
***  WHAT'S ON  ********************************
************************************************/

$(document).ready(function(){

		$('.archive h3').click(function(){
						var ul = $(this).closest('.archive').find('.archiveList');
						$('.archiveList.expanded').not(ul).stop().removeClass('expanded').animate({
								height: '0px'
							});
						ul.stop().animate({
								height: $(ul).is('.expanded') ? '0px' : $(ul).data('originalHeight') +'px'
							});
						ul.toggleClass('expanded');
					});
		$('.archive .archiveList').each(function(){
				var height = $(this).height();
				$(this).css({
						height: '0px'
					}).data('originalHeight', height);
			});
	});
	
/***********************************************
***  LDA  **************************************
************************************************/

$(document).ready(function(){
		$('form[action="/age-check/"]').submit(function(){
				if(typeof _gaq !== 'undefined')
					_gaq.push(['_trackEvent', 'LDA', 'Click', 'Submit']);
			});
	});
	
	
/***********************************************
***  WINEMAKERS HOVER  *************************
************************************************/

$(document).ready(function() {
		$('.charles a, .trina a, .kelly a, .carrie a').hover(function(){
				var div = $(this).parent().find('.thumb');
				div.css({
						background : 'url(' + div.find('img').attr('src') + ') no-repeat center bottom'
					});
				div.find('span').stop(true, true).fadeOut('fast');
			}, function(){
					var div = $(this).parent().find('.thumb');
					div.find('span').stop(true, true).fadeIn('fast');
				});
	});
	

