$(document).ready(function(){
	$("#transcriptdates").datepicker();

		//save facilitator comment on a lesson
		$("#savelessoncomment").click(function(){
			var savenoticeelem = $(this).siblings('#lessoncommentnotice'); 
			$(savenoticeelem).html('<img src="img/ajax-loader.gif" style="height:12px;display:inline;" />');
			var user = $(this).attr("user");
			var attempt = $(this).attr("attempt");
			var comment = $(this).siblings('#lessoncomments').val();
			comment = comment.replace('&','%26');
			$.ajax({
   				type: "POST",
   				url: "savelessoncomment.php",
   				data: "user="+user+"&attempt="+attempt+"&comment="+comment,
   				success: function(msg){
					$(savenoticeelem).html(msg);
   				}
 			});
		
		});


	//hide all accordion elemements to start
    $(".accordion").hide();
	$(".journalweeks").hide();
	
	
	var bindBehaviors = function() {
   		$('.savegoalcal').click(function(){
			var savenoticeelem = $(this).siblings('.savenotice'); 
			$(savenoticeelem).html('<img src="img/ajax-loader.gif" style="height:12px;display:inline;" />');
			var userid = $(this).attr("userid");
			var goal = $(this).siblings('.caloriegoal').val();
			//TODO: do some validation of this goal...should be only integers
			if (validateValue(goal))
			{
				$.ajax({
	   				type: "POST",
	   				url: "changegoal.php",
					//TODO: send the appropriate data below
	   				data: "userid="+userid+"&type=cal&goal="+goal,
	   				success: function(msg){
						$(savenoticeelem).html(msg);
	   				}
	 			});
			}
			else
				$(savenoticeelem).html('Invalid Value');
		});
   		
   		function validateValue(val)
   		{
   			intRegEx = /^[0-9]+[\.]?[0-9]*$/;
   			if (intRegEx.test(val))
   				return true;
   			else
   				return false;
   		}
   		
   		$('.savestartweight').click(function(){
			var savenoticeelem = $(this).siblings('.savenotice'); 
			$(savenoticeelem).html('<img src="img/ajax-loader.gif" style="height:12px;display:inline;" />');
			var user = $(this).attr("user");
			var weight = $(this).siblings('.startweight').val();
			//TODO: do some validation of this goal...should be only integers
			if (validateValue(weight))
			{
				$.ajax({
					type: "POST",
					url: "changestartweight.php",
					//TODO: send the appropriate data below
					data: "user="+user+"&weight="+weight,
					success: function(msg){
						$(savenoticeelem).html(msg);
   					}
				});
			}
			else
				$(savenoticeelem).html('Invalid Value');
		
		});
		
		
		
		//save exercise goal
		$(".savegoalex").click(function(){
			var savenoticeelem = $(this).siblings('.savenotice'); 
			$(savenoticeelem).html('<img src="img/ajax-loader.gif" style="height:12px;display:inline;" />');
			var userid = $(this).attr("userid");
			var goal = $(this).siblings('.caloriegoal').val();
			//TODO: do some validation of this goal...should be only integers
			if (validateValue(goal))
			{
				$.ajax({
	   				type: "POST",
	   				url: "changegoal.php",
					//TODO: send the appropriate data below
	   				data: "userid="+userid+"&type=ex&goal="+goal,
	   				success: function(msg){
						$(savenoticeelem).html(msg);
	   				}
				});
			}
			else
				$(savenoticeelem).html('Invalid Value');
		
		});
	
		//save all facilitator comments for this user when the saveall button is clicked
		$(".saveall").click(function(){
			var commentelems = [];
			commentelems =  $(this).siblings('.commentbox').children('.saveentry');
			var l = commentelems.length;
			$(this).siblings('.savenotice').html('<img src="img/ajax-loader.gif" style="height:12px;display:inline;"/>');
			var userid = $(this).attr("userid");
			var titleid = '#user_'+userid;
			var titlebarelem = (titleid);
			var titlebarappend = $(titlebarelem).children('.titleappend');
			var issuccessful = 0;
			for(var i = 0; i < 7; i++){
				var comment = commentelems[i];
				var timestamp = $(comment).attr("timestamp");
				var bodytext = $(comment).siblings(".facilitatorcomment").val();
				bodytext = bodytext.replace('&','%26');
				$.ajax({//this must be called synchronously or the for loop will finish
					//before the first ajax call returns...or even gets sent. It's slow...
					//We could probably put all of these title and body values in an array
					async:false,
   					type: "POST",
   					url: "savecomment.php",
					//TODO: send the appropriate data below
   					data: "userid="+userid+"&body="+bodytext+"&timestamp="+timestamp,
   					success: function(msg){
						if(msg == 1){//success
							issuccessful++;
						}
   					}
 				});
			}
			if (issuccessful > 0){
				$(this).siblings(".savenotice").html(issuccessful+' Comments saved');
				
			}else{
				$(this).siblings(".savenotice").html('Error - Comments could not be saved.');
			}
	
		});
	}


	
	
	//handle the expanding and collapsing of accordion elements
	/*THE OLD FUNCTIOn
	$(".accordiontitle").click(function(){
		var mode = $(this).attr("mode");
		if (mode == "collapsed"){
			$(".accordion").slideUp();
			$(".accordiontitle").attr("mode", "collapsed");
			$(this).siblings().slideDown();
			$(this).attr("mode", "expanded");
		}else if(mode == "expanded"){
			$(this).attr("mode", "collapsed");
			$(this).siblings().slideUp();
		}
    });
    */
	
	$(".accordiontitle").click(function(){
		var scrollto = $(this).attr("id");
		scrollto='#'+scrollto;
		var mode = $(this).attr("mode");
		var content = $(this).attr("content");
		if (mode == "collapsed"){
			if(content=="true"){
				//just do the sliding
				$(".accordion").slideUp();
				$(".journalweeks").slideUp();
				$(this).siblings().html(result);
				$(this).siblings().slideDown(function(){
					$('html, body').animate({ 
                 		scrollTop: $(scrollto)
                 		.offset().top }, 500);
				});
				$(this).attr("mode", "expanded");
				

			}else{
				$(".accordion").slideUp();
				$(".journalweeks").slideUp();
				$(".accordiontitle").attr("mode", "collapsed");
			
				var userid = $(this).attr("userid");
				var timestamp = $(this).attr("timestamp");
				var section = $(this).attr("section");
				var result = '';
				//ajax call
				$.ajax({
					async:false,
   					type: "POST",
   					url: "accordionajax.php",
					//TODO: send the appropriate data below
   					data: "userid="+userid+"&timestamp="+timestamp+"&section="+section,
   					success: function(msg){	
						result = msg;
					}
 				});
				//'#'+$(this).attr("id")
				$(this).siblings('.accordion').html(result);
				$(this).siblings().slideDown(function(){
					$('html, body').animate({ 
                 	scrollTop: $(scrollto)
                 	.offset().top }, 500);
					bindBehaviors();
				});
				$(this).attr("mode", "expanded");
				$(this).attr("content", "true");
			}	
		}else if(mode == "expanded"){
			$(this).attr("mode", "collapsed");
			$(this).siblings().slideUp();
			
		}
    });
	
	
	
	//save facilitator comments
	// Commented out by Ron Stewart on May 26, 2009.
	// This function used to apply to a save button for each indivual day, but that
	// button has since been made hidden.
	/*
	$(".saveentry").click(function(){
		var savenoticeelem = $(this).siblings('.savenotice'); 
		var titlebarelem = $(this).parent().parent().siblings();
		$(savenoticeelem).html('<img src="img/ajax-loader.gif" style="height:12px;display:inline;" />');
		var userid = $(this).attr("userid");
		var timestamp = $(this).attr("timestamp");
		var titletext = $(this).siblings(".facilitatortitle").val(); 
		var bodytext = $(this).siblings(".facilitatorcomment").val();
		$.ajax({
   			type: "POST",
   			url: "savecomment.php",
			//TODO: send the appropriate data below
   			data: "userid="+userid+"&title="+titletext+"&body="+bodytext+"&timestamp="+timestamp,
   			success: function(msg){
				var result;
				if(msg == 1){//success
					result = 'Comment Saved';
					//Another ajax call to get the number of comments saved
					//and replace the text in the titlebarelem
					var starttime = $("#useraccordion").attr("starttime");
					$.ajax({
   						type: "POST",
   						url: "numberofcomments.php",
   						data: "userid="+userid+"&timestamp="+starttime,
   						success: function(msg){
							$(titlebarelem).html(msg);
   						}
 					});
				}else{//failure
					result = 'Error: Please try again';
				}
				$(savenoticeelem).html(result);
   			}
 		});
		
		
	}); */
	
	
	
	//save all facilitator comments for this user when the saveall button is clicked
	$(".saveall").click(function(){
		var commentelems = [];
		commentelems =  $(this).siblings('.commentbox').children('.saveentry');
		var l = commentelems.length;
		$(this).siblings('.savenotice').html('<img src="img/ajax-loader.gif" style="height:12px;display:inline;"/>');
		var userid = $(this).attr("userid");
		var titleid = '#user_'+userid;
		var titlebarelem = (titleid);
		var titlebarappend = $(titlebarelem).children('.titleappend');
		var issuccessful = 0;
		for(var i = 0; i < 7; i++){
			var comment = commentelems[i];
			var timestamp = $(comment).attr("timestamp");
			var bodytext = $(comment).siblings(".facilitatorcomment").val();
			$.ajax({//this must be called synchronously or the for loop will finish
					//before the first ajax call returns...or even gets sent. It's slow...
					//We could probably put all of these title and body values in an array
				async:false,
   				type: "POST",
   				url: "savecomment.php",
				//TODO: send the appropriate data below
   				data: "userid="+userid+"&body="+bodytext+"&timestamp="+timestamp,
   				success: function(msg){
					if(msg == 1){//success
						issuccessful++;
					}
   				}
 			});
		}
		if (issuccessful > 0){
			$(this).siblings(".savenotice").html(issuccessful+' Comments saved');
			$(titlebarappend).html(': Comments Saved');
			$(titlebarelem).css("background-color", "#9EFF00");
		}else{
			$(this).siblings(".savenotice").html('Error - Comments could not be saved.');
		}
	
	});
	
	
	//make the savenotice go away if the user clicks within a comment box
	//	This will let the user know that they may have made changes that haven't been
	// saved. 
	$(".facilitatorcomment").click(function(){
		var savenoticeelem = $(this).siblings('.savenotice'); 
		$(savenoticeelem).html('');
	});
	//make the savenotice go away if the user clicks within a comment title box
	//	This will let the user know that they may have made changes that haven't been
	// saved. 
	$(".facilitatortitle").click(function(){
		var savenoticeelem = $(this).siblings('.savenotice'); 
		$(savenoticeelem).html('');
	});
	
	
//This function allows for switching between weeks while viewing the journals
var switchWeeks = function () {
	//change weeks for a user
	$(".weeklink").click(function(){
		var userid = $(this).attr("userid");
		var timestamp = $(this).attr("timestamp");
		var section = $(this).attr("section");
		var courseid = $(this).attr("courseid");
		var result = '';
		var weekresult = '';
		//ajax call
		$.ajax({
			async:false,
   			type: "POST",
   			url: "accordionajax.php",
			//TODO: send the appropriate data below
   			data: "userid="+userid+"&timestamp="+timestamp+"&section="+section,
   			success: function(msg){	
				result = msg;
				
			}
 		});
		$(this).parent().parent().parent().parent().parent().siblings('.accordion').html(result);
		bindBehaviors();
		
		//ajax call
		$.ajax({
			async:false,
   			type: "POST",
   			url: "weeksajax.php",
			//TODO: send the appropriate data below
   			data: "courseid="+courseid+"&userid="+userid+"&section="+section,
   			success: function(msg){	
				weekresult = msg;
			}
 		});
		$(this).parent().parent().parent().parent().parent().html(weekresult);
		switchWeeks();
	});
}
switchWeeks();
	
	
});