var displaydiv = 1;
var historyHTML = "";

if (!displaydiv) document.write("<select style='width:200px;' id='historylist' onchange='selectgo(this)'></select>");

var s = _infx.cookie.read('_infx_history');

// cookie stores a json array
var arr = new Array();
if (s == null || s == ''){ s = "[]"; }

try{
	eval("arr = "+s+";");
}catch(err){
	_infx.cookie.clear('_infx_history');
	arr = [];
}

var fnd = -1;
var loc = document.location;
for (var x=0;x<arr.length ;x++ ){
	if (loc == arr[x].href) fnd = x; // if in array already don't add it
}

if (fnd == -1){
	var title = document.title;
	title = title.replace(/[^a-zA-Z0-9 ]/g,'');

	// TRUNCATE TITLE
	if (title.length > 36) title = title.substring(0,33) + "...";
	arr.unshift({title:title,href:loc});
}

if (displaydiv){
	historyHTML += "<div class='history_frame'>";
	historyHTML += "  <div class='close' title='Close history' onmousedown='close_history();'> </div>";
	historyHTML += "  <div class='title'>Your Page History</div>";
	for (var x=0;x<arr.length ;x++ ){
		// 4 variations: alternate background / with/without a tick - on,off,tickon,tickoff
		var onoff = (x % 2)?"on":"off";
		var tickandonoff = (fnd==x)?'tick '+onoff:onoff;
		var ttitle=(fnd==x)?'You have viewed this page before':'';
		historyHTML += "  <div title='"+ttitle+"' class='"+tickandonoff+"' onclick=\"location.href='"+arr[x].href+"'\">"+arr[x].title+"</div>";
	}
	historyHTML += "	</div>";
}else{
	var mysel = rtnEl('historylist');
	mysel.options.length = 0;//clear array
	mysel.options[mysel.options.length] = new Option('Select a visited page...','',0,0);
	for (var x=0;x<arr.length ;x++ ){
		mysel.options[mysel.options.length] = new Option(arr[x].title,arr[x].href,0,0);
		if (loc == arr[x].href){loc=''}; // if array already don't add it
	}
}
 
var jarr=[];
// Truncate array to 10 items only
var y = (arr.length>=10)?arr.length-1:arr.length;
for (var x=0;x<y;x++){
	jarr.push('{title:"'+arr[x].title+'",href:"'+arr[x].href+'"}')
}

// store array back in cookie
_infx.cookie.create({ name:"_infx_history", days:2, value:"["+jarr.join(',')+"]" });

function selectgo(obj){
	var i = obj.selectedIndex;
	var t = obj.options[i].text;
	var v = obj.options[i].value;
	//alert("text="+t+" value="+v);
	document.location = v;
}

function open_history(){
	rtnEl('history_results').innerHTML = historyHTML;
}

function close_history(){
	rtnEl('history_results').innerHTML = '';
}
