// *** Copyright (c)2002 Jim Massey. All rights reserved  ***********
// *** massey@stlouis-shopper.com **************
// *** No Warranty - certified to work with nothing ***
// *** developed to work with mozilla kiosk **** 

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPconnect");
var NavObj = Components.classes['@mozilla.org/webshell;1'].getService(Components.interfaces.nsIWebProgressListener);
var NavLoader = Components.classes['@mozilla.org/docloader;1'].getService(Components.interfaces.nsIProgressEventSink);
var LastPageVisited = "";
var history_kludge = new Array();
var history_kludge_count = 0;
var history_kludge_boolean = 0;
var history_back_count = 0;
var load_flag = 0;
timercount = 0;
window.addEventListener("unload", meter_on, true);
window.addEventListener("load", meter_off, true);
window.addEventListener("click", timer_reset, true);


  function new_page(site) {
   //This is called from a xul interface. 'site' is an URL 
   Remember_last_page();
   ed = document.getElementById("content");
   ed.setAttribute("src", site);
   _content.focus();
  }

 function url_bar() {
	temp = document.getElementById("textInput");
	site = temp.value;
	dump("urlBAR" + site +"\n");
	new_page(site);
}

  function shut_down() {
   window.close();
  }

// **** flip page *****
  function testing() {
   ed = document.getElementById("content");
   tempA = window._content.location.href;
   
   ed.setAttribute("src", LastPageVisited);
   //dump(LastPageVisited+"--test\n");
   LastPageVisited = tempA;
   reflect_action();
  }

// **** mobetter history back function  **********
 function history_back() {
 ed = document.getElementById("content");
	if (history_kludge_boolean == 1) {
	  Remember_last_page();
	  history_kludge_boolean = 0;
	  history_back_count = 1;
	} else {
	  history_back_count++;
	}
	if ( history_back_count >= history_kludge.length) {
	  history_back_count = 0;
	}

	ed.setAttribute("src", history_kludge[history_back_count]);
	dump ("BACK TO: " + history_back_count + " - " + history_kludge[history_back_count] + "\n");
}

// **** History forward *********
function history_forward() {
 if (history_back_count == 0) {
	return;
 } else {
	history_back_count--;
	ed = document.getElementById("content");
	ed.setAttribute("src", history_kludge[history_back_count]);
 }
}

// **** set the Filp-to message and other good things ********
  function reflect_action() {
  history_klugde_count = history_kludge.length;
  history_kludge_boolean = 1;
  history_back_count = 0;
  ed = document.getElementById("content");
  
  ed_main = document.getElementById("progress_text");
  ed_main.setAttribute("value", "flip-to:"+_content.location.href);
  dump(_content.location.href+"--reflect-\n");
  dump(_content.document.location+"*@*@*\n");
  }

// **** Add uri just left to history *****
 function Remember_last_page() {
//dump(history_kludge(1) + "ADD IT TO HISTORY  \n");
var tempA = window._content.location.href;
var tempB = history_kludge[0];
dump(tempA +"--"+tempB+"--REMEMBER\n");
 if (tempB != tempA) {
      dump("ADDED TO HISTORY \n");
      session_history_kludge();
	reflect_action();
	LastPageVisited = tempA;
 }
//dump_stuff();
  var temp = window._content.location.href;
  const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
  if (NavObj.STATE_NETWORK) {
   dump(" \n"+temp+history_kludge.length+history_kludge[0]+"---Progress RememberLast\n********\n"+ NavLoader.onProgress +"!@#$ \n");
  }
 }

function meter_on() {
	dump("****New Meter ON\n");
	meter = document.getElementById("progress");
	meter.setAttribute("mode", "undetermined");
	throbber = document.getElementById("throbber");
	throbber.setAttribute("src", "load-throb-anim.gif");
}

function meter_off() {
	dump("****New Meter OFF\n");
	meter = document.getElementById("progress");
	meter.setAttribute("mode", "normal");
	throbber = document.getElementById("throbber");
	throbber.setAttribute("src", "view-throb.png");
}

 function session_history_kludge() {
 //This SUCKS. XPConnect provides this - if I could understand how it works
 // *** This does not suck as bad as I once thought
   var temp = window._content.location.href;
   history_kludge_boolean = 0;
   if (history_kludge.length < 26) {
      history_kludge.unshift(temp);
   } else {
     history_kludge.pop();
      history_kludge.unshift(temp);
   }
 dump("HISTROY KLUDGE "+temp+"\n");
 }

// ***** Somehow this messes up the history back function *******
// ***** Only call for testing purposes                   *******
 function dump_stuff() {
  var i=0;
  var test_item = NavObj;
  ed = document.getElementById("browser1");
 dump("**-dump---**" + test_item + "\n");
 for (var list in window) {
   i++;
   dump(list+"\n");
 }
   //dump("-----** \n" + test_item +"--**Focus\n");
 }

// **** reset kiosk to initial startup state *********
 function kiosk_reset() {
	site = "http://stlouis-shopper.com/";
	history_kludge.length = 0;
	LastPageVisited = site;
	clearTimeout(timer);
	
	ed = document.getElementById("content");
  	ed.setAttribute("src", site);
	
	ed_main = document.getElementById("progress_text");
	ed_main.setAttribute("value", "flip-to:"+site);
	
	history_kludge[0] = site;
	
	dump("** history length = " + history_kludge.length + " ***\n");
}

// **** kiosk timer reset 5 min reset********
function timer_reset() {
	dump("***$$$ Timer RESET A *****\n");
	if (timercount >= 1) {
		clearTimeout(timer);
		dump("********* CLEARED Timer " + timer + " ****\n");
	}
	timer = setTimeout("kiosk_reset()", 20000);
	timercount = timer
	dump("***$$$ Timer RESET B " + timercount + " *****\n");
	

}
