function BreadcrumbTrail(args){
	this.data = args.data;
	this.baseHref = args.baseHref || '';
	this.groupData = this.data.groups;	//array of data objects used to create a BreadcrumbGroup object
	this.gidPath = this.data.gidPath;	//data
	this.groupListEl = this.createGroupListEl();	//UL
	this.breadcrumbGroups = this.createGroups();	//array of BreadcrumbGroup objects
    this.parentContainer = document.getElementById('geographic_navigation');
    this.subsection = (args.subsection != 'overview' ? args.subsection : '' );
    this.subsectionText = args.subsectionTxt;
    this.destination = this.groupData[this.groupData.length-1].groupPath;
	this.populateGroupListEl();
	this.classifyGroupListItems();
}
BreadcrumbTrail.prototype.write = function(){
	this.injectGroupListEl();
	this.injectGlobe();
    var breadcrumbMenu = new DropdownMenu({
            listId: 'breadcrumb',
            hiddenMenuClass: 'menu',
            openFunc: toggleLeftnavSelect,
            closeFunc: toggleLeftnavSelect
    });
}
function toggleLeftnavSelect(){
    var leftNav, firstSelect;
    leftNav = getElements('leftNav', null, null)[0];
    if(!leftNav) return;
    firstSelect = leftNav.getElementsByTagName('SELECT')[0];
    if(firstSelect)
        firstSelect.style.visibility = (firstSelect.style.visibility == 'hidden' ? 'visible' : 'hidden' );
}
BreadcrumbTrail.prototype.createGroups = function(){
	var breadcrumbGroups = new Array();
	for(var i=0; i<this.groupData.length; i++){
		var newBreadcrumbGroup = new BreadcrumbGroup({
				groupTitle: this.groupData[i].groupTitle,
				groupPath: this.groupData[i].groupPath,
				groupChildLinks: this.groupData[i].groupChildLinks,
				baseHref: this.baseHref,
				trailPosition: i
		})
		breadcrumbGroups.push(newBreadcrumbGroup);
	}
	return breadcrumbGroups;
}
BreadcrumbTrail.prototype.createGroupListEl = function(){
	var groupListEl = document.createElement('UL');
	groupListEl.id = 'breadcrumb';
	groupListEl.className = 'dropdownMenu';
	return groupListEl;
}
BreadcrumbTrail.prototype.populateGroupListEl = function(){
	for(var i=0; i<this.breadcrumbGroups.length; i++)
		this.groupListEl.appendChild(this.breadcrumbGroups[i].itemEl);
    //section link
    var sectionItem, sectionLink;
    sectionItem = document.createElement('LI');
    sectionItem.className = 'sectionLink';
    sectionLink = document.createElement('A');
    sectionLink.innerHTML = this.subsectionText;
    sectionLink.href= this.baseHref + this.destination + '/' + this.subsection;
    sectionItem.appendChild(sectionLink);
    this.groupListEl.appendChild(sectionItem);
}
BreadcrumbTrail.prototype.classifyGroupListItems = function(){
	for(var i=0; i<this.breadcrumbGroups.length; i++)
		this.breadcrumbGroups[i].itemEl.className = 'breadcrumbGroup'
	this.breadcrumbGroups[0].itemEl.className += ' first';
	this.breadcrumbGroups[this.breadcrumbGroups.length-1].itemEl.className += ' last';
}
BreadcrumbTrail.prototype.injectGroupListEl = function(){
    if( this.parentContainer.hasChildNodes( ) )
        this.parentContainer.insertBefore(this.groupListEl, this.parentContainer.firstChild)
    else
        this.parentContainer.appendChild(this.groupListEl);
}
BreadcrumbTrail.prototype.injectGlobe = function(){
	var globeIcon = document.createElement('SPAN');
	globeIcon.id = 'globe_icon';
	this.parentContainer.appendChild(globeIcon);
}
function BreadcrumbGroup(args){
	this.baseHref = args.baseHref;
	this.trailPosition = args.trailPosition;
	this.hasChildren = (args.groupChildLinks.length > 0 ? true : false);
	this.groupTitle = args.groupTitle;
	this.groupPath = args.groupPath;
	this.groupChildLinks = args.groupChildLinks || false;
	this.childLinkEls = new Array();
	this.itemEl = this.createItemEl();
	if(this.hasChildren){ 
		this.createChildLinkLists();
		this.registerClickHandlers();
	}
}
BreadcrumbGroup.prototype.createItemEl = function(){
	var itemEl, triggerEl, separatorEl, parentLink, hasTrigger;
	itemEl=document.createElement('LI');
	itemEl.className = 'breadcrumbUnit';
	parentLink = document.createElement('A');
	parentLink.href = this.baseHref + this.groupPath;
	parentLink.innerHTML = this.groupTitle;
	separatorEl = document.createElement('SPAN');
	separatorEl.className = 'separator'; 	/*MAKE CONDITIONAL FOR FIRST AND LAST*/
	itemEl.appendChild(parentLink);
	itemEl.appendChild(separatorEl);
	if(this.hasChildren){
		triggerEl = document.createElement('SPAN');
		triggerEl.className = "trigger";
		itemEl.appendChild(triggerEl);
	}
	return itemEl;
}
BreadcrumbGroup.prototype.createChildLinkLists = function(){
	var childLinkMenu, numLinks, numLists, listLength, listStart, listEnd, listData;
	listData = this.groupChildLinks;
	this.childLinkMenu = document.createElement('DIV');
	this.childLinkMenu.className = 'inactive menu';
	sizeOfLists = 6;
	numLinks = this.groupChildLinks.length;
	numLists = Math.ceil(numLinks/sizeOfLists);
	for(var i=0; i<numLists; i++){
		listStart = sizeOfLists * i;
		listEnd = (listStart + sizeOfLists <= (numLinks-1) ? listStart+(sizeOfLists-1) : numLinks -1 );
		this.createChildLinkListEl( listData.slice(listStart, listEnd+1), i+1 )
	}
	this.itemEl.appendChild(this.childLinkMenu);
	this.childLinkMenu.className += ' lists_' + numLists;
}
BreadcrumbGroup.prototype.createChildLinkListEl = function(listData, listIndex){
	var childLinkList, crumbGroup;
	crumbGroup = this;
	childLinkList = document.createElement('UL');
	for(var i=0; i<listData.length; i++){
		var childLinkItem, childLink;
		childLinkItem = document.createElement('LI');
		childLink = document.createElement('A');
		childPath = listData[i].path;
		childLink.href = this.baseHref + childPath;
		childLink.innerHTML = listData[i].copy;
		childLinkItem.appendChild(childLink);
		childLinkList.appendChild(childLinkItem);
		this.childLinkEls.push(childLink);
	}
	this.childLinkMenu.appendChild(childLinkList);
}
BreadcrumbGroup.prototype.registerClickHandlers = function(){
	var crumbGroup = this;
	for(var i=0; i<this.childLinkEls.length; i++){
		this.childLinkEls[i].onclick = function(event){
			crumbGroup.childLinkClickHandler(event)
		}
	}
}
BreadcrumbGroup.prototype.childLinkClickHandler = function(event){ //update this to use basehref instead of 'travelguide' in regexp
	setCookie('bc_clicked','true',null,null,"/");
	var cookie = getCookie('bc_gidPath');
        if(cookie == null)
             cookie = "";
	var parts = cookie.split('/');
	var newGidPath = new Array();
	for(var i=0; i < this.trailPosition; i++) 
		newGidPath.push(parts[i]);
	setCookie('bc_gidPath',newGidPath.join('/'),null,null,"/");
}

