YUI({
	base:"/lib/yui/"
}).use('node','anim', 'event', function(Y){
	
	var nodeHeights = [],
		nodeAnims = [];

	/**
	****************/
	function hideNode( targetNode ){
		if ( targetNode.hasClass( 'isClosed' ) ){
			return;
		}
		if ( targetNode.hasClass( 'isAnimating' ) ){
			nodeAnims[targetNode.get('_yuid')].stop();
		}
		
		var myAnim = new Y.Anim({
			node: targetNode,
			to: { opacity: 0, height: "0px", paddingTop:0, paddingBottom:0 },
			duration: 0.25
		});
		
		nodeAnims[targetNode.get('_yuid')] = myAnim;
		
		myAnim.on('end', function(e){
			myAnim.get('node').replaceClass('isOpen', 'isClosed');
			myAnim.get('node').removeClass('isAnimating');
		});

		targetNode.addClass('isAnimating');
		myAnim.run();
		
	}
	
	/**
	****************/
	function showNode( targetNode ){

		if ( targetNode.hasClass( 'isOpen' ) ){
			return;
		}
		if ( targetNode.hasClass( 'isAnimating' ) ){
			nodeAnims[targetNode.get('_yuid')].stop();
		}
		
		var myAnim = new Y.Anim({
			node: targetNode,
			to: { opacity: 1, height: nodeHeights[targetNode.get('_yuid')], paddingTop:8, paddingBottom:8 },
			duration: 0.25
		});

		nodeAnims[targetNode.get('_yuid')] = myAnim;
		targetNode.replaceClass('isClosed', 'isOpen');
		
		myAnim.on('end', function(e){
			myAnim.get('node').removeClass('isAnimating');
		});

		targetNode.addClass('isAnimating');
		myAnim.run();
		
	}
	
	Y.on('domready', function(){
		Y.one('a.booking_info_link').on('click', function(e){
			var targetNode = e.currentTarget.get('parentNode').one('.booking_info_content'); 
			if (targetNode.hasClass('isClosed')){ showNode( targetNode ); } else { hideNode( targetNode ); };
			e.preventDefault();
		});
		
	
		//store the current heights of the nodes we're going to animate
		Y.all('.booking_info_content').each( function( node, index, nodeList ){
			nodeHeights[ node.get('_yuid') ] = node.getStyle('height');
			hideNode( node );
		});
	});
	
});
