$.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();
		$('#range_badge').hide();
		$('#shuttle').hide();
		return;
	}
	if($('#sections li').index(target) > -1) {
		target.addClass('expanded').find('.collapsable').slideDown(400, "easeInCubic");
	}
});

$(document).ready(function() {
	
	//------ Sniff for whether to use html or body element for scrolling.
	$('html, body').each(function () {
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') == initScrollTop + 1) {
			SCROLLELEMENT = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}
	});			

	//------ Establish events on the shuttle controls.
	$("#shuttle").click(function(event) {
		//------ Set our animation direction based on which control was clicked.
		direction = ($(event.target).attr('id') == "prev") ? -1 : 1;
		//------ Send the event through our Address API, sans hash sign.
		$.address.value(event.target.hash.replace(/^#/,''));
		event.preventDefault();
	});
	
	$("#jew").click(function(event) {
		//------ Close everything down when clicking on the logo.
		//$.address.value("/");
	});
	
	
	//var browse = $('#browse li:eq(0)');
	
	//------ If these aren't displayed there is a bit of a stutter when animating the first time.
	$('.product').show().css({ left: "1200px" }).each(function() {
		//------ Build out the releases menu.
		var wine = $(this);
		var item = $('<li>' + wine.find('h4:eq(0)').text() + '<\/li>');
		
		item.click(function(e) {
			e.stopPropagation();
			$.address.value(wine.attr('id'));
		});
		
		//browse.before(item);
	});
				
	

		//------ On load, check our address value to see if we shouldn't display a certain wine.
	switch($.address.value().replace(/^\//,'')) 
	{
		case "reserve":
			$("#_8776").addClass("current").css({ left: "0" });
			break;
		case "estate":
			$("#_11").addClass("current").css({ left: "0" });
			break;
		case "on premise":
			$("#_8588").addClass("current").css({ left: "0" });
			break;
		default:
			$("#_8776").addClass("current").css({ left: "0" });
	}

			
			
	//------ Interrupt events going to the actions menu; anchors behave unpredicably in overflowed containers.
	$('.actions').click(function(event) {
		var target = $(event.target.hash);
		
		if(target.length) {
			$(SCROLLELEMENT).stop().animate(
				{ scrollTop: target.offset().top },
				600
			);				
			event.preventDefault();
		}
	});
	
			
	//------ Clicking on any header should toggle that section.
	// $("#sections h2:not(:last)").click(function() {
	$("#sections h2").click(function() {
		var section = $(this).parent();
		//------ If the section is expanded, close it.
		if(section.hasClass("expanded")) {
			section.removeClass("expanded");
			section.find('.collapsable').slideUp(400, "easeOutCubic");
      
			//------ If we're closing the product display, also tell the grid to slide up.
			if(section.attr('id') == "releases") $('.current .grid').slideUp(400, "easeOutCubic");
			
			$.address.value("-");
			
		} else {
			//------ Otherwise, send the section ID to the Address API for opening.
			var path = (section.attr('id') == "releases") ? $('#wines .current').attr('id') : $(this).parent().attr('id');
			$.address.value(path);
		}
		//event.preventDefault();
		//event.stopPropagation();
	});		

	$("#sections h2:first").click(function() {
	  var section = $(this).parent();
    //------ If the section is expanded, close it.
    if(section.hasClass("expanded")) {
      $('#range_badge').show();
      $('#shuttle').show();
    } else {
      $('#range_badge').hide();
      $('#shuttle').hide();
    }
  });
	
	
	$('a[href^=https]').click(function() {
		if(pageTracker != undefined) {
			pageTracker._trackEvent("orderform", "click");
		}
	});
			
});

// show/hide cart functions
$(document).ready(function() {
	$('#jcart').hide();
	$('#jcart-show').click(function() {
		$('#jcart').show('slow');
		return false;
	});
	$('#jcart-hide').click(function() {
		$('#jcart').hide('fast');
		return false;
	});
	$('#jcart-hide-incart').click(function() {
		$('#jcart').hide(400);
		return false;
	});
	$('#jcart-toggle').click(function() {
		$('#jcart').toggle(400);
		return false;
	});
	var jct = $('#jcart-toggle');
	var position = jct.position();
	var top = position.top+25;
	var left = position.left-200;
	$('#jcart').css({
		"top": top,
		"left": left
	});
});
