// Adds callback from DOM ready
$(document).ready(adminDomReady);

// Adds table sorter parser to deal with numeric values correctly.
$.tablesorter.addParser({ 
  id: 'integer', 
  is: function(s) { 
      return false; 
  }, 
  format: function(s) { 
      return parseInt(s);
  }, 
  type: 'numeric' 
});
  
// Function when DOM is ready for manipulation.
function adminDomReady() {
  if ($('#adminTabs').length > 0) {
    selectCurrentNavigationLink();
  	tabsParams = {
  	    load: adminTabsLoaded,
        show: adminTabShown,
  	    cache: true,
  	    spinner: '',
  	    select: 0
    	};
  	$('#adminTabs').tabs(tabsParams);
	}
}

// Function to select the current navigation link so it appears bold and it is
// clear that the current page is the destination of that link.
function selectCurrentNavigationLink() {
  $("a[href='" + location.pathname + "']").addClass("selected");
}

// Function called when the tabs container and initial tab is loaded.
function adminTabsLoaded(event, ui) {
  $('ul#adminTabsList').show();
  if (ui.panel.id == "create_new_user") {

  }
  else {
    tb_init('a.thickbox');
    $("a.thickbox").removeClass("thickbox");
  //  $('#tabs').tabs('load', ui.index); 
  }
}

// Function called when a tab is shown.
function adminTabShown(event, ui) {
//  updateUrlWithTab(ui.panel.id);
}

// Function called to invite the user specificed by the user_id parameter.
function invite(user_id) {
	confirmation_message = "Are you sure you want to send this user an invitation email?";
	if (confirm(confirmation_message)) {
		$.post("/users/invite_user", 
			{ user_id: user_id },
			refresh);
	}
}

// Function to uninvite the user specified by the user_id parameter.
function uninvite(user_id) {
	$.post("/users/uninvite_user",
		{ user_id: user_id },
		refresh);
}

// Function to reload the page.
function refresh() {
	location.reload();
}

// Function to add the table sorter functionality to the table specified.
function addTableSorter(table) {
  if (table.find('tbody').children().length > 0) {
		table.tablesorter({
				widthFixed: true, 
				sortList: [[0, 1]]
				})
			 .tablesorterPager({
			  size:100,
				container: table.next(),
				positionFixed: false
				});
	}
}

// Function to delete a specific reward based on its ID
function deleteReward(reward_id) {
  confirmation_message = "Are you sure you want to delete this reward?";
	if (confirm(confirmation_message)) {
    $.post("/admin/rewards/delete_reward",
      { reward_id: reward_id },
      null);
    $(".reward_" + reward_id).remove();
	}
}

// Function to delete a specific classroom based on its ID
function deleteClassroom(classroom_id) {
  confirmation_message = "Are you sure you want to delete this classroom?";
	if (confirm(confirmation_message)) {
    $.post("/admin/classrooms/delete_classroom",
      { classroom_id: classroom_id },
      null);
    $(".classroom_" + classroom_id).remove();
	}
}


// Function to open the app server to edit a user
function editUser(user_key) {
  url = "http://appengine.google.com/datastore/edit?app_id=carrotsticks-www&key=" + user_key;
  window.open(url, "_blank");
}


// Function to delete a specific user based on their ID
function deleteUser(user_id) {
  confirmation_message = "Are you sure you want to delete this user?";
	if (confirm(confirmation_message)) {
    $.post("/admin/users/delete_user",
      { user_id: user_id },
      null);
    $(".user_" + user_id).remove();
	}
}
