$(document).ready(function(){

	$("body").addClass("scripted");
	
	$("#dcmpTitle ol").show();
	$("#dcmpTitle ul li:nth-child(4), #dcmpTitle ul li:nth-child(7)").each(function(){
		$(this).css("clear","left");
		});
	$("#dcmpTitle ul li").append("<img src='images/clickToExpand.png' tabindex='0' alt='' />");
	
	var hoveryFocus = function(){
		$(this).attr("src","images/clickToExpand-hover.png");	
	};
	var unHoveryFocus = function(){
		$(this).attr("src","images/clickToExpand.png");
	};
	
	$("#dcmpTitle ul img").hover(hoveryFocus,unHoveryFocus);
	$("#dcmpTitle ul img").focusin(hoveryFocus).focusout(unHoveryFocus);
	
	$("#dcmpTitle ul li img").click(function(){
		$(this).parent("li").addClass("autoheight").end().hide();
	});
	$("#dcmpTitle ul li img").keypress(function(e){
		if (e.which == 13) {
		$(this).parent("li").next("li").find("a").focus();
		$(this).parent("li").addClass("autoheight").end().hide();
			}
		});
	$("#dcmpTitle ul").not("ul.set1").addClass("hidden");
	$("#dcmpTitle ol li a").click(function(){
	
		var className = $(this).parent("li").attr("class");
		
		$("#dcmpTitle ul[class!="+className+"]").addClass("hidden");
		$("#dcmpTitle ul[class*="+className+"]").removeClass("hidden");
	
	});
	
	$("#quiz .question").each(function(){
	
		var thePosition = $(this).parent("li").index();
		var theSum = thePosition + 1;
		
		$(this).css("background","url('images/trivia-" + theSum + ".png') no-repeat left center");
	
	});
	
	$("#quiz label").not(".selected").hover(function(){
		$(this).addClass("hovered");	
	}, function(){
		$(this).removeClass("hovered");
	});
	
	$("#quiz label").click(function(){
		var commonThread = $(this).attr("for");
		$(this).siblings("input[id='" + commonThread + "']").focus().attr("selected","selected").siblings("input[selected='selected']").removeAttr("selected");
		$(this).siblings(".selected").removeClass("selected").end().addClass("selected");
	});
	
	$("#quiz .inputs input").focusin(function(){
		var commonThread = $(this).attr("id");
		$(this).siblings("label").removeClass("selected").end().siblings("label[for='" + commonThread + "']").addClass("selected");
		$(this).siblings("input").removeAttr("selected").end().attr("selected","selected");
	
	});
	
	$("#quizSubmit a").click(function(){
	
		var howManyCorrect = $("#quiz input.correct[selected='selected']").size();
	
		$(this).hide();
		$("#quiz .background").hide();
		$("#results").show();
		$("#results h3 span").prepend(howManyCorrect);
		$("#results p").prepend(function(){
			//< 5
			if ( howManyCorrect < 5 && howManyCorrect > 0 ) {
				return "<span>Practice Makes Perfect!</span>Even Boatner and O&#8217;Connor didn&#8217;t succeed in every attempt they made when they were trying to get <abbr title='Captioned Films for the Deaf'>CFD</abbr> off the ground. Go back and read some more and then <a href='http://dcmp.org/ai/deafhistory/index.html#quizWrap'>try again</a>! Check below to see which questions you answered correctly.";
				}
			//=0
			if ( howManyCorrect == 0 ) {
				return "<span>Practice Makes Perfect!</span>Even Boatner and O&#8217;Connor didn&#8217;t succeed in every attempt they made when they were trying to get <abbr title='Captioned Films for the Deaf'>CFD</abbr> off the ground. Go back and read some more and then <a href='http://dcmp.org/ai/deafhistory/index.html#quizWrap'>try again!</a>";
				}
			//>=6
			else if ( howManyCorrect >= 6 && howManyCorrect < 8 ) {
				return "<span>Great Job!</span>You know much more about captioning already than the average Joe (or Jane). Keep up the great work, and you'll be an expert in no time! Check below to see which questions you answered correctly.";
				}
			//8,9
			else if ( howManyCorrect >= 8 && howManyCorrect !=10 ) {
				return "<span>You&#8217;re Almost There!</span>You are on the cusp of captioning history greatness! One more spin through our clearinghouse of accessibility information ought to do it! Check below to see which questions you answered correctly.";
				}
			//==5
			else if ( howManyCorrect == 5 ) {
				return "<span>The Glass is Half Full!</span>If you study hard and keep a positive outlook, people will be referring to you as a captioning history buff in no time flat! Check below to see which questions you answered correctly.";
			}
			//==10
			else if ( howManyCorrect == 10 ) {
				return "<span>Perfection!</span>You sure know a lot about the history of captioning! Are you sure you&#8217;re not Mac Norwood?";
				}
		
			});
				
		$(".inputs").has("input.correct[selected='selected']").each(function(){
		
			$(this).siblings(".resource").replaceWith("<span class='correct'>You answered this question correctly!</span>");
		
			});
			
		$(".inputs").has("input.correct:not(:selected)").each(function(){
			
				$(this).siblings(".resource").replaceWith("<span class='incorrect'>You answered this question incorrectly.</span>");
		
			});
	
	});
	
	var showQuiz = function() {
		var theScroll = $("#quizWrap").offset();
		$(window).scrollTop(theScroll.top + 250);
		}
		
	var theHash = location.hash;
	
	switch(theHash) {
		case "#quizWrap":
		showQuiz();
		break;
		}

});
