var symantec = symantec || {};

symantec.forms = symantec.forms || {};

$(document).ready(function() {
  symantec.forms.setup_taxonomy_select();
	symantec.flagSupport.taxonomy_sort();
});

/**
 * Determines whether or not any groups are selected
 */
symantec.forms.group_selected = function() {
    var group_selected = false;
    $('input[id^=symantec-groups]').each(function() {      
      if ($(this).attr('checked') == true) {
        group_selected = true;
        return false; // break
      }
    });
    
    return group_selected;
}

/**
 * Sets up vocabulary swapping based on changes to the community override form widget
 */
symantec.forms.setup_community_override = function() {
  if ($('#edit-community-override').length) {
    var $widget = $('#edit-community-override');
    $widget.change(function() {
      $('#edit-community-context-tid').val($(this).val());
      $("div[id^=edit-symantec-location-]").hide();
      if (Drupal.settings.symantec) {
        var target_element = "#edit-symantec-location-" + Drupal.settings.symantec.location_vid_map[$(this).val()];
        $(target_element).show();
      }
    });
  }
}

/**
 * Setup function for taxonomy selection on the custom add forms
 */
symantec.forms.setup_taxonomy_select = function() {
  
  // if any items have been checked by default, have their children expanded
  $("input[checked=1]").each(function() { 
    $(this).parents('.tax-leaf').children(':last').show()
  });
  
  // "location" taxonomy click handling
  $("input[id^='symantec-location']").click(function() {
    return symantec.forms.handle_taxonomy_click($(this));
  });
  
  $("input[id^='symantec-product']").click(function() {
    // special handling of product selection on forum posts
    if ($('#edit-sc-forum-node-form').length && 1 == 0) {
        var selected_product_max = 1;
        var selected_product_tids = [];
        $("input[id^='symantec-product']").each(function() {
          if ($(this).attr('checked')) {
            var term_id = $(this).attr('id').split('-')[2];
            var id_captured = false;
            for (var i = 0; i < selected_product_tids.length; i++) {
              if (selected_product_tids[i] == term_id) {
                id_captured = true;
              }
            }
            
            if (!id_captured && !$(this).hasClass('product-version')) {
              selected_product_tids.push(term_id);
            }
          }
        });
        
        if (selected_product_tids.length > selected_product_max) {
          alert('You may only select a single product on forum posts');
          return false;
        }
    }    
    
    var taxonomy_id = $(this).attr('id').split('-')[2];
    var $option_element = $("option[value='" + taxonomy_id + "']");
    if ($(this).attr('checked')) {
      $option_element.attr('selected', true);
    } else {
      $option_element.removeAttr('selected');
    }
    return true;
  }); 
  
  $("input[id^='symantec-topic']").click(function() {
    // special handling of product selection on forum posts
    if ($('#edit-sc-forum-node-form').length) {
      $("input[id^='symantec-product]").click(function() {
        var selected_
      });    
    }    
    
    var taxonomy_id = $(this).attr('id').split('-')[2];
    var $option_element = $("option[value='" + taxonomy_id + "']");
    if ($(this).attr('checked')) {
      $option_element.attr('selected', true);
    } else {
      $option_element.removeAttr('selected');
    }
    return true;
  });   
  
  $("input[id^=symantec-groups]").click(function() {
    var group_id = $(this).attr('id').split('-')[2];
    var $option_element = $("option[value='" + group_id + "']");
    var $check_element = $('#edit-og-groups-' + group_id);
    if ($(this).attr('checked')) {
      $option_element.attr('selected', true);
      if ($check_element.length) {
        $check_element.attr('checked', true);        
      }
    } else {
      $option_element.removeAttr('selected');
      if ($check_element.length) {
        $check_element.attr('checked', false);
      }      
    }
    
    // disable the public/private checkbox and set to "public" if no groups are selected
    var group_selected = symantec.forms.group_selected();
    
    if (!group_selected) {
      $('#symantec-public-post').attr('checked', 'true').attr('disabled', 'true');
      $('#edit-og-public').attr('checked', 'true');      
    } else {
      $('#symantec-public-post').removeAttr('disabled');
    }
    
    return true;
  });
    
  // og groups checkbox is disabled by default, make sure it's enabled
  $('#edit-og-public').attr('disabled', false);
  
  // if no groups are selected, disable the custom public/private checkbox
  var group_selected = symantec.forms.group_selected();
  if (!group_selected) {
    $('#symantec-public-post').attr('checked', 'true').attr('disabled', 'true');
    $('#edit-og-public').attr('checked', 'true');
  }
  
  $('.groups-public-checkbox-wrapper').click(function() {
      $('#edit-og-public').attr('checked', $(this).find('input').attr('checked'));
      return true;
  });
  
  $('.symantec-checkbox-custom').click(function() {
    if ($(this).find('input').attr('checked')) {
      $(this).next().show();
    } else {
      //uncheck any selected children
      $(this).next().find('input').attr('checked', false);
      $(this).next().hide();
    }
    
    return true;
  });
  
  $('.tax-see-all').click(function() {
    $(this).hide();
    $(this).next().show();
    return false;
  });
  
  $('.topic-see-all').click(function() {
    $(this).parent().next().toggle();
    return false;
  });
  
}

/**
 * Basic click handling for taxonomy selection
 * Expects item's ID to be in the format: symantec-something-tid
 */
symantec.forms.handle_taxonomy_click = function(item) {
  var ret_value = true;
  
  var taxonomy_id = item.attr('id').split('-')[2];
  var $option_element = $("option[value='" + taxonomy_id + "']");
  var $parent = item.parents('.tax-select-row');
  if (item.attr('checked')) {
    if (!$option_element.parent().attr('multiple')) {
      // vocab widget is set to single-select, make the checkboxes behave accordingly
      var num_selected = 0;
      $parent.find('.tax-checkbox').each(function() {
        if ($(this).attr('checked')) {
          num_selected++;
        }
      });

      if (num_selected > 1) {
        alert('Only a single item may be selected in this area.');
        ret_value = false;
      }
      
      if (ret_value) {
        $option_element.attr('selected', true);
      }
    } else {
      // vocab widget is multi-select: select away, me hearties
      $option_element.attr('selected', true);
    }
  } else {
    $option_element.removeAttr('selected');
  }
  
  return ret_value;  
}

/**
 *  Sort by product and community on flag support page
 */
 symantec.flagSupport = {
    product_option_selector: 'div.flag-support-product select',
		community_option_selector: 'div.flag-support-community select'		
};

 
 
symantec.flagSupport.taxonomy_sort = function() {
   $(symantec.flagSupport.product_option_selector + ',' + symantec.flagSupport.community_option_selector).unbind('change').change(function() {
	
	var $product = $(symantec.flagSupport.product_option_selector).val();
	var $community = $(symantec.flagSupport.community_option_selector).val();
	
	var view_args = [];
	if ($product && $product.length) {
		view_args.push($product);
	}
	if ($community && $community.length) {
		view_args.push($community);
	}
	
	var sortURL = Drupal.settings.basePath + "flags/support";
	if (view_args.length) {
		sortURL +=  "/" + view_args.join(',');
	}
	
	window.location = sortURL;
	
	//alert(sortURL);
	
  //this.filter_taxonomy = filters.join(',');
	//$('.ideas-rss-icon').attr('href', Drupal.settings.basePath +'feeds/ideas/' + this.filter_taxonomy);

	/*
	var selected_value =  $(this).val();
		var item_id = $(this).attr('id');
    var node_id = item_id.split('-')[2];
		var $status_widget = $('.idea-status-wrapper');
		var current_contents = $status_widget.html();
		
		$status_widget.html('saving...');
    var post_data = {nid: node_id, tid: selected_value};
    $.post(Drupal.settings.basePath + 'ideas/set-status', post_data, function(data, textResponse) {
			$status_widget.html(current_contents);
			$(select_input_class).val(selected_value);
			symantec.ideas.setup_edit_idea_status();
		});
    */
    return false;
  });
}

// Setup on page load
Drupal.behaviors.setup_community_override = symantec.forms.setup_community_override;

