/** * trainer_completed.js * * week 9 section * fall 2013 * * train dolphins */ $(document).ready(function() { // put all of the dolphins in the ocean ocean_array = []; $.each(DOLPHINS, function(index, value) { ocean_array.push(value); }); // no dolphins are in the pool to begin with pool = null; // draw the ocean draw_ocean(); // train a dolphin when its picture is clicked $("#ocean").on("click", "td img", function() { train($(this).data("index")); }); // set a dolphin free when the button is clicked $("#pool").on("click", "input", function() { set_free($(this).data("index")); }); }); /** * draws the ocean */ function draw_ocean() { // start HTML string var html = ""; // store pictures of dolphins in the ocean in an HTML string $.each(ocean_array, function(index, value) { // three dolphins per row if (index % 3 == 0 && index > 0) { html += "" } // store a dolphin if it's not being trained if (value == null) { html += ""; } else { html += ""; } }); // end HTML string html += "
"; // print out HTML string $("#ocean").html(html); } /** * draws the pool */ function draw_pool() { // start HTML string var html = ""; // check if a dolphin is in the pool if (pool != null) { html += "

Now training " + pool.name + "

"; html += "

Type: " + pool.type + "

"; html += "

Genus: " + pool.genus + "

"; html += "

Length: " + pool.length + " feet

"; html += "

Weight: " + pool.weight + " pounds

"; html += ""; } // print out HTML string $("#pool").html(html); } /** * moves a dolphin from the ocean to the pool */ function train(index) { // only one dolphin can be trained at a time if (pool != null) { alert("You're already training a dolphin!"); return; } // remove chosen dolphin from the ocean and put in the pool pool = ocean_array[index]; pool.index = index; ocean_array[index] = null; // draw the ocean and the pool draw_ocean(); draw_pool(); } /** * moves a dolphin from the pool to the ocean. */ function set_free(index) { // remove the dolphin from the pool and put in the ocean ocean_array[index] = pool; pool = null; // draw the ocean and the pool draw_ocean(); draw_pool(); }