var courses = [];
var courseevents = [];

var coursecats = {};

var sCategory = "";
var sCourse = "";
var sLocation = "";
var sDate = "";

$(document).ready(function(){

	
	if ( $("#block-views-Booking-block_1").length != 0 ) {
		
		
		var content = '<div class="edit-submit-generated">&nbsp;</div>';
		
		$("#block-contact_form_blocks-2 .form-submit").hide();
 		$("#block-contact_form_blocks-2 .form-submit").parent().append( content );
 		$("#block-contact_form_blocks-2 .edit-submit-generated").click(function (){
 			$("#contact-mail-page2").submit();
 		});
		
		
		var write = "";
		
		// get info from page
		
		$("#block-views-Booking-block_1 .view-content .views-row").each(function(){
		
			var title = "" + $(this).children(".views-field-title").children(".field-content").text();
			
			var type = "" + $(this).children(".views-field-type").children(".field-content").text();
			
			var cats = [];
			$(this).children(".views-field-tid").children(".field-content").children(".item-list").children("ul").children("li").each(function(){
				cats.push( $(this).text() );
			});
			
			var location = "" + $(this).children(".views-field-field-location-nid").children(".field-content").text();
			var course = "" + $(this).children(".views-field-field-course-nid").children(".field-content").text();
			
			var dates = "";
			var firstdate = true;
			$(this).children(".views-field-field-date-time-value").children(".field-content").children(".field-item").each(function(){
				if (firstdate){
					firstdate = false;
					dates += $(this).text().replace(/,/gi,"") ;
				} else {
					dates += ", " +$(this).text().replace(/,/gi,"") ;
				}
			});
			if (dates == ""){ // single values are formatted differently. 
				dates = "" + $(this).children(".views-field-field-date-time-value").children(".field-content").text().replace(/,/gi,"");
			}
			
			
			write += "type: " + type + ", title: " + title +  ", cats: " + cats +  ", location: " + location +  ", course: " + course + ", dates: " + dates +"\n\n";
			
			
			if (type == "Course") {
				var thiscourse = {
					'title' : title,
					'cats' : cats
					}
				courses.push(thiscourse);
			}
			
			if (type == "Course Event") {
				var thiscourseevent = {
					'title' : title,
					'course' : course,
					'location' : location,
					'dates' : dates
					}
				courseevents.push(thiscourseevent);
			}
			
			
		});
		
		
		//alert(write);
		write = "";
		
		// get coursecats
		for ( var i in courses )
		{
			for ( var j in courses[i].cats )
			{
    			coursecats[courses[i].cats[j]] = courses[i].cats[j];
    		}    		
		} 
		
		buildCategories ();
		
	}
});

function buildCategories () {
	var content = "";
		
	// category
	$("#edit-coursecategory").unbind('change');

	content += '<select id="edit-coursecategory" name="coursecategory" class="form-text">';
	content += '<option>- Select Course Category -</option>\n';
	
	var valid = false;
	for ( var i in coursecats )
	{			
	    content += '<option value="'+i+'">'+i+'</option>\n';
	    valid = true;
	}
	content +='</select>\n';
	
	if(valid){
	
		$("#edit-coursecategory").replaceWith(content);
		
		// category event
		$("#edit-coursecategory").change(function() {
			sCategory = $(this).children(":selected").text();
			buildCourses();
		});
		
	} else {
	
		//content = '<div id="edit-coursecategory" class="form-text select-inactive">&nbsp;- No course categories found, please try again or book by phone -</div>\n';
		content = '<input type="text" class="form-text" value="" size="60" id="edit-coursecategory" name="coursecategory" maxlength="128">\n';
		$("#edit-coursecategory").replaceWith(content);
		
	}
	
	
	// course
	if(valid){
		content = '<div id="edit-coursetitle" name="coursetitle" class="form-text select-inactive">&nbsp;</div>\n';
	} else {
		content = '<input type="text" class="form-text" value="" size="60" id="edit-coursetitle" name="coursetitle" maxlength="128">\n';
	}
	$("#edit-coursetitle").replaceWith(content);
	
	
	// location
	if(valid){
		content = '<div id="edit-courselocation" class="form-text select-inactive">&nbsp;</div>\n';
	} else {
		content = '<input type="text" class="form-text" value="" size="60" id="edit-courselocation" name="courselocation" maxlength="128">\n';
	}
	$("#edit-courselocation").replaceWith(content);

	
	// date
	if(valid){
		content = '<div id="edit-coursedate" class="form-text select-inactive">&nbsp;</div>\n';
	} else {
		content = '<input type="text" class="form-text" value="" size="60" id="edit-coursedate" name="coursedate" maxlength="128">\n';
	}
	$("#edit-coursedate").replaceWith(content);
}

function buildCourses () {
	var tempCourses = {};
	// get courses by category
	
	for ( var i in courses )
	{
		for ( var j in courses[i].cats )
		{
			if (courses[i].cats[j] == sCategory){
				tempCourses[courses[i].title] = courses[i].title;
			}
		}
	} 

	var content = "";
		
	// course
	$("#edit-coursetitle").unbind('change');

	content += '<select id="edit-coursetitle" name="coursetitle" class="form-text">';
	content += '<option>- Select Course -</option>\n';
	
	var valid = false;
	for ( var i in tempCourses )
	{			
	    content += '<option value="'+i+'">'+i+'</option>\n';
	    valid = true;
	}
	
	content +='</select>\n';
	
	
	if(valid){
	
		$("#edit-coursetitle").replaceWith(content);
		
		// course event
		$("#edit-coursetitle").change(function() {
			sCourse = $(this).children(":selected").text();
			buildLocations();
		});
		
	} else {
	
		//content = '<div id="edit-coursetitle" class="form-text select-inactive">&nbsp;- No course categories found, please try again or book by phone -</div>\n';
		content = '<input type="text" class="form-text" value="" size="60" id="edit-coursetitle" name="coursetitle" maxlength="128">\n';
		$("#edit-coursetitle").replaceWith(content);
		
	}
	
		
	

	// location
	if(valid){
		content = '<div id="edit-courselocation" class="form-text select-inactive">&nbsp;</div>\n';
	} else {
		content = '<input type="text" class="form-text" value="" size="60" id="edit-courselocation" name="courselocation" maxlength="128">\n';
	}
	$("#edit-courselocation").replaceWith(content);

	
	// date
	if(valid){
		content = '<div id="edit-coursedate" class="form-text select-inactive">&nbsp;</div>\n';
	} else {
		content = '<input type="text" class="form-text" value="" size="60" id="edit-coursedate" name="coursedate" maxlength="128">\n';
	}
	$("#edit-coursedate").replaceWith(content);

}

function buildLocations () {

	var tempLocations = {};
	// get locations
	for ( var i in courseevents )
	{
		
		if (courseevents[i].course == sCourse){
			tempLocations[courseevents[i].location] = courseevents[i].location;
		}
	} 

	var content = "";
		
	// location
	$("#edit-courselocation").unbind('change');

	content += '<select id="edit-courselocation" name="courselocation" class="form-text">';
	content += '<option>- Select location -</option>\n';
	
	var valid = false;
	for ( var i in tempLocations )
	{	
	    content += '<option value="'+i+'">'+i+'</option>\n';
	    valid = true;
	}
	
	content +='</select>\n';
	
	
	if(valid){
	
		$("#edit-courselocation").replaceWith(content);
		
		// location event
		$("#edit-courselocation").change(function() {
			sLocation = $(this).children(":selected").text();
			buildDates();
		});
		
	} else {
	
		//content = '<div id="edit-courselocation" class="form-text select-inactive">&nbsp;- No locations found for this course. You will be added to the waiting list. -</div>\n';
		//content = '<input type="text" class="form-text" value="" size="60" id="edit-courselocation" name="courselocation" maxlength="128">\n';
		content = '<input type="text" class="form-text" value="No events booked. Where would you like this course?" size="60" id="edit-courselocation" name="courselocation" maxlength="128">\n';
		$("#edit-courselocation").replaceWith(content);
		
	}
	
	
	
	
	// date
	if(valid){
		content = '<div id="edit-coursedate" class="form-text select-inactive">&nbsp;</div>\n';
	} else {
		//content = '<input type="text" class="form-text" value="" size="60" id="edit-coursedate" name="coursedate" maxlength="128">\n';
		content = '<input type="text" class="form-text" value="No events booked. When would you like this course?" size="60" id="edit-coursedate" name="coursedate" maxlength="128">\n';
	}
	$("#edit-coursedate").replaceWith(content);

}


function buildDates () {
	var tempDates = {};
	
	// get Dates
	for ( var i in courseevents )
	{
		
		if (courseevents[i].course == sCourse && courseevents[i].location == sLocation){
			tempDates[courseevents[i].dates] = courseevents[i].dates;
		}
	} 

	var content = "";
		
	// date
	$("#edit-coursedate").unbind('change');

	content += '<select id="edit-coursedate" name="coursedate" class="form-text">';
	content += '<option>- Select Date -</option>\n';
	
	var valid = false;
	for ( var i in tempDates )
	{	
	    content += '<option value="'+i+'">'+i+'</option>\n';
	    valid = true;
	}
	
	content +='</select>\n';
	if(valid){
		$("#edit-coursedate").replaceWith(content);
	} else {
		//content = '<input type="text" class="form-text" value="" size="60" id="edit-coursedate" name="coursedate" maxlength="128">\n';
		content = '<input type="text" class="form-text" value="No events booked. When would you like this course?" size="60" id="edit-coursedate" name="coursedate" maxlength="128">\n';
		$("#edit-coursedate").replaceWith(content);
	}
	
	// date event
	$("#edit-coursedate").change(function() {
		sDate = $(this).children(":selected").text();
	});
	
	
}

