//config 
var configObject = {
		minHeight: 40,
		maxHeight: 260,
		expandDuration: 0.4,
		collapseDuration: 0.4,
		defaultIndex: 0

};


//config

YUI().use( 'anim', function(Y) {
	var currentSlide = "slide1";
	function expand(targetNode, expandTime)
	{
		var expandAnim = new Y.Anim({
				node: targetNode,
				to:{
					height: configObject.maxHeight
				}, 
				duration: expandTime,
				easing: Y.Easing.easeIn

				}
			);
		//console.log(expandAnim);
		
		targetNode.get('parentNode').removeClass('collapsed');
		targetNode.get('parentNode').addClass('expanded');
		expandAnim.run();
		currentSlide = targetNode;
	}
	
	
	function collapse(targetNode, collapseTime, onEndCallback)
	{
		var collapseAnim = new Y.Anim({
				node: targetNode,
				to:{
					height: configObject.minHeight
				}, 
				duration: collapseTime,
				easing: Y.Easing.easeOut
				}
			);
		//console.log(collapseAnim);
		collapseAnim.on("end", onEndCallback);
		targetNode.get('parentNode').removeClass('expanded');
		targetNode.get('parentNode').addClass('collapsed');
		collapseAnim.run();
		
	}
	
	
	function init() {
		var defaultSlide = "slide1";
		//colapse all except default
		Y.all(".slideContent").each( function(node, nodeIndex, nodeList) {
			if(nodeIndex != configObject.defaultIndex)
			{
				collapse(node, configObject.collapseDuration);
				
				
			}else 
			{
				expand(node, configObject.expandDuration);
			}
		});
		
		Y.all("a.slideLink").each( function(node, nodeIndex, nodeList){
			//collapse all extended
			node.set('innerHTMl', ">");
			//expand this one
			node.on('click', function(e){
				e.preventDefault();
				var slideContent = Y.one('#' + this.get('parentNode').get("id") + " .slideContent");
				if(currentSlide== slideContent)
					return;
				//console.log(slideContent, '#' + this.get('parentNode').get("id") + " .slideContent");
				collapse(currentSlide, configObject.collapseDuration,  function(e) { expand(slideContent, configObject.expandDuration); } );
				
				
			});
		});
	}
	
	
	Y.on("contentready", init, "#accordion");
});

