YUI(yuiConfig).use(
    'anim', 'node', 'idle-timer',
    function(Y) {

	Showcase = {
	    descriptions: Array(),
	    images: Array(),
	    uiIndex: -1,             // UI indexing starts at 1, 
	    internalIndex: -1,       // internal indexing starts at 0
	    arrowClicked: false,
	    autocycleEnabled: false,
	    autocycleDelay: 11000,
	    fadeInDuration: 0.2,
	    fadeOutDuration: 0.3,
	    fadeInDelay: 100,
	    fragmentPollingDelay: 100,
	    naviCenteringDelay: 300
	};

	Showcase.initialize = function() {
	    
	    this.loadImages();
	    this.loadDescriptions();
	    this.attachLinks();
	    this.navigateWithFragment();
	    this.startFragmentPolling();
	    this.startAutocycle();
	    
	};

	Showcase.loadImages = function() {

	    Y.all("#images div").each(
		function(node, i) { 
		    var img = Y.Node.create("<img>");
		    img.setAttribute("src", node.getAttribute("data-src"));
		    Showcase.images.push(img);
		});

	    if (Y.one("#showcase-navi ul")) {
		Y.all("#thumbnails img").each(
		    function(node, i) { 
			var listItem = Y.Node.create("<li></li>").append(
			    Y.Node.create("<a data-internal-index=\"" + i + "\" href=\"#" + (i + 1) + "\" href=\"#"+i+"\"></a>").append(node));
			Y.one("#showcase-navi ul").append(listItem);
		    });

		Y.all("#showcase-navi ul a").on("click", function() { Showcase.autocycleEnabled = false; });
	    }

	    Y.all("#showcase-navi li").on("mouseover",
					  function(e) {
					      e.currentTarget.addClass("hover");
					  });
	    Y.all("#showcase-navi li").on("mouseout",
					  function(e) {
					      e.currentTarget.removeClass("hover");
					  });

	};

	Showcase.loadDescriptions = function() {
	    Y.all("#descriptions h2").each(
		function(node, i) {
		    var intro = Y.Node.create('<div></div>');
		    var siblings = node.get('parentNode').get('children');
		    var ownIndex = siblings.indexOf(node);
		    var detailCount = 0;

		    for (var j = ownIndex; j < siblings.size(); ++j) {
			if (j > ownIndex && siblings.item(j).get('nodeName') == "H2") {
			    break;
			}

			var sibling = siblings.item(j);

			if (sibling.hasClass("detail")) {
			    ++detailCount;
			    sibling.addClass("detail-" + detailCount);
			} else if (sibling.get('nodeName') == "P") {
			    sibling.addClass("lead");
			}

			intro.append(siblings.item(j));
		    }

		    intro.addClass("detail-count-" + detailCount);

		    Showcase.descriptions.push(intro);
		});
	};

	Showcase.attachLinks = function() {
	  Y.all(".arrow").on("click", 
			     function() {
				 Showcase.arrowClicked = true;
				 Showcase.autocycleEnabled = false;
			     });
	};

	Showcase.startAutocycle = function() {
	    Y.IdleTimer.subscribe("idle", Showcase.autocycle);
	    Y.IdleTimer.start(Showcase.autocycleDelay);
	};

	Showcase.autocycle = function() {
	    if (Showcase.autocycleEnabled) {
		var newUiIndex = (Showcase.uiIndex) % Showcase.maxUiIndex() + 1;
		window.location.hash = "#" + newUiIndex;
		Y.IdleTimer.stop();
		Y.IdleTimer.start(Showcase.autocycleDelay);
	    }	    

	};

	Showcase.startFragmentPolling = function() {
	    Y.later(this.fragmentPollingDelay, this, "navigateWithFragment", Array(), true);
	};

	Showcase.navigateWithFragment = function() {
	    var newUiIndex = this.parseFragment();
	    if (newUiIndex != this.uiIndex) {

		if (this.uiIndex == -1) {
		    var firstNavigation = true;
		}

		this.setIndex(newUiIndex);
		this.updateSelection();

		if (firstNavigation) {

 		    this.navigate(true);

		} else {

		    var fade = fadeOutAnim();
		    if (this.arrowClicked) {
			this.centerNavi();
		    }
		    fade.on('end', this.navigate, this);
		    fade.run();
		    
		}
 	    }
	};

	Showcase.parseFragment = function() {

	    var newUiIndex = parseInt(window.location.hash.substring(1));

	    if (!newUiIndex ||
			newUiIndex < 1) {
			newUiIndex = 1;
	    }
	    if (newUiIndex > this.maxUiIndex()) {
			newUiIndex = this.maxUiIndex();
	    }
	    
	    return newUiIndex;
	};

	function fadeOutAnim() {
	    return new Y.Anim(
		    {
			node: "#current",
			duration: Showcase.fadeOutDuration,
			to: {
			    opacity: 0
			}
		    });
	}

	function fadeInAnim() {
	    return new Y.Anim(
		    {
			node: "#current",
			duration: Showcase.fadeInDuration,
			to: {
			    opacity: 1
			}
		    });	    
	}

	Showcase.maxUiIndex = function() {
	    return Math.min(this.images.length, this.descriptions.length);
	};

	Showcase.setIndex = function(uiIndex) {
	    this.uiIndex = uiIndex;
	    this.internalIndex = uiIndex - 1;
	};

	Showcase.navigate = function(first) {

	    this.updateArrows();

	    replaceChildren(Y.one("#image"), this.images[this.internalIndex]);
	    replaceChildren(Y.one("#current .text"), this.descriptions[this.internalIndex]);

	    if (first === true) {
		Y.one("#current").setStyle("opacity", "1");
	    } else {
		var show = fadeInAnim();
		Y.later(this.fadeInDelay, show, "run");
	    }

	    if (!this.arrowClicked) {
		Y.later(this.naviCenteringDelay, this, "centerNavi");
	    }
	    this.arrowClicked = false;
	};

	function replaceChildren(parent, child) {
	    parent.get("childNodes").remove();
	    parent.append(child);
	}

	Showcase.centerNavi = function() {
	    if (Y.one("#showcase-navi .scrollbox")) {

		var anim = new Y.Anim({
					  node: "#showcase-navi ul",
					  duration: 0.8,
					  to: {
					      left: (308 - 128 * this.internalIndex)
					  },
					  easing: Y.Easing.easeBoth
					  
				      });
		anim.run();
		
	    }	
	};

	Showcase.updateSelection = function() {
	    if (Y.one("#showcase-navi ul")) {
			Y.all("#showcase-navi li").removeClass("selected");
			//Y.one("#showcase-navi a[data-internal-index="+this.internalIndex+"]").get("parentNode").addClass("selected");
	    }
	};

	Showcase.updateArrows = function() {
	    if (Y.one(".arrow")) {
		Y.one(".arrow.previous").setAttribute("href", "#" + Math.max(1, this.uiIndex-1));
		Y.one(".arrow.next").setAttribute("href", "#" + Math.min(this.maxUiIndex(), this.uiIndex+1));
		Y.all(".arrow").each(
		    function(n) {
			if (n.getAttribute("href").substring(1) == Showcase.uiIndex) {
			    n.addClass("disabled");
			} else {
			    n.removeClass("disabled");
			}
		    });
		
	    }
	};

	Y.on("domready", Showcase.initialize, Showcase);
	
    }
);

function playVideo() {
    Showcase.autocycleEnabled = false;
    var player = document.getElementById("video");
    player.style.display = 'block';
    flowplayer("player", "/inc/player/flowplayer-3.2.5.swf", {
		   play: { opacity: 0 }
	       });
}

function hideVideo() {
    document.getElementById("video").style.display = 'none';
}
