$(function(){
  // initialize custom finder if js-enabled
  $('select.top_dropdown, select.bottom_dropdown').hide();
  $('.dropdown').show();
  $('.dropdown_container').css('background', 'url(/mil/media/img/finder_small_dropdowns.png)');
  
  // initially collapse the finder
  toggleFinder();
  
  $('.dropdown').click(function(event) {

    var cache_state = $(this).next().css('display');
    $('.overlay').hide();
    if (cache_state == 'none') {
      // .next() being the .overlay
      $(this).next().fadeIn();
      // $(this).next().data('state', 'open');
    }
    
    event.preventDefault();
  });
  
  $('.overlay a').live('click', function(event) {

    $(this).closest('.overlay').prevAll('a').first().text($(this).text());
    $(this).closest('.overlay').prevAll('select').first().val($(this).parent().data('value'));
    $(this).closest('.overlay').hide();




    
    event.preventDefault();
  });
  
  $('#product_finder a.collapse').click(function(event){
    toggleFinder();
    event.preventDefault();
  });
  
  // $('.dropdown_container').focusout(function(){
  //   
  //   var active_object, perform_hide;
  //   
  //   $(this).children('.overlay').each(function(){
  //     if ($(this).data('state') == 'open') {
  //       active_object = $(this);
  //       perform_hide = true;
  //     }
  //   });
  //   
  //   if (perform_hide == true) {
  //     setTimeout(function(){
  //       active_object.hide();
  //       active_object.data('state', 'closed');
  //       active_object = null;
  //       perform_hide = false;
  //     },300);
  //   }
  // });
  
  ///////////////////////////////////////////
  //
  // HELPERS
  //
  ///////////////////////////////////////////

  // GENERATE CUSTOM FLYOUTS
  
  // product_finder
  $('#product_finder select.top_dropdown option').each(function(index){
    var option_value = $(this).val();
		var option_name = $(this).text();
    var custom_option = $("<li><a href='#' title='" + option_name + "'>" + option_name + "</a></li>");
    $('#product_finder .top_dropdown ul').append(custom_option);
		custom_option.data('value',option_value)

    if (index == 0) {
      $('#product_finder a.top_dropdown').text($(this).text());
    }
  });
  
  $('#product_finder select.bottom_dropdown option').each(function(index){
    var option_value = $(this).val();
		var option_name = $(this).text();
    var custom_option = $("<li><a href='#' title='" + option_name + "'>" + option_name + "</a></li>");
    $('#product_finder .bottom_dropdown ul').append(custom_option);
		custom_option.data('value',option_value);

    if (index == 0) {
      $('#product_finder a.bottom_dropdown').text($(this).text());
    }
  });
  
  // finder_small
  $('#finder_small select.top_dropdown option').each(function(index){
    var option_value = $(this).val();
		var option_name = $(this).text();
    var custom_option = $("<li><a href='#' title='" + option_name + "'>" + option_name + "</a></li>");
		$('#finder_small .top_dropdown ul').append(custom_option);
		custom_option.data('value',option_value);

    if (index == 0) {
      $('#finder_small a.top_dropdown').text($(this).text());
    }
  });
  
  $('#finder_small select.bottom_dropdown option').each(function(index){
    var option_value = $(this).val();
		var option_name = $(this).text();
    var custom_option = $("<li><a href='#' title='" + option_name + "'>" + option_name + "</a></li>");
    $('#finder_small .bottom_dropdown ul').append(custom_option);
		custom_option.data('value',option_value);
		
    if (index == 0) {
      $('#finder_small a.bottom_dropdown').text($(this).text());
    }
  });
  
  // // generate state-data-objects for each dropdown
  // $('.overlay').each(function(){
  //   $(this).data('state', 'closed');
  // });
});

function toggleFinder() {
  $('#product_finder').toggleClass('active');
  
  if ($('#product_finder').hasClass('active')) {
    // $('#product_finder').css('background-image', "url(/mil/media/img/product_finder_big.png)");
    $('#product_finder').children().fadeIn();
  } else {
    // $('#product_finder').css('background-image', "url(/mil/media/img/product_finder_big_closed.png)");
    $('#product_finder').children().not('a.collapse').hide();
  }
}

