$(document).ready(function() {
    $(".tablesorter").tablesorter();
    $('.tablesorter').tableHover({cellClass: 'hovercell', clickClass: 'click'});
	$('#add_presenter_button').click(function() {
		addPresenter();
	})
	
	// Start dialog code		
	$('#dialog_container').dialog({
		autoOpen: false,
		width: 650,
		height: 800,
		modal: true,
		buttons: {
			"Cancel": function() { 
				$(this).dialog("close"); 
			}
		}
	});
	
	$('a.dialog').click(function(){
		loadDialog (this);
		return false;
	});
	// End dialog code
	
	
	$(function(){
		$(".tooltip").mbTooltip({
			opacity : .90, //opacity
			wait:250, //before show
			ancor:"mouse", //”parent”
			cssClass:"default", // default = default
			timePerWord:700, //time to show in milliseconds per word
			hasArrow:true,
			imgPath:"http://connections.jimsnodgrass.com/img/",
			shadowColor:"black",
			fade:200
		});
	})
	$(document).enableTooltip();
	
});


function loadDialog (clickedLink) {
	
	$("#dialog_container").html('<p align="center"><br /><br /><br /><img src="/img/loader.gif"></p>');
	$('#dialog_container').dialog('option', 'title', 'Dialog Box');
	$("#dialog_container").dialog("open");
	
	$.ajaxSetup({
	  'beforeSend': function(xhr) {xhr.setRequestHeader("If-Modified-Since",
	    "Sat, 1 Jan 2000 00:00:00 GMT")}
	})
  
	$.ajax({
		url: $(clickedLink).attr("href"),
		timeout: 20000,
		error: function() {
			console.log("Failed to submit");
		},
		success: function(html) {
			$("#dialog_container").html(html);
			$('a.switchDialog').click(function(){
				$("#dialog_container").dialog("close");
				loadDialog (this);
				return false;
			});
			$(".tablesorter").tablesorter();
			$('.tablesorter').tableHover({cellClass: 'hovercell', clickClass: 'click'});
		}
	});
	return false;
}

function removeThis (item, nested) {
	$(item).parent().parent().remove();
}

function addPresenter (root) {
	var title = $("#add_presenter_span").attr("title");
	var num = Number(title);

	$.ajaxSetup({
	  'beforeSend': function(xhr) {xhr.setRequestHeader("If-Modified-Since",
	    "Sat, 1 Jan 2000 00:00:00 GMT")}
	})
  
	$.ajax({
		url: root + 'ajax/listpresenters/',
		timeout: 20000,
		error: function() {
			console.log("Failed to submit");
		},
		success: function(html) {
			$("#add_presenter_span").append('<li><label for="presenter'+num+'">Presenter'+num+':<img src="'+root+'img/cancel.png" class="form_button" onclick="removeThis(this)" /></label><select name="presenter'+num+'" >'+html+'</select></li>');
			$("#add_presenter_span").attr("title", num + 1);
		}
	});
	
}




