$(function(){
	
	$("a[rel=external]").live("click", function(){this.target="_blank"});
	$(".IE6 li:first-child").addClass('first-child'); 
	$("#quote .livedit").click(editQuote).css('cursor: pointer');
	$(".livedit.calendar h3 img.livedit").click(editCalendar).css('cursor','pointer');
	$(".liveditform .save").live("click",saveCalendar);
	$(".liveditform .add").live("click",addToCalendar);
	$(".liveditform .delete").live("click",removeFromCalendar);
	$("a.send").live("click",function(){$(this).closest("form").submit();});
	$('a').each(function(){if(this.href==window.location) $(this).parents("li").addClass('indicator');}); 
});

function editQuote(){
	var quote = $("#quote p span").text();
	$("#quote p").hide();
	$("#quote").append("<form class='liveditform'><textarea name='quote'>"+quote+"</textarea><input type='button' value='Save' onclick='saveQuote();'/></form>");

}
function saveQuote(){
	$.post(window.location+'?action=save-quote', $('#quote form').serialize());
	var quote = $('#quote textarea').val();
	$('#quote form').remove()
	$("#quote p span").text(quote).parent().show();
}

function editCalendar(){
	
	var id = this.parentNode.id.match(/\d+/);
	
	if($("#form"+id).length>0)
		return;
	
	
	if( $("#day"+id).length > 0 ){
		var calendar = $("#day"+id).hide().after("<form class='liveditform' id='form"+id+"'></form>");		
	}else{
		$(this).after( "<form class='liveditform' id='form"+id+"'></form>");			
	}
	var form = $("#form"+id);
	
	$("#day"+id+" li").each(function(){
		var id = this.id.match(/\d+/);
		var formhtml = "";
		if($(this).find(".time"      ).length > 0) formhtml += "<label for='time"       +id+"'>Time      </label><input type='text'           id='time"       +id+"' name='time"       +id+"' /><br />";
		if($(this).find(".title"     ).length > 0) formhtml += "<label for='title"      +id+"'>Title     </label><input type='text'           id='title"      +id+"' name='title"      +id+"' /><br />";
		if($(this).find(".instructor").length > 0) formhtml += "<label for='instructor" +id+"'>Instructor</label><input type='text'           id='instructor" +id+"' name='instructor" +id+"' /><br />";
		var time       = $(this).find(".time"      ).text();
		var title      = $(this).find(".title"     ).text();
		var instructor = $(this).find(".instructor").text();
		$(form).append("<p class='entree' id='p"+id+"'><input type='button' value='delete' style='float:right;' class='delete' />"+formhtml+"</p>");
		
		$("#time"       +id).val(time);
		$("#title"      +id).val(title);
		$("#instructor" +id).val(instructor);
		
	});
	$(form).append("<input type='button' value='Save' class='save' /><input type='button' value='Add Another' class='add' />")
	
}


function saveCalendar(){

	var id = $(this).closest("form").attr('id').match(/\d+/);
	var data = $(this).closest("form").serialize();
	
	$(this).closest("form").remove();
	$("#day"+id).remove();
	$("#day"+id).show();
	$.post(window.location+"?action=saveCalendar&day="+id, data, function(data){
		$("#dayl"+id).after(data);
	});
}

function addToCalendar(){
	var id = $(this).closest("form").attr('id').match(/\d+/);
	var data = $(this).closest("form").serialize();

	$(this).closest("form").remove();
	$("#day"+id).remove();
	
	$.post(window.location+"?action=addToCalendar&day="+id, data, function(data){
		$("#dayl"+id).after(data);
		$("#dayl"+id).click();
	});
}

function removeFromCalendar(){
	var id = $(this).closest("p").attr('id').match(/\d+/);
	$(this).closest("p").remove();
	$.post(window.location+"?action=removeFromCalendar&cid="+id);
}

