var LIST_LENGTH = 6;	// defines the length of lists.  must be in sync with the server-side value for paging to work correctly
var coursePages = new Array(1, 1, 1);

function showLoading(element) {
	//var s = "<div class='ajax_loading'><img src='/assets/images/ajax/ajax_loading.gif'/>Loading</div>";
	//element.innerHTML = s;
}

function loadTopRatedCourses(page, range) {
	new Ajax.Updater("top_rated_courses", "/linegolfer/ajax/courses/toprated.do?div=top_rated_courses" + "&page=" + page + "&range=" + range);
}

function loadLatestCourses(page, cache) {
	new Ajax.Updater("latest_courses", "/linegolfer/ajax/courses/latest.do?div=latest_courses" + "&page=" + page);
}

function loadMostPlayedCourses(page, range) {
	new Ajax.Updater("most_played_courses", "/linegolfer/ajax/courses/mostplayed.do?div=most_played_courses" + "&page=" + page + "&range=" + range);
}

function loadSearchResults(page) {
	
	var st = $F("level_search_input");
	
	if (st.length > 0) {
		if (page) {
			new Ajax.Updater("search_results", "/linegolfer/ajax/search.do?st=" + st + "&page=" + page);
		} else {
			new Ajax.Updater("search_results", "/linegolfer/ajax/search.do?st=" + st);
		}
	}
	showSearchResults();
}

function showSearchResults() {
	
	showBox("search_results");
	showBox("hide_search_results");
	hideBox("show_search_results");
}
	
function hideSearchResults() {

	hideBox("search_results");
	hideBox("hide_search_results");
	showBox("show_search_results");
}

function getCourseId() {
	
	var href = window.location.href;
	var match = "#/?course_id=";
	var pos = href.indexOf(match);
	
	if (pos >= 0) {
		return href.substr(pos + match.length, href.length);
	} else {
		return "";
	}
}

function rateCourse(rating) {

	var courseId = getCourseId();
	if (courseId.length == 0) {
		alert("Unable to rate course.");
		return;
	}
	
	var answer = confirm("Are you sure you want to give this course a " + rating + "-star rating?");
	if (!answer) {
		return;
	}
	
	
	var url = "/linegolfer/ajax/rate.do?rating=" + rating + "&course_id=" + courseId;
	
	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport) {
			$("lg_search_footer").innerHTML = transport.responseText;
			alert("Game rating successful!");
		},
		onFailure: function(transport) {
			alert("Game rating failed with error: " + transport.responseText + ".");
		}
	});
	
}

function flagCourse() {

	var courseId = getCourseId();	
	if (courseId.length == 0) {
		alert("Unable to flag course.");
		return;
	}
	
	var answer = confirm("Are you sure you want to flag this course as inappropriate?");
	if (!answer) {
		return;
	}

	var url = "/linegolfer/ajax/flag.do?course_id=" + courseId;
	
	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport) {
			alert(transport.responseText);
		},
		onFailure: function(transport) {
			alert("Game flag failed with error: " + transport.responseText + ".");
		}
	});
	
}

function courseLoaded(mode) {

	if (mode == "play") {
	
		var courseId = getCourseId();
		if (courseId.length == 0) {
			return;
		}

		var url = "/linegolfer/ajax/countplay.do?course_id=" + courseId;
		
		new Ajax.Request(url, {
			method: 'post',
			onSuccess: function(transport) {
				$("lg_search_footer").innerHTML = transport.responseText;
			},
			onFailure: function(transport) {
				
			}
		});
		
	} else if (mode == "edit") {

	
	}
	
}

function courseCreated () {


}

function searchKeys(event) {
	if (event.keyCode == Event.KEY_RETURN) {
		loadSearchResults();
		return false;
	}
}

function getReturnUrl() {
	return window.location.pathname + window.location.search + window.location.hash;
}

function lgLogin() {
	showLogin("/play/line-golfer?autosave=true");
}