dojo.declare("DojoExpandAccordionGroup", DojoExpandGroup, 
{
	// This class contains "expand" behavior that is particular to
	// the Links Accordion portlet.
	//
	// servletUrl - the URL of the AJAX servlet
	// portletUniqueName - the unique name of the portlet
	// portletMode - the mode the portlet is currently in
	// namespace - the encoded namespace of the portlet
	// numLists: the total number of accordion sections.
	constructor: function(servletUrl, portletUniqueName, portletMode, namespace,numLists)
	{
		this.servletUrl = servletUrl;
		this.portletUniqueName = portletUniqueName;
		this.portletMode = portletMode;
		this.namespace = namespace;
		this.numLists = numLists;
		this.groupNumber = -1;
		this.previouslyExpandedGroupNumber = -1;
		this.groupName = "";
	},
	
	// This function is designed for use by the Links Accordion portlet. It expands the
	// section of the accordion corresponding to groupNumber.
	// NOTE: The following attributes must be set BEFORE this function is called.
	// groupNumber: the number (starting at 1) of the accordion section to be expanded.
	// groupName: the name of the accordion section being expanded.
	expandAccordionGroup: function()
	{
		var headerGroup;
		var headerGroupSpan;
		var expandGroup;
		var headerGroup2;
		var headerGroup2Span;
		var expandGroup2;
		
		// Prevent re-expanding a group that is already expanded.
		if(this.groupNumber != this.previouslyExpandedGroupNumber)
		{
			//expand the selected section, and collapse all of the rest.
			headerGroup = "accordion_group_header_" + this.groupNumber;
			headerGroupSpan = "accordion_group_header_span_" + this.groupNumber;
			expandGroup = "accordion_group_" + this.groupNumber + "_body"			
			document.getElementById(expandGroup).style.display = "block";
			document.getElementById(headerGroup).className = "accordion_header_selected";	
			document.getElementById(headerGroupSpan).className = "accordion_header_span_selected";									
			this.previouslyExpandedGroupNumber = this.groupNumber;			
			
			//loop through all of the sections, and collapse all of them except the selected section.
			for (var i = 0; i < this.numLists; i++)
			{				
				if (i != this.groupNumber - 1)
				{
					//not the selected section, so collapse it.
					headerGroup2 = "accordion_group_header_" + (i + 1);
					headerGroup2Span = "accordion_group_header_span_" + (i + 1);
					expandGroup2 = "accordion_group_" + (i + 1) + "_body"				
					document.getElementById(expandGroup2).style.display = "none";
					document.getElementById(headerGroup2).className = "accordion_header";		
					document.getElementById(headerGroup2Span).className = "accordion_header_span";			
				}
			}
			
			// If a main accordion group was actually expanded in the UI, invoke an AJAX action to 
			// reflect the expanded group in the view bean structure. 
			if(this.groupName != "null")
			{
				var parms = {"com.ibm.wplc.controller.action": "com.ibm.spe.genericList.action.ExpandAccordionGroup", expandedGroupName: this.groupName, "com.ibm.wplc.controller.uniquePortletName": this.portletUniqueName, "com.ibm.wplc.controller.portletMode": this.portletMode};
				this.sendRequest(this.servletUrl, parms);
			}
			
		}
								
	},
	// This function handles the All Products link being pressed.  The link is only visible when a sub group is expanded, so call to
	// collapse the sub group.  This is called when a key is pressed on the section.
	processAllProductsKeyPress: function(event,parentGroupCatName,expandedSubGroupId, expandedSubGroupCatName)
	{		
		//ignore the tab key.
		if (event.keyCode != 9)
		{
			return this.processAllProductsClick(parentGroupCatName,expandedSubGroupId, expandedSubGroupCatName);
		}
	},	
	// This function handles the All Products link being pressed.  The link is only visible when a sub group is expanded, so call to
	// collapse the sub group.
	processAllProductsClick: function(parentGroupCatName,expandedSubGroupId, expandedSubGroupCatName)
	{
		this.switchTwistieGroup(parentGroupCatName,expandedSubGroupId, expandedSubGroupCatName, this.namespace);
	},
	// This function expands the twistie with the passed id and collapses all other
	// twisties on the page.  This is called when a key is pressed on the section.
	switchTwistieGroupWithKeyPress: function(event,parentGroupCatName,id,catName,nameSpace)
	{		
		//ignore the tab key.
		if (event.keyCode != 9)
		{
			return this.switchTwistieGroup(parentGroupCatName,id, catName, nameSpace);
		}
	},	
	// This function expands the twistie with the passed id and collapses all other
	// twisties on the page.
	switchTwistieGroup: function(parentGroupCatName,id,subGroupCatName,nameSpace)
	{	
		// Call function to do the heavy lifting of expanding the twistie with the passed id,
		// and collapsing the others.
		expcolVal = this.styleUpdate(id,subGroupCatName,nameSpace);
		
		// Send the appropriate Ajax request to the server to record the expansion or collapse 
		// of a twistie in the portlet's view bean.
 		if (expcolVal == "expand")
  		{
  			this.expandSubGroup(parentGroupCatName,id,subGroupCatName);
  		}
  		else 
  		{
  			this.collapseSubGroup(parentGroupCatName,id,subGroupCatName);
  		}
  		//handle the All products link.  This code is by itself (even though it could be included as part of the above if/then/else) to make
  		//it easier to remove/disable if the UE folks decide not to use the All Products link.
 		if (expcolVal == "expand")
  		{
  			this.expandedSubGroupId = id;
  			this.expandedSubGroupCatName = subGroupCatName;
  			
  			//show All products link and hide all of the other subgroups
  			this.hideShowAllProductsLink(id, expcolVal);
  		}
  		else 
  		{
  			this.expandedSubGroupId = "";
  			this.expandedSubGroupCatName = "";
  			
  			//hide All products link and show all of the other subgroups
  			this.hideShowAllProductsLink(id, expcolVal);
  		}  		
		return false;							
	},
	// This function sends an AJAX request to the server to record the expansion of
	// a twistie in the portlet's view bean.
	expandSubGroup: function(parentCatName, id, subGroupCatName)
	{
		this.categoryID  = id;
		this.catName = parentCatName;
		this.subCatName = subGroupCatName;
		var parms = {"com.ibm.wplc.controller.action": "com.ibm.spe.genericList.action.ExpandSubGroup", expandedParentGroupName: this.catName, expandedSubGroupName: this.subCatName, "com.ibm.wplc.controller.uniquePortletName": this.portletUniqueName, "com.ibm.wplc.controller.portletMode": this.portletMode};
		this.sendRequest(this.servletUrl, parms);
		return false;							
	},
	// This function sends an AJAX request to the server to record the collapse of
	// a twistie in the portlet's view bean.
	collapseSubGroup: function(parentCatName, id, subGroupCatName)
	{
		this.categoryID  = id;
		this.catName = parentCatName;
		this.subCatName = "collapse";
		var parms = {"com.ibm.wplc.controller.action": "com.ibm.spe.genericList.action.ExpandSubGroup", expandedParentGroupName: this.catName, expandedSubGroupName: this.subCatName, "com.ibm.wplc.controller.uniquePortletName": this.portletUniqueName, "com.ibm.wplc.controller.portletMode": this.portletMode};
		this.sendRequest(this.servletUrl, parms);
		return false;							
	},
	//common function to hide/show the All products link
	//
	// id - the expanded sub group category ID
	// expcolVal - "expand" if the category is being expanded
	hideShowAllProductsLink: function(id, expcolVal)
	{
  		var splitId=id.split("_");
		var portletType = splitId[5];
		var expandedCategoryId = splitId[3].substr(5);		
		var allProductsDiv;
		var headerDiv;
		var headerDivCount = 0;
		var groupIndex;
				
		//show/hide All products link and hide/show all of the other subgroups
		groupIndex = 1;
		while (true)
		{
			//generate the next group's header ID.  if the div can't be found, we've processed all of the groups so get out of the loop.
  			headerDiv = dojo.byId(this.namespace + "links_" + portletType + "_group_" + groupIndex);	
			if (headerDiv == undefined)
  			{
  				break;
  			}	
  			headerDivCount = headerDivCount + 1;
  			allProductsDiv = dojo.byId(this.namespace + "links_" + portletType + "_group_AllProducts_" + groupIndex);		
  			//for the group that isn't being expanded/collapsed, hide the group completely if the selected group is being expanded, otherwise
  			//show the group.
			if (groupIndex != expandedCategoryId)
			{
				if (expcolVal == "expand")
				{
		  			headerDiv.style.display = "none";
				}
				else
				{
		  			headerDiv.style.display = "block";					
				}
			}
			else
			{
				//the selected group.  show the all products link if the group is being expanded.  hide the link if the group is being collapsed.
				if (allProductsDiv != undefined)
				{
					if (expcolVal == "expand")
					{				
		  				allProductsDiv.style.display = "block";
					}
					else
					{				
		  				allProductsDiv.style.display = "none";
					}
				}
			}			
			groupIndex = groupIndex + 1;
		}
	}	 
	
});