$(function() {
	
	var createHashQuery = function() {
		
		var hashQuery = {};
		var hashes = location.hash.substring(1).split("&");
		
		for(var i=0; i<hashes.length; i++) {
			
			var hash = hashes[i].split("=");
			hashQuery[hash[0]] = hash[1];
			
		}
		
		return hashQuery;
		
	}
	
	var hashQuery = createHashQuery();
	
	$(window).bind("hashchange", function() {
		
		hashQuery = createHashQuery();
		
	});
	
	$("#main-nav").each(function() {
		
		var $container = $(this);
		var $inner = $(">.inner", $container);
		$container.append("<div class='sub-menu-drop'/>");
		var $dropper = $(".sub-menu-drop", $container);
		
		$dropper._hideOnClose = true;
		
		$(".sub-menu", $container).siblings("a").addClass("droppable").closest("li").mouseenter(function() {
			
			var $subMenu = $(this).children(".sub-menu");
			
			clearTimeout($subMenu[0]._hideTimeout);
			$dropper._hideOnClose = false;
			
			if (!$dropper.is(":visible"))
				$dropper.height(0).show();
			
			if (!$subMenu.is(":visible")) {
				
				$subMenu.css("opacity", 0).show();
				
				if ($subMenu.is("ul")) {
				
					var width = 0;
					
					$(">li", $subMenu).each(function() {
						
						width += $(this).outerWidth(true);
						
					});
					
					$subMenu.width(width + 1);
				
				}
				
			}
			
			$subMenu.css({
				
				left: "50%",
				right: "auto",
				marginLeft: -Math.round($subMenu.width()/2)
				
			});
			//if ($subMenu.offset().left < 0)
			if ($subMenu.offset().left < $inner.offset().left)
				$subMenu.css({
					
					left: 0,
					marginLeft: 0
					
				});
			//else if ($subMenu.offset().left + $subMenu.width() > $("body").width())
			else if (($subMenu.offset().left + $subMenu.width()) > ($inner.offset().left + $inner.width()))
				$subMenu.css({
					
					left: "auto",
					right: 0,
					marginLeft: 0
					
				});
			
			$dropper.stop(true).animate({
			
				height: 32
				
			}, {
				
				duration: 300,
				complete: function() {
						
					$subMenu.stop(true).animate({
						
						opacity: 1
						
					}, {
						
						duration: 300,
						complete: function() {

							$(this).css("opacity", "");
						
						}
					
					});
				
				}
			
			});
			
		}).mouseleave(function() {
			
			var $subMenu = $(this).children(".sub-menu");
			
			clearTimeout($subMenu[0]._hideTimeout);
			$dropper._hideOnClose = true;
			
			$subMenu[0]._hideTimeout = setTimeout(function() {
			
				$subMenu.stop(true).animate({
				
					opacity: 0
					
				}, {
					
					duration: 300,
					complete: function() {
							
						$(this).hide().css("opacity", "");
						
						if ($dropper._hideOnClose == true) {
						
							$dropper.stop(true).animate({
								
								height: 0
								
							}, {
								
								duration: 300,
								complete: function() {
								
									if ($(this).height() == 0)
										$(this).hide();									
								
								}
							
							});
						
						}
					
					}
				
				});
			
			}, 500);
			
		});
		
	});
	
	$("#main-nav ul.menu-right li.search").mouseenter(function() {
		
		$("input[name=s]", $(this)).focus();
		
	});

	$(".testimonial-rotator").each(function() {
		
		var $container = $(this);
		var $inner = $(">.inner", $container);
		var $items = $(">.testimonial", $inner);
		var $nav = $(">.nav", $container);
		
		var delay = 4500;
		
		$nav.append("<ul />");
		var $navList = $(">ul", $nav);
		
		var timeoutId = -1;
		
		$items.each(function() {
			
			$navList.append("<li><a href='javascript:;'></a></li>");
			
		});
		
		var $navListItems = $(">li", $navList);
		
		var startIdx = 0;
		
		$items.eq(startIdx).addClass("active").show();
		$navListItems.eq(startIdx).addClass("active");
		
		$inner.height($items.eq(startIdx).height());
		
		var rotateToItem = function($activeItem, $nextItem) {
			
			if (!$nextItem.is(".active") && !$activeItem.is(":animated") && !$nextItem.is(":animated")) {
				
				$activeItem.stop(true).animate({
					
					opacity: 0
					
				}, {
					
					duration: 500,
					complete: function() {

						$activeItem.hide().removeClass("active");
						$nextItem.addClass("active");
						
						$navListItems.eq($activeItem.index()).removeClass("active");
						$navListItems.eq($nextItem.index()).addClass("active");

						$inner.stop(true).animate({
							
							height: $nextItem.height()
							
						}, {
							
							duration: Math.abs($inner.height() - $nextItem.height()) / .25,
							complete: function() {
							
								$nextItem.stop(true).css("opacity", 0).show().animate({
									
									opacity: 1
									
								}, {
									
									duration: 500,
									complete: function() {
										
										$nextItem.css("opacity", "");
										scheduleAutoRotate();
									
									}
									
								});
								
								
							}
						});
						
					}
					
				});
			
			}
			
		};
		
		var scheduleAutoRotate = function() {
		
			clearTimeout(timeoutId);
			
			timeoutId = setTimeout(function() {
				
				var $activeItem = $items.filter(".active");
				var $nextItem = $activeItem.next();
				
				if (!$nextItem.length)
					$nextItem = $activeItem.siblings().first();
				
				rotateToItem($activeItem, $nextItem);
				
			}, delay);
		
		};
		
		$(">a", $navListItems).click(function() {
			
			clearTimeout(timeoutId);
			
			rotateToItem($items.filter(".active"), $items.eq($(this).parent("li").index()));
			
			return false;
			
		});
		
		scheduleAutoRotate();
		
		$inner.mouseenter(function() {
			
			clearTimeout(timeoutId);
			
		}).mouseleave(function() {
			
			scheduleAutoRotate();
			
		});
		
	});
	
	$(".tabbed-pages").each(function() {
		
		var $container = $(this);
		var $inner = $(">.inner", $container);
		var $items = $(">.item", $inner);
		var $nav = $(">.tabs", $container);
		var $navListItems = $(">li", $nav);
		
		var startIdx = 0;

		if (hashQuery.tab) {

			startIdx = $items.filter("#" + hashQuery.tab).index();
			if (startIdx < 0) startIdx = 0;
			
		}
		
		$items.eq(startIdx).addClass("active").show().siblings(".active").removeClass("active");
		$navListItems.eq(startIdx).addClass("active").siblings(".active").removeClass("active");;
		
		$inner.height($items.eq(startIdx).height());
		
		var displayItem = function($nextItem) {
			
			var $activeItem = $items.filter(".active");
			
			if (!$nextItem.is(".active") && !$activeItem.is(":animated") && !$nextItem.is(":animated")) {
				
				$activeItem.stop(true).animate({
					
					opacity: 0
					
				}, {
					
					duration: 500,
					complete: function() {

						$activeItem.hide().removeClass("active");
						$nextItem.addClass("active");
						
						$navListItems.eq($activeItem.index()).removeClass("active");
						$navListItems.eq($nextItem.index()).addClass("active");

						$inner.stop(true).animate({
							
							height: $nextItem.height()
							
						}, {
							
							duration: Math.abs($inner.height() - $nextItem.height()) / .5,
							complete: function() {
							
								$nextItem.stop(true).css("opacity", 0).show().animate({
									
									opacity: 1
									
								}, {
									
									duration: 500,
									complete: function() {
										
										$nextItem.css("opacity", "");
										
									}
									
								});
								
								
							}
						});
						
					}
					
				});
			
			}
			
		};
		
		$(">a", $navListItems).click(function() {

			displayItem($items.eq($(this).parent("li").index()));
			
		});
		
		$(window).bind("hashchange", function() {

			if (hashQuery.tab) {
			
				var $nextItem = $items.filter("#" + hashQuery.tab);
				
				if ($nextItem.length)
					displayItem($nextItem);
			
			}
			
		});
		
	});
	
	$("a.lightbox").fancybox({
		padding: 0,
		overlayColor: "#232d2a"
	});
	
	$(".product-list .product").live("mouseenter", function() {
		
		var $button = $(".button", $(this));
		
		if (!$button.is(":visible"))
			$button.css("opacity", 0).show();
		
		$button.stop(true).animate({
			
			opacity: 1
			
		}, {
			
			duration: 300,
			complete: function() { $(this).css("opacity", ""); }
			
		});
		
	}).live("mouseleave", function() {
		
		var $button = $(".button", $(this));
		
		$button.stop(true).animate({
			
			opacity: 0
			
		}, {
			
			duration: 300,
			complete: function() { $(this).hide().css("opacity", ""); }
			
		});
		
	});
	
	$(".featured-deals").each(function() {
		
		var $container = $(this);
		var $items = $(">.product", $container);
		
		var delay = 4500;
		var timeoutId = -1;
		var maxHeight = 0;
		
		$items.each(function() {
			
			if ($(this).height() > maxHeight)
				maxHeight = $(this).height();
			
		});
		
		$container.wrapInner("<div class='inner' />");
		var $inner = $(">.inner", $container);
		$items = $(">.product", $inner);
		$container.append("<ul class='nav' />");
		var $nav = $(">.nav", $container);
		
		$items.each(function() {
			
			$nav.append("<li><a href='javascript:;'></a></li>");
			
		});
		
		var $navItems = $(">li", $nav);
		
		$inner.height(maxHeight);
		
		var startIdx = 0;
		
		$items.eq(startIdx).addClass("active").show();
		$navItems.eq(startIdx).addClass("active");
		
		var rotateToItem = function($nextItem) {
			
			var $activeItem = $items.filter(".active");
			
			if (!$nextItem.is(".active") && !$activeItem.is(":animated") && !$nextItem.is(":animated")) {
				
				$activeItem.removeClass("active");
				$nextItem.addClass("active");
				
				$navItems.eq($nextItem.index()).addClass("active").siblings(".active").removeClass("active");
				
				if (!$nextItem.is(":visible"))
					$nextItem.css("opacity", 0).show();
				
				$nextItem.stop(true).animate({
					
					opacity: 1
					
				}, {
					
					duration: 1000,
					complete: function() {
							
						$nextItem.css("opacity", "");
						$activeItem.hide();
						scheduleAutoRotate();
					
					}
					
				});
			
			}
			
		};
		
		$(">a", $navItems).click(function() {
			
			rotateToItem($items.eq($(this).parent("li").index()));
			
			return false;
			
		});
		
		var scheduleAutoRotate = function() {
		
			clearTimeout(timeoutId);
			
			timeoutId = setTimeout(function() {
				
				var $activeItem = $items.filter(".active");
				var $nextItem = $activeItem.next();
				
				if (!$nextItem.length)
					$nextItem = $activeItem.siblings().first();
				
				rotateToItem($nextItem);
				
			}, delay);
		
		};
		
		scheduleAutoRotate();
		
		$inner.mouseenter(function() {
			
			clearTimeout(timeoutId);
			
		}).mouseleave(function() {
			
			scheduleAutoRotate();
			
		});
		
	});
	
	$(".product-rotator").each(function() {
		
		var $container = $(this);
		var $items = $(">.product", $container);
		
		if ($items.length > 3) {
		
			$container.wrapInner("<div class='inner'><div class='mover'/></div>");
			var $inner = $(">.inner", $container);
			var $mover = $(">.mover", $inner);
			$items = $(">.product", $mover);
			
			var itemWidth = $items.outerWidth(true);
			var timeoutId = -1;
			var delay = 4500;
			var rotateForward = true;
			
			$mover.width(itemWidth * $items.length);
			
			$container.append("<a href='javascript:;' class='prev' title='Previous' /><a href='javascript:;' class='next' title='Next' />");
			
			$items.first().addClass("active");
			
			$(".prev", $container).hide();
			
			if ($items.length <= 3)
				$(".next", $container).hide();
			
			$(".next, .prev", $container).click(function() {
				
				clearTimeout(timeoutId);
				
				var isNext = $(this).is(".next");
				
				var $activeItem = $items.filter(".active");
				var $lastGroupItem = isNext ? $activeItem.nextAll().eq(1) : $activeItem.prevAll().eq(1);
				var $nextItem = isNext ? $lastGroupItem.next() : $lastGroupItem.prev();
				
				if (isNext && $nextItem.nextAll().length < 2) {
					
					$nextItem = $items.eq($items.length - 3);
					rotateForward = false;
					
				} else if (!isNext && $nextItem.prevAll().length < 2) {
					
					$nextItem = $items.first();
					rotateForward = true;
					
				}
				
				if (!$nextItem.is(".active")) {
				
					$nextItem.addClass("active");
					$activeItem.removeClass("active");
					
					var diff = Math.abs(-$nextItem.position().left + $mover.position().left);
					
					$mover.stop(true).animate({
						
						left: -$nextItem.position().left
						
					}, {
					
						duration: diff / 1.2,
						complete: function() {
						
							scheduleAutoRotate();
							
							if ($nextItem.index() + 3 == $items.length)
								$(".next", $container).hide();
							else
								$(".next", $container).show();
	
							if ($nextItem.index() > 0)
								$(".prev", $container).show();
							else
								$(".prev", $container).hide();
						
						}
						
					});
				
				}
				
			});
			
			var scheduleAutoRotate = function() {
				
				clearTimeout(timeoutId);
				
				timeoutId = setTimeout(function() {
	
					$(rotateForward ? ".next" : ".prev", $container).click();
					
				}, delay);
			
			};
			
			scheduleAutoRotate();
			
			$inner.mouseenter(function() {
				
				clearTimeout(timeoutId);
				
			}).mouseleave(function() {
				
				scheduleAutoRotate();
				
			});
		
		}
		
	});
	
	$(".image-container a").mouseenter(function() {
		
		$(".image>span", $(this)).css("opacity", "");
		
	}).mouseleave(function() {
		
		$(".image>span", $(this)).css("opacity", .5);
		
	}).find(".image>span").css("opacity", .5);
	
	$("#content form input[type=text], #content form textarea").focusin(function() {
		
		var $input = $(this);
		var $label = $input.siblings("label");
		
		if (!$input.val()) {
		
			$label.show().stop(true).animate({
				
				opacity: .2
				
			}, 150);
		
		}
		
	}).focusout(function() {
		
		var $input = $(this);
		var $label = $input.siblings("label");
		
		if (!$input.val()) {
			
			$label.stop(true).animate({
				
				opacity: 1
				
			}, 150, function() { $(this).css("opacity", ""); });

		}
		
	}).bind("keyup change", function() {
		
		var $input = $(this);
		var $label = $input.siblings("label");
		
		if (!$input.val()) {
		
			$label.show().stop(true).animate({
				
				opacity: .2
				
			}, 150);
		
		} else {
			
			$label.hide();
			
		}
		
	}).each(function() {
		
		var $input = $(this);
		var $label = $input.siblings("label");
		
		if ($input.val())
			$label.hide();
		
	});
	
	if ($(".google-map").length) {
		
		$.getScript("http://maps.googleapis.com/maps/api/js?sensor=false&callback=loadMaps");
		
		window.loadMaps = function() {
			
			$(".google-map").each(function() {
				
				var $map = $(this);
				var coords = $map.data("coords").split(",");
				coords = new google.maps.LatLng(coords[0], coords[1]);

				var map = new google.maps.Map($map[0], {
					
					zoom: 15,
					maxZoom: 17,
					minZoom: 10,
					center: coords,
					mapTypeId: google.maps.MapTypeId.ROADMAP
					
				});
				
				var marker = new google.maps.Marker({
					
					position: coords,
					map: map,
					title: $map.data("marker-title")
					
				});
				
			});
			
		}
		
	}
	
	$(".product-has-upgrades").each(function() {
		
		var $content = $(">.content", $(this));
		var $upgrades = $(">.upgrades", $content);
		
		$content.css("min-height", $upgrades.outerHeight(true));
		
	});
	
});
