// JavaScript history bar 
// (c) Copyright Chris Bolt 2009
// Implemented for Manukau City Council Towards 2060
// Stores a history of URLs and titles in a cookie and writes a history menu to the page.
// Implemented in JavaScript so that the feature can work offline without requiring server components.
// Requires JQuery and json2.js
var HistoryBar = {
	title:'',
	capitalize:false,
	cookiePath:'/',
	history:[],
	containerId:'historyspan',
	maxWidth:620,
	doRecord:true,
	
	writeCookie : function  (name, value, hours, path) {
	  var expire = "";
	  if(hours != null) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	  }
	  if(path != null) {
		path = "; path=" + path;
	  }
	  document.cookie = name + "=" + escape(value) + path + expire;
	},
	
	readCookie : function  (name) {
	  var cookieValue = "";
	  var search = name + "=";
	  if(document.cookie.length > 0) { 
		var offset = document.cookie.indexOf(search);
		if (offset != -1) { 
		  offset += search.length;
		  var end = document.cookie.indexOf(";", offset);
		  if (end == -1) end = document.cookie.length;
		  cookieValue = unescape(document.cookie.substring(offset, end))
		}
	  }
	  return cookieValue;
	},
	
	go : function(i) {
		if (this.history[i]) {
			// shorten array to this index
			while (this.history.length > i+1) this.history.pop();
			this.writeCookie('HistoryBar', JSON.stringify(this.history), null, this.cookiePath);
			// redirect to index
			document.location.replace(this.history[i].l);
		}
		// prevent default
		return false;
	},
	
	init : function() {
		var location = String(document.location);
		if (this.capitalize) this.title = this.title.toUpperCase(); 
		var serialized = this.readCookie('HistoryBar');
		this.history = (serialized) ? JSON.parse(serialized) : [];
		var n = this.history.length;
		if (!n) this.history = [];
		var changed = false;
		// record data
		if (this.doRecord) {
			if (!n || (this.history[n-1] && this.history[n-1]['l'] != location)) {
				this.history.push({'l':location,'t':this.title});
				changed = true;
			}
			// cookie data cannot be more than 4000 characters
			if (escape(JSON.stringify(this.history)).length > 4000) {
				// shift one off the start
				this.history.shift();
			}
		}
		// draw history trail
		var a = [];
		n = this.history.length;
		for (i=0; i<n; i++) {
			if (i+1==n) {
				a.push('<a class="current">'+this.history[i]['t']+'</a>');	
			} else {
				a.push('<a href="'+this.history[i].l+'" title="Return to '+this.history[i].t+'" onclick="return HistoryBar.go('+i+')">'+this.history[i].t+'</a>');	
			}
		}
		document.getElementById(this.containerId).innerHTML = a.join(' : ');
		// fix the width of the trail
		var a = document.getElementById('historybar').getElementsByTagName('A');
		var l = 0;
		var v = a.length - 1
		for (var i=v; i>=0; i--) {
			var tl = a[i].offsetWidth + 15;
			if (l + tl > this.maxWidth) {
				a[i].style.display = 'none';
				a[i].parentNode.removeChild(a[i].nextSibling);
			}
			l += tl;
		}
		// write cookie if there have been changes
		if (changed && this.doRecord) this.writeCookie('HistoryBar', JSON.stringify(this.history), null, this.cookiePath);
	}
	
}

$(document).ready(function(){HistoryBar.init();});