if ((top.BreadCrumbInit == "undefined") || (top.BreadCrumbInit != true))
{
	/**
	*
	* BreadCrumbItem CLASS definition. Base class for Crumb and Container.
	*
	*/
	function BreadCrumbItem(){
		this.id = '';
		this.label = '';
		this.attributes = null;
	}
	BreadCrumbItem.prototype.setId = function (_id){
		this.id = _id; 
	}
	BreadCrumbItem.prototype.getId = function (){
		return this.id; 
	}
	BreadCrumbItem.prototype.setLabel = function (_label){
		this.label = _label; 
	}
	BreadCrumbItem.prototype.getLabel = function (){
		return this.label; 
	}
	BreadCrumbItem.prototype.setAttributes = function (_attributes){
		this.attributes = _attributes; 
	}
	BreadCrumbItem.prototype.getAttributes = function (){
		return this.attributes; 
	}
	
	/**
	*
	* Crumb CLASS definition. Extends BreadCrumbItem.
	*
	*/
	function Crumb(){
	}
	Crumb.prototype = new BreadCrumbItem();
	Crumb.prototype.constructor = Crumb;
	
	/**
	*
	* Container CLASS definition. Extends BreadCrumbItem.
	*
	*/
	function Container(){
		this.crumbList = new Array();
	}
	Container.prototype = new BreadCrumbItem();
	Container.prototype.constructor = Container;

	Container.prototype.getCrumbList = function (){
		return this.crumbList;
	}
	Container.prototype.add = function (crumb){
		this.crumbList.push(crumb);
	}
	Container.prototype.removeLast = function (){
		if ( this.crumbList != null && this.crumbList.length > 0 ) {
			this.crumbList.pop();
		}
	}
	Container.prototype.removeAll = function (){
		this.crumbList = new Array();
	}
	Container.prototype.highlight = function (){
	}

	/**
	*
	* BreadCrumbPayload CLASS definition.
	*
	*/
	function BreadCrumbPayload(crumb, id){
		this.crumb = crumb;
		this.containerId = id;
	}

	/**
	*
	* BreadCrumb COMPONENT CLASS definition.
	*
	*/
	function BreadCrumb(tableSetName){
		this.tableName = tableSetName;
		this.containerList = new Array();
	}
	BreadCrumb.prototype = new DynamicTableSet();
	BreadCrumb.prototype.constructor = BreadCrumb;
	
	BreadCrumb.prototype.findContainer = function (containerId){
		var containerLen = this.containerList.length;
		for(var i=0; i<containerLen; i++){
			if ( this.containerList[i].getId() == containerId ){
				break;
			}
		}
		return this.containerList[i];
	}
	BreadCrumb.prototype.getRowIndex = function (containerId){
		var containerLen = this.containerList.length;
		var index = 0;
		for(var i=0; i<containerLen; i++){
			index++;	//the container row itself
			index += this.containerList[i].getCrumbList().length;
			if ( this.containerList[i].getId() == containerId ){
				break;
			}
		}
		return index;
	}
	BreadCrumb.prototype.addContainer = function (tableName, container){
		this.containerList.push(container);
		var cellList = new Array();
		cellList[0] = new Cell();
		cellList[0].setText(container.getLabel());
		cellList[0].setAttributes(container.getAttributes());
		this.insertRow(container.getId(), this.containerList.length - 1, cellList);
		
	}
	BreadCrumb.prototype.addCrumb = function (crumb, containerId){
		var container = this.findContainer(containerId);
		if ( container != null && container != "undefined" ){
			var cellList = new Array();
			cellList[0] = new Cell();
			cellList[0].setText(crumb.getLabel());
			cellList[0].setAttributes(crumb.getAttributes());
			this.insertRow(crumb.getId(), this.getRowIndex(containerId), cellList);
			container.add(crumb);
		}
	}
	BreadCrumb.prototype.removeLastCrumb = function (){
		var containerLen = this.containerList.length;
		for(var i=containerLen - 1; i>=0; i--){
			var crumbList = this.containerList[i].getCrumbList();
			if (crumbList.length > 0) {
				this.deleteRow(crumbList[crumbList.length - 1].getId());
				this.containerList[i].removeLast();
				break;
			}
		}
	}
	BreadCrumb.prototype.removeCrumbsAfter = function (rowId){
		var removeCrumb = false;
		var containerLen = this.containerList.length;
		
		// check to see if the selected row is a container row
		var container = this.findContainer(rowId);
		if ( container != "undefined" && container != null ) {
			for(var i = containerLen - 1; i >= 0; i--){
				this.removeAllCrumbsFromContainer(this.containerList[i]);
				if ( this.containerList[i].getId() == container.getId() ){
					break;
				}
			}
		} else { // the selected row is a crumb row
			for(var i=0; i<containerLen; i++){
				if ( removeCrumb ){
					this.removeAllCrumbsFromContainer(this.containerList[i]);
				} else {
					var removedCrumbsCount = 0;
					var crumbList = this.containerList[i].getCrumbList();
					var crumbLen = crumbList.length;
					for(var j=0; j<crumbLen; j++){
						if ( removeCrumb ){
							this.deleteRow(crumbList[j].getId());
							removedCrumbsCount++;
						} else {
							if(crumbList[j].getId() == rowId){
								removeCrumb = true;
							}
						}
					}
					for(var r=0; r<removedCrumbsCount; r++){
						crumbList.pop();
					}
				}
			}
		}
	}
	BreadCrumb.prototype.removeAllCrumbsFromContainer = function (container){
		var crumbList = container.getCrumbList();
		var crumbLen = crumbList.length;
		for(var j=0; j<crumbLen; j++){
			this.deleteRow(crumbList[j].getId());
		}
		container.removeAll();
	}
	
	BreadCrumb.prototype.highlightContainer = function (tableName, containerId){
		this.highlightRow(tableName, containerId);
	}


	/**
	*
	* BreadCrumb CREATE definition.
	*
	*/

	TableSetActionManager.prototype.createBreadCrumb = function (tableSetName, documentObj){
		var breadCrumb = new BreadCrumb(tableSetName);
		breadCrumb.setHighlightOnSelect(false);
		breadCrumb.initializeRowDataInitial(documentObj);
		this.tableSetArray[tableSetName] = breadCrumb;
		TableSetActionManager.setManager(tableSetName, this);
		breadCrumb.setCellAsAnchor(true);
	}

	/**
	*
	* BreadCrumb EVENT MANAGER definition.
	*
	* Handles component updates
	*
	*/

	function BreadCrumbEventManager(){
		this.xmlWrapper = new XmlWrapper();
	}

	BreadCrumbEventManager.prototype.handle = function(publisher, eventName, eventId, payLoad) {
		payLoad = this.xmlWrapper.unWrapXml(payLoad);

		var tableSet = top.tableSetActionManager.getTableSet(eventId);

		if(eventName == 'addContainer'){
			tableSet.addContainer(tableSet.tableName, payLoad);
		} else if(eventName == 'addCrumb') {
			tableSet.addCrumb(payLoad.crumb, payLoad.containerId);
		} else if(eventName == 'removeCrumb') {
			tableSet.removeLastCrumb();
		} else if(eventName == 'removeLastCrumb'){
			tableSet.removeLastCrumb();
		} else if(eventName == 'removeCrumbsAfter'){
			tableSet.removeCrumbsAfter(payLoad);
		} else if(eventName == 'highlightContainer'){
			tableSet.highlightContainer(tableSet.tableName, payLoad);
		}
	}
	top.breadCrumbEventManager = new BreadCrumbEventManager();
	
	top.eventService.subscribe("BreadCrumb","addContainer","-",top.breadCrumbEventManager);
	top.eventService.subscribe("BreadCrumb","addCrumb","-",top.breadCrumbEventManager);
	top.eventService.subscribe("BreadCrumb","removeLastCrumb","-",top.breadCrumbEventManager);
	top.eventService.subscribe("BreadCrumb","removeCrumbsAfter","-",top.breadCrumbEventManager);
	top.eventService.subscribe("BreadCrumb","highlightContainer","-",top.breadCrumbEventManager);

	//////////////////////////////////////

	top.BreadCrumbInit = true;
}
	