$(document).ready(function(){

	$('dl.dropdown').hover(
		function(){ $(this).find('dd').addClass('on'); },
		function(){ $(this).find('dd').removeClass('on'); }
	);
	
	$('#newAlert').limit('1000','#charsLeft');
	
	/* Child Timeline */
	
	if ( $('#childTimeline').length > 0 ) {
	
		var ctW = $('#childTimeline').width();
		
		if (ctW > 622) {
			setUpScroll(790);
		}
		else {
			setUpScroll(440);
		}
		
	}
	
	/* Create Child */
	
	$('form.radioflying fieldset fieldset input').click(function(){ 
		$('form.radioflying fieldset fieldset').removeClass('on');
		$(this).parent().addClass('on'); }
	);
	
	/* Personality/Cognitive Quiz Scrolling */
	
	//disable tabbing (it messes up the scrolling)
	$('form.quiz').live('keypress', function(e){
			if(e.keyCode==9){
				return false;
			}
		});
		
	$('form.quiz fieldset.buttons a.btn').click(function(){
			nextQuestion();
			return false;
		});
	
	function nextQuestion(){

		/* set heights based on which quiz (Cognitive as default) */
			var h1 = 600; // total scroll height
			var h2 = 200; // individual question height
			if ($('form.quiz').attr('id') == 'pQuiz') { 
				h1 = 3150;
				h2 = 175;
			}

		/* get the current question number */
			var qN = $('#qN').html();
			qN = parseFloat(qN);

		/* get the current position of the container */
			var position = $('#quizQuestions ol').position();
		/* if we're not on the next-to-last one, and we're not still animating, animate -175 and change the question number */
			if ( position.top > (0-h1) && !$('form.quiz ol').is(':animated') ) {
				$('form.quiz ol').animate(
					{ "top": position.top - h2 },
					750,
					function(){
						$('#qN').html(qN+1);
					}
				);

		/* however, if we are on the next-to last one, but not still animating, animate -175, change the question number, and also hide the animation trigger */
			} else if ( position.top <= (0-h1) && !$('form.quiz ol').is(':animated') ) {
				$('form.quiz ol').animate(
					{ "top": position.top - h2 }, 
					750,
					function(){
						$('form.quiz').attr({"onsubmit":"return true;"})
						$('#qN').html(qN+1);
						$('form.quiz fieldset.buttons a.btn3').hide();
						$('form.quiz fieldset.buttons button').show();
					}
				);
			}
			return false; 
		}
	
	/* Teacher My Classes Box */
//	setUpAddClassForm();
	
	$('.delete-class').click(function(){
		className = $(this).parent().parent().find("th strong").html();
		input_box = confirm("Are you sure you want to delete this class?\nClass Name: " + className + "\nThis will delete all students in this class as well.  Click Cancel or OK to Continue.");
		if (input_box == true){ // Output when OK is clicked
			return true;
		} else { // Output when Cancel is clicked
			return false;
		}
	});
	
	/* Teacher Dashboard Table */
	
		if ($('body.teacher').length > 0) {
		
			$('.teacher table.sortable').tablesorter({ 
					// define a custom text extraction function 
					textExtraction: function(node) { 
						// extract data from markup and return it  
						return node.childNodes[0].innerHTML; 
					},
					headers: { 
								// assign the secound column (we start counting zero) 
								2: { 
									// disable it by setting the property sorter to false 
									sorter: false 
								}, 
								// assign the third column (we start counting zero) 
								5: { 
									// disable it by setting the property sorter to false 
									sorter: false 
								} 
							}
				});
			$('.teacher table.sortable tr').find('td:eq(0)').addClass('names');
			$('.teacher #studentRoster table tr').find('td:eq(1)').addClass('names');
			$('.teacher #studentRoster table tr').find('td:eq(5)').addClass('decisions');
			$('.teacher #studentRoster table tr').find('td:eq(6)').addClass('complete');
			
		}
	 
	 /* Tabs */
	
	$('.tabnav li a').click(function(){
		var which = $(this).attr('href');
		$('.tabnav li a').removeClass('current');
		$('.tabs div').removeClass('current');
		$(which).addClass('current');
		$(this).addClass('current');
		return false;
	});
	
	/* Open/Close Videos */
	
	$('#openVideos').click(function(){
		$('#momentDeets').hide();
		$('#momentVideos').show();
		return false;
	});
	
	$('#closeVideos').click(function(){
		$('#momentDeets').show();
		$('#momentVideos').hide();
		return false;
	});
	
	/* Show definition */
	
		var defID;
		
		$('.students #momentDeets a.defined').hover(function(e){
			defID = $(this).attr('href');
			defText = $(this).attr('rel');
			$('.students #momentDeets div.definitions dl'+defID).html(defText);
			var defPos = $(this).position();
			var boxH = $('.students #momentDeets div.definitions').height();
			$('.students #momentDeets div.definitions dl:not('+defID+')').css('display','none');
			$('.students #momentDeets div.definitions').css({
				'display' : 'block',
				'left' : ( defPos.left - 75 )+'px',
				'top' : ( defPos.top - ( boxH ) )+'px' 
			});
			return false;
		},
			function(e) {
				$('.students #momentDeets div.definitions').css({
					"display" :  "none"
				});
			}
		);
		
		/*
		$('.students #momentDeets a.defined').click(function(e){
			defID = $(this).attr('href');
			defText = $(this).attr('rel');
			$('.students #momentDeets div.definitions dl'+defID).html(defText);
			var defPos = $(this).position();
			var boxH = $('.students #momentDeets div.definitions').height();
			$('.students #momentDeets div.definitions dl:not('+defID+')').css('display','none');
			$('.students #momentDeets div.definitions').css({
				'display' : 'block',
				'left' : ( defPos.left - 75 )+'px',
				'top' : ( defPos.top - ( boxH ) )+'px' 
			});
			return false;
		});
			
		$('body.moment').unbind('click').click(function(e) {
			if( $(e.target).is('a.defined[href='+defID+']') ) {
				return false;
			} else {
				$('.students #momentDeets div.definitions').css({
					"display" :  "none"
				});
			}
		});
	*/
	/* Open/Close Student Detail Events */
	
	$('#studentEvents h2').click(function(){
		var ma = $(this).parent('li');
		ma.find('dl').slideToggle('medium', function(){
			ma.toggleClass('open');
		});
	});
	
	$('#openAllEvents').click(function(){
		$('#studentEvents dl').slideDown('medium', function(){
			$(this).parent('li').addClass('open');
		}); return false;
	});

	$('#closeAllEvents').click(function(){
		$('#studentEvents dl').slideUp('medium', function(){
			$(this).parent('li').removeClass('open');
		}); return false; 
	});
	
	/* Teacher view of Student Detail */
	
	$('#resetChild').click(function(){
		input_box = confirm("Are you sure you want to reset this child?  Click Cancel or OK to Continue.");
		if (input_box == true){ // Output when OK is clicked
			return true;
		} else { // Output when Cancel is clicked
			return false;
		}
	});
	
	$('#printDetail').click(function(){
		$('#studentEvents dl').slideDown('medium', function(){
			$(this).parent('li').addClass('open');
			if ( $(this).parent('li').is(':last-child') ) {
				window.print();
			} else return;
		}); 
		return false; 
	});
	
	/* Datepicker */
	
		if ($('#manageSchedule').length > 0) {
		
			$("#manageSchedule table.sortable td input").datepicker({
				showOn: 'button',
				buttonImage: 'img/icon-calendar.gif',
				buttonImageOnly: true,
				showButtonPanel: true,
				currentText: 'Show Today',
				dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S']
			});
			
		}
		
	/* Student Dashboard Change Class ID */

		$('#currentID a').click(function(){
			$('#currentID').css('display','none');
			$('#changeID').css('display','block')
			return false;
		});

});

function setUpScroll(w){
	
	var tlWidth = $('#childTimeline .child-wrap1 ol.years li').length*88;
	
	$('#childTimeline .child-wrap1 ol.years').width(tlWidth);
	
	$('#childTimeline .child-wrap1 a.prev').click(function(){
		var position = $('#childTimeline .child-wrap1 ol.years').position();
		if ( position.left != 0 && !$('#childTimeline .child-wrap1 ol.years').is(':animated') ) {  // Are we at the beginning?  Is the list still animating?
			$('#childTimeline .child-wrap1 ol.years').animate({
				"left": position.left + w 
			}, { queue: true, duration: 1000 });
		};
		return false; 
	});
	
	$('#childTimeline .child-wrap1 a.next').click(function(){
		var position = $('#childTimeline .child-wrap1 ol.years').position();
		if ( position.left > (0-(tlWidth-w)) && !$('#childTimeline .child-wrap1 ol.years').is(':animated') ) {  // Are we at the end?  Is the list still animating?
			$('#childTimeline .child-wrap1 ol.years').animate({
				"left": position.left - w
			}, { queue: true, duration: 1000 });
		};
		return false; 
	});
	
	/* Go to current age */
	
	var current = $('#childTimeline ol.years li.current').position();
	var m = Math.floor(current.left/w);
	$('#childTimeline .child-wrap1 ol.years').css('left',-(m*w));	

}	
function setUpAddClassForm(){
	$('form.hide-a-form').css("display","none");
	$('.other-buttons a#openAddClass').click(function(){
		$(this).parent().css("display","none");
		$('form.hide-a-form').css("display","block");
		return false;
	});
	$('a#closeAddClass').click(function(){
		$(this).parent().css("display","none");
		$('.other-buttons').css("display","block");
		return false;
	});
}