$(function(){
	
	// setting defaults and calling any plug-ins
	$.datePicker.setDateFormat('mdy', '/');
	$("input.datePicker").datePicker();
	
	// these are all the functions that take place when the page load is finished
	
	// if the TF question div has the class then it should be visible (the class is written if the page posts an error)
	// otherwise we want to hide the TF div on initial page load and show MC fields by default.
	if( $("#questionType_TF").is(".questionTypePost_TF") ){
		$("#questionType_MC").hide();
	} else {
		$("#questionType_TF").hide();
	}

	$('#newQuestionOptions').hide();
	$('a#showNewQuestionOptions').click(function(){
		$('#newQuestionOptions').slideDown();
		return false;
	});
	
	$('#new_test_file_yes').click(function(){
		if( $(this).is(':checked') ) $('.test_file_wrapper').show();
	});
	$('#new_test_file_no').click(function(){
		if( $(this).is(':checked') ) $('.test_file_wrapper').hide();
	});

	// switches the visibility of the TF/MC fields when the test type is changed
	$("#question_type_select").change(function(){
		$(this).each(function(){
			var val = $(this).val();
			if(val == 'mc') {
				$("#questionType_TF").hide();
				$("#questionType_MC").show();
			} else {
				$("#questionType_TF").show();
				$("#questionType_MC").hide();
			}
		});		
	});
	
	/*
	$("#testPPTUpload").hide();
	$("#test_type_select").change(function(){
		$(this).each(function(){
			var val = $(this).val();
			if(val == 1) {
				$("#testPPTUpload").show();
			} else {
				$("#testPPTUpload").hide();
			}
		});		
	});
	*/
	
	$("input#course_name").keyup(function(){
		var val = $(this).val();
		var pre = "";
		var post = "";
		if(val != ""){
			pre = " (Pre-Test)";
			post = " (Post-Test)";
		} else {
			pre = "";
			post = "";
		}
		$("input#pre_name").val(val+pre);	
		$("input#post_name").val(val+post);	
	});

		
	$(".subNav li").hover(
			function(){ $("ul", this).show(); }, 
			function() { } 
	);
	if (document.all) {
		$(".subNav li").hoverClass("sfHover");
	} 
		
	$('.courseWrapper').hide();	
	$('.showCourseWrapper').click(function(){
		var id = $(this).attr('href');
		
		if( $(id).is(":hidden") ) {
			$(id).slideDown();
			$(this).text('Hide Details');
		} else {
			$(id).slideUp();
			$(this).text('Show Details');
		}	
		
		return false;
	});
	
	$('a.viewPostTestAnswers').click(function(){
		if( $('.postTestAnswers').is(':hidden') ) {
			$('.postTestAnswers').slideDown();
		} else {
			$('.postTestAnswers').slideUp();
		}
	})
	
	$(".archiveQuestion").click(function(){
		if (confirm("Are you sure you want to archive this question?")) {
			var key = $(this).attr("rel");
			var id = $(this).attr("id");
			id = id.split('_')[2];
			$.post("../ajax/archiveQuestion.php",{
				question_id: id,
				ajaxkey: key
			},function(){
				$("#question_row_"+id).fadeOut();
			});				
		}
		return false;
	});
	
	$(".activateQuestion").click(function(){
		if (confirm("Are you sure you want to activate this question?")) {
			var key = $(this).attr("rel");
			var id = $(this).attr("id");
			id = id.split('_')[2];
			$.post("../ajax/activateQuestion.php",{
				question_id: id,
				ajaxkey: key
			},function(){
				$("#question_row_"+id).fadeOut();
			});				
		}
		return false;
	});
	
	$(".archiveTest").click(function(){
		if (confirm("Are you sure you want to archive this test?")) {
			var key = $(this).attr("rel");
			var id = $(this).attr("id");
			id = id.split('_')[2];
			$.post("../ajax/archiveTest.php",{
				test_id: id,
				ajaxkey: key
			},function(){
				$("#test_row_"+id).fadeOut();
			});				
		}
		return false;
	});
	
	$(".activateTest").click(function(){
		if (confirm("Are you sure you want to activate this test?")) {
			var key = $(this).attr("rel");
			var id = $(this).attr("id");
			id = id.split('_')[2];
			$.post("../ajax/activateTest.php",{
				test_id: id,
				ajaxkey: key
			},function(){
				$("#test_row_"+id).fadeOut();
			});				
		}
		return false;
	});
	
	$(".archiveUser").click(function(){
		if (confirm("Are you sure you want to archive this user? The user will still be able to login and buy or complete courses.")) {
			var key = $(this).attr("rel");
			var id = $(this).attr("id");
			id = id.split('_')[2];
			$.post("../ajax/archiveUser.php",{
				user_id: id,
				ajaxkey: key
			},function(){
				$("#user_row_"+id).fadeOut();
			});				
		}
		return false;
	});
	
	$(".activateUser").click(function(){
		if (confirm("Are you sure you want to activate this user?")) {
			var key = $(this).attr("rel");
			var id = $(this).attr("id");
			id = id.split('_')[2];
			$.post("../ajax/activateUser.php",{
				user_id: id,
				ajaxkey: key
			},function(){
				$("#user_row_"+id).fadeOut();
			});				
		}
		return false;
	});
		
		
});


$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
}; 

