$(document).ready(function() {
	
	$('.media').media();
	
	$('#growl').hide();
	setInterval("$('#growl').fadeIn(); $('#growl').html('Some Notification');", 10000);
	$('#growl').click(function() { $(this).fadeOut(); });
	
	$('.file').click(function() {
		var current_id = $(this).attr('id');
		$("#description").fadeIn();
		$('#description').html('<h1 class="heading" style="text-align:center;"><img src="/img/loading.gif" /> Loading</h1>');
		setTimeout($("#description").load("/fields/description", { 'id': current_id } ), 1250);
	});	
	
	$("#browser").treeview({
   		animated: "fast",
   		collapsed: true,
   		unique: true
	})
	
	// Sidebar 	
	$('#link_1').click(function() { $('#set_1').slideToggle(); });
	$('#link_2').click(function() { $('#set_2').slideToggle(); });
	$('#link_3').click(function() { $('#set_3').slideToggle(); });
	
	$('#add_field').hide();
	$('#add_category').hide();
	$('#add_heading').hide();
	$('#add_student').hide();
	$('#student_list').hide();
	$('#add_folder').hide();
	
	$('#add_field_link').click(function() {
		$('#add_field_link').hide();
		$('#add_field').fadeIn();
		return false;
	});
	
	$('#add_category_link').click(function() {
		$('#add_category_link').hide();
		$('#add_category').fadeIn();
		return false;
	});

	$('#add_heading_link').click(function() {
		$('#add_heading_link').hide();
		$('#add_heading').fadeIn();
		return false;
	});		
	
	$('#add_folder_link').click(function() {
		$('#add_folder_link').hide();
		$('#add_folder').fadeIn();
		return false;
	});	
	
	$('#add_student_link').click(function() {
		$('#student_controls').show();
		$('#student_controls').hide();
		$('#add_student').fadeIn();
		return false;
	});

	jQuery(function(){
		jQuery('ul.superfish').superfish();
	});


	$('#category_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/categories/sort",
				data: $.tableDnD.serialize()
			});
		}
	});
	
	$('#views_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/views/sort",
				data: $.tableDnD.serialize()
			});
		}
	});	
	
	$('#headings_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/headings/sort",
				data: $.tableDnD.serialize()
			});
		}
	});		
	
	$('#fields_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/fields/sort",
				data: $.tableDnD.serialize()
			});
		}
	});			
	
	/*
	$('#courses_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/courses/sort",
				data: $.tableDnD.serialize()
			});
		}
	});		
	*/
	
	$('#media_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/media/sort",
				data: $.tableDnD.serialize()
			});
		}
	});					
	
	$('#quizzes_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/quizzes/sort",
				data: $.tableDnD.serialize()
			});
		}
	});

	$('#form_table').tableDnD({
		onDrop: function(table, row) {
			$.ajax({
				type: "POST",
				url: "/students/sort",
				data: $.tableDnD.serialize()
			});
		}
	});	

	
	$('#courses_table').click($.delegate({
	'.title': function(event) {
		var id = $(event.target).attr('id');
		$('#student_list_close').fadeIn();
		$('#student_list').fadeIn();
		$("#student_list").load("/courses/permissions", { 'id': id } );
		return false;
	}
	}));
	
	$('#media_table').click($.delegate({
	'.title': function(event) {
		var id = $(event.target).attr('id');
		$('#student_list_close').fadeIn();
		$('#student_list').fadeIn();
		$("#student_list").load("/media/permissions", { 'id': id } );
		return false;
	}
	}));	

	$('#student_list_close').click(function() {
		$('#student_list_close').hide();
		$('#student_list').fadeOut();
		return false;
	});		
	
	$('#add_student_close').click(function() {
		$('#add_student_close').hide();
		$('#add_student').fadeOut();
		$('#add_student_link').show();
		return false;
	});			
	
	$('#boolean').hide();
	$('#multiple').hide();
	$('#essay').hide();
	
	$('#boolean_link').click(function() {
		$('#boolean').show();
		$('#essay').hide();
		$('#multiple').hide();
		$('#graphic').hide();
		return false;
	});
	
	$('#essay_link').click(function() {
		$('#boolean').hide();
		$('#essay').show();
		$('#multiple').hide();
		$('#graphic').hide();
		return false;
	});
	
	$('#multiple_link').click(function() {
		$('#boolean').hide();
		$('#essay').hide();
		$('#multiple').show();
		$('#graphic').hide();
		return false;
	});
	
	// VERY IMPORTANT!
	$('#c_perm').hide();
	$('#m_perm').hide();
	$('#q_perm').hide();
	$('#v_perm').hide();

	$('#media_permissions_link').click(function() {
		$('#c_perm').hide();
		$('#v_perm').hide();
		$('#q_perm').hide();
		$('#m_perm').show();
		return false;
	});

	$('#course_permissions_link').click(function() {
		$('#m_perm').hide();
		$('#q_perm').hide();
		$('#v_perm').hide();
		$('#c_perm').show();
		return false;
	});
	
	$('#quiz_permissions_link').click(function() {
		$('#m_perm').hide();
		$('#c_perm').hide();
		$('#v_perm').hide();
		$('#q_perm').show();
		return false;
	});	
	

	$('#view_permissions_link').click(function() {
		$('#m_perm').hide();
		$('#c_perm').hide();
		$('#q_perm').hide();
		$('#v_perm').show();
		return false;
	});

	$('.program_container').click
  (
    function ()
    {
      var current_id = $(this).attr('id');
      var e = document.getElementById(current_id);
      var p = e.parentNode.parentNode;
      p.className = p.className == 'active' ? '' : 'active';
      return false;
    }

  );

  $("#media_pool").sortable({
		connectWith: ["#media_permissions"]
	});
	$("#media_permissions").sortable({
		connectWith: ["#media_pool"]
	});

	$(".course_pool").sortable({
		connectWith: ["#course_permissions"]
	});
	$("#course_permissions").sortable({
		connectWith: [".course_pool"]
	});	
	
	$("#quiz_pool").sortable({
		connectWith: ["#quiz_permissions"]
	});
	$("#quiz_permissions").sortable({
		connectWith: ["#quiz_pool"]
	});

	$("#view_pool").sortable({
		connectWith: ["#view_permissions"]
	});
	$("#view_permissions").sortable({
		connectWith: ["#view_pool"]
	});
	
	$('#search_courses').keyup(function() {
		var term = $("[@name^='course_term']").val();
		$.ajax({
			type: "POST",
			url: "/courses/lookup_courses",
			data: "term="+term+"&course=null",
			success: function(msg){
				$("#course_pool").html(msg);
			}
		});
		return false;
	});
	
	$('#search_media').keyup(function() {
		var term = $("[@name^='media_term']").val();
		$.ajax({
			type: "POST",
			url: "/media/lookup_media",
			data: "term="+term+"&media=null",
			success: function(msg){
				$("#media_pool").html(msg);
			}
		});
		return false;
	});	
	
	$('#search_quizzes').keyup(function() {
		var term = $("[@name^='quiz_term']").val();
		$.ajax({
			type: "POST",
			url: "/quizzes/lookup_quiz",
			data: "term="+term+"&quiz=null",
			success: function(msg){
				$("#quiz_pool").html(msg);
			}
		});
		return false;
	});		

	$('#search_views').keyup(function() {
		var term = $("[@name^='view_term']").val();
		$.ajax({
			type: "POST",
			url: "/views/lookup_view",
			data: "term="+term+"&quiz=null",
			success: function(msg){
				$("#view_pool").html(msg);
			}
		});
		return false;
	});

  /*
  $('#student_import_form').submit
  (
    function()
    {
      $("[@name^='student_field_map']").val($('#student_field_map').sortable('toArray'));
    }
  );

	$("#student_field_map").sortable({
		//connectWith: ["#view_permissions"]
	});

  */

	// save course permissions
	$('#save_permissions').submit(function() {
		var course_perms = $('#course_permissions').sortable('serialize');
		var media_perms = $('#media_permissions').sortable('serialize');
		var quiz_perms = $('#quiz_permissions').sortable('serialize');
		var view_perms = $('#view_permissions').sortable('serialize');
		var user_id = $("[@name^='user_id']").val();
		$.ajax({
			type: "POST",
			url: "/students/save_permissions",
			data: media_perms+"&"+course_perms+"&"+quiz_perms+"&"+view_perms+"&user_id="+user_id,
			success: function(msg){
				$('#response').html(msg);
			}
		});
		return false;
	});
	
	$('.media_data').click(function() {
		var id = $(this).attr('id');
		$('#preview').load('/media/player', {id:id});
		return false;
	});	
	
	$('.course_data').click(function() {
		var id = $(this).attr('id');
		$('#preview').load('/courses/player', {id:id});
		return false;
	});		
	

	$('#sample_media').click(function() {
		var term = $("[@name^='source']").val();
		$.ajax({
			type: "POST",
			url: "/media/validate",
			data: "source="+term+"&type=media",
			success: function(msg){
				$("#source_return").html(msg);
			}
		});
		return false;
	});
	
	$('#sample_thumb').click(function() {
		var term = $("[@name^='thumbnail']").val();
		$.ajax({
			type: "POST",
			url: "/media/validate",
			data: "source="+term+"&type=image",
			success: function(msg){
				$("#image_return").html(msg);
			}
		});
		return false;
	});	

	$("#datepicker1").datepicker();
	$("#datepicker2").datepicker();
	$("#datepicker3").datepicker();
	$("#datepicker4").datepicker();
	$("#datepicker5").datepicker();
	$("#datepicker6").datepicker();
	$("#datepicker7").datepicker();
	$("#datepicker8").datepicker();
	$("#datepicker9").datepicker();
	$("#datepicker10").datepicker();
	$("#datepicker11").datepicker();
	$("#datepicker12").datepicker();

  //alert('add stuff');
});



