$(document).ready(function() {
	$('.tabs').tabs();

	$('a.ajax').click(function() {
		var href = $(this).attr('href');

		$('#PullDown').css({height: '0px'});

		$.ajax({
			method: 'get',
			url: href,

			success: function(html) {
				$('html, body').animate({scrollTop: 0}, 100);

				var height = $('#PullDown').html(html).css({height: 'auto'}).height();

				$('#PullDown').css({height: '0px'});
				$('#PullDown').animate({height: height}, 250);

				$('a.close_link').click(function() {
					$('#PullDown').animate({height: '0px'}, 500);
					return false;
				});

				$('.tabs').tabs();

				if (href.match('^/courses/search')) {
					CourseSearchReady();
				} else if (href.match('^/courses/edit')) {
					CourseEditReady();
				} else if (href.match('^/courses/add')) {
					CourseAddReady();
				}
			},
			
			error: function (xhr, ajaxOptions) {
				alert(xhr.status);
				alert(xhr.statusText);
				alert(xhr.responseText);
			}    
		});

		return false;
	});
});

function CourseSearchReady() {
	$('#CourseState').change(function() {
		$('#CourseCity').html('<option value="">Loading...</option>');
		
		$.ajax({
			url: '/courses/cities/' + $(this).val().toLowerCase().replace(' ', '-'),
			success: function(html) {
				$('#CourseCity').html(html);
				
				if ($('#CourseCity').attr('ajax') != '') {
					$('#CourseCity').val($('#CourseCity').attr('ajax'));
					$('#CourseCity').attr('ajax', '');
					$('#CourseCity').trigger('change');
				}
			}
		});
	});
}

function CourseEditReady() {
	$('.course_info.tabs').tabs();

	$('.course_info.tabs').bind('tabsshow', function(event, ui) {
		var height = $(ui.panel).height() + $('.courseinfo').height() + 70;

		$('#PullDown').animate({height: height}, 250);
	});

	$('.course_info.tabs').tabs('select', $('.course_info.tabs').attr('tab'));

	CourseAddReady();

	$('div.TeeboxInput').each(function() {
		BindTeebox($(this));
	});

	$('a.add-teebox').click(function() {
		$('div.remove').remove();

		if ($("div.TeeboxInput").length > 5) {
			alert("You may only select six teeboxes maximum.");
		} else {
			var html = '<div id="TeeboxInput' + $('#TeeboxCount').val() + '" class="fieldset TeeboxInput" TeeboxId="' + $('#TeeboxCount').val() + '"> ';
			html = html + '<div class="input select"> ';
			html = html + '<label class="align_right" for="Teebox' + $('#TeeboxCount').val() + 'Color"><strong>Tee box:</strong></label> ';
			html = html + '<select name="data[Teebox][' + $('#TeeboxCount').val() + '][color]" id="Teebox' + $('#TeeboxCount').val() + 'Color" class="TeeboxColor"> ';
			html = html + '<option value=""></option> ';
			html = html + '<option value="Black">Black</option> ';
			html = html + '<option value="Blue">Blue</option> ';
			html = html + '<option value="Cyan">Cyan</option> ';
			html = html + '<option value="Gold">Gold</option> ';
			html = html + '<option value="Green">Green</option> ';
			html = html + '<option value="Orange">Orange</option> ';
			html = html + '<option value="Pink">Pink</option> ';
			html = html + '<option value="Purple">Purple</option> ';
			html = html + '<option value="Red">Red</option> ';
			html = html + '<option value="Silver">Silver</option> ';
			html = html + '<option value="Teal">Teal</option> ';
			html = html + '<option value="White">White</option> ';
			html = html + '<option value="Yellow">Yellow</option> ';
			html = html + '</select> ';
			html = html + '</div> ';
			html = html + '<div class="input select"> ';
			html = html + '<select name="data[Teebox][' + $('#TeeboxCount').val() + '][type]" id="Teebox' + $('#TeeboxCount').val() + 'Type" class="TeeboxType"> ';
			html = html + '<option value=""></option> ';
			html = html + '<option value="0">Championship</option> ';
			html = html + '<option value="1">Pro</option> ';
			html = html + '<option value="2">Men</option> ';
			html = html + '<option value="3">Seniors</option> ';
			html = html + '<option value="4">Women</option> ';
			html = html + '<option value="5">Juniors</option> ';
			html = html + '</select> ';
			html = html + '</div> ';
			html = html + '<div class="input text"> ';
			html = html + '<label class="editteebox_label" for="Teebox' + $('#TeeboxCount').val() + 'Slope">Slope:</label> ';
			html = html + '<input name="data[Teebox][' + $('#TeeboxCount').val() + '][slope]" type="text" size="6" maxlength="100" value="" id="Teebox' + $('#TeeboxCount').val() + 'Slope" class="editteebox_field" /> ';
			html = html + '</div> ';
			html = html + '<div class="input text"> ';
			html = html + '<label class="editteebox_label" for="Teebox' + $('#TeeboxCount').val() + 'Rating">Rating:</label> ';
			html = html + '<input name="data[Teebox][' + $('#TeeboxCount').val() + '][rating]" type="text" size="6" maxlength="100" value="" id="Teebox' + $('#TeeboxCount').val() + 'Rating" class="editteebox_field" /> ';
			html = html + '</div> ';
			html = html + '<div> ';
			html = html + '<a href="#DeleteTeebox" class="btn_del delete-teebox"> ';
			html = html + '<img src="/img/spacer1.gif" alt="Delete Teebox" width="20" height="20" /> ';
			html = html + '</a> ';
			html = html + '</div> ';
			html = html + '</div> ';

			$('#TeeboxInputs').append(html);

			var height = $('#CourseTeeboxesTab').height() + $('.courseinfo').height() + 70;

			$('#PullDown').animate({height: height}, 250);

			BindTeebox($('#TeeboxInput' + $('#TeeboxCount').val()));

			$("#TeeboxCount").val(parseInt($("#TeeboxCount").val()) + 1);
		}

		return false;
	});
}

function CourseAddReady() {
	$('#CourseState').change(function() {
		$('#CourseZipCode').val('');
		$('#CourseCity').html('<option>Loading...</option>');
		
		$.ajax({
			url: '/courses/counties/' + $(this).val().toLowerCase().replace(' ', '-'),
			success: function(html) {
				$('#CourseCity').html(html);
				
				if ($('#CourseCity').attr('ajax') != '') {
					$('#CourseCity').val($('#CourseCity').attr('ajax'));
					$('#CourseCity').attr('ajax', '');
					$('#CourseCity').trigger('change');
				}
			}
		});
	});
}

function BindTeebox(teebox) {
	teebox.find('select.TeeboxColor').change(function() {
		var TeeboxColor = $(this).val();
		var TeeboxColorCount = 0;
		
		$('select.TeeboxColor').each(function() {
			if ($(this).val() == TeeboxColor) {
				TeeboxColorCount++
			}
		});
		
		if ((TeeboxColorCount > 1) && (TeeboxColor != '')) {
			alert('Please select a teebox color that has not already been used.');
			$(this).val(-1);
		}
	});

	teebox.find('select.TeeboxType').change(function() {
		var TeeboxType = $(this).val();
		var TeeboxTypeCount = 0;

		$('select.TeeboxType').each(function() {
			if ($(this).val() == TeeboxType) {
				TeeboxTypeCount++;
			}
		});

		if ((TeeboxTypeCount > 1) && (TeeboxType != '')) {
			alert('Please select a teebox type that has not already been used.');
			$(this).val(-1);
		}

		return false;
	});
	
	teebox.find('a.delete-teebox').click(function() {
		$("#TeeboxInput" + $(this).parent().parent().attr("TeeboxId")).remove();
		return false;
	});
}