(function($) {
	$.fn.bodyselector = function () {
		return this.each(function () {
		
			var cycleProgress = 1;
			function cycle($currBlip) {
				if ($currBlip.length == 0) {
					$currBlip = $(".bodyblips .bodyblip").first();
				}
				if (cycleProgress == 1) {
					changeState($currBlip,1);
					var timeout = setTimeout(function () {
						changeState($currBlip,0);
						cycle($currBlip.next().next());
					}, 1000);
				}
			}
			
			var begindelay = setTimeout(function () {
				cycle($(".bodyblips .bodyblip").first());
			}, 2000);
			
			// Background swapper
			if ($(".bodyselector").length != 0) {
				// all phrases
				var phrase = [];
				phrase[0] = "Cold & Hot Mobile Compression Therapy Supports";
				phrase[1] = "Advanced Sports Technology For Accelerated Recovery";
				phrase[2] = "Accelerate Your Healing From Painful Injuries or Surgery";
				
				// all the background images
				var bkg = [];
				bkg[0] = "/sites/all/themes/thermo/images/body-bg-ankle.jpg";
				bkg[1] = "/sites/all/themes/thermo/images/body-bg-back.jpg";
				bkg[2] = "/sites/all/themes/thermo/images/body-bg-elbow.jpg";
				bkg[3] = "/sites/all/themes/thermo/images/body-bg-knee.jpg";
				bkg[4] = "/sites/all/themes/thermo/images/body-bg-shoulder.jpg";
				bkg[5] = "/sites/all/themes/thermo/images/body-bg-wrist.jpg";
				// randomize the list
				var cache = [];
				for (var n = 0; n < bkg.length; n++) {
					var rand = Math.floor(Math.random() * bkg.length);
					var temp = bkg[n];
					bkg[n] = bkg[rand];
					bkg[rand] = temp;
				}
				// set timer
				bkgTimer = setInterval(function () { changeBkg() },5000);
				$(".bodyselector").css("background-image","url("+bkg[0]+")");
			}
			var currBkg = 1;
			var currPhrase = 1;
			function changeBkg() {
				$(".bodyselector").css("background-image","url("+bkg[currBkg]+")");
				if (currBkg == bkg.length-1) {
					currBkg = 0;
				} else {
					currBkg++;
				}
				
				$(".bodyselector .toptext h1").html(phrase[currPhrase]);
				if (currPhrase == phrase.length - 1) {
					currPhrase = 0;
				} else {
					currPhrase++;
				}
			}
	
			function changeState($blip,toWhat) {	
				// Obtain the body part
				var part = $blip.attr('id').replace("bodyblip-","");

				// User has hovered over a blip
				if (toWhat == 1) {
					// change sprite
					$blip.css('background-position','33px 0');
				// User has removed cursor from blip
				} else {
					// Reset blip state, unless it has been locked
					if ($blip.data('locked') != 1 && $blip.data('clicked') != 1) {
						$blip.css('background-position','0 0');
					}
				}
			}
			
			// Loads a new product into the homepage body
			function loadProduct(prodID) {
				// Obtain product cell
				$product = $(".bodyselector-products").find(".prodid-"+prodID);
				// Hide the placeholder
				$(".bodyselector .placeholder").hide();
				// Hide visible products
				$(".bodyselector .bodyselector-product:visible").hide();
				// Show this product
				$product.show();
			}
			
			function clearAllExcept($curr) {
				// unclick all other blips
				$(".bodyselector .bodyblip").each(function () {
					$(this).data('clicked',0);
					$(this).data('locked',0);
					if ($(this).attr('id') != $curr.attr('id')) {
						changeState($(this),0);
					}
				});
			}
			
			$(".bodyblip")
				.hover(function () {
					cycleProgress = 0;
				
					// Lock it so it stays on
					$(this).data('locked',1);
					// change state of blip to ON
					changeState($(this),1);

					// show the container
					var part = $(this).attr('id').replace("bodyblip-","");
					$blipContainer = $(this).parent().find("#bodyblip-content-"+part);
					showContainer($blipContainer);
					
				}, function () {
					// change state of blip to OFF
					changeState($(this),0);
					
					// hide the container, but with a delay
					var part = $(this).attr('id').replace("bodyblip-","");
					$blipContainer = $(this).parent().find("#bodyblip-content-"+part);
					hideContainer($blipContainer);
					
				});
			
			$(".bodyblip-content") 
				.hover(function () {
					// hovered into a container, so remove the delayed hide
					// (it may not have one, but doesn't matter)
					$(this).clearQueue();
				}, function () {
					// hide container
					hideContainer($(this));
				});
				
			function showContainer($container) {
				$container.clearQueue().stop().show();
			}
			
			function hideContainer($container) {
				$container.clearQueue().delay(100).hide(0, function () {
					$container.prev().data('locked',0);
					changeState($container.prev(),0);
				});
			}
				
			// For the main page body selector, cancel out links and instead load
			// the product into view
			$(".bodyselector .bodyblip-content-product a").click(function () {
				// reset background
				$(".bodyselector").css("background-image","url(/sites/all/themes/thermo/images/body-bg.jpg)");
				// reset timer
				bkgTimer = clearTimeout(bkgTimer);
				
				$blip = $(this).closest(".bodyblip-content").prev();
				clearAllExcept($blip);
				
				// set this one to clicked
				$blip.data('clicked',1);
				
				// obtain product id
				var prodID = $(this).parent().attr('class').replace("prodid-","");
				loadProduct(prodID);
				
				return false;
			});

			
		});
	};
})(jQuery);

