global proc AEVariationTemplate( string $nodeName )
{
	editorTemplate -beginScrollLayout;
	
		// The all go into the collapsable "Parameters" section
		editorTemplate -beginLayout "Overall" -collapse false;

			// Add the default controls
			editorTemplate -addControl "type";
			editorTemplate -addControl "seed";
			editorTemplate -addControl "exception";
			editorTemplate -addControl "evaluation";
			// editorTemplate -callCustom "hydraChCountNewAttrib" "hydraChCountReplaceAttrib" "childCount";
			
		editorTemplate -endLayout;
		
		editorTemplate -beginLayout "Multiplication" -collapse true;
		
			editorTemplate -beginNoOptimize;
		
			editorTemplate -addControl "updateTesselatedMesh";
			editorTemplate -addControl "updateTextures";
			editorTemplate -addControl "updateMultipliedTransforms";
			
			editorTemplate -addSeparator;
			
			editorTemplate -endNoOptimize;
				
			//Add the default controls for the Multiplication-Handling
			string $newFuncAttrib = "variationMultNewAttrib";
			string $replaceFuncAttrib = "variationMultReplaceAttrib";
			editorTemplate -callCustom $newFuncAttrib $replaceFuncAttrib "";
								
		editorTemplate -endLayout;
		
		editorTemplate -beginLayout "Family" -collapse false;
		
			//Add the default controls for the Family-Members-Handling
			string $newFuncAttrib = "variationFamilyNewAttrib";
			string $replaceFuncAttrib = "variationFamilyReplaceAttrib";
			editorTemplate -callCustom $newFuncAttrib $replaceFuncAttrib "";	
						
		editorTemplate -endLayout;
		
		editorTemplate -beginLayout "Xfrog HD" -collapse true;
		
			editorTemplate -addControl "singleEvaluation";
				
			//Add the default controls for the Alternates-Handling
			string $newFuncAttrib = "variationAlternateNewAttrib";
			string $replaceFuncAttrib = "variationAlternateReplaceAttrib";
			editorTemplate -callCustom $newFuncAttrib $replaceFuncAttrib "clearMeshOrLink";
								
		editorTemplate -endLayout;
		

		// Create an "Extras" section and also add controls for any
		// attributes we have not explicitly mentioned.
		editorTemplate -addExtraControls;
	editorTemplate -endScrollLayout;

	// Tell the attribute editor not to display the attributes we
	// don't care about.
	editorTemplate -suppress "caching";
	editorTemplate -suppress "nodeState";
}

global proc variationMultNewAttrib(string $attr)
{
	variationMultReplaceAttrib($attr);
}

global proc variationMultReplaceAttrib(string $attr)
{
	$type = `nodeType ($attr+"childCount")`;
	int $end   = `size($attr)`-1;
	$node = `substring $attr 1 $end`;
	
	string $objectName[];
	tokenize($attr, ".", $objectName);

	if(!`layout -exists ($type+"_multiplication_layout")`) columnLayout ($type+"_multiplication_layout");
	setParent ($type+"_multiplication_layout");
	
	if(!`layout -exists ($type+"_multiplication_nodeNumber_layout")`) rowLayout -nc 3 ($type+"_multiplication_nodeNumber_layout"); //multiplication_nodeNumber
	if(!`text -exists ($type+"_multiplication_nodeNumber_text")`){
		setParent ($type+"_multiplication_nodeNumber_layout");
		text -label ("Child Count") ($type+"_multiplication_nodeNumber_text");
	}
	if(!`textField -exists ($type+"_multiplication_nodeNumber_tf")`){
		setParent ($type+"_multiplication_nodeNumber_layout");
		intField  -value 10 ($type+"_multiplication_nodeNumber_tf");
	}
	intField  -e -value (`getAttr ($node+".requestedChildCount")`) ($type+"_multiplication_nodeNumber_tf");
	
	setParent ($type+"_multiplication_layout");
	if(!`layout -exists ($type+"_multiplication_type_layout")`) rowLayout -nc 3 ($type+"_multiplication_type_layout");
	if(!`text -exists ($type+"_multiplication_type_text")`){
		setParent ($type+"_multiplication_type_layout");
		text -label ("Multiplication Type") ($type+"_multiplication_type_text");
	}
	if(!`optionMenu -exists ($type+"_multiplication_type_menu")`){
		setParent ($type+"_multiplication_type_layout");
		optionMenu ($type+"_multiplication_type_menu");
			menuItem -data 1 -label "Tesselated Mesh";
			menuItem -data 2 -label "Instance";
			menuItem -data 3 -label "Copy";
	}
	optionMenu -e -select (`getAttr ($node+".multiplyType")`+1) ($type+"_multiplication_type_menu");
		
	setParent ($type+"_multiplication_layout");
	if(!`separator -exists ($type+"_multiplication_separator2")`) 
	separator -h 10 -style "none" ($type+"_multiplication_separator2");
		
	if(!`layout -exists ($type+"_multiplication_button_layout")`) rowLayout -nc 3 ($type+"_multiplication_button_layout");
	if(!`text -exists ($type+"_multiplication_button_text")`){
		setParent ($type+"_multiplication_button_layout");
		text -label ("") ($type+"_multiplication_button_text");
	}
	if(!`button -exists ($type+"_multiplication_button")`){
			setParent ($type+"_multiplication_button_layout");
			button -l "Update" -h 20 ($type+"_multiplication_button");
	}
	
	setParent ($type+"_multiplication_layout");
	if(!`separator -exists ($type+"_multiplication_separator3")`) 
		separator -h 10 -style "none" ($type+"_multiplication_separator3");
		
	string $actionCommand;
	$actionCommand = "setAttr "+$node+".requestedChildCount (`intField  -q -value "+$type+"_multiplication_nodeNumber_tf`); 			\n";
	$actionCommand += "setAttr "+$node+".multiplyType (`optionMenu -q -select "+$type+"_multiplication_type_menu`-1);		 		\n";
	$actionCommand += "XFUpdateObject "+$node+";													\n";
	$actionCommand += "select "+$objectName[0]+";													";
	button -e -command $actionCommand ($type+"_multiplication_button");		
}

global proc hydraChCountNewAttrib(string $attr)
{
	hydraChCountReplaceAttrib($attr);
}

global proc hydraChCountReplaceAttrib(string $attr)
{
	$type = `nodeType ($attr)`;
	string $objectName[];
	tokenize($attr, ".", $objectName);
	
	if(!`layout -exists ($type+"_childCount_layout")`) rowLayout -nc 2 ($type+"_childCount_layout");
	if(!`text -exists ($type+"_childCount_text")`){
		setParent ($type+"_childCount_layout");
		text -label ("Child Count") ($type+"_childCount_text");
	}
	if(!`intField -exists ($type+"_childCount_if")`){
		setParent ($type+"_childCount_layout");
		intField  -ed 1 ($type+"_childCount_if");
	}
	intField -e -cc ("select "+$objectName[0]+";") ($type+"_childCount_if");
	connectControl ($type+"_childCount_if") $attr;
}

global proc variationFamilyNewAttrib(string $attr)
{
	variationFamilyReplaceAttrib($attr);
}

global proc variationFamilyReplaceAttrib(string $attr)
{
	$type = `nodeType ($attr+"inParameters")`;
	string $objectName[];
	tokenize($attr, ".", $objectName);
	
	int $childs = `getAttr ($attr+"childCount")`;
	
	if(!`layout -exists ($type+"_children_layout")`) columnLayout ($type+"_children_layout");
	
	setParent ($type+"_children_layout");
	if(!`layout -exists ($type+"_parent_layout")`) rowLayout -nc 3 ($type+"_parent_layout"); //parent
	if(!`text -exists ($type+"_parent_text")`){
		setParent ($type+"_parent_layout");
		text -label ("Parent") ($type+"_parent_text");
	}
	if(!`textField -exists ($type+"_parent_tf")`){
		setParent ($type+"_parent_layout");
		textField  -ed 0 -tx "<no connection>" ($type+"_parent_tf");
	}
	if(!`symbolButton -exists ($type+"_parent_button")`){
			setParent ($type+"_parent_layout");
			symbolButton -i "inArrow.xpm" -h 20 ($type+"_parent_button");
	}
	
	setParent ($type+"_children_layout");
	if(!`layout -exists ($type+"_childs_layout")`) columnLayout ($type+"_childs_layout"); //childs
	
	int $childsIterate=0;
	while(`layout -exists ($type+"_childs_"+$childsIterate+"_layout_row1")`){
			$childsIterate++;	
	}
	
	if($childs>$childsIterate){	// create additional layouts
		for($i=$childsIterate; $i<$childs; $i++){
			setParent ($type+"_childs_layout");
			rowLayout -nc 3 ($type+"_childs_"+$i+"_layout_row1"); //child
			text -label ("Child Object "+$i) ($type+"_childs_"+$i+"_text");
			textField  -ed 0 -tx "<no connection>" ($type+"_childs_"+$i+"_tf");
			symbolButton -i "addy.xpm" -h 20 ($type+"_childs_"+$i+"_add_button");
			setParent ($type+"_childs_layout");
			rowLayout -nc 3 ($type+"_childs_"+$i+"_layout_row2");
			text -label ("Child Primitive "+$i) ($type+"_childs_"+$i+"_text2");
			textField  -ed 0 -tx "<no connection>" ($type+"_childs_"+$i+"_prim_tf");
			symbolButton -i "outArrow.xpm" -h 20 ($type+"_childs_"+$i+"_button");
			popupMenu -b 3 ($type+"_childs_"+$i+"_button_popup");
		}
	}
	if($childs<$childsIterate){	// delete unneeded layouts
		for($i=$childs; $i<$childsIterate; $i++){
			deleteUI -control ($type+"_childs_"+$i+"_text");
			deleteUI -control ($type+"_childs_"+$i+"_tf");
			deleteUI -control ($type+"_childs_"+$i+"_add_button");
			deleteUI -control ($type+"_childs_"+$i+"_text2");
			deleteUI -control ($type+"_childs_"+$i+"_prim_tf");
			$itemCount = `popupMenu -q -numberOfItems ($type+"_childs_"+$i+"_button_popup")`;
			for($j=0; $j<$itemCount; $j++){
				deleteUI -menuItem ($type+"_childs_"+$i+"_button_popup_item_"+$j);
			}		
			deleteUI -menu ($type+"_childs_"+$i+"_button_popup");
			deleteUI -control ($type+"_childs_"+$i+"_button");
			deleteUI -layout ($type+"_childs_"+$i+"_layout_row1");
			deleteUI -layout ($type+"_childs_"+$i+"_layout_row2");
		}
	}
	
	if( catchQuiet ($pParams = `connectionInfo -sfd ($attr+"inParameters")`)) $pParams="";	//parent
	if(`size($pParams)`!=0){
		string $conInput[];
		tokenize($pParams, ".", $conInput);
		textField -e -tx $conInput[0] ($type+"_parent_tf");
		symbolButton -e -en 1 -command ("select "+$conInput[0]+";") ($type+"_parent_button");
	} else {
		textField -e -tx "<no connection>" ($type+"_parent_tf");
		symbolButton -e -en 0 ($type+"_parent_button");
	}
	
	string $params[];
	string $primParams[];
	for ($i=0; $i<$childs; $i++){
		setParent -menu ($type+"_childs_"+$i+"_button_popup");
		$itemCount = `popupMenu -q -numberOfItems ($type+"_childs_"+$i+"_button_popup")`;
		for($j=0; $j<$itemCount; $j++){
			deleteUI -menuItem ($type+"_childs_"+$i+"_button_popup_item_"+$j);
		}
		if( catchQuiet ($params = `connectionInfo -dfs ($attr+"outChildParameters["+$i+"]")`)) $params={};		//child
		if(`size($params)`>0){
			string $conInput[];
			tokenize($params[0], ".", $conInput);
			textField -e -tx $conInput[0] ($type+"_childs_"+$i+"_tf");
			symbolButton -e -en 1 -command ("select "+$conInput[0]+";") ($type+"_childs_"+$i+"_button");
			for($j=0; $j<`size($params)`; $j++){
				tokenize($params[$j], ".", $conInput);
				menuItem -label $conInput[0] -command ("select "+$conInput[0]) ($type+"_childs_"+$i+"_button_popup_item_"+$j);
			}
		} else {
			textField -e -tx "<no connection>" ($type+"_childs_"+$i+"_tf");
			symbolButton -e -en 0 ($type+"_childs_"+$i+"_button");
		}
		if( catchQuiet ($primParams = `connectionInfo -dfs ($attr+"outChildTranslation["+$i+"]")`)) $primParams={};	//childPrim
		if(`size($primParams)`>0){
			string $conInput[];
			tokenize($primParams[0], ".", $conInput);
			string $childs[] =`listRelatives -children -fullPath $conInput[0]`;
			if (`size($childs)`>0){
				symbolButton -e -en 1 -command ("select "+$childs[0]+";") ($type+"_childs_"+$i+"_button");
				$childs =`listRelatives -children $conInput[0]`;
				textField -e -tx $childs[0] ($type+"_childs_"+$i+"_prim_tf");
			} else {
				textField -e -tx "<no connection>" ($type+"_childs_"+$i+"_prim_tf");
			}	
		} else {
			textField -e -tx "<no connection>" ($type+"_childs_"+$i+"_prim_tf");
		}
		string $actionCommand 	=  "string $objName = \"\";						\n";
		$actionCommand		+= "if(!catch($objName = `XFConnectChild "+$i+"`)){			\n";
		$actionCommand		+= "	textField -e -tx $objName (\""+$type+"_childs_"+$i+"_tf\");	\n";
		$actionCommand		+= "}									\n";
		$actionCommand		+= "select "+$objectName[0]+";						\n";
		symbolButton -e -command $actionCommand ($type+"_childs_"+$i+"_add_button");
	}
}

global proc variationAlternateNewAttrib(string $attr)
{
	variationAlternateReplaceAttrib($attr);
}

global proc variationAlternateReplaceAttrib(string $attr)
{	
	$type = `nodeType ($attr)`;
	string $altInput[];
	tokenize($attr, ".", $altInput);
	if(!`layout -exists ($type+"_alternate_layout")`) columnLayout ($type+"_alternate_layout");
	
	if(`layout -exists ($type+"_clear_line_layout")`) {
		deleteUI -control ($type+"_clear_text1");
		deleteUI -control ($type+"_clear_choice");
		deleteUI -control ($type+"_clear_fill1");
		deleteUI -layout ($type+"_clear_line_layout");
	}
	int $altIterate=0;
	while(`layout -exists ($type+"_clear_"+$altIterate+"_offset")`){
		$altIterate++;
	}
	if($altIterate>0){	// delete unneeded lines
		for($i=0; $i<$altIterate; $i++){
			deleteUI -layout ($type+"_clear_"+$i+"_offset");
		}
	}
	if(`layout -exists ($type+"_alternate1_line_layout")`) {
		deleteUI -control ($type+"_alternate1_fill");
		deleteUI -control ($type+"_alternate1_text");
		deleteUI -control ($type+"_alternate1_button");
		deleteUI -layout ($type+"_alternate1_line_layout");
	}
	if(`layout -exists ($type+"_alternate2_line_layout")`) {
		deleteUI -control ($type+"_alternate2_fill");
		deleteUI -control ($type+"_alternate2_text");
		deleteUI -control ($type+"_alternate2_button");
		deleteUI -layout ($type+"_alternate2_line_layout");
	}
	int $altIterate=0;
	while(`layout -exists ($type+"_alternate1_"+$altIterate+"_layout1")`){
		$altIterate++;
	}
	if($altIterate>0){	// delete unneeded lines
		for($i=0; $i<$altIterate; $i++){
			deleteUI -control ($type+"_alternate1_"+$i+"_text1");
			deleteUI -control ($type+"_alternate1_"+$i+"_offset_tf");
			deleteUI -control ($type+"_alternate1_"+$i+"_fill");
			deleteUI -layout ($type+"_alternate1_"+$i+"_tf");
			deleteUI -layout ($type+"_alternate1_"+$i+"_layout1");
		}
	}
	int $altIterate=0;
	while(`layout -exists ($type+"_alternate2_"+$altIterate+"_layout1")`){
		$altIterate++;
	}
	if($altIterate>0){	// delete unneeded lines
		for($i=0; $i<$altIterate; $i++){
			deleteUI -control ($type+"_alternate2_"+$i+"_text1");
			deleteUI -control ($type+"_alternate2_"+$i+"_offset_tf");
			deleteUI -control ($type+"_alternate2_"+$i+"_fill");
			deleteUI -layout ($type+"_alternate2_"+$i+"_tf");
			deleteUI -layout ($type+"_alternate2_"+$i+"_layout1");
		}
	}
	
	setParent ($type+"_alternate_layout");
	if(!`layout -exists ($type+"_clear_line_layout")`) rowLayout -nc 3 ($type+"_clear_line_layout"); //alternate
	if(!`text -exists ($type+"_clear_text1")`){
		setParent ($type+"_clear_line_layout");
		text -label ("HD Type") -align "right" ($type+"_clear_text1");
	}
	if(!`optionMenu -exists ($type+"_clear_choice")`){
			setParent ($type+"_clear_line_layout");
			optionMenu ($type+"_clear_choice");
			menuItem -data 1 -label "Classic";
			menuItem -data 2 -label "Positive";
			menuItem -data 3 -label "Negative";
	}
	if(!`text -exists ($type+"_clear_fill1")`){
		setParent ($type+"_clear_line_layout");
		text -label ("") ($type+"_clear_fill1");
	}
	string $typeValue = `getAttr ($altInput[0]+".clearMeshOrLink[0]")`;
	if ($typeValue != "+" && $typeValue != "-") {
		optionMenu -e -sl 1 ($type+"_clear_choice");
		setParent ($type+"_alternate_layout");
		attrControlGrp -l "Clear Child-Link" -attribute ($altInput[0] + ".clearMeshOrLink[1]") ($type+"_clear_0_offset");
	} else {
		int $childs = `getAttr ($altInput[0] + ".childCount")`;
		if ($typeValue == "+") {
			optionMenu -e -sl 2 ($type+"_clear_choice");
			setParent ($type+"_alternate_layout");
			if ($childs > 0) for ($i=0; $i<$childs; $i++) attrControlGrp -l ("Added Links Child "+$i) -attribute ($altInput[0] + ".clearMeshOrLink[" + ($i+1) + "]") ($type+"_clear_" + $i + "_offset");
		} else { 
			optionMenu -e -sl 3 ($type+"_clear_choice");
			setParent ($type+"_alternate_layout");
			if ($childs > 0) for ($i=0; $i<$childs; $i++) attrControlGrp -l ("Removed Links Child "+$i) -attribute ($altInput[0] + ".clearMeshOrLink[" + ($i+1) + "]") ($type+"_clear_" + $i + "_offset");
		}
	}
	string $actionCommand	=  "int $selOffset = `optionMenu -q -sl (\"" + $type + "_clear_choice\")`;											\n";
	$actionCommand			+= "if ($selOffset == 1) setAttr (\"" + $altInput[0] + ".clearMeshOrLink[0]\") -type \"string\" \"\";				\n";
	$actionCommand			+= "else if ($selOffset == 2) setAttr (\"" + $altInput[0] + ".clearMeshOrLink[0]\") -type \"string\" \"+\";			\n";
	$actionCommand			+= "else setAttr (\"" + $altInput[0] + ".clearMeshOrLink[0]\") -type \"string\" \"-\";								\n";
	$actionCommand			+= "select (\"" + $altInput[0] + "\");																				\n";
	optionMenu -e -cc $actionCommand ($type+"_clear_choice");
	
	setParent ($type+"_alternate_layout");
	if(!`layout -exists ($type+"_alternate1_line_layout")`) rowLayout -nc 3 ($type+"_alternate1_line_layout"); //alternate type
	if(!`text -exists ($type+"_alternate1_fill")`){
		setParent ($type+"_alternate1_line_layout");
		text -label ("") ($type+"_alternate1_fill");
	}
	if(!`text -exists ($type+"_alternate1_text")`){
		setParent ($type+"_alternate1_line_layout");
		text -label ("Generate alternate type") -align "right" ($type+"_alternate1_text");
	}
	if(!`symbolButton -exists ($type+"_alternate1_button")`){
			setParent ($type+"_alternate1_line_layout");
			symbolButton -i "addy.xpm" -h 20 ($type+"_alternate1_button");
	}

	int $altIdxList[] = `getAttr -mi ($altInput[0]+".alternateTypeOffset")`;
	int $altCount  = `size($altIdxList)`;
	if($altCount>0){	// create additional lines
		for($i=0; $i<$altCount; $i++){
			setParent ($type+"_alternate_layout");
			rowLayout -nc 3 ($type+"_alternate1_"+$i+"_layout1");
			text -label ("Alternate Offset"+$i) ($type+"_alternate1_"+$i+"_text1");
			textField  -ed 1 -tx "" ($type+"_alternate1_"+$i+"_offset_tf");
			text -label ("") ($type+"_alternate1_"+$i+"_fill");
			setParent ($type+"_alternate_layout");
			attrControlGrp -attribute ($altInput[0] + ".alternateType[" + $i + "]") ($type+"_alternate1_"+$i+"_tf");
		}
	}
	for($i=0; $i<$altCount; $i++){
		string $inputOffset = "[" + $altIdxList[$i] + "]";
		text -e -label ("Alternate Offset"+$inputOffset) ($type+"_alternate1_"+$i+"_text1");
		string $changeCommand = "setAttr (\"" + $altInput[0] + ".alternateTypeOffset" + $inputOffset + "\") -type \"string\" (`textField -q -tx (\"" + $type + "_alternate1_" + $i + "_offset_tf\")`);\n";
		string $changeValue = `getAttr ($altInput[0] + ".alternateTypeOffset" + $inputOffset)`;
		textField -e -tx ($changeValue) -cc ($changeCommand) ($type+"_alternate1_"+$i+"_offset_tf");
	}
	
	string $actionCommand	= "setAttr (\"" + $altInput[0] + ".alternateTypeOffset[" + $altCount + "]\") -type \"string\" \"\";			\n";
	symbolButton -e -command $actionCommand ($type+"_alternate1_button");
	
	setParent ($type+"_alternate_layout");
	if(!`layout -exists ($type+"_alternate2_line_layout")`) rowLayout -nc 3 ($type+"_alternate2_line_layout"); //alternate seed
	if(!`text -exists ($type+"_alternate2_fill")`){
		setParent ($type+"_alternate2_line_layout");
		text -label ("") ($type+"_alternate2_fill");
	}
	if(!`text -exists ($type+"_alternate2_text")`){
		setParent ($type+"_alternate2_line_layout");
		text -label ("Generate alternate seed") -align "right" ($type+"_alternate2_text");
	}
	if(!`symbolButton -exists ($type+"_alternate2_button")`){
			setParent ($type+"_alternate2_line_layout");
			symbolButton -i "addy.xpm" -h 20 ($type+"_alternate2_button");
	}

	int $altIdxList[] = `getAttr -mi ($altInput[0]+".alternateSeedOffset")`;
	int $altCount  = `size($altIdxList)`;
	if($altCount>0){	// create additional lines
		for($i=0; $i<$altCount; $i++){
			setParent ($type+"_alternate_layout");
			rowLayout -nc 3 ($type+"_alternate2_"+$i+"_layout1");
			text -label ("Alternate Offset"+$i) ($type+"_alternate2_"+$i+"_text1");
			textField  -ed 1 -tx "" ($type+"_alternate2_"+$i+"_offset_tf");
			text -label ("") ($type+"_alternate2_"+$i+"_fill");
			setParent ($type+"_alternate_layout");
			attrControlGrp -attribute ($altInput[0] + ".alternateSeed[" + $i + "]") ($type+"_alternate2_"+$i+"_tf");
		}
	}
	for($i=0; $i<$altCount; $i++){
		string $inputOffset = "[" + $altIdxList[$i] + "]";
		text -e -label ("Alternate Offset"+$inputOffset) ($type+"_alternate2_"+$i+"_text1");
		string $changeCommand = "setAttr (\"" + $altInput[0] + ".alternateSeedOffset" + $inputOffset + "\") -type \"string\" (`textField -q -tx (\"" + $type + "_alternate2_" + $i + "_offset_tf\")`);\n";
		string $changeValue = `getAttr ($altInput[0] + ".alternateSeedOffset" + $inputOffset)`;
		textField -e -tx ($changeValue) -cc ($changeCommand) ($type+"_alternate2_"+$i+"_offset_tf");
	}
	
	string $actionCommand	= "setAttr (\"" + $altInput[0] + ".alternateSeedOffset[" + $altCount + "]\") -type \"string\" \"\";			\n";
	symbolButton -e -command $actionCommand ($type+"_alternate2_button");
}