function BreadCrumb(menu, destination, seperatorImage){
	for(var i in menu.childNodes) {
		var node = menu.childNodes[i];
		if(node.nodeType == 1){
			this.navMenu = node.cloneNode(true);
			break;
		}
	}	
	this.seperatorImage = seperatorImage;
	this.destination = destination;
	this.currentLocation = document.location.href.toLowerCase();	
	//The first link will always be the current page.
	this.crumbLinks = new Array();	
	//this.log = new Log();	
	this.CreateTrail();	
	var crumbs = this.GenerateHtml();
	//this.log.Display();
}

BreadCrumb.prototype.GenerateHtml = function(){
	this.crumbLinks.reverse();	
	for(var i = 0; i < this.crumbLinks.length; i++){
		var link = this.crumbLinks[i];		
		if(i != this.crumbLinks.length - 1){
			var newLink = document.createElement('a');		
				newLink.href = link.Href;
				newLink.innerHTML = link.Title;
				
			var img = new Image();
				img.src = this.seperatorImage;
			
			this.destination.appendChild( newLink );
			this.destination.appendChild( img );
		}else{
			var span = document.createElement('span');
			span.innerHTML= link.Title;
			
			this.destination.appendChild( span );			
		}
	}
};

BreadCrumb.prototype.CreateTrail = function(){
	this.ParseChildren( this.navMenu );					
	var link = new BreadCrumbLink("/default.aspx", "Home");
	this.crumbLinks.push(link);
};

BreadCrumb.prototype.ParseChildren = function(node){
	for(var i in node.childNodes){
		var current = node.childNodes[i];		
		if(current.nodeType == 1){
			if(current.nodeName == 'A'){			
				var str = new String(current).toLowerCase();
				//This is the currently viewed page. AFter find this page, we can just climb
				//back up the tree to get all of the parents.
				if(str == this.currentLocation){					
					var link = new BreadCrumbLink(current, current.innerHTML);
					this.crumbLinks.push(link);
					this.ClimbBackUpTree(current.parentNode);
					break;		
				}	
			}					
			this.ParseChildren(current);
		}	
	}
};

BreadCrumb.prototype.ClimbBackUpTree = function(node){					
	//Climb back up the tree.
	var parent = node.parentNode;
	if(parent != null && parent.nodeType == 1){		
		if(parent.nodeName == 'LI'){			
			if(parent.id.length > 1){
				var aTag = parent.getElementsByTagName('A')[0];
				var link = new BreadCrumbLink(aTag, aTag.innerHTML);
				this.crumbLinks.push(link);				
			}
		}		
		this.ClimbBackUpTree(parent);
	}
};

function BreadCrumbLink(href, title){
	this.Href = href;
	this.Title = title;
}

/*
function Log(){
	this.logFile = "";
}

Log.prototype.WriteLine = function(text){
	this.logFile += text + "\n";
}

Log.prototype.Write = function(text){
	this.logFile += text;
}

Log.prototype.Clear = function(){
	this.logFile = "";
}

Log.prototype.Display = function(){
	alert(this.logFile);
}
*/
