var strCurrSchType = "Secondary"

function bookCourse(intCourseID){
	$('.dialogHolder').remove();
	$('#selectCourseID').val(intCourseID);
	//alert('Course Selected');
}

function getCourseVenues(blnIsSecondary) {
	if (blnIsSecondary) {
		if (strCurrSchType != "Secondary") {
			strCurrSchType = "Secondary"
			$.getJSON('/events.asp?limited=0', function(data) {
				processVenues(data);
			});
		}
	} else {
		if (strCurrSchType != "Other") {
			strCurrSchType = "Other"
			$.getJSON('/events.asp?limited=1', function(data) {
				processVenues(data);
			});
		}
	}
}

function processVenues(data) {
	var strHiddenOverlays = "";
	var strRadioField = "";
	removeMarkers()
	intTown = 0;
	$.each(data, function(key,value){
		intTown = intTown + 1;
		objTown = value;
		addMarker(intTown,key,objTown.position.lat,objTown.position.lon);
		strHiddenOverlays = strHiddenOverlays + '<div id="marker'+intTown+'" class="courseContainer">';
		strHiddenOverlays = strHiddenOverlays + '	<p>The following courses are available at your selected location:</p>';
		strHiddenOverlays = strHiddenOverlays + '	<span class="courseDetails">';
		strHiddenOverlays = strHiddenOverlays + '		<span class="courseName courseTitle">Course Name</span>';
		strHiddenOverlays = strHiddenOverlays + '		<span class="courseDate courseTitle">Course Date(s)</span>';
		strHiddenOverlays = strHiddenOverlays + '	</span>';
		strRadioField = strRadioField + '<h4>'+key+'</h4>';
		$.each(objTown.courses, function(key,value){
			strHiddenOverlays = strHiddenOverlays + '	<span class="courseDetails">';
			strHiddenOverlays = strHiddenOverlays + '		<span class="courseName">'+key+'</span>';
			strHiddenOverlays = strHiddenOverlays + '		<span class="courseDate">'+value.date+'</span>';
			strHiddenOverlays = strHiddenOverlays + '	</span>';
			strHiddenOverlays = strHiddenOverlays + '	<a onclick="bookCourse('+value.id+')" class="selCourseButton">Select</a>';
			strRadioField = strRadioField + '<span class="radioField"><input type="radio" id="course_'+value.id+'" value="'+value.id+'" name="courseID"><label for="course_'+value.id+'">'+key+' - '+value.date+'</label></span>';
		});
		strHiddenOverlays = strHiddenOverlays + '</div>';
	});
	if (strCurrSchType == "Other") {
		strRadioField = strRadioField + '<h4>Other Options</h4>';
		strRadioField = strRadioField + '<span class="radioField"><input type="radio" id="course_-1" value="-1" name="courseID">';
		strRadioField = strRadioField + '<label for="course_-1">I am not eligible to participate in the CPD</label></span>';
		strRadioField = strRadioField + '<span class="radioField"><input type="radio" id="course_0" value="0" name="courseID">';
		strRadioField = strRadioField + '<label for="course_0">I will select my course later</label></span>';
	} else {
		strRadioField = strRadioField + '<h4>Other Options</h4>';
		strRadioField = strRadioField + '<span class="radioField"><input type="radio" id="course_-1" value="-1" name="courseID">';
		strRadioField = strRadioField + '<label for="course_-1">I am not a secondary school teacher in England, therefore will not be participating in the CPD</label></span>';
		strRadioField = strRadioField + '<span class="radioField"><input type="radio" id="course_0" value="0" name="courseID">';
		strRadioField = strRadioField + '<label for="course_0">I am a secondary school teacher in England, but would like to select my course later</label></span>';
	}
	$("#hiddenOverlays").html(strHiddenOverlays);
	$("#locationOptions").html(strRadioField);
}

function addMarker(strID,strName,strLat,strLon) {
	window["marker" + strID] = new GMarker(new GLatLng(strLat,strLon),{draggable:true,title:strName});
	map.addOverlay(window["marker" + strID]);
	GEvent.addListener(window["marker" + strID],'click',function(latlng){
		$('body').dialog({standalone:true,target:'#marker'+strID,width:540,height:300});
	});
}

function removeMarkers() {
	map.clearOverlays()
	$("#hiddenOverlays").html('');
}

$(document).ready(function(){
	$(".courseOther").click(function() {
		$("#selectCourseID").val('n/a');
	});
	$("#selectCourseID").change(function() {
		if ($(this).val() != 'n/a') {
			$(".courseOther").each(function() {
				$(this).attr('checked', false);
			});
		}
	});
	$("#CustomField7").change(function(){
		$option = $(this);
		switch($option.val()) {
			case "Secondary state maintained school":
				$("#independent_cost").remove();
				getCourseVenues(true);
				break;
			case "Independent school":
				$option.parent().append('<p id="independent_cost">Please note teachers from independent schools are required to pay £100 for the HEDP CPD. See FAQ for further details.</p>');
				getCourseVenues(true);
				break;
			case "LA Adviser":
				$("#independent_cost").remove();
				getCourseVenues(false);
				break;
			case "PGCE Tutor":
				$("#independent_cost").remove();
				getCourseVenues(false);
				break;
			case "SACRE Member":
				$("#independent_cost").remove();
				getCourseVenues(false);
				break;
			case "Other":
				$("#independent_cost").remove();
				getCourseVenues(true);
				break;
		}
	});
});