var $bfa = jQuery.noConflict(); 
var plugin = "wordpress/wp-content/plugins/boat_register/boat_register.php"

// boat overview display. In your post, add the following around a reference to a sail number or boat name:
// <a href="#" class="boat-link">(The actual text: boat name or sail number)</a>
$bfa(document).ready(function(){ 
	
	$bfa("div[rel=#edit-boat]").overlay({	
		close : ".edit-boat-button",
		onBeforeLoad: function() {
			populateForm(this.getTrigger().attr('boat'));
		} 		
	});
	
	var overviewDiv = $bfa("div[rel=#boat-overview]");
	overviewDiv.overlay({
		onBeforeLoad: function() {
			var id = $bfa("div[rel=#boat-overview]").attr("boat");
			var url = plugin+"?func=get_boat_overview&search="+id+"&context=boat-register";
			$bfa("div.wrap").load(url, null, function(){
					// called after the boat details have finished loading.
					// taken from header.php.
					var button = $bfa("input#edit-boat-button");
					button.mouseover(function() {
						$bfa(this).addClass("buttonhover");
						});
					button.mouseout(function() {
						$bfa(this).removeClass("buttonhover");
						});
					button.bind('click', function(){
						// loat edit form.
						$bfa("div[rel=#edit-boat]").attr("boat", id);
						$bfa("div[rel=#edit-boat]").overlay().load();
					});
			});	
		} 		
	});
	
	$bfa("a[rel='#boat-overview']").overlay({
		onBeforeLoad: function() {
			var boat = this.getTrigger().text();
			boat = boat.replace(/\s+/g, '_');
			var url = plugin+"?func=get_boat_overview&search="+boat;
			$bfa("div.wrap").load(url);	
		}		
	});
	
	initActiveBoats();
	initDeadBoats();
	initEditButtons();
		
	
	$bfa("#form-edit-boat").submit(function() {

		// we want to store the values from the form input box, then send via ajax below
		var hull_number = $bfa("#edit-boat-id").val();
		var name = $bfa("input#edit-boat-name").val();
		var owner = $bfa("input#edit-boat-owner").val();
		var helmsman = $bfa("input#edit-boat-helmsman").val();
		var club = $bfa("input#edit-boat-club").val();
		var state = $bfa("select#edit-boat-state").val();
		var notes = $bfa("textarea#edit-boat-notes").val();
		var year_built = $bfa("input#edit-boat-year_built").val();

		$bfa.ajax({
			type: "POST",
			url: plugin,
			data: "hull_number="+ hull_number + "&name=" + name + "&owner=" + owner + "&helmsman=" + helmsman+ "&club=" + club+ "&state=" + state+ "&notes=" + notes+ "&year_built=" + year_built,
			
			success: function(data, textStatus){

				// reload the database tables.
				$bfa("#active_boat_placeholder").load(plugin+"?func=get_active_boats", function(){
					initActiveBoats();
				});
				$bfa("#dead_boat_placeholder").load(plugin+"?func=get_dead_boats", function(){
					initDeadBoats();
					initEditButtons();
				});
			}
		});
		return false;
	});
} );


function initActiveBoats()
{
	$bfa(".boat-overview-row").mouseover(function(){ 
		$bfa(this).css("cursor", "pointer");
	}).mouseout(function(){ 
		$bfa(this).css("cursor", "default");
	});
	
	$bfa(".boat-overview-row").click(function() { 
		// set the boat attribute of the overlay div to the clicked row.
		var id = $bfa(this).attr('id'); 
		// display the overlay
		var overviewDiv = $bfa("div[rel=#boat-overview]");
		overviewDiv.attr("boat", id);
		overviewDiv.overlay().load();
	});
	// taken from header.php
	$bfa(".post table tr:even").addClass("alt");
	$bfa(".post table tr").mouseover(function() {$bfa(this).addClass("over");}).mouseout(function() {$bfa(this).removeClass("over");});
	
}

function initEditButtons()
{
	$bfa("input[rel]").each(function () {
		$bfa(this).bind('click', function(){
			$bfa("div[rel=#edit-boat]").attr("boat", $bfa(this).attr("boat"));
			$bfa("div[rel=#edit-boat]").overlay().load();
		});
	});
	// taken from header.php.
	$bfa("input.button, input.Button").mouseover(function() {$bfa(this).addClass("buttonhover");}).mouseout(function() {$bfa(this).removeClass("buttonhover");});
	
}

function initDeadBoats()
{
	$bfa(".dead-boat-link").hide();
	$bfa(".tablesorter").tablesorter();
	$bfa(".dead-boat-row").mouseover(function(){ 
		$bfa("input:first",this).show(); 
	}).mouseout(function(){ 
		$bfa("input:first",this).hide(); 
	}); 
}

function populateForm(boat)
{
	// using the boat id, get the boat details via ajax.
	$bfa.getJSON(plugin, {func: "get_boat_data", search: boat}, function(json){
			$bfa("#edit-boat-hull_number").text(json.hull_number);
			$bfa("#edit-boat-id").val(json.hull_number);
			$bfa("#edit-boat-name").val(json.name);
			$bfa("#edit-boat-owner").val(json.owner);
			$bfa("#edit-boat-helmsman").val(json.helmsman);
			$bfa("#edit-boat-club").val(json.club);
			$bfa("#edit-boat-notes").val(json.notes);
			$bfa("#edit-boat-state").val(json.state);
		});
}


