/* Author: Andrew Norell
*/
(function ($) {
	"use strict";
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function () {
		var i;
		var args_len = arguments.length;
		for (i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	};
  
	var userAgent = navigator.userAgent.toLowerCase();
})(jQuery);

$(document).ready(function () {
	"use strict";

	$(".dkbg").css({opacity: 0.9});
	
	$(".details").each(function(index) {
		//gathers height of each dropdown, and sets their initial position
		var intOuterHeight = $(this).outerHeight();
		var intHolderHeight = 480;
		$(this).data("toppos", -(intOuterHeight)).css("top", "-" + intOuterHeight + "px");
		$(this).data("left", $(this).position().left + 110);
		
		if (intOuterHeight < 475) {
			intHolderHeight = intOuterHeight;
		}
		
		if ($("#email", this).css("top") !== "auto") {
			intHolderHeight += 7;
		}
		
		$(this).parent().data("height", intHolderHeight + 10);
	});
	
	var strEasingEq = "easeInOutQuint";
	var intAnimationTime = 420;
	
	function navHoverOn(jqThis) {
		var detailJq = $("#" + $(jqThis).parent().attr("class") + " .details");
		clearTimeout($(jqThis).data("hoverTimeout"));
		$("#" + $(jqThis).parent().attr("class")).stop(true, false).delay(100).animate({height:$("#" + $(jqThis).parent().attr("class")).data("height")}, intAnimationTime, strEasingEq);
		detailJq.stop(true, false).delay(100).animate({top:0}, intAnimationTime, strEasingEq);
		$(jqThis).data("active", true);
		$("aside").css("z-index", "8");
		$("#" + $(jqThis).parent().attr("class")).css("z-index", "9");
	}
	function navHoverOut(jqThis) {
		clearTimeout($(jqThis).data("hoverTimeout"));
		var navItem = $(jqThis);
		//delays retraction, in case of user mousing over the dropdown		
		navItem.data("hoverTimeout", setTimeout(function() {
			var detailDropDown = $("#" + navItem.parent().attr("class") + " .details");
			$("#" + navItem.parent().attr("class")).stop(true, false).animate({height:0}, intAnimationTime, strEasingEq);
			detailDropDown.stop(true, false).animate({top: detailDropDown.data("toppos")}, intAnimationTime, strEasingEq);
			navItem.data("active", false);
		}, 80));
	}
	
	$("header ul li:not(.blog) a").hover(function() {
		navHoverOn(this);
	}, function() {
		navHoverOut(this);
	});
	$("header ul li:not(.blog) a").click(function() {
		return false;
	});
	
	$("body:not(.mobile) .details, body:not(.iphoneipad) .details").hover(function() {
		var navItem = $("." + $(this).parent().attr("id") + " a");
		clearTimeout(navItem.data("hoverTimeout"));
		if (navItem.data("active") == false) {
			$("#" + navItem.parent().attr("class")).stop(true, false).animate({height:$("#" + navItem.parent().attr("class")).data("height")}, intAnimationTime, strEasingEq);
			$("#" + navItem.parent().attr("class") + " .details").stop(true, false).animate({top:0}, intAnimationTime, strEasingEq);
			navItem.data("active", true);
		}
	}, function() {
		var navItem = $("." + $(this).parent().attr("id") + " a");
		clearTimeout(navItem.data("hoverTimeout"));
		//delays retraction, in case of user mousing over the dropdown
		navItem.data("hoverTimeout", setTimeout(function() {
			var detailDropDown = $("#" + navItem.parent().attr("class") + " .details");
			$("#" + navItem.parent().attr("class")).stop(true, false).animate({height:0}, intAnimationTime, strEasingEq);
			detailDropDown.stop(true, false).animate({top: detailDropDown.data("toppos")}, intAnimationTime, strEasingEq);
			navItem.data("active", false);
		}, 80));
	});
	
	//mobile/touch version of nav hover?
	/*
	$(".mobile header ul li:not(.blog) a").click(function() {
		if ($(jqThis).data("expanded") == true) {
			navHoverOut(this);
		}
		return false;
	});
	*/
	
	$("header").delay(250).animate({"top": "-10px"}, 720, "easeOutBack");
	
	function offToTheRaces() {
		$("#television").delay(200).fadeTo(700, 1, "easeInOutSine");
		$("#internet").delay(1000).fadeTo(700, 1, "easeInOutSine", function() {
			setTimeout(function() {
				$("#container").hide().css({visibility: "visible"}).fadeIn(650, "easeOutSine", function() {
					if ($(".iphoneipad").length > 0) {
						$("#container").height($("#container").height());
					}
				});	
			}, 400);
		});
	}
	
	var bolFirstLoaded = false;
	var televisionImg = new Image();
	$(televisionImg).load(function () {
		if (bolFirstLoaded) {
			offToTheRaces();
		} else {
			bolFirstLoaded = true;
		}
	}).attr("src", "/images/television.png");
	
	var internetImg = new Image();
	$(internetImg).load(function () {
		if (bolFirstLoaded) {
			offToTheRaces();
		} else {
			bolFirstLoaded = true;
		}
	}).attr("src", "/images/internet.png");
	
	jQuery.preLoadImages("/images/nav/capabilities2.png", "/images/nav/clients2.png", "/images/nav/contact2.png", "/images/nav/blog2.png");
});
