

/*-----------------------------------------------------------------------------+
| File name ............ ProductTree.js
| Project .............. vdomain.co.uk
| Created by ........... Mark Young
| Creation date ........ 01/08/2009
| Last modified ........ 18/02/2010
| Last modified by...... Mark Young
+------------------------------------------------------------------------------+
|
| Copyright (C) VDT(UK) Ltd.  -  C O N F I D E N T I A L !!!
|
|   THIS DOCUMENT CONTAINS TRADE SECRET DATA WHICH IS THE PROPERTY OF
|   VDT(UK) Ltd. THIS DOCUMENT IS SUBMITTED TO RECIPIENT IN CONFIDENCE.
|   INFORMATION CONTAINED HEREIN MAY NOT BE USED, COPIED OR DISCLOSED
|   IN WHOLE OR IN PART EXCEPT AS PERMITTED BY WRITTEN AGREEMENT
|   SIGNED BY VDT(UK) Ltd.
|
| Description:
|   Class to provide functionality for expanding and contracting product tree
|
| Documentation refs: 
|                     
|
|-Programmers Log (major changes only)-----------------------------------------+
| 
| Description                                               Date        Initials
| --------------------------------------------------------- ----------  --------
| First created                                             01/08/2009  MY 
| Style alterations                                         13/11/2009  MY 
| 
+-----------------------------------------------------------------------------*/


/*
----------------------------------------------------------------
|
|	Constructor/object definition
|	Purpose:  Initialises the variables and defines the member functions
|	Inputs:  pDivId(unique ID of the enclosing product tree div box)
|	Outputs/returns:  None
|
----------------------------------------------------------------
*/
function ProductTree(pDivId,pProtocol,pURL)
{
	this.protocol = pProtocol;
	this.URL = pURL;
	this.clickCount = 0;
	this.previousCategory = "";
	this.previousRoot = "";
	this.category = "";

	//Member functions
	this.processCategoryClick = mProcessCategoryClick;
	this.writeSubs = mWriteSubs;
}
/*  End of constructor/object definition */


/*
----------------------------------------------------------------
|
|	mProcessCategoryClick
|	Purpose:  Checks the desired category and extracts all category info
|	Inputs:  selectedCategory(name of the category clicked)
|	Outputs/returns:  None
|
----------------------------------------------------------------
*/
function mProcessCategoryClick(selectedCategory)
{	
	this.category = selectedCategory;
	var rootClicked = false;
	var i,j;
	
	//if the selected category is a root category
	for(i=0;i<categories.length;i++)
	{
		if(categories[i].name == this.category)  //when found the category name
		{
			for(j=0;j<masters.length;j++) //go through master categories asking if parent
			{
				//if the category has a master as a parent then its a root category
				if(masters[j].name == categories[i].parent)
				{
					if(this.clickCount > 0)  //if the tree has been opened before
					{
						//erase the contents of the previous root
						var root = document.getElementById('cat' + this.previousRoot);
						var temp = document.getElementById('cat' + this.previousRoot + 'content');
						
						root.removeChild(temp);
						rootClicked = true;
					}
					this.previousRoot = selectedCategory;
				}
			}
		}
	}

	if((rootClicked == false) && (this.clickCount > 0))
	{
		var currentCat = document.getElementById('cat' + this.category);
		if(currentCat.childNodes.length > 1) //2 because of link and a line brake
		{		
			var temp = document.getElementById('cat'+ this.category + 'content');
			currentCat.removeChild(temp);
		}
	}
	
	this.writeSubs();
	this.clickCount = this.clickCount + 1;
}
/*  End of mProcessCategoryClick function */


/*
----------------------------------------------------------------
|
|	mWriteSubs
|	Purpose:  Writes all subcategories to screen in div boxes
|	Inputs:  None
|	Outputs/returns:  None
|
----------------------------------------------------------------
*/
function mWriteSubs()
{
	var container = document.getElementById('cat' + this.category);
	var childContainer = document.createElement("div");
	childContainer.setAttribute("id",'cat' + this.category + 'content');
	childContainer.setAttribute("class","cat");
	
	var html = "";
	
	container.appendChild(childContainer);

	//Output the links for subcategories and direct links to the product pages that do not have subcategories
	for(i=0;i<categories.length;i++)  //go through the categories array
	{
		if(categories[i].parent == this.category)  //if category a member of the parent
		{
			if(categories[i].hasChild == "yes")  //check for children
			{
				html = html + '<a href="javascript:void(0)" onclick="tree.processCategoryClick(\'' + categories[i].name + '\')"><div id="cat' + categories[i].name + '"> - ' + categories[i].name + '</div></a>';
			}
			else
			{
				html = html + '<a href="/products/view.php5?request=' + categories[i].name + '&view=' + categories[i].parent + '" id="anchorhighlight' + categories[i].name + '"><div class="catLink" id="highlight' + categories[i].name + '"> - ' + categories[i].name + '</div></a>';
			}
		}
	}

	this.previousCategory = this.category;
	childContainer.innerHTML = html;
}
/*  End of mWriteSubs function */
