(function($) {
	$(document).ready(
		function() {
			$(".benefits-graphic").find(".grams").hover(
				function() {
					$(this).addClass("active");
					$(this).closest(".benefits-graphic").find(".plus").addClass("active");
				},
				function() {
					$(this).removeClass("active");
					$(this).closest(".benefits-graphic").find(".plus").removeClass("active");
				});

			$(".benefits-graphic").find(".three-to-one").hover(
				function() {
					$(this).addClass("active");
					$(this).closest(".benefits-graphic").find(".plus").addClass("active");
					$(this).closest(".benefits-graphic").find(".equals").addClass("active");
				},
				function() {
					$(this).removeClass("active");
					$(this).closest(".benefits-graphic").find(".plus").removeClass("active");
					$(this).closest(".benefits-graphic").find(".equals").removeClass("active");
				});

			$(".benefits-graphic").find(".faster-recov").hover(
				function() {
					$(this).addClass("active");
					$(this).closest(".benefits-graphic").find(".equals").addClass("active");
				},
				function() {
					$(this).removeClass("active");
					$(this).closest(".benefits-graphic").find(".equals").removeClass("active");
				});

			//////////////// Testimonials //////////////

			$(".testimonial").each(
				function() {
					var testimonial = this;

					var testimonials_loading = async.queue(
						function (func, callback) {
							func(callback);
						}, 1);

					async.series([
						function(callback) {
							$(testimonial).TestimonialScenario();
							$(testimonial).Testimonials({}, callback);
						},
						function(callback) {
							$(testimonial).TestimonialsSetCurrent(Math.ceil(Math.random() * ($(testimonial).TestimonialsSize() - 1)));

							$(testimonial).find(".prev-testimonial").click(
								function() {
									testimonials_loading.push(
										function (callback) {
											if($(testimonial).TestimonialsGet() <= 0) {
												callback();
												return;
											}

											async.series([
												function(callback) {
													$(testimonial).TestimonialScenarioPrev($(testimonial).find(".testimonial-template").eq(1), callback);
												},
												function(callback) {
													$(testimonial).TestimonialsExtend({
														move: "right"
													});
													$(testimonial).TestimonialsSetNext($(testimonial).TestimonialsGet() - 1);
													$(testimonial).TestimonialsAnimate(callback);
												},
												function(callback) {
													$(testimonial).TestimonialScenarioPrevAfter($(testimonial).find(".testimonial-template").eq(1), callback);
												}
												], function() {
													callback();
												});
										});
								});
							$(testimonial).find(".next-testimonial").click(
								function() {
									testimonials_loading.push(
										function (callback) {
											if($(testimonial).TestimonialsGet() >= $(testimonial).TestimonialsSize() - 1) {
												callback();
												return;
											}

											async.series([
												function(callback) {
													$(testimonial).TestimonialScenarioNext($(testimonial).find(".testimonial-template").eq(1), callback);
												},
												function(callback) {
													$(testimonial).TestimonialsExtend({
														move: "right"
													});
													$(testimonial).TestimonialsSetNext($(testimonial).TestimonialsGet() + 1);
													$(testimonial).TestimonialsAnimate(callback);
												},
												function(callback) {
													$(testimonial).TestimonialScenarioNextAfter($(testimonial).find(".testimonial-template").eq(1), callback);
												}
												], function() {
													callback();
												});
										});
								});
							callback();
						}
						]);
				});
			////////////////////////////////////////////

			//////////////// Banners //////////////

			var banners_info = [
			{
				"header": "Try it today. <br />Feel it tomorrow.<span class='trade'>&#153;</span>",
				"background-image": "/_images/home_page/banner-2.jpg",
				"text": "Proper muscle recovery is critical to <br />achieving your personal best.",
				"more": {
					"href": "/muscle-recovery/muscle-recovery-science.aspx?icid=RG0007",
					"text": "WHY MUSCLE RECOVERY MATTERS"
				},
				"addClass": "banner-2"
			},
			{
				"header": "The Power of <br /><em>Natural Cocoa</em>",
				"background-image": "/_images/home_page/banner-1.jpg",
				"text": "What makes reGen&#153; stand out? It's the <br />natural cocoa.",
				"more": {
					"href": "/cocoa-science/power-of-cocoa.aspx?icid=RG0005",
					"text": "SEE THE SCIENCE BEHIND IT ALL"
				},
				"addClass": "banner-1"
			},
			{
				"header": "",
				"background-image": "/_images/home_page/banner-3.png",
				"text": "<img alt=\"\" src=\"/_images/home_page/team_regen.png\">",
				"more": {
				    "href": "/contact-us/team.aspx",
					"text": "LEARN ABOUT TEAM REGEN ATHLETES"
				},
				"addClass": "banner-3"
            },
			{
			    "header": "reGen<sup>&trade;</sup>",
			    "background-image": "/_images/home_page/banner-4.jpg",
			    "text": "Now available in select<br/><span><em><img alt=\"\" src=\"/_images/home_page/GNC.png\"></em> stores</span>",
			    "more": {
			        "href": "/promotions/where_to_buy.aspx?icid=RG0039",
			        "text": "WHERE TO BUY"
			    },
			    "addClass": "banner-4"
			},
			{
			    "header": "The Ultimate<br/><span>Power Play</span>",
			    "background-image": "/_images/home_page/banner-5.jpg",
			    "text": "reGen<sup>&trade;</sup> is the Official Muscle Recovery<br/>Beverage of the Philadelphia Independence<br/>Women's Professional Soccer Team.",
			    "more": {
			        "href": "/promotions/partners.aspx?icid=RG0049",
			        "text": "LEARN ABOUT OUR PARTNERSHIPS"
			    },
			    "addClass": "banner-5"
			}
			];

			$(".banners").each(
				function() {
					var banners = this;

					$(banners).Banners({}, banners_info);

					$(banners).BannersSetCurrentBanner(0);

					var banners_loading = async.queue(
						function (func, callback) {
							func(callback);
						}, 1);

					function next_banner(index, auto) {
						banners_loading.push(
							function (callback) {
								if(window.banner_repeat_timeout != undefined) {
									clearInterval(window.banner_repeat_timeout);
								}

								var current_index = $(banners).BannersGetCurrentIndex();
								if(current_index == index) {
									callback.call($(banners).get(0));
									return;
								}

								if(index > current_index) {
									$(banners).BannersExtend({
										move: "left"
									});
								} else {
									$(banners).BannersExtend({
										move: "right"
									});
								}
								$(banners).BannersSetNextBanner(index);

								async.series([
									function(callback) {
										$(banners).BannerScenarioRevertAnimate(callback);
									},
									function(callback) {
										$(banners).BannerScenarioInit($(banners).find(".banner").eq(2));
										$(banners).BannersAnimate(callback);
									},
									function(callback) {
										$(banners).BannerScenarioAnimate(callback);
									}
									], function() {
										var timing = 10000;
										if(auto) {
										    timing = 10000;
										}
										window.banner_repeat_timeout = setInterval(
											function() {
												var all_count = $(banners).find(".carousel input").size();
												var new_index = (index + 1) % all_count;
												$(banners).find(".carousel input").eq(new_index).trigger("click");
											}, timing);
										callback();
									});
							});
					}

					$(banners).BannerScenario();
					$(banners).BannerScenarioInit($(banners).find(".banner").eq(1));
					$(banners).BannerScenarioAnimate(function() {
						if(window.banner_repeat_timeout != undefined) {
							clearInterval(window.banner_repeat_timeout);
						}
						window.banner_repeat_timeout = setInterval(
							function() {
								var all_count = $(banners).find(".carousel input").size();
								var new_index = ($(banners).BannersGetCurrentIndex() + 1) % all_count;
								next_banner(new_index, true);
				            }, 10000);
					});

					$(banners).find(".carousel input").click(
						function() {
							var index = $(this).index();
							next_banner(index, false);
						});
				});
			///////////////////////////////////////

			//////////////// FacebookFeed //////////////

			$(".conversation-block").each(
				function() {
					var conversation_block = this;

					$(conversation_block).find(".vertical-scroll .up-button, .down-button").bind(
						"dragstart", function(event) {
							event.preventDefault();
							return false;
						})
					.bind(
						"selectstart",
						function(event) {
							event.preventDefault();
							return false;
						});

					$(conversation_block).find(".conversation").FacebookFeed({
						uri: "https://graph.facebook.com/regen2recover/feed?access_token=186233921408385|9ed174fe0c360c2be756a6a1.0-100001142311534|uE0tJa_YYtZKX9a0jfmyFF4HrGA"
					});
					var loading = async.queue(
						function (func, callback) {
							func(callback);
						}, 1);

					loading.push(
						function (callback) {
							$(conversation_block).find(".conversation").FacebookFeedLoad(10, callback);
						});

					var load_more = function(event) {
						if(event.percent < 0.9) {
							return;
						}
						$(conversation_block).unbind("VerticalScrollerPercent", load_more);
						loading.push(
							function (callback) {
								$(conversation_block).find(".conversation").FacebookFeedLoad(10, callback);
							});
						loading.push(
							function (callback) {
								$(conversation_block).VerticalScroller();
								$(conversation_block).bind("VerticalScrollerPercent", load_more);
								callback();
							});
					}

					$(conversation_block).bind("VerticalScrollerPercent", load_more);

					loading.push(
						function (callback) {
							$(conversation_block).VerticalScroller();
							callback();
						});
				});
		////////////////////////////////////////////
		});
})(jQuery);

