$(function() {
  /**
   * Functionality for adding subsidiaries via the inline form on the company structure node edit form
   */
  function add_subsidiary() {
    if ($("#edit-subsidiary-subform-subsidiary-name").val() != '') {
      var subname = $("#edit-subsidiary-subform-subsidiary-name").val();
      var subdescription = $("#edit-subsidiary-subform-subsidiary-description").val();
      var subnid = $("#edit-subsidiary-subform-subsidiary-nid").val();
      $.get("/subsidiary/add/" + subname + "/" + subdescription + "/" + subnid,
    	function(data) {
          $("#subsidiary-wrapper").prepend(data);
          $("#edit-subsidiary-subform-subsidiary-name").val('');
          $("#edit-subsidiary-subform-subsidiary-description").val('');
          //TODO: Need to get this into a theme function but not sure exactly how. I'm thinking we'll need to 
          //add it to the existing function and split on a special character or something since everything gets returned in one variable?
          var updated = '<div class="subsidiary-updated"><img src="/sites/all/themes/inc_v1/images/subsidiary-updated.gif" />Subsidiary added. Please add other subsidiaries.</div>';
          if (typeof(here)=="undefined") {
            $("fieldset#subsidiary-form-wrapper").append(updated);
          }
          $("a.edit-subsidiary").click(function() { return edit_subsidiary(this); });
          $("a.delete-subsidiary").click(function() { return remove_subsidiary(this)});
          here = true;
        }
    	);
    }
		return false;
  }
  
  function edit_subsidiary(object) {
    var url = $(object).attr("href") + '/' + ($("#edit-subsidiary-subform-subsidiary-nid").val());
    $.getJSON(url, function(obj) {
      $("#edit-subsidiary-subform-subsidiary-name").val(obj.name);
      $("#edit-subsidiary-subform-subsidiary-description").val(obj.description);
      $("#subsidiary-wrapper").html(obj.content);
      $("a.edit-subsidiary").click(function() { return edit_subsidiary(this); });
      $("a.delete-subsidiary").click(function() { return remove_subsidiary(this)});
    });
    return false;
  }
  
  function remove_subsidiary(object) {
    var url = $(object).attr("href") + '/' + ($("#edit-subsidiary-subform-subsidiary-nid").val());
    $.getJSON(url, function(obj) {
      $("#subsidiary-wrapper").html(obj.content);
      $("a.edit-subsidiary").click(function() { return edit_subsidiary(this); });
      $("a.delete-subsidiary").click(function() { return remove_subsidiary(this)});
    });
    return false;
  }
  
  $("#edit-subsidiary-subform-subsidiary-submit").click(function () {return add_subsidiary()});
  $("a.edit-subsidiary").click(function() { return edit_subsidiary(this); });
  $("a.delete-subsidiary").click(function() { return remove_subsidiary(this)});
  
  /**
   * Hide "categories" legend on company form
   */
  if ($("input#edit-company-node-form").val()) {
    $("fieldset legend").eq(0).hide();
  }
  
  /**
   * Hiding/showing form fields on company structure form
   */
  if ($("input#edit-structure-node-form").val()) {
    $("fieldset#subsidiary-form-wrapper legend").hide();
	  if ($("input[@name='field_subsidiaries[key]'][@checked]").val() != 1) {
	    $("fieldset#subsidiary-form-wrapper").hide();
	  }
	  $("input[@name='field_subsidiaries[key]']").click(function() {
	    if ($("input[@name='field_subsidiaries[key]'][@checked]").val() == 1) {
	      $("fieldset#subsidiary-form-wrapper").show();
	    } else {
	      $("fieldset#subsidiary-form-wrapper").hide();
	    }
	  });
	  
	  if ($("input[@name='field_subsidiary[key]'][@checked]").val() != 1) {
	    $("fieldset.group-please-enter-the-name-of").hide();
	  }
	  $("input[@name='field_subsidiary[key]']").click(function() {
	    if ($("input[@name='field_subsidiary[key]'][@checked]").val() == 1) {
	      $("fieldset.group-please-enter-the-name-of").show();
	    } else {
	      $("fieldset.group-please-enter-the-name-of").hide();
	    }
	  });
	
	  if ($("input[@name='field_primary_customer_base[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("input[@name='field_primary_customer_base[keys][Other]']").click(function() {
	    if ($("input[@name='field_primary_customer_base[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });
  }

  /**
   * Hiding/showing form fields on Key People form
   */
  if ($("input#edit-key-people-node-form").attr('value')) {
  	
  	if ($("#edit-field-title-key").attr('value') == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	     $("fieldset.group-other").slideUp('slow');
	  }
	  $("select[@name='field_title[key]']").change(function() {
	    if ($("select[@name='field_title[key]']").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });
  }
  
  /**
   * Hiding/showing form fields on governance form
   */
  if ($("input#edit-governance-node-form").val()) {
	  if ($("input[@name='field_do_you_plan_to_add_staff_[key]'][@checked]").val() == 1) {
	    $("fieldset.group-staff").show();
	  } else {
	    $("fieldset.group-staff").hide();
	  }
	  $("input[@name='field_do_you_plan_to_add_staff_[key]']").click(function() {
	    if ($("input[@name='field_do_you_plan_to_add_staff_[key]'][@checked]").val() == 1) {
	      $("fieldset.group-staff").show();
	    } else {
	      $("fieldset.group-staff").hide();
	    }
	  });  
	
	  if ($("input[@name='field_is_this_a_family_owned_bu[key]'][@checked]").val() == 1) {
	    $("fieldset.group-family-owned").show();
	  } else {
	    $("fieldset.group-family-owned").hide();
	  }
	  $("input[@name='field_is_this_a_family_owned_bu[key]']").click(function() {
	    if ($("input[@name='field_is_this_a_family_owned_bu[key]'][@checked]").val() == 1) {
	      $("fieldset.group-family-owned").show();
	    } else {
	      $("fieldset.group-family-owned").hide();
	    }
	  });    
	
	  if ($("input[@name='field_at_what_levels[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("input[@name='field_at_what_levels[keys][Other]']").click(function() {
	    if ($("input[@name='field_at_what_levels[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });
	
	
	  if ($("input[@name='field_how_does_the_family_parti[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other-0").show();
	  } else {
	    $("fieldset.group-other-0").hide();
	  }
	  $("input[@name='field_how_does_the_family_parti[keys][Other]']").click(function() {
	    if ($("input[@name='field_how_does_the_family_parti[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other-0").show();
	    } else {
	      $("fieldset.group-other-0").hide();
	    }
	  });
	}
  
  /**
   * Hiding/showing form fields on product form
   */
  if ($("input#edit-product-node-form").val()) {
	  if ($("select[@name='field_type[key]']").val() == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("select[@name='field_type[key]']").change(function() {
	    if ($("select[@name='field_type[key]']").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });
	  
	  // TODO change the hardcoded 61 to "Others" 
	  if ($("select[@name='taxonomy[5]']").val() == '61') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("select[@name='taxonomy[5]']").change(function() {
	    if ($("select[@name='taxonomy[5]']").val() == '61') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });  
  }

  /**
   * Hiding/showing form fields on vendor form
   */
  if ($("input#edit-vendor-node-form").val()) {
	  if ($("input[@name='field_private_company[key]'][@checked]").val() == 'Yes') {
	    $("fieldset.group-contact-info").show();
	  } else {
	    $("fieldset.group-contact-info").hide();
	  }
	  $("input[@name='field_private_company[key]']").click(function() {
	    if ($("input[@name='field_private_company[key]'][@checked]").val() == 'Yes') {
	      $("fieldset.group-contact-info").show();
	    } else {
	      $("fieldset.group-contact-info").hide();
	    }
	  });
	  
	  if ($("select[@name='field_type[key]']").val() == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("select[@name='field_type[key]']").change(function() {
	    if ($("select[@name='field_type[key]']").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });  
  }   
  
  /**
   * Hiding/showing form fields on company-start form
   */
  if ($("input#edit-company-start-node-form").val()) {
	  if ($("input[@name='field_has_founder_started_other[key]'][@checked]").val() == 1) {
	    $("fieldset.group-other-companies").show();
	  } else {
	    $("fieldset.group-other-companies").hide();
	  }
	  $("input[@name='field_has_founder_started_other[key]']").click(function() {
	    if ($("input[@name='field_has_founder_started_other[key]'][@checked]").val() == 1) {
	      $("fieldset.group-other-companies").show();
	    } else {
	      $("fieldset.group-other-companies").hide();
	    }
	  }); 
	
	  if ($("input[@name='field_startup_capital_sources[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("input[@name='field_startup_capital_sources[keys][Other]']").click(function() {
	    if ($("input[@name='field_startup_capital_sources[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });
	
	  if ($("input[@name='field_motivations[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other-0").show();
	  } else {
	    $("fieldset.group-other-0").hide();
	  }
	  $("input[@name='field_motivations[keys][Other]']").click(function() {
	    if ($("input[@name='field_motivations[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other-0").show();
	    } else {
	      $("fieldset.group-other-0").hide();
	    }
	  });
	}

  /**
   * Hiding/showing form fields on company-now form
   */
  if ($("input#edit-company-now-node-form").val()) {
	  if ($("input[@name='field_benefits[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("input[@name='field_benefits[keys][Other]']").click(function() {
	    if ($("input[@name='field_benefits[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });
	
	  if ($("input[@name='field_marketing_channels[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other-0").show();
	  } else {
	    $("fieldset.group-other-0").hide();
	  }
	  $("input[@name='field_marketing_channels[keys][Other]']").click(function() {
	    if ($("input[@name='field_marketing_channels[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other-0").show();
	    } else {
	      $("fieldset.group-other-0").hide();
	    }
	  });
	  
	  if ($("input[@name='field_operating_strategy[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other-1").show();
	  } else {
	    $("fieldset.group-other-1").hide();
	  }
	  $("input[@name='field_operating_strategy[keys][Other]']").click(function() {
	    if ($("input[@name='field_operating_strategy[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other-1").show();
	    } else {
	      $("fieldset.group-other-1").hide();
	    }
	  });
	
	  if ($("input[@name='field_expansion[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other-2").show();
	  } else {
	    $("fieldset.group-other-2").hide();
	  }
	  $("input[@name='field_expansion[keys][Other]']").click(function() {
	    if ($("input[@name='field_expansion[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other-2").show();
	    } else {
	      $("fieldset.group-other-2").hide();
	    }
	  });
	
	  if ($("input[@name='field_exit_strategy[key]'][@checked]").val() == 1) {
	    $("fieldset.group-exit-strategy").show();
	  } else {
	    $("fieldset.group-exit-strategy").hide();
	  }
	  $("input[@name='field_exit_strategy[key]']").click(function() {
	    if ($("input[@name='field_exit_strategy[key]'][@checked]").val() == 1) {
	      $("fieldset.group-exit-strategy").show();
	    } else {
	      $("fieldset.group-exit-strategy").hide();
	    }
	  });
	
	  if ($("input[@name='field_exit_strategy_explanation[keys][Other]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other-3").show();
	  } else {
	    $("fieldset.group-other-3").hide();
	  }
	  $("input[@name='field_exit_strategy_explanation[keys][Other]']").click(function() {
	    if ($("input[@name='field_exit_strategy_explanation[keys][Other]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other-3").show();
	    } else {
	      $("fieldset.group-other-3").hide();
	    }
	  });
	}

  /**
   * Hiding/showing form fields on about-the-ceo form
   */
  if ($("input#edit-about-the-ceo-node-form").val()) {
	  if ($("input[@name='field_education[key]'][@checked]").val() == "Associate’s degree" || $("input[@name='field_education[key]'][@checked]").val() == "Bachelor’s degree" || $("input[@name='field_education[key]'][@checked]").val() == "Master’s degree" || $("input[@name='field_education[key]'][@checked]").val() == "MBA" || $("input[@name='field_education[key]'][@checked]").val() == "Ph.D." || $("input[@name='field_education[key]'][@checked]").val() == "Professional degree") {
	    $("fieldset.group-area-of-study").show();
	  } else {
	    $("fieldset.group-area-of-study").hide();
	  }
	  $("input[@name='field_education[key]']").click(function() {
	    if ($("input[@name='field_education[key]'][@checked]").val() == "Associate’s degree" || $("input[@name='field_education[key]'][@checked]").val() == "Bachelor’s degree" || $("input[@name='field_education[key]'][@checked]").val() == "Master’s degree" || $("input[@name='field_education[key]'][@checked]").val() == "MBA" || $("input[@name='field_education[key]'][@checked]").val() == "Ph.D." || $("input[@name='field_education[key]'][@checked]").val() == "Professional degree") {
	      $("fieldset.group-area-of-study").show();
	    } else {
	      $("fieldset.group-area-of-study").hide();
	    }
	  });
	
	  if ($("input[@name='field_political_affiliation[key]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other").show();
	  } else {
	    $("fieldset.group-other").hide();
	  }
	  $("input[@name='field_political_affiliation[key]']").click(function() {
	    if ($("input[@name='field_political_affiliation[key]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other").show();
	    } else {
	      $("fieldset.group-other").hide();
	    }
	  });   
	
	  if ($("input[@name='field_most_important_factor[key]'][@checked]").val() == 'Other') {
	    $("fieldset.group-other-0").show();
	  } else {
	    $("fieldset.group-other-0").hide();
	  }
	  $("input[@name='field_most_important_factor[key]']").click(function() {
	    if ($("input[@name='field_most_important_factor[key]'][@checked]").val() == 'Other') {
	      $("fieldset.group-other-0").show();
	    } else {
	      $("fieldset.group-other-0").hide();
	    }
	  });
	}

  /**
   * Functionality for help links
   */
  $("a.help-link").click(function() {
    if ($("div.help-popup").is("div")) {
      $("div.help-popup").remove();
      return false;
    }
    $(this).after("<div class='help-popup'/>");
    $.get($(this).attr("href"), function(data) {
      $("div.help-popup").html(data).append("<a class='close-popup' href='#'>Close</a>");
      $("a.close-popup").click(function() {
	      $("div.help-popup").remove();
	      return false;
      });
    })
    return false;
  });
});

