// adjust this value to indicate the number of pages to display in the dropdown list
var cnt = 10;
// set this value to indicate the default text to display in the dropdown list
//NOTE - don't specify single quote or double quote characters
var defaultText = "Recently viewed pages";
//
// *******************************************
// you shouldn't need to modify anything below
// *******************************************
var cookieName = 'usbox-breadcrumbs';

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  //return null;
  return "";
}
function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = "/";
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

var path = "";
var href = document.location.href;
var title = document.title;
if (title == "")
{
	// handle title-less pages
	var m = 0;
	var tmp = href;
	for(m = tmp.length-1; m >= 0; m--) 
	{
		if (tmp.charAt(m) == "/") 
		{
			title = tmp.substring(m,tmp.length)
			title = title.replace("/","");
			break;
		}
	}
}
var s = "";

//if (document.cookie == "")
if (GetCookie(cookieName) == "")
{
	s = href + '!' + title + "|";
} 
else
{
	//s = document.cookie;
	s = GetCookie(cookieName);
	var i = 0;
	var n = 0;
	var l = 0;
	//see how many pages we have
	for(i = 0; i < s.length; i++) 
	{
		if (s.charAt(i) == "|")
		{
			n++;
			if (i < s.length)
			{
				l = i; // capture the last 
			}
		}
	}
	
	if (s.substring(0, href.length).toLowerCase() == href.toLowerCase())
	{
	// current page - don't do anything
	}
	else
	{
		if (n < cnt)
		{
			//append mode (haven't saved enough pages yet)
			s = href + "!" + title + "|" + s;
		}
		else
		{
			//shift and replace
			s = href + "!" + title + "|" + s.substring(0,l);
		}
	}
}
//document.cookie = s;
SetCookie(cookieName, s);
writeCode();

function Jump(selectList)
{
	if (selectList.selectedIndex < 1)
	{
		//do nothing
	}
	else
	{
		window.location.href = selectList.options[selectList.selectedIndex].value;
	}
}

function writeCode()
{
	var alldivs = document.all.tags("div");
	var j = 0;
	var i = 0;
	var s = "";
	var c = "";
	var v = "";
	var b = 0;
	var t = "";
	//if (document.cookie != "")
	if (GetCookie(cookieName) != "")
	i
	{
		//c = document.cookie;
		c = GetCookie(cookieName);
		s = '<select name="myHistory" onChange="javascript:Jump(this);">'
		s = s + '<option value="" selected>' + defaultText + '</option>';
		//build the options
		i = 0;
		for(i = 0; i < c.length; i++) 
		{
			if (c.charAt(i) == "!")
			{
				b = i;
			}
			if (c.charAt(i) == "|")
			{
				v = v.replace("|", "");
				v = v.replace("!", "");
				t = t.replace("|", "");
				t = t.replace("!", "");
				
				s = s + '<option value="' + v + '">' + t + '</option>';
				v = "";
				t = ""
				b = 0;
			}
			else
			{
				if (b == 0)
				{
					v = v + c.charAt(i);
				}
				else
				{
					t = t + c.charAt(i);
				}
			}
		}
		s = s + '</select>'
		//find the div and write the code
		for (j = 0; j < alldivs.length; j++)	{
			if (alldivs[j].id.toLowerCase() == 'trail')	
			{
				alldivs[j].innerHTML = s;
				break;
			}
		}
	}
}
