// *** 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;

function window_size() {
	height = screen.availHeight - 25;
	width = screen.availWidth - 10;
	//dump("*** WINDOW SIZE IS " + height + " : " + width + "***\n");
	window.resizeTo(width, height);
}

  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);
   //the line below depends on the browser.xml xbl binding method loadURI
   document.getElementById("content").loadURI(site);
   _content.focus();
  }

 function url_bar() {
	temp = document.getElementById("textInput");
	site = temp.value;
	var RegexpControl = document.getElementById("kioskDefaults");
	//var filter = /^file:|^chrome:/i;
	var filter = new RegExp(document.getElementById("kioskDefaults").getString("uriFilterRegexp"), "i");
	//alert(site + filter);
	if ((site.match(filter)) != null) {
	   if (RegexpControl.getString("uriFilterErrMsg") != null) {
	     alert("Invalid URI type: " + site.match(filter) + "\n" + RegexpControl.getString("uriFilterErrMsg"));
	    }
	     site = "";
	}
	if (site == "") { return; }
	dump("urlBAR" + site +"\n");
	new_page(site);
}

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

// **** Hard copy function ****
 function hard_copy() {
	window.content.print();
 }

// **** 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");
 if (document.getElementById("progress_text") == null) { return; }
  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();

 }



 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");
 }


