// *** 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 PrintLoaderCL = Components.classes['@mozilla.org/gfx/printoptions;1'];
//var PrintLoaderUI = Components.interfaces.nsIPrintOptions;
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);
//document.focus.addFocusLinstener;


function history_drop_down(temp, tempV) {
var tempB = document.getElementById("HistoryDropDown");
//dump("****** HISTORY DROP DOWN : " + tempB + "\n");
	tempB.setAttribute("editable", false);
	tempB.setAttribute("label", temp);
	var report = tempB.getAttribute("label");
	tempB.appendItem(temp, tempV);
	//tempB.setAttribute("editable", true);
dump("** --" + report + "--*** report\n");
}

function hard_copy() {
//alert("DUMP?");
dump_stuff();
//window.content.print();
//window.openDialog("chrome://global/content/printdialog.xul");
const PrintLoaderCL = Components.classes['@mozilla.org/gfx/printoptions;1'];
const PrintLoaderUI = Components.interfaces.nsIPrintOptions;
window.openDialog("chrome://kiosk/content/kiosk-printdialog.xul");
//PrintDialog.createInstance.ShowPrintSetupDialog;
//window.openDialog();

}

 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 

dump("*******NEW PAGE SRC = " + site + " ******\n");
   Remember_last_page();
   //ed = document.getElementById("content");
dump("*******NEW PAGE ITEM = " + ed + " ******\n");
   //ed.setAttribute("src", site);
   document.getElementById("content").loadURI(site);
   _content.focus();
read_tooltip();

  }


 function url_bar() {
//dump_stuff();
	temp = document.getElementById("textInput");
	//var temp = document.getElementById("HistoryDropDown");
	site = temp.value;
	//var filter = /^file:/i;
	var filter = new RegExp(document.getElementById("kioskDefaults").getString("uriFilterRegexp"), "i");
	//alert(site + filter);
	//var site = temp.getAttribute("value");
	//focus = document.focus.current;
	if ((site.match(filter)) != null) {
	     //alert("Invalid URI type: " + site.match(filter) + " Valid types are http:");
	     site = "";
	}
	if (site == "") { return; }
	dump("urlBAR" + site +"\n");
	//dump("**** current focus " + focus + " **\n");
	new_page(site);
}

  function shut_down() {
//dump_stuff();
   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;
	//history_drop_down(LastPageVisited);
 }
//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 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");
	//history_drop_down(temp, temp);

 }

// ***** Somehow this messes up the history back function *******
// ***** Only call for testing purposes                   *******
 function dump_stuff() {
alert("inside DumpStuff1");   
 // var i=1;
  var j=0;
  //var test_item = PrintLoader;
  //var baseName = "testString";
  //var baseValue = "testValue";
  var ed = document.getElementById("VKboard1");
  //menu_items = document.getElementById("base-sites");
  //eddie = ed.stringBundle.getSimpleEnumeration("teststrings").getNext();
 //eddie = Components.classes['@mozilla.org/intl/stringbundle;1'].getService();
  //eddie = eddie.QueryInterface(Components.interfaces.nsIStringBundle);
alert("inside DumpStuff");   
dump("**-AAAAAdump NEW---**" + ed.id + "\n");
 for (var list in ed) {
   ++j;
   dump(list + "*ENUM*" + j + "\n");
 }

//  do {
//  	eddie = ed.getString(baseName + i);
//	eddieV = ed.getString(baseValue + i);
//	++i;
//	dump("**-dumpIT---**" + eddie  + "\n");
	//New_items = document.createElement("menuitem");
	//New_items.setAttribute("label", eddie);
	//menu_items.appendChild(eddie, eddie);
//	history_drop_down(eddie, eddieV);
//	dump("**-dump---**" + eddie + eddieV + "\n");
//   } while (ed.getString(baseName + i));

   //dump("-----** \n" + ed +"--**WE ARE BIGFocus\n");
 }


