/************** MooTools DomReady event *********************/
window.addEvent('domready', function(){ 
	// Apply smooth scrolling 
	new SmoothScroll();//{duration: 800,transition: Fx.Transitions.quadOut,fps: 60}		

	// Define our navigational structure
	var lessons = $$('#Nav #lesson label');
	var chapters = $$('#Nav .chapter');
	
	// Create the Accordion
	var moodleNav = new Accordion(
	  lessons, chapters, {
	    duration: 500,
	    opacity: true,
	    alwaysHide: true,
	    start: false,
	    // Keep current lesson open on page load			    
	    show: start_id
	    ,onActive: function( tog ){			    
	    	tog.getParent().getParent().addClass('active');
	    }
	    ,onBackground: function( tog ){
	    	tog.getParent().getParent().removeClass('active');
	    }
	    //,onComplete: function(){}
	  }
	);
	
	
	if( has_line ){
		// Identify the active page			
		$$( location_ref ).getParent().addClass('active');
	}else if( has_mid ){
		// Select the very first chapter
		$$( section_ref + ' .'+$$('.chapter a')[0].getProperty('class').toInt() ).getParent().addClass('active');		
	}
	
	// Clean up function (remove active)
	function cleanUp(){
		$$('#Nav .active').each( function( el ){
			el.removeClass('active');
		});
	}
	
	// Become active when clicked, remove existing active
	$$('#Nav .chapter li a').addEvent('click',function(){
		cleanUp();
		
		// Activate the new chapter
		this.getParent().addClass('active');
		// Activate the new lesson
		this.getParent().getParent().getParent().addClass('active');
		// No more focus needed here
		this.blur();				
	});

	// Apply hover effect
	$$('#Nav #lesson label,#Nav .CourseNav li').each( function( li ){
		li.addEvents({
			mouseover:function(){
				this.addClass('hover');
			},
			mouseout:function(){					
				this.removeClass('hover');
			}
		});
	});
	
});


/*function popitup(url) {
newwindow=window.open(url,'name','height=900,width=900,scrollbars=yes');
if (window.focus) {newwindow.focus()}
return false;
}*/