jQuery(document).ready(function($) {
	// Hero Images
	function preloadImages(){
		var i = 0;
		var img;
		for (i=0; i < imageList.length; i++){
			img = $(new Image());
			img.load(onPreloadComplete);
			img.attr("src", imageList[i]["image_url"]);
		}
	}
	
	function onPreloadComplete(e){
		$(this).remove();
	}
	
	preloadImages();
	var featuredNav = $("#index-featured-nav ul");
	featuredNav.removeClass("css");
	var hoverable = $("li a", featuredNav);
	hoverable.each(function(index){$(this).append("<span class='hover'>"+ $(this).text()+"</span>");});
	$(".hover", hoverable).hide();
	hoverable.hover(tNavFadeIn, tNavFadeOut);
	hoverable.each(function(i){$(this).bind('click', {index:i}, tNavClick);});
	
	var featuredText = $("#index-featured-text");
	var currentText = $(":eq(0)", featuredText);
	$("li:not(:eq(0))", featuredText).fadeOut(0);
	var indexCTA = $("#index-cta");
	indexCTA.text(imageList[0]["click_text"]);
	indexCTA.attr("href", imageList[0]["click_url"]);
	var reference = $("#index-reference");
	reference.text(imageList[0]["reference"]);
	
	var loadingImg;
	var currentIndex = -1;
	
	function tNavFadeIn(eventObj){
		$(".hover", this).stop(true);
		$(".hover", this).fadeTo(300, 1);
	}
	
	function tNavFadeOut(eventObj){
		$(".hover", this).stop(true);
		$(".hover", this).fadeOut(300, 0);
	}
	
	var cta = $("#index .index-btn");
	cta.removeClass("css");
	var ctaHoverable = cta.append("<span class='hover'><!-- Hover --></span>");
	$(".hover", cta).hide();
	cta.hover(tNavFadeIn, tNavFadeOut);
	
	function tNavClick(e){
		e.preventDefault();
		if(autoInterval){
			clearInterval(autoInterval);
		}
		switchToStory(e.data.index);
	}
	
	function switchToStory(index){
		if(currentIndex != index){
			currentIndex = index;
			if(loadingImg){
				loadingImg.remove();
			}
			indexCTA.text("Loading...");
			indexCTA.attr("href", "#");
			indexCTA.click(function(c){c.preventDefault();});
			indexCTA.addClass("loading");
			var img = new Image();
			$img = $(img);
			loadingImg = $img;
			$img.addClass("trans-image");
			setAttr($img, index);
			$("li:not(:eq("+index+"))", featuredNav).removeClass("current");
			$("li:eq("+index+")", featuredNav).addClass("current");
			$img.bind('load', {index:index}, backgroundImgLoad);
			$img.attr("src", imageList[index]["image_url"]);
		}
	}
	
	var autoInterval = setInterval(autoSwitch, 15000);
	
	function autoSwitch(){
		var tempIndex = currentIndex + 1;
		if(tempIndex >= imageList.length){
			tempIndex = 0;
		}
		switchToStory(tempIndex);
	}
	
	function backgroundImgLoad(e){
		loadingImg = null;
		indexCTA.text(imageList[e.data.index]["click_text"]);
		indexCTA.attr("href", imageList[e.data.index]["click_url"]);
		indexCTA.unbind("click");
		indexCTA.removeClass("loading");
		reference.text(imageList[e.data.index]["reference"]);
		featuredText.stop(true);
		$("li:not(:eq(" + e.data.index + "))", featuredText).fadeOut(0);
		$("li:eq(" + e.data.index +")", featuredText).fadeIn(0);
		$(this).hide();
		$(this).appendTo($("#stretch"));
		$(this).fadeIn(400, onFadeInComplete);
		resizeHandler();
		var currentImg = $("#stretch .perm-image");
		var difference;
		if(currentImg.length){
			difference = (currentImg.height() + parseFloat(currentImg.css('top'))) - ($(this).height() + parseFloat($(this).css('top')));
		} else {
			difference = 0;
		}
		if(difference >= 0){
			$(this).css("padding-bottom",difference);
		}
	}
	
	function onFadeInComplete(){
		$("#stretch .perm-image").remove();
		$(this).removeClass("trans-image");
		$(this).css("padding-bottom", 0);
		$(this).addClass("perm-image");
	}
	
	switchToStory(0);
	
	$(window).resize(resizeHandler);
	
	function resizeHandler(){
		var winWidth = $(window).width();
		var limitHeight = 0;
		$("#fullscreen img").each(function(){
			
			var eachRatio = Number($(this).attr("aspect_ratio"));
			var maxWidth = Number($(this).attr("max_width"));
			var minWidth = Number($(this).attr("min_width"));
			var maxOff = Number($(this).attr("max_offset"));
			var minOff = Number($(this).attr("min_offset"));
			
			var theWidth = (minWidth >= winWidth)?minWidth:winWidth;
			theWidth = (maxWidth <= winWidth)?maxWidth:theWidth;
			
			$(this).css('width',theWidth);
			$(this).css('height',Math.round(theWidth/ eachRatio));
			var eachScaleAmount = Math.abs((theWidth - minWidth)/(maxWidth - minWidth));
			$(this).css("top", String(Math.round(minOff + eachScaleAmount*(maxOff - minOff))) + "px");
			$(this).css("left", String(Math.round((((winWidth > 1024)?winWidth:1024) - theWidth)/2)) + "px");
			
			if($(this).height() > limitHeight){
				limitHeight = Number($(this).height());
			}
		});
		$("#stretch").height(limitHeight);
		var sideGrad = $("#fullscreen .side-gradient");
		if(winWidth >= 1440){
			sideGrad.css("display", "block");
			sideGrad.css("left", String(Math.round((winWidth - 1440)/2)) + "px");
		} else {
			sideGrad.css("display", "none");
		}
	}
	
	function setAttr(tar, index){
		var tarObj = imageList[index];
		tar.attr("aspect_ratio", tarObj["aspect_ratio"]).attr("max_width", tarObj["max_width"]).attr("min_width", tarObj["min_width"]).attr("max_offset",tarObj["max_offset"]).attr("min_offset",tarObj["min_offset"]);
	}
	
	//---------
	var sidenav = $(".sidenav");
	var sidenavHover = $("li a", sidenav);
	sidenavHover.hover(sNavIn, sNavOut);
	
	function sNavIn(e){
		var caption = $(".caption", this);
		var p = $("p", caption);
		if(p.text().length){
			caption.stop(true);
			caption.animate({"top":p.innerHeight() * -1, "height":p.innerHeight() + 20}, 500);
		}
	}
	
	function sNavOut(e){
		var caption = $(".caption", this);
		caption.stop(true);
		caption.animate({"top":0, "height":20}, 500);
	}
});
