/* 
*  NacreData L.L.C.
*  PO Box 646 Chapel Hill, NC 27514
*  (919) 442-8899
*  info@nacredata.com 
*/

var NacreData = window.NacreData = {
  animal_count : 0, 
  generate_top_level : function( cur_block ) {
    var select_block = '<select name="animal_' + NacreData.animal_count + '"';
    select_block += ' id="animal_' + NacreData.animal_count + '">';
    select_block += '<option value="" selected="selected">(select animal from list)</option>'
    select_block += jQuery("#top_level_animals").html();
    select_block += '</select>';
    select_block += ' &nbsp; <input type="button" value="Select Animal" ';
    select_block += 'class="button" onclick="NacreData.show_breeds( \'';
    select_block += 'animal_' + NacreData.animal_count + '\', \'';
    select_block += cur_block + '\')" />';
    NacreData.animal_count++;
    return select_block;
  },
  show_breeds : function( animal_id, block_id ) {
    var animal_type = jQuery("#"+animal_id).val();
    if( "" == animal_type ) { return; }
    var new_html    = animal_type + ' : ';
    new_html += NacreData.generate_breed( block_id, animal_type, animal_id );
    jQuery("#"+block_id).html( new_html );
    jQuery("#breed_"+animal_id).val("");
    var the_id = NacreData.animal_count - 1;
    jQuery("#animal_"+the_id).val("");
  },
  generate_breed : function( block_id, animal_type, animal_id ) {
    var select_block = '<input type="hidden" name="' + animal_id + '" ';
    select_block += 'value="' + animal_type + '" />';
    select_block += '<select name="breed_' + animal_id + '" id="breed_';
    select_block += animal_id + '" onchange="NacreData.generate_product( \'';
    select_block += animal_id + '\', \'' + animal_type + '\' );"';
    select_block += '><option value="" selected="selected">(select breed from list)';
    select_block += '</option>';
    var animal_div = animal_type.replace( /\W/, "_" );
    select_block += jQuery("#"+animal_div+'_breeds').html();
    select_block += '</select>';
    select_block += ' &nbsp; <input type="checkbox" name="breeders_';
    select_block += animal_id + '" value="1" /> We are active breeders of this breed and may offer stock for sale.<br />';
    var new_id = 'animal_block_' + NacreData.animal_count;
    select_block += '<br /><div id="' + new_id + '">';
    select_block += NacreData.generate_top_level( new_id );
    select_block += '</div>';
    return select_block;
  },
  product_block : 0,
  generate_product : function( animal_id, animal_type ) {
    var breed = jQuery("#breed_"+animal_id).val();
    if( "" == breed ) {
      /* Deal with removing options? */
      return;
    }
    var select_block = animal_type + ' : ' + breed + ' : ';
    select_block += '<select name="' + animal_id + '_product_';
    select_block += NacreData.product_block + '" ';
    select_block += 'id="' + animal_id + '_product_';
    select_block += NacreData.product_block + '" ><option value="" selected="selected">';
    select_block += '(select product from list)</option>';
    var animal_div = animal_type.replace( /\W/, "_" );
    select_block += jQuery("#"+animal_div+'_products').html(); 
    select_block += '</select> &nbsp; <input type="button" ';
    select_block += 'value="Another Product..." class="button" ';
    select_block += 'onclick="NacreData.generate_product( \'';
    select_block += animal_id + '\', \'' + animal_type + '\' );" />';
    var new_id = NacreData.product_block + 1;
    select_block += '<br /><br /><div id="product_' + new_id + '"></div>';
    jQuery("#product_"+NacreData.product_block).html( select_block );
    var this_select = animal_id+'_product_'+NacreData.product_block;
    jQuery("#breed_"+animal_id).bind("change", function() { 
      jQuery("#"+this_select).val("");
      this_select = "";
    } );
    NacreData.product_block++;
  }
};

jQuery(document).ready(function(){  
  jQuery("#animal_block").html( NacreData.generate_top_level('animal_block') );
  jQuery("#animal_0").val("");
} );
