/*
Javascript for AntonEmdin.com

Uses jQuery 1.3.1
*/

$(function(){
	ae.init();
});

var ae = {

	init : function() {
	
		/*
		Global settings
		============================================================ 
		*/
		var settings = {
			extraShortDelay					: "500",
			shortDelay					: "1000",
			mediumDelay					: "3000",
			featureAreaElement	: ".featured",
			thumbnails					: ".portfolio ul li img"
		};
		
		/*
		// do this on DOMReady:
		============================================================ 
		*/
		
		$.getScript("javascript/jquery.hoverIntent.minified.js", function(){
			// functions that require hoverIntent go here
		  	initArrows(settings.featureAreaElement);
			initThumbnails(settings.thumbnails);
			initLightbox();
			fadePanelIn(settings.featureAreaElement);
		});
		
		var currentFeatureIndex = 0;
		var galleryIndex = 0;
		var currentGalleryList;

		/*
		// functions
		============================================================ 
		*/
		// feature area functions
		function featureAjaxCall(featureUrl){
			var featureAjaxObject = $.ajax({
				type		: "GET",
				url			: "ajaxfeature/" + featureUrl,
				error		: function(xhr, desc, exceptionobj) {
					alert("There was an error loading this content");
				},
				success	: function(data) {
					$(settings.featureAreaElement).html(data);
					initArrows(settings.featureAreaElement);
					fadePanelIn(settings.featureAreaElement);
				}
			});
		}
		
		function fadePanelIn(container){
			var hero = $(container + " .hero:first");

			// preload image first, once loaded, fade it in
			var img = new Image();
			$(img).load(function () {

				$(this).hide();

				hero.replaceWith(this);
			
				if ($.browser.msie && $.browser.version <= 6){
					$(this).css("display", "block");		// IE goes funny if you try to animate opacity
				} else {
					$(this).fadeIn("normal").css("display", "block");		// but the rest seem fine
				}
				
				// Stretch the lightbox
				if (container.indexOf("lightbox") > 0) {
					$(".lightbox").css("height", "")
						.animate({ 	
						"width" : $(this).width() + 100, 
						"marginTop" : ($(this).height() > $(window).height()) ? "0px" : ($(window).height() - $(this).height()) / 2 + "px"
					}, "fast");
				}
				
				$(this).addClass("hero");
				$(this).queue(function() {
					$(container + " .overlay")
						.css( "width", Math.floor($(container + " .hero").outerWidth() - 40) + "px" );
					if (container.indexOf("lightbox") == -1) {
						$(container + " .overlay").slideDown(settings.shortDelay);
					} else {
						$(container + " .info").show();
					}
					$(".lightbox .overlay").css( "left", (Math.floor( ($(".lightbox").outerWidth() - $(container + " .hero").outerWidth())/ 2) - 10) + "px" );
					initSlidingPanel(container);
					$(this).dequeue();
				});
			})
			.attr('src', hero.get(0).src);
			
		}
		
		function initSlidingPanel(hoverElement) {
			$(hoverElement).hoverIntent( function() {
				$(hoverElement + " .info").hide();
				$(hoverElement + " .overlay").slideDown(settings.shortDelay);
			}, function() {
				$(hoverElement + " .info").show();
				$(hoverElement + " .overlay").slideUp(settings.shortDelay);
			});
		}
		
		function initArrows(container) {
		
			var rightArrow = $("<div></div>")
				.addClass("button-right")
				.attr("title","Next")
				.click(function() { nextFeature(container); });
				
			var leftArrow = $("<div></div>")
				.addClass("button-left")
				.attr("title","Previous")
				.click(function() { previousFeature(container); });
			
			$(leftArrow).appendTo(container);
			$(rightArrow).appendTo(container);
			
			if (!($.browser.msie && $.browser.version <= 6)){
				$(".button-left, .button-right", container)
					.css("opacity", 0.65)
					.hoverIntent( function() {
						$(this).fadeTo("fast",1);
					}, function() {
						$(this).fadeTo("slow",0.5);
					});
			}
		}
		
		function nextFeature(container) {
			$(settings.featureAreaElement + " .hero", container).fadeOut("fast");
			$(settings.featureAreaElement + " .overlay", container).slideUp("fast");
			$(container).unbind("hoverIntent");
			$(container + " .overlay").remove();
			
			// for when in the feature area
			if (container.indexOf("lightbox") == -1) {
				if (currentFeatureIndex < featureUrls.length - 1) {
					currentFeatureIndex++;
				} else {
					currentFeatureIndex = 0;
				}
				featureAjaxCall(featureUrls[currentFeatureIndex]);
				
			// for when in a lightbox
			} else {
				if (currentGalleryIndex < currentGalleryList.length - 1) {
					currentGalleryIndex++;
				} else {
					currentGalleryIndex = 0;
				}
				lightboxAjaxCall($(currentGalleryList[currentGalleryIndex]).children("a")[0].href.replace(/folio\//,"ajax/"));
			}
		}
		
		function previousFeature(container) {
			$(settings.featureAreaElement + " .hero", container).fadeOut("fast");
			$(settings.featureAreaElement + " .overlay", container).slideUp("fast");
			$(container).unbind("hoverIntent");
			
			// for when in the feature area
			if (container.indexOf("lightbox") == -1) {
				if (currentFeatureIndex > 0) {
					currentFeatureIndex--;
				} else {
					currentFeatureIndex = featureUrls.length - 1;
				}
				featureAjaxCall(featureUrls[currentFeatureIndex]);
				
			// for when in a lightbox	
			} else {
				if (currentGalleryIndex > 0) {
					currentGalleryIndex--;
				} else {
					currentGalleryIndex = currentGalleryList.length - 1;
				}
				lightboxAjaxCall($(currentGalleryList[currentGalleryIndex]).children("a")[0].href.replace(/folio\//,"ajax/"));
			}
		}
		
		function initThumbnails(thumbnails) {
			var thumbs = $(thumbnails);
			if (!($.browser.msie && $.browser.version <= 6))
			{
				thumbs.hoverIntent(function() {
					$(this).parents("li").siblings().fadeTo(settings.extraShortDelay, 0.35);
					$(this).parents("li").fadeTo(settings.extraShortDelay, 1);
				}, function(){ });
				thumbs.parents("ul").hoverIntent(function() {
				}, function() {
					$(this).children("li").fadeTo(settings.shortDelay, 1);
				});
			}
			thumbs.parents("a").click(function() {
				openLightbox(this.href.replace(/folio\//,"ajax/"));
				currentGalleryList = $(this).parents("ul").children("li");
				currentGalleryIndex = currentGalleryList.index($(this).parents("li"));

				return false;
			});
		}
		
		// lightbox functions
		function initLightbox() {
			var lightboxScreen = $("<div></div>")
				.addClass("lightboxScreen")
				.click(function() { closeLightbox(); });
			var lightboxContainer = $("<div></div>")
				.addClass("lightboxContainer")
				.click(function() { closeLightbox(); });
			var lightbox = $("<div></div>")
				.addClass("lightbox")
				.click(function(event) {
					event.stopPropagation();
				});
			var lightboxClose = $("<div></div>")
				.addClass("close")
				.attr("title", "Close")
				.click(function() { closeLightbox(); });
			$("#container").after(lightboxScreen);
			$(".lightboxScreen").after(lightboxContainer);
			lightbox.appendTo(".lightboxContainer");
			$("<div></div>").addClass("inner").appendTo(".lightbox");
			lightboxClose.appendTo(".lightbox");
			initArrows(".lightbox");
		}
		
		function openLightbox(lightboxContentUrl) {
			$(".lightboxScreen")
				.css({
					"display"	: "block",
					"opacity"	: "0.1"
				})
				.fadeTo(settings.shortDelay, 0.75)
				.queue(function() {
					$(".lightboxContainer").css("top", $().scrollTop()).show();
					$(".lightbox").css("margin-top", ($(window).height() - $(".lightbox").height()) / 3 + "px" );
					$(".lightbox .inner").empty();
					lightboxAjaxCall(lightboxContentUrl);
					$(this).dequeue();
				});
		}
		
		function closeLightbox() {
			$(".lightboxContainer").hide();
			$(".lightboxScreen").fadeOut();
		}
		
		function lightboxAjaxCall(lightboxContentUrl){
			var lightboxAjaxObject = $.ajax({
				type		: "GET",
				url			: lightboxContentUrl,
				error		: function(xhr, desc, exceptionobj) {
					alert("There was an error loading this content");
				},
				success	: function(data) {
					var boxHeight = $(".lightbox").height(); // This gets set here, and removed on fadeIn to prevent collapsing
					boxHeight = parseInt(boxHeight) < 50 ? 600 : boxHeight;
					$(".lightbox").css( { "height" : boxHeight, "marginTop" : (boxHeight > $(window).height()) ? "0px" : ($(window).height() - boxHeight) / 2 + "px" } );
					$(".lightbox .inner").html(data);
					fadePanelIn(".lightbox " + settings.featureAreaElement);
				}
			});
		}
		
	}
};