/** LeftMenuCampus
* A class that when instantiated contains and provides the properties and methods for working with a left menu campus,
* opening its submenus, showing its sub menu markers, and clicking on its links to open info windows on the map.
* When initialized with a campus xml node from markerData.xml, it creates its own submenu and its elements.
* 
* @class LeftMenuCampus
*	@constructor
* @returns {void}
*/
var LeftMenuCampus = function() {

	this.campusID = "";
	this.campusName = "";
	this.element = document.createElement( "DIV" );
	
	
	/** getElement
	* Returns a reference to this campus's DIV element
	*
	* @method getElement
	* @returns {HTMLElement}
	*/
	this.getElement = function() {
		return this.element;
	}
		
	
	/** init
	* Initializes this Campus's left menu
	*
	*	@method init
	* @returns {void}
	*/
	this.init = function( campusXmlNode ) {
		this.campusID = campusXmlNode.getAttribute( "id" );
		this.campusName = campusXmlNode.getAttribute( "name" );
		this.element.className = "campus";
		
		var h3Tag = document.createElement( "H3" );
		h3Tag.appendChild( document.createTextNode( this.campusName ) );
		this.element.appendChild( h3Tag );
		this.element.appendChild( document.createElement( "BR" ) );		
		
		this.createLeftMenu( campusXmlNode );
	}
	
	

	/** createLeftMenu
	* Creates the left menu based on the campus XML node received
	*
	* @method createLeftMenu
	* @param {XMLNode} campusXmlNode
	* @return {void}
	*/
	this.createLeftMenu = function( campusXmlNode ) {
		var categories = campusXmlNode.getElementsByTagName( "category" );
		for( var j = 0, numCategories = categories.length; j < numCategories; j++ ) {
			var currentCategory = categories[ j ];
			
			// Append the category title (if it has one)
			if( currentCategory.getAttribute( "name" ) && currentCategory.getAttribute( "name" ) != "" ) {
				var categoryTitle = document.createElement( "H4" );
				categoryTitle.appendChild( document.createTextNode( currentCategory.getAttribute( "name" ) ) );
				this.element.appendChild( categoryTitle );
				this.element.appendChild( document.createElement( "HR" ) );
			}
			
			// Append the sub menu's from the recursive createSubMenu method
			this.element.appendChild( this.createSubMenu( currentCategory ) );
			this.element.appendChild( document.createElement( "BR" ) );
		}
	}
	
	
	
	/** createSubMenu
	* Creates a submenu for a category.  Recursively creates child sub menu's if the element has a sub menu.
	*
	* @method createSubMenu
	* @param {XMLElement} categoryNode : The category (or subcategory) who's submenu should be created
	* @return {DocumentFragment} : A document fragment with the HTML code of the sub menu
	*/
	this.createSubMenu = function( categoryNode ) {
		var Event = YAHOO.util.Event,
				documentFragment = document.createDocumentFragment();
				
		// Loop child nodes and append the correct html for each
		var childNodes = categoryNode.childNodes;
		for( var i = 0, numChildNodes = childNodes.length; i < numChildNodes; i++ ) {
			if( childNodes[ i ].nodeType == 1 ) {   // Element Node
				
				// Subcategory
				if( childNodes[ i ].tagName == "subcategory" ) {		
					var subCategory = childNodes[ i ];
					var subCategoryDiv = document.createElement( "DIV" ); subCategoryDiv.className = "subCategory";
					var subCategorySubMenu = document.createElement( "DIV" ); subCategorySubMenu.className = "subMenu";
					var label = document.createElement( "LABEL" );
					
					// Append the sub category icon (if there is one)
					if( subCategory.getAttribute( "icon" ) ) {
						var img = document.createElement( "IMG" ); 
						img.src = subCategory.getAttribute( "icon" );
						img.className = "icons";
						label.appendChild( img );
					}
					
					// Append the checkbox
					var checkbox = document.createElement( "INPUT" );
					checkbox.type = "checkbox";
					label.appendChild( checkbox );
					
					
					// Find the direct children mapItemID's
					var aMapItemIDs = new Array();
					var subCategoryChildNodes = subCategory.childNodes;
					for( var j = 0, numSubCategoryChildNodes = subCategoryChildNodes.length; j < numSubCategoryChildNodes; j++ ) {
						if( subCategoryChildNodes[ j ].nodeType == 1 && subCategoryChildNodes[ j ].tagName == "item" )
							aMapItemIDs.push( subCategoryChildNodes[ j ].getAttribute( "mapItemID" ) );
					}
					
					// Set a listener on the checkbox to show the markers in its sub menu
					Event.addListener( checkbox, 'click', LeftMenu.showMarkers, { subMenuElement: subCategorySubMenu, mapItemIDs: aMapItemIDs }, LeftMenu );
					
					// Append the category name
					label.appendChild( document.createTextNode( subCategory.getAttribute( "name" ) ) );
					label.appendChild( document.createElement( "BR" ) );	
					
					subCategoryDiv.appendChild( label );
					
					// Recursively call createSubMenu with the current subcategory to append other subcategories and items
					subCategorySubMenu.appendChild( this.createSubMenu( subCategory ) );
					subCategoryDiv.appendChild( subCategorySubMenu );
					
					documentFragment.appendChild( subCategoryDiv );
				}
				
				// Item
				else if ( childNodes[ i ].tagName == "item" ) {
					var childItem = childNodes[ i ];
					var currentMapItemID = childItem.getAttribute( "mapItemID" );
					var currentMarkerItem = Map.getMapItem( currentMapItemID );
					if( currentMarkerItem != null ) 	// If the current marker item is not null
						var currentMarkerItemName = currentMarkerItem.getName();
					else
						var currentMarkerItemName = "** Item not found **";
					
					var li = document.createElement( "LI" ); li.className = "placeList";
					var a = document.createElement( "A" ); a.className = "placeLink"; a.href="javascript:void(0);";
					Event.addListener( a, 'click', Map.showMapItemInfoWindow, currentMapItemID, Map );
					a.appendChild( document.createTextNode( currentMarkerItemName ) );
					li.appendChild( a );
					documentFragment.appendChild( li );
				}
			}
		}
		
		return documentFragment;
	}
}