

window.onload = function(){
	// Hide the lesson if there is an introduction
	if ($("lessonIntro")){
		startLessonIntro ();
	}

	activatePulldown("courseNavList");
	
	checkForTabs();
}





var timeIntroIn = 0.8;
var timeIntroSegIn = 0.8;
var timeIntroPause = 2;
var timeShowLesson = .65;
var displayState = "none";
var introAniType = null;
var introSegments = 0;
var interval;
var thisPage = document.URL.substring(document.URL.lastIndexOf("/")+1 ,document.URL.length);



function lessonIntroInit (){
//	if (readCookie(thisPage)){
//		introAlreadyShown();
//		return;
//	}
	matchIntroToContentHeight();
	
	introAniType = checkForIntroSegements();
	
	// Hide both Lesson and Intro to Start
	Element.hide("lesson");
	Element.hide("lessonIntro");
	Element.hide("courseHeadline");
	Element.hide("courseNav");
	Element.hide("courseNotes");
	Element.hide("lessonNav");
	Element.hide("reviewIntroduction");
	Element.hide("sidebarcolumn");
	$("wrapperMain").className = "intro";
	
	// Set the State
	displayState = "intro";
}
function introAlreadyShown (){
	Element.hide("lessonIntro");
}
function matchIntroToContentHeight (){
	var lessonHeight = $("lesson").clientHeight;
	var lessonIntroHeight = $("lessonIntro").clientHeight;
	var totalHeight = (lessonHeight >lessonIntroHeight ) ? lessonHeight+150: lessonIntroHeight+150;
	$("courseContent").style.height = totalHeight+"px";
}
function checkForIntroSegements (){
	if ($("lessonIntro1")){
		var i = 1;
		while($("lessonIntro"+i)){
			i++;
		}
		introSegments = i-1;
		if (introSegments > 3) {
			timeIntroSegIn = .75;
		}
		return("multiple");
	} else {
		return("single");
	}
} 
function showLesson(){
	clearTimeout(interval);
	
	if (displayState == "lesson"){ return;}
	displayState = "lesson";
	new Effect.Fade('lessonIntro',{duration:timeShowLesson});
	
//	new Effect.Fade('lessonNav',{duration:timeShowLesson});
	window.setTimeout(' endIntroFadeOut() ',  timeShowLesson*1000);
}
function endIntroFadeOut(){
	$("wrapperMain").className = "animating";
	checkForTabs()
	new Effect.BlindDown('courseNav',{duration:timeShowLesson});
	window.setTimeout(' new Effect.Appear(\'courseHeadline\',{duration:'+timeShowLesson+'}) ',  1*timeShowLesson*1000);
	window.setTimeout(' new Effect.Appear(\'lesson\',{duration:'+timeShowLesson+'}) ',   1.5*timeShowLesson*1000);
	window.setTimeout(' new Effect.Appear(\'sidebarcolumn\',{duration:'+timeShowLesson+'}) ',  1.0*timeShowLesson*1000);
	window.setTimeout(' new Effect.Appear(\'courseNotes\',{duration:'+timeShowLesson+'}) ',  1.5*timeShowLesson*1000);
	if ($("reviewIntroduction"))
		window.setTimeout(' new Effect.Appear(\'reviewIntroduction\',{duration:'+timeShowLesson+'}) ',  1.5*timeShowLesson*1000);
		window.setTimeout(' new Effect.Appear(\'lessonNav\',{duration:'+timeShowLesson+'}) ',  2.5*timeShowLesson*1000);
		window.setTimeout(' introComplete() ',  2.5*timeShowLesson*1000);
	
	
}
function introComplete(){
	$("wrapperMain").className = "";
}
function startLessonIntro(){
//	if (readCookie(thisPage)){return;}
	
//	createCookie(thisPage,true,60); 

	$("wrapperMain").className = "intro";

	if (introAniType == "single"){
		new Effect.Appear('lessonIntro',{duration:timeIntroIn});
	} else {
		Element.show("lessonIntro");
		for (var i=1; i<=introSegments; i++){
			Element.hide("lessonIntro" + i);
			var pause = .25*(i-1)*timeIntroSegIn*1000;
			window.setTimeout(' new Effect.Appear(\'lessonIntro'+i+'\',{duration:timeIntroSegIn}) ',  pause);
			//var pause = (i-1)*timeIntroSegIn*1000;
			//window.setTimeout(' new Effect.SlideDown(\'lessonIntro'+i+'\',{duration:timeIntroSegIn}) ',  pause);
		}
	}
	interval = window.setTimeout('showLesson();',  timeIntroPause*1000);
}

function reshowIntro(){
	if (displayState == "intro"){ return;}
	displayState = "intro";

	matchIntroToContentHeight();
	
	introAniType = checkForIntroSegements();

	var reshowTime = .75*timeShowLesson;
	new Effect.BlindUp('lessonNav',{duration:reshowTime});
	window.setTimeout(' new Effect.BlindUp(\'courseNav\',{duration:'+reshowTime+'}) ',  1.5*reshowTime*1000);
	window.setTimeout(' new Effect.Fade(\'courseHeadline\',{duration:'+reshowTime+'}) ',  1*reshowTime*1000);
	window.setTimeout(' new Effect.Fade(\'lesson\',{duration:'+reshowTime+'}) ',   reshowTime*1000);
	window.setTimeout(' new Effect.Fade(\'sidebarcolumn\',{duration:'+reshowTime+'}) ',  reshowTime*1000);
	window.setTimeout(' new Effect.Fade(\'courseNotes\',{duration:'+reshowTime+'}) ',  reshowTime*1000);
	window.setTimeout(' new Effect.Fade(\'reviewIntroduction\',{duration:'+reshowTime+'}) ',  reshowTime*1000);
//	window.setTimeout(' new Effect.Appear(\'lessonNav\',{duration:'+reshowTime+'}) ',   2.5*reshowTime*1000);

//	eraseCookie(thisPage);
	window.setTimeout('startLessonIntro() ',  2.5*reshowTime*1000);
}
function nextScreen(nextURL){
	if (displayState == "intro"){
		showLesson();
	} else {
		window.location.href=nextURL;
		window.event.returnValue=false;	
	}
}


// Course Notes Code

function hideCourseNotes(){
	$("wrapperMain").className = "courseNotesHidden";
}
function showCourseNotes(){
	$("wrapperMain").className = "courseNotesActive";
}

function hideCourseNote(node){
	var courseNoteNode = node.parentNode.parentNode;
	courseNoteNode.className = "closed";
}
function showCourseNote(node){
	var courseNoteNode = node.parentNode.parentNode;
	courseNoteNode.className = "open";
}
function showAllNotes(){
	var notes = $("courseNotesList").childNodes;
	for (var i=0; i<notes.length; i++){
		notes[i].className = "open";
	}
}
function hideAllNotes(){
	var notes = $("courseNotesList").childNodes;
	for (var i=0; i<notes.length; i++){
		notes[i].className = "closed";
	}
}

// Pulldown Code for IE
function setIePulldowns() {
	if (document.all&&document.getElementById) {
		activatePulldownInIE ("courseNavList");
	}
}
function activatePulldown (divName) {
	navRoot = document.getElementById(divName);
	if (navRoot){
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
					this.className=this.className.replace("over", "");
				}
			}
		} //for (i=0; i<navRoot.childNodes.length; i++)
	} // if (navRoot){
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
function eraseAllCookies() {
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		var cookieName = c.split("=");
		createCookie(cookieName[0],"",-1);
	}
}

