// JavaScript Document

tinyMCE.init({
mode: "textareas",
theme: "advanced",
theme_advanced_buttons1: "bold,italic,underline,|,link,unlink,|,undo,redo,|,bullist,numlist,|,code",
theme_advanced_buttons2: "",
theme_advanced_buttons3: "",
theme_advanced_toolbar_align : "left",
theme_advanced_toolbar_location : "top",
valid_elements : "@[id|class|style|title],a[rel|rev|charset|hreflang|tabindex|accesskey|type|name|href|target|title|class],script,strong/b,em/i,strike,u,#p,-ol[type|compact],-ul[type|compact],-li,br"
});

$(function(){
  // Document is ready
  $("#allDaySelect").bind("click", checkAllDay);
  checkAllDay();
  initSponsors();
  initLocation();
  initToolTips();
  $("#dateSelect").datepicker();
  $("#startTime").timePicker({show24Hours:false});
  $("#endTime").timePicker({show24Hours:false});
  $("#dateSelect").attr('autocomplete', 'OFF'); // Disable browser autocomplete
  $("#Username").focus();
  $("#roomSelect").autocomplete("locations.cfm", {
	   extraParams: {
       bid: function() { return $("#buildingSelect").val(); }
   }
});

});

function initToolTips() {

	$(".caption").hide();
	$(".captioned").each(
						 function() {
							 it = $(this);
							 cap = it.parent().next('.caption');
							 cap.addClass("fixed");
							 cap.css("position", "absolute");
							 cap.css("width", 222);
							 cap.css("left", it.position().left + it.width() + 15);
							 cap.css("top", it.position().top);
						 });
	
	$(".captioned").focus(function() {$(this).parent().next('.caption').css("top", $(this).position().top); $(this).parent().next('.caption').fadeIn(200)});
	$(".captioned").blur(function() {$(this).parent().next('.caption').hide()});
}


function checkAllDay()
{
	check = document.getElementById("allDaySelect");
	if (check)
	{
	enableTimeSelect = check.checked;
	$("span.timeSelect input").each (function (i) {this.disabled = enableTimeSelect;});
	$("span.timeSelect label").each (function (i) {if (enableTimeSelect) $(this).addClass("disabled"); else $(this).removeClass("disabled");});
	}
}

var numSponsors;
var maxSponsors;

function addSponsor() {
	if (numSponsors < maxSponsors)
	{
		numSponsors++;
		$("div#spons" + numSponsors).show(100);
		if (numSponsors == maxSponsors)
			$("#addSpons").hide();
	}
}

function removeSponsor() {

}

function initSponsors() {
	
	numSponsors = 1;
	maxSponsors = $(".sponselect").size();
	$("#spons" + maxSponsors).after("<p id='addSpons'><a href='javascript:void(0)' onclick='addSponsor()'>Add another sponsor</a></p>");
	
	for (var i=1; i <= maxSponsors; i++)
	{
		$("#optDept" + i).bind("click", updateOpt);
		$("#optOrg" + i).bind("click", updateOpt);
		if (document.getElementById("optDept" + i).checked)
		{
			$("#spons" + i + " select.stud").hide();
			$("#spons" + i + " li.dept").addClass("selected");
			$("#spons" + i + " li.stud").removeClass("selected");
		}
		else
		{
			$("#spons" + i + " select.dept").hide();
			$("#spons" + i + " li.dept").removeClass("selected");
			$("#spons" + i + " li.stud").addClass("selected");
		}
	}	

	for (var i=2; i <= maxSponsors; i++)
	{
		if ($("#spons" + i + " select[value!='']").val() == null)
		{
			$("#spons" + i).hide();
		}
		else
		{
			numSponsors = i;
			if (numSponsors == maxSponsors)
				$("#addSpons").hide();
		}
	}

}

function updateOpt() {
	var dept = this.value;
	var num = this.name.substr(7);
	if (dept == "dept")
	{
		$("#spons" + num + " select.dept").show();
		$("#spons" + num + " select.stud").hide();
		$("#spons" + num + " li.dept").addClass("selected");
		$("#spons" + num + " li.stud").removeClass("selected");
	}
	else {
		$("#spons" + num + " select.dept").hide();
		$("#spons" + num + " select.stud").show();
		$("#spons" + num + " li.dept").removeClass("selected");
		$("#spons" + num + " li.stud").addClass("selected");
	}
}

var onCampusBox;
var offCampusTab;
var onCampusTab;

function initLocation() {

	onCampusBox = document.getElementById("optOnCampusYes");
	onCampusTab = $(onCampusBox).parent().get(0);
	offCampusTab = $("#optOnCampusNo").parent().get(0);
	$("#optOnCampusNo").bind("click", updateLocation);
	$("#optOnCampusYes").bind("click", updateLocation);
	updateLocation();
}

function updateLocation() {
	if (onCampusBox.checked)
	{
		$(".offCampusLocation").hide();
		$(".onCampusLocation").show();
		$(offCampusTab).removeClass("selected");
		$(onCampusTab).addClass("selected");
	}
	else
	{
		$(".onCampusLocation").hide();
		$(".offCampusLocation").show();
		$(onCampusTab).removeClass("selected");
		$(offCampusTab).addClass("selected");
	}
}
