(function() {
/*
	Makes a series of pages into a "wizard"
	- Link to breadcrumbs.js right before </body>
	- Insert the CRUMB_TAG somewhere on each of the pages (a tag was chosen, rather than a class, so that it would fit into DB varchar limits)
	- To get next/previous links, insert an empty tag somewhere with the class defined in NEXTPREV_SELECTOR
	- Bask in dynamic-breadcrumb-inspired glory!
	
	requires core.js v1.1
*/

	var CRUMB_TAG = '{BC}'
	  , CRUMB_CONTEXT_SELECTOR = 'h1'
	  , CRUMB_SELECTOR = ".breadcrumbs"
	  , CRUMBS = '<span class="breadcrumbs">><a href="serve.aspx?page=wdd_overview">Overview</a>> <a href="serve.aspx?page=wdd_value">Value of a website</a> > <a href="serve.aspx?page=wdd_functions">Business Functions</a> > <a href="serve.aspx?page=wdd_site_get">How do you get it?</a></span>'
	  , CURRENT = 'current'
	  , NEXTPREV_SELECTOR = '.nextPrev'
	  , NEXT_SELECTOR = '.next'
	  , PREV_SELECTOR = '.prev'
	  , NEXT_PREV = '<a class="prev">< Previous</a><a class="next">Next ></a>'
	  , ACTIVE_CLASS = 'active';
		
	var breadcrumbLinks;
	
	var currentPageIs = (function() {
	
		function adjacentLinkIsCurrent(link, adjacentLink) {
			if (adjacentLink) {
				return core.css.hasClass(adjacentLink, CURRENT);
			}
			else {
				return false;
			}
		}
		
		var ret = {
		
			rightBefore: function(link) {
				var nextLink = link.nextSibling ? link.nextSibling.nextSibling : null;
				return adjacentLinkIsCurrent(link, nextLink)
			},
			
			rightAfter: function(link) {
				var prevLink = link.previousSibling ? link.previousSibling.previousSibling : null;
				return adjacentLinkIsCurrent(link, prevLink)
			}
		
		}
		
		return ret;
	
	})();
	
	function insertBreadcrumbs() {
		var context = core.getElements(CRUMB_CONTEXT_SELECTOR);
		core.each(context, function(el){
			if (el.innerHTML && el.innerHTML.indexOf(CRUMB_TAG) != -1) {
				el.innerHTML = el.innerHTML.replace(CRUMB_TAG, CRUMBS);
			}
		});
	}

	function initBreadcrumbs() {
        var currentPage = getFileName(location.pathname) + location.search
          , anchors = core.getElements(CRUMB_SELECTOR + ' a');

		core.each(anchors, function(a) {
            var thisFile = getFileName(a.href);

            if (currentPage === thisFile) {
                core.css.addClass(a, CURRENT);
            }
        });
		
		return anchors;
    }

	function initNextPrev(links) {
		var prev
		  , next
		  , nextPrev = core.getElements(NEXTPREV_SELECTOR);
		
		core.each(nextPrev, function(el){
			if (el) {
				el.innerHTML = NEXT_PREV;
			}
		});
		
		// Store the just-created links
		prev = core.getElements(PREV_SELECTOR)
		next = core.getElements(NEXT_SELECTOR)

		core.each(links, function(link) {

			if (currentPageIs.rightAfter(link) && link.href) {
			
				core.each(next, function(n) {
					n.href = link.href;
					core.css.addClass(n, ACTIVE_CLASS);
				});
				
			}
			else if (currentPageIs.rightBefore(link) && link.href) {
			
				core.each(prev, function(p) {
					p.href = link.href;
					core.css.addClass(p, ACTIVE_CLASS);
				});
				
			}
		
		});
	}
		
    function getFileName(path) {
        return path.substr(path.lastIndexOf('/') + 1) || 'index.aspx';
    }

	insertBreadcrumbs();
	breadcrumbLinks = initBreadcrumbs();
	if (breadcrumbLinks && breadcrumbLinks.length > 0) {
		initNextPrev(breadcrumbLinks);
	}
	
	
})();
