$(document).ready(function() {

		$(".menu-item").hover(function() {
			var classString = $(this).find(':first-child').attr('class');
			var selectedIconString = classString.split(' ')[1] + '-selected';
			
			
			$(this).find(':first-child').attr('class', 'menu-icon ' + selectedIconString);
			
		}, function() {
			var classString = $(this).find(':first-child').attr('class');
			var selectedIconString = classString.split(' ')[1];
			var unselectedIconString = selectedIconString.substring(0, selectedIconString.length - 9);
			$(this).find(':first-child').attr('class', 'menu-icon ' + unselectedIconString);
		});
		
		var slides = $(".slide");
		var slideshowDots = $(".slideshow-dot");
		
		$.address.change(function(event) {  
			try {
				var slideIndex = slides.index($("#" + event.value.substr(1)));
				
				if(slideIndex >= 0) {
					goToSlide(slideIndex);
					return;
				}
			} catch(e) {}
			
			goToSlide(0); // bookmark not found.
			
		});  
		
		var initialSlide = true;
		function goToSlide(index) {
			
			var slide = slides[index];
			
			//$.address.value($(slide).attr("id"));
			
			/*slides.removeClass("slideSelected");
			//$(slide).delay(200).addClass("slideSelected", 1300);
			$(slide).delay(200).addClass("slideSelected");*/
			
			slideshowDots.removeClass("dot-selected");
			$(slideshowDots[index]).addClass("dot-selected");
			
			
			var pixelOffset = index * -900;
			var animationDuration = initialSlide ? 0 : 1500;
			
			$("#slideshow").animate({ 
				marginLeft: pixelOffset + "px"
			  }, {queue:false, duration:animationDuration, easing:"easeOutExpo"} );
			
			initialSlide = false;
			
		}
		
		//goToSlide(3);
		
		// todo: could stop the anim timers if the slide is not currently visible
		var zeeboxScreenshots = $("#zeeboxiPhoneSlideshow .iPhoneScreenshot");
		zeeboxScreenshots.data('currentScreenshotIndex', 0);
		var zeeboxQuotes = $("#zeeboxQuotes .zeeboxQuote");
		zeeboxQuotes.data('currentScreenshotIndex', 0);
		
		var busNycScreenshots = $("#busNYCiPhoneSlideshow .iPhoneScreenshot");
		busNycScreenshots.data('currentScreenshotIndex', 0);
		var offlineMailScreenshots = $("#offlineMailiPhoneSlideshow .iPhoneScreenshot");
		offlineMailScreenshots.data('currentScreenshotIndex', 0);
		setInterval(function() { nextIPhoneScreenshot(zeeboxScreenshots); }, 4000);
		setInterval(function() { nextIPhoneScreenshot(zeeboxQuotes); }, 4000);
		setInterval(function() { nextIPhoneScreenshot(busNycScreenshots); }, 4000);
		setInterval(function() { nextIPhoneScreenshot(offlineMailScreenshots); }, 4000);
		
		function nextIPhoneScreenshot(screenshots){
			var index = screenshots.data("currentScreenshotIndex");
			$(screenshots[index % screenshots.length]).fadeOut(800);
			$(screenshots[((index + 1)  % screenshots.length)]).fadeIn(800);
			screenshots.data("currentScreenshotIndex", index + 1);
		}
		
		$("#whatWeDo .generalSpriteIcon").click(function() {
			return false; // Do not browse to #
		});
		
		
		var comingSoonImage = $("#comingSoon");
		$("#teamMenuItem").hover(function() {
			var menuItemPosition = $(this).position();
			comingSoonImage.css( { "left": (menuItemPosition.left) + "px", "top":menuItemPosition.top-35 + "px" } );
			comingSoonImage.fadeIn();
		}, function() {
			comingSoonImage.hide();
		});
		

		$("#teamMenuItem").click(function() {
			return false; // do nothing for now
		});
		
		
		var contactPanel = $("#contactPanel");
		$("#contactMenuItem").click(function() {
			$('html, body').animate({scrollTop: $(document).height()-$(window).height()}, {duration:1000});
			//contactPanel.delay(100).animate({ backgroundColor: "#B4EEB4" }, 1000);
			//contactPanel.delay(1100).animate({ backgroundColor: "transparent" }, 1000);
			contactPanel.css("backgroundColor", "#FFFFFF");
			contactPanel.delay(200).effect("highlight", { }, 3000);
			return false;
		});
		
		$(".humansInventGMagImageHolder").mouseenter(function() { 
			var humansInventGMagImageHolder = $(this);
			$(".hiGmagOverlay", this).stop(true,true).fadeTo(1000, 0.9, function() {
				$(".hiGMagScreenshotText, .hiGmagVisitNow", humansInventGMagImageHolder).stop(true,true).fadeIn();
			});
		});
		

		$(".humansInventGMagImageHolder").mouseleave(function() { 
			
			var humansInventGMagImageHolder = $(this);
			
			$(".hiGmagOverlay, .hiGMagScreenshotText, .hiGmagVisitNow", this).stop(true,true).fadeOut(function() {
			});
		});
		
});
