/**
 * This file contains all of the necessary functions for tree control display
 * on the organization registration page.
 * 
 * @author Dave Allen and Mark Burdett
 */
var activity_types_codes;  	//DA: a 3-deep array that holds all activity types and subtypes (codes)
var base_path;		        	//DA: a string that is the base path for the site

/**
 * This function initializes the variables that will be used later after the page is loaded.
 * 
 * @param {Object} activity_types_codes -- "3-deep" nested array of activity types and subtypes sent in JSON format
 * @param {Object} base_path -- base directory of the Drupal site
 */
function initialize_vars(activity_types_codes_param, base_path_param) {
	activity_types_codes = activity_types_codes_param;
	base_path = base_path_param;
}

/**
 * This function opens and closes a particular activity tree branch.
 * 
 * @param {Object} activity_type_id -- ID of the activity type
 */
function hide_unhide(activity_type_id) {
	$('#activity_block_type_' + activity_type_id).toggle();
  $('#image_plus_type_' + activity_type_id).toggle();
  $('#image_minus_type_' + activity_type_id).toggle();
}

/**
 * This function opens a particular activity tree branch.
 * 
 * @param {Object} activity_type_id -- ID of the activity type
 */
function unhide(activity_type_id) {
	$('#activity_block_type_' + activity_type_id).show();
  $('#image_plus_type_' + activity_type_id).hide();
  $('#image_minus_type_' + activity_type_id).show();
}

/**
 * This function sets all activity values for activity type and subtype.
 * 
 * @param {Object} activity_type_id -- ID of the activity type
 * @param {Object} activity_code_id -- ID of the activity code (subtype)
 * @param {Object} type_name -- name of the activity type
 * @param {Object} code_name -- name of the activity code (subtype)
 */
function set_activity_values(activity_type_id, activity_code_id, type_name, code_name) {
	old_activity_code_id = $('#selected_activity_code').attr("value");
	
	$('#activity_code_' + old_activity_code_id).css("color", "black"); //DA: make old code name not red
	$('#image_code_' + old_activity_code_id).hide(); //DA: remove check or any preceding image icon from old code name display
	
	$('#activity_code_' + activity_code_id).css("color", "red"); //DA: make new selected code name red
	$('#image_code_' + activity_code_id).show(); //DA: add preceding check mark or any other image icon
		
	//DA: sets the name (for display) and activity code (for form submission)
	$('#selected_activity_name').val(code_name);
	$('#selected_activity_type').val(activity_type_id); //DA: set activity type for form submission
        $('#selected_activity_code').val(activity_code_id); //DA: set activity code for form submission
        organization_register_library_survey(activity_type_id);
  if(typeof register_warrants == 'function') {
    register_warrants(activity_code_id);
  }
}

/**
 * Hide or display the library survey form.
 */
function organization_register_library_survey(activity_type_id) {
  if (activity_type_id == '3') {
    $('#id_' + Drupal.settings.library_survey_field_group).show();
  }
  else {
    $('#id_' + Drupal.settings.library_survey_field_group + '_show').hide();
    $('#id_' + Drupal.settings.library_survey_field_group).hide();
  }
}

/**
 * Set up and invoke jquery validate plugin for the user-register form.
 */
function organization_register_validate() {
  jQuery.validator.messages.required = Drupal.settings.library_survey_required;
  var validate = new Object();
  validate.rules = new Object();
  $('#id_' + Drupal.settings.library_survey_field_group).find('select').each(function() {
    validate.rules[this.id] = {
      required: function() {
        return  ($('#id_' + Drupal.settings.library_survey_field_group).is(":visible")) ? true : false;
      }
    }
  });
  $('#id_' + Drupal.settings.library_survey_field_group).find('input').each(function() {
    if ($(this).is(":checkbox")) {
      validate.rules[this.name] = { 
        required: function(element) { 
          return ($('#id_' + Drupal.settings.library_survey_field_group).is(":visible") && $(element).siblings(":checked").length < 1) ? true : false;
        }
      }
    }
    else if ($(this).is(":radio")) {
      validate.rules[this.name] = {
        required: function() {
          return ($('#id_' + Drupal.settings.library_survey_field_group).is(":visible")) ? true : false;
        }
      }
    }
    else {
      validate.rules[this.id] = {
        required: function() {
          return ($('#id_' + Drupal.settings.library_survey_field_group).is(":visible")) ? true : false;
        }
      }
    }
  });
  validate.showErrors = function(errorMap, errorList) { 
    if (errorList.length && !$("#required-message").length) { 
      $('#id_' + Drupal.settings.library_survey_field_group + ' table').before('<strong class="error" id="required-message">' + Drupal.settings.library_survey_message + '</strong>');
    } 
    this.defaultShowErrors(); 
  }; 
  $("#user-register").validate(validate);
}

/**
 * This function hides the "non-tree" version of the activity selection section and
 * displays the "tree" version of the activity selection section.  It executes after
 * the non-javascrip version is rendered.
 */
$(function() {        
	$('#no_tree_control_version').after($('<div></div>').attr('id','tree_control_version'));
	$('#no_tree_control_version').hide();

	selected_activity_text = "Select from the options below";
	tree_control_instructions = "Expand the options below and select the activity that best describes what your organization does.";
	plus_path = base_path + 'sites/all/modules/techsoup/includes/small_plus.gif';
	minus_path = base_path + 'sites/all/modules/techsoup/includes/small_minus.gif';
	red_checkmark_path = base_path + 'sites/all/modules/techsoup/includes/red_checkmark.gif';
	
	$('#tree_control_version').append($('<div></div>').addClass("form-item").css("font-weight","bold").html('Activity Type: <span title="This field is required." class="form-required">*</span>'));
  $('#tree_control_version').append('<input type="text" id="selected_activity_name" value="Select from the options below" size="80" readonly="readonly" class="form-text-required" style="margin-bottom: 1em"/>');
  $('#tree_control_version').append('<input type="hidden" id="selected_activity_type" name="organization_activity_type" value = "">');
  $('#tree_control_version').append('<input type="hidden" id="selected_activity_code" name="organization_activity" value = "">');
	$('#tree_control_version').append($('<div></div>').addClass("description").html(tree_control_instructions));
	
	//DA: Start of actual tree
	$('#tree_control_version').append('<ul id="tree_control_outer_ul"></ul>');
	$('#tree_control_outer_ul').css("list-style-image", "none").css("list-style-type","none");
	
  $.each(activity_types_codes, function(type_id, type_parts) {
		$('#tree_control_outer_ul').append($('<li id="type_li_' + type_id + '"></li>').css("list-style-image", "none").css("list-style-type","none"));
		$('#type_li_' + type_id).append($('<div></div>').attr("id","type_li_div_" + type_id).css("display","inline"));
    $('#type_li_div_' + type_id).click( function() {hide_unhide(type_id) } );		
    $('#type_li_div_' + type_id).append($('<span></span>').attr("id","image_plus_type_" + type_id));
		$('#image_plus_type_' + type_id).append($('<img />').attr("src",plus_path));
    $('#type_li_div_' + type_id).append($('<span></span>').attr("id","image_minus_type_" + type_id).hide());
		$('#image_minus_type_' + type_id).append($('<img />').attr("src",minus_path));
		
		//DA: Begin section that prints the type name and lists the subtypes (codes)
		$.each(type_parts, function(activity_type_part_key, activity_type_part_value) {
			if (activity_type_part_key == "name") {  //DA: This part lists the names of the types
				type_name = activity_type_part_value;				
				$('#type_li_div_' + type_id).append($('<span></span>').css('text-decoration', 'underline').css('font-weight', 'bold').css('margin-left','.5em').css('cursor', 'pointer').text(type_name));
				$('#type_li_' + type_id).append($('<div></div>').attr('id','activity_block_type_' + type_id).hide());
				$('#activity_block_type_' + type_id).append($('<ul></ul>').attr('id','codes_inner_ul_' + type_id).css('list-style-image', 'none').css('list-style-type','none'));
			} else {    //DA: This part lists the subtypes (codes)
				$.each(activity_type_part_value,function(code_id, code_name){
                                        //SR: Don't loop through description array
                                        if (activity_type_part_key != "description") {
                                                $('#codes_inner_ul_' + type_id).append($('<li></li>').attr('id','code_li_' + code_id).css('list-style-image', 'none').css('list-style-type','none'));
                                                $('#code_li_' + code_id).append($('<span></span>').attr('id','image_code_' + code_id).hide());
                                                $('#image_code_' + code_id).append($('<img />').attr('src',red_checkmark_path));
                                                //SR: Don't display description if description is null or blank
                                                if ( typeof(type_parts.description)== 'undefined' || (null == type_parts['description'][code_id]) || (type_parts['description'][code_id] == '')) {
              $('#code_li_' + code_id).append($('<span></span>').attr('id','activity_code_' + code_id).mouseover( function() {$(this).attr('class', 'toolTip-on');} ).mouseout( function() {$(this).attr('class', 'toolTip');} ));
                  $('#activity_code_' + code_id).text(code_name);                                                        
                                                }else{
              $('#code_li_' + code_id).append($('<span></span>').attr('id','activity_code_' + code_id).attr('title',type_parts['description'][code_id]).attr('class','toolTip').mouseover( function() {$(this).attr('class', 'toolTip-on'); } ).mouseout( function() {$(this).attr('class', 'toolTip'); } ));
                  $('#activity_code_' + code_id).text(code_name);                                                        
                                                }
                                                $('#activity_code_' + code_id).click( function() { set_activity_values(type_id, code_id, type_name, code_name) } );
                                        }
				});					
			}			
		});
		//DA: End section that prints the type name and lists the subtypes (codes)		
		
	});
	//DA: End of actual tree

	//DA: Begin section for populating values if errors on page 
	var selected_activity_type_id = $('#edit-organization-activity-type').val();
	var selected_activity_type_name = $("option[@value=" + selected_activity_type_id + "]").text()
	
	var selected_activity_code_id = $('#edit-organization-activity').val();
	var selected_activity_code_name = $("option[@value=" + selected_activity_code_id + "]").text()	
	
	if ($('#edit-organization-activity-type').val() != '') {
		unhide($('#edit-organization-activity-type').val());
		set_activity_values($('#edit-organization-activity-type').val(), 
												$('#edit-organization-activity').val(), 
												selected_activity_type_id, 
												selected_activity_code_name);
	}
	//DA: End section for populating values if errors on page	
  organization_register_library_survey(selected_activity_type_id);
  organization_register_validate();
  if(typeof register_warrants_validate == 'function') {
    register_warrants_validate();
  }
  
	$('#load').fadeOut(1500);
});

/*-----------------------------------------------------------------------------------------------*/
/*                                      SIMPLE jQUERY TOOLTIP                                    */
/*                                      VERSION: 1.1                                             */
/*                                      AUTHOR: jon cazier                                       */
/*                                      EMAIL: jon@3nhanced.com                                  */
/*                                      WEBSITE: 3nhanced.com                                    */
/*-----------------------------------------------------------------------------------------------*/

$(document).ready(function() {
	$('.toolTip').hover(
		function() {
		this.tip = this.title;
		$(this).append(
			'<div class="toolTipWrapper">'
				+'<div class="toolTipMid">'
					+this.tip
				+'</div>'
			+'</div>'
		);
		this.title = "";
		this.width = $(this).width();
		$(this).find('.toolTipWrapper').css({left:150});
		$('.toolTipWrapper').fadeIn(300);
    $(this).css('z-index', '100500');
	},
	function() {
		$('.toolTipWrapper').fadeOut(100);
    $(this).css('z-index', '');
		$(this).children().remove();
			this.title = this.tip;
		}
	);
});

