// JavaScript Document

	jQuery(document).ready(function($) {
		
		
		$('.notifications > .remove').live('click', function(e) {
			
			var data = 'id=' + $(this).attr('rel') + '&action=deleteMessage&userId=' + $(this).attr('title');
			$(this).parent().addClass('read');;
			var element = $(this).parent();
			$(this).parent().remove();
			$('div.notifications:last-child').after(element);
			$('div.read > a').remove();
			
			
			e.preventDefault();
			$.ajax({
				url: 'notifications/notifyControl.php',
				type: 'POST',
				dataType: 'html',
				data: data
			});
		});
		
		$('.notifications > h3').live('click', function(e) {
			
			e.preventDefault();
			$(this).parent().children('*').toggle();
			$(this).parent().children('h3').css('display', 'block');
			$(this).parent().children('span').css('display', 'block');
		});
		
		$('#notifyLink').click(function(e) {
			e.preventDefault();
			
			$('td[align="left"]').remove();
			$('#notify').remove();
			$.ajax({
					url: 'notifications/notifyControl.php', 
					type: 'POST',
					dataType: 'html',
					data: 'action=getForm',
					success: function(html) {
						$('#navigation').parents('tr').after('<tr><td>'+html+'</td></tr>');
						
						
						$('.notifications input[type="radio"]').change( function() {
							var value = $(this).val();
							if(value === 'status') {
								$('#notifySelects > select').removeAttr('style');
								$('#notifySelects > legend').css('display', 'block');
								$('#notificationStatuses').css('display', 'inline-block');
  							} else if (value == 'class') {
  								$('#notifySelects > select').removeAttr('style');
  								$('#notifySelects > legend').css('display', 'block');
  							    $('#notificationStatuses').css('display', 'inline-block');
  							    $('#notificationClass').css('display', 'inline-block');
  							} else if (value == 'student') {
  								$('#notifySelects > select').removeAttr('style');
  								$('#notifySelects > legend').css('display', 'block');
  								$('#notificationStudents').css('display', 'inline-block');
  							} else if (value == 'grade') {
  								$('#notifySelects > select').removeAttr('style');
  								$('#notifySelects > legend').css('display', 'block');
  								$('#notificationGrade').css('display', 'inline-block');
  							}
						});
						
						$('.notifications').submit(function(e) {
							e.preventDefault();
							
							var fail = false;
							$('.required').each(function() {
								if($(this).val() == '') {
									$(this).addClass('error');
									$(this).prev().addClass('error');
									fail = true;
								}
							});
							if(fail == true) {
								return false;
							}
							
							$('.error').removeClass('error');

							$.ajax({
								url: 'notifications/notifyControl.php', 
								type: 'POST',
								dataType: 'html',
								data: 'action=sendNotify&method=' + $('input[name=method]:checked').val() + '&student=' + $('#notificationStudents').val() + '&status=' + $('#notificationStatuses').val() + '&class=' + $('#notificationClass').val() + '&subject=' + $('#subject').val() + '&message=' + $('#message').val() + '&from=' + $('#from').val() + '&grade=' + $('#notificationGrade').val(),
								success: function(html) {
									$('p.notice').remove();
									$('select').removeAttr('sel');
									$('input[type=text]').val('');
									$('textarea').val('');
									$('#navigation').parents('tr').after('<tr><td>'+html+'</td></tr>');
									
								 }
							});
						});						
					}
			});			
		});
	});

	
