//------ Hide, progressively.
$('html').addClass('init');
//------ Animation direction used in change events.
var direction = 1;
$.address.change(function(event) {
	var target = $("#" + event.pathNames[0]);
	if(!target.length && event.pathNames[0] != "-") {
		$("#sections .collapsable").hide();
		$("#sections .expanded").removeClass("expanded");
		$('.product .grid').hide();
		return;
	}
	var wines = $("#wines .product");
	var index = wines.index(target);
	if(index > -1) {
		//------ We're displaying a wine. First, update shuttle controls.
		var prev = (index == 0) ? wines.eq(wines.length - 1) : target.prev();
		var next = (index < wines.length - 1) ? target.next() : wines.eq(0);
		//------ Apply...
		$("#prev").attr("href", "#" + prev.attr("id"));
		$("#next").attr("href", "#" + next.attr("id"));
		//------ Look up the index of the currently displayed wine.
		var current = $("#wines .current");
		//------ Determine animation targets based on right-left or left-right targets.
		var w = $('#wines').width();
		//------ Is the container expanded?
		if($("#sections li:eq(0)").hasClass("expanded")) {
			//------ Show the product information, as it is hidden by the init class.
			target.find(".grid").show();
			//$('#range_badge').show();
		} else {
			//------ Otherwise, reveal it with animation.
			$('#releases').addClass('expanded').find('.collapsable').slideDown(400, "easeInCubic");
			target.find(".grid").slideDown(400, "easeInCubic");
		}
		//------ Animate, but if somehow the target is currently displayed don't pan.
		if(current.attr('id') != target.attr('id')) {
			//------ Exit right.
			current.removeClass("current");
			current.show().css({ left: 0 }).animate({ left: (w * direction) },600, "easeOutCubic");
			//------ Bring on from left.
			target.addClass("current");
			target.show().css({ left: (-1 * w * direction) }).stop().animate({ left: "0" },600, "easeOutCubic");
		}
	} else if($('#sections li').index(target) > -1) {
		target.addClass('expanded').find('.collapsable').slideDown(400, "easeInCubic");
	}
	//var title = (event.pathNames[0]) ? " - " + event.pathNames[0].toUpperCase() : "";
	//$.address.title(title);
});
