// start - configurable variables

var displayCookie = "_swbc";
var pageCookie = "_swbcpage";
var homepage = "index.html";

// end - configurable variables


function breadcrumbs(){
  sURL = new String;
  bits = new Object;
  var x = 0;
  var stop = 0;
  var output = "<a href=\"/\">Home</a> &#187; ";
  sURL = location.href;
  sURL = sURL.slice(8,sURL.length);
  chunkStart = sURL.indexOf("/");
  sURL = sURL.slice(chunkStart+1,sURL.length)
  while(!stop){
    chunkStart = sURL.indexOf("/");
    if (chunkStart != -1){
      bits[x] = sURL.slice(0,chunkStart)
      sURL = sURL.slice(chunkStart+1,sURL.length);
    }else{
      stop = 1;
    }
    x++;
  }
  for(var i in bits){
    output += "<a href=\"";
    for(y=1;y<x-i;y++){
      output += "../";
    }
    output += bits[i] + "/\">" + bits[i] + "</a> &#187; ";
  }
  document.write(output + document.title);
}

var expdate = new Date();
expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

function setBCCookieArray(cookieValue, isRoot)
{
   var expireNow = new Date();
   var sURL = new String;
   var bits = new Object;
   var x = 0;
   var stop = 0;
   sURL = location.href;
   sURL = sURL.slice(8,sURL.length);
   chunkStart = sURL.indexOf("/");
   sURL = sURL.slice(chunkStart+1,sURL.length);
   while(!stop)
   {
      chunkStart = sURL.indexOf("/");
      if (chunkStart != -1)
      {
         bits[x] = sURL.slice(0,chunkStart)
         sURL = sURL.slice(chunkStart+1,sURL.length);
      }
      else
      {
         stop = 1;
      }
      x++;
   }

   if (isRoot) //the main links on the home page
   {
      // delete all the cookies and set a cookie for this page
      deleteBCCookies();
      
      // set a new cookie for this page
      setCookie (displayCookie + '0', cookieValue, expdate);

      // also set the page
      setCookie (pageCookie + '0', sURL, expdate);
   
      return;
   }
   
   
   var cookie_arr = new Array();
   
   // get previous cookies
   var i = 0;
   while (getCookie(displayCookie + i) != null) 
   {
      cookie_arr[i] = getCookie(displayCookie + i);
      i++; 
   }
   
   // check if back button was clicked
   // if back button was clicked then the cookie array should contain the value of the current page
   
   var j = 0;
   var back_clicked = 0;
   for (j = 0; j < cookie_arr.length; j++)
   {
      if (cookie_arr[j] == cookieValue) // since the current passed on value is in cookie array set the back_clicked
      {
         back_clicked = 1;
         break; 
      }
   }
   
   // clear all the cookies from the current page onwards
   i = 0;
   for (i = j+1; i < cookie_arr.length; i++) 
   {
      setCookie (displayCookie + i, "", expireNow);
      setCookie (pageCookie + i, "", expireNow);      
   }     
   
   // no back button was clicked, we found a new page, so add the value to the cookie array
   if(back_clicked == 0)
   {
      var curr_location = cookie_arr.length;
      cookie_arr[curr_location] = cookieValue;

      // set a new cookie for this page
      setCookie (displayCookie + curr_location, cookie_arr[curr_location], expdate);

      // also set the page
      setCookie (pageCookie + curr_location, sURL, expdate);
   }
}

function getDispCookieArray()
{
   var i = 0;
   var cookie_arr = new Array();
   while (getCookie(displayCookie + i) != null) 
   {
      cookie_arr[i] = getCookie(displayCookie + i);
      i++;
   }
   return cookie_arr;
}

function getPageCookieArray()
{
   var i = 0;
   var cookie_arr = new Array();
   while (getCookie(pageCookie + i) != null) 
   {
      cookie_arr[i] = getCookie(pageCookie + i);
      i++;
   }
   return cookie_arr;
}

function deleteBCCookies()
{
   var cookie_arr = getDispCookieArray();
   var expireNow = new Date();
   for (var i = 0; i < cookie_arr.length; i++) 
   {
      setCookie (displayCookie + i, "", expireNow);
      setCookie (pageCookie + i, "", expireNow);      
   }        
}

function setCookie(name, value, expires, path, domain, secure) 
{
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function showBC()
{
   var bc_display_arr = getDispCookieArray();
   var bc_page_arr = getPageCookieArray();
   
   var output = "<a class=\"crummy\" href=\"" + homepage + "\">HOME</a> &#187; ";
   
   // create showPage() function link to clear cookies
   for (var i = 0; i < bc_display_arr.length-1; i++) 
   {
      output += "<a class=\"crummy\" href=\"javascript:showPage('";
      var link_string = bc_page_arr[i] + "'," + i + ");\">" + bc_display_arr[i] + "</a> &#187; ";
      output += link_string;
   }
   // the last one is not a link
   output += "<font class=\"crummy\"> " + bc_display_arr[bc_display_arr.length-1] + "</font>";
   
   // print the breadcrumb trail
   document.write(output);   
}

function showPage(pagename, index)
{
   var cookie_arr = getDispCookieArray();
   var expireNow = new Date();
   //delete all cookies from the clicked breadcrumb
   for (var i = index; i < cookie_arr.length; i++) 
   {
      setCookie (displayCookie + i, "", expireNow);
      setCookie (pageCookie + i, "", expireNow);      
   }        
   window.location.href=pagename;
}


