// 
// Copyright (C) 6.6.2000 by Applepie Solutions Ltd.
// http://www.applepiesolutions.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// This javascript code defines the wapreview user interface and 
// manages communications with the wapreview applet
//
var aplpidbg = 0;
//var spd=30; //scrolling speed
var scrn_w=120; //screen width
var scrn_h=450;//window.innerHeight;
var fnt_h=10;//font height
var pos=60;// initial top position
var off_t=34;//top offset
var off_b=200;//bottom offset
var upr=-1000;//upper offset (not used)
var lwr=pos;// lower limiter
//var tim;    // timer variable for scrolling
var off_l=20;   // left offset
var fnt_f="verdana"; // font name
var bgc_t="gray"; // top backgroundcolor
var bgc_b="white"; // bottom backgroundcolor
var bgc_s="#00CE00"; //6ABA7B"; // scrn backgroundcolor 106,186,123
var fgc_s="black"; // scrn forecolor
var cardtimer;
var loadtimer;
//what browser are we using
//var dyn=(document.layers||document.all)?true:alert("Sorry... support for later versions of navigator and explorer only.");
var nav=(document.layers);
var iex=1;//(document.all);
var resetgroup1="//";
var resetgroup2="//";
var resetgroup3="//";
var resetgroup4="//";
var resetgroup5="//";
var resetgroup6="//";
var resetgroup7="//";
var resetgroup8="//";
var resetgroup9="//";


function reval(cmd){
  eval(cmd+';');
}
function choose(){
  // do nothing;
  true;
}


function flip(group,itemno,linktext,selected,resetgroup,allowmulti){
  // This function simulates a (possibly multichoice) selection
  // list, by highlighting the selected option(s).
  var elinktext=linktext;
  var resetcode="//";
  var clickstart='selectitem(\''+group+'\','+itemno+',\''+linktext+'\',';
  var resetstart='flip(\''+group+'\','+itemno+',\''+linktext+'\',';
  if(selected){
      elinktext="<b>"+linktext+"</b>";
      resetcode=resetstart+"false,false,"+allowmulti+");"
  }
  var cntnt = '<a href="javascript:choose()" onmousedown="'+clickstart+!selected+','+!allowmulti+','+allowmulti+');">'+elinktext+'</a>';
  if(document.layers){
    var doc = 'document.aplpicard.document.nav'+group+'_'+itemno+'.document';
    eval(doc+'.open();');
    eval(doc+'.write(cntnt);');
    eval(doc+'.close();');
  }
  if(document.all){
    var doc='iex'+group+'_'+itemno;
    eval(doc+'.innerHTML=cntnt');
  }
  if(resetgroup){
     eval(eval("reset"+group+";")+"//");
     eval("reset"+group+"=resetcode;");
  }

}

function selectitem(group,itemno,ltext,selected,resetgroup,allowmulti){
  // call the flip() method after contacting the applet
  // the value of selected might be wrong, so we reset it
  selected = document.wapplet.selectitem(group,itemno);
  flip(group,itemno,ltext,selected,resetgroup,allowmulti);
  return(true);
}

function beginnavigation(){
//  if(iex)document.changeurl.navto.focus();
//  if(nav)window.focus();
}


function cardtimeout(){
//  alert("in timeout");
  reval(document.wapplet.ontimer());
}

// could use this function if the applet retrieved
// the deck on a separate thread.
//function loadtimeout(msg){
//  loadmsg = '<br><br><center><b>'+msg+'</b></center>';
//  if(iex)aplpinav.innerHTML=loadmsg; 
//  if(nav){
//    document.aplpinav.document.open();
//    document.aplpinav.document.write(loadmsg);
//    document.aplpinav.document.close();
//  }
//  ltimeout = 'loadtimeout("'+ msg+ '.'+'\")';
//  loadtimer=setTimeout(ltimeout,1);
//}
/*
function showRawData() {
	var srcWindow = window.open("", "subWindow",
			"toolbar=no, location=no, directories=no, status=no, menubar=no," +
			"scrollbars=yes, resizable=yes, width=400, height=300");
	srcWindow.focus();  srcWindow.document.open();
   srcWindow.document.write('<HTML><HEAD><TITLE>' +
		document.wapplet.currentURL() + '</TITLE></HEAD><BODY BGCOLOR="#FFFFFF"><P><PRE>\n' +
		document.wapplet.getCardData() + '\n</PRE></P></BODY></HTML>\n');
   srcWindow.document.close();
}
*/
function showSource() {
	var srcWindow = window.open("", "subWindow",
			"toolbar=no, location=no, directories=no, status=no, menubar=no," +
			"scrollbars=yes, resizable=yes, width=400, height=300");
	srcWindow.focus();  srcWindow.document.open();
   srcWindow.document.write('<HTML><HEAD><TITLE>' +
		document.wapplet.currentURL() + '</TITLE></HEAD><BODY BGCOLOR="#FFFFFF"><P><PRE>\n' +
		document.wapplet.getSource() + '\n</PRE></P></BODY></HTML>\n');
   srcWindow.document.close();
}

var scrollStep = 4;
var scrollSpeed = 30;	// scrolling speed
var scrollTimer; 			// timer variable for scrolling
var scrollBar;
var maxGaugeHeight;
var gauge;
var region;
var clientHeight;
var scrollHeight;
var gaugeHeight;
var bottomMenu;
var titleArea;

function showCard(){
	var cardData = document.wapplet.getCardData();
	var titlePrefix = '<center>';
	var titleSuffix = '</center>';
	var beforeTitle = cardData.indexOf(titlePrefix) + titlePrefix.length;
	var afterTitle = cardData.indexOf(titleSuffix);
	var cardTitle = cardData.substr(beforeTitle, afterTitle - beforeTitle);

	hideForm();  hideMenu();
	if(cardtimer){
		clearTimeout(cardtimer);
	}

	scrollBar = document.getElementById('bar');
	maxGaugeHeight = scrollBar.clientHeight;
	gauge = document.getElementById('ind');
	region = document.getElementById('cardData');
	clientHeight = region.clientHeight;
	titleArea = document.getElementById('cardTitle');

	cardData = '<form>' + cardData.substr(afterTitle + titleSuffix.length);
	titleArea.innerHTML=cardTitle;
	region.innerHTML=cardData;
	region.style.backgroundColor="#00CE00";

	var t = document.wapplet.getCardTimerValue();
	if(t >= 0){
		cardtimer=setTimeout("cardtimeout()",t);
	}
  
 	region.scrollTop = 0;
 	bottomMenu = document.getElementById('bottomArea');
	scrollHeight = region.scrollHeight;
	if (clientHeight < scrollHeight) {
		gaugeHeight = parseInt(maxGaugeHeight * clientHeight / scrollHeight);
		gauge.style.height = gaugeHeight + "px";
 	}
 	
 	bottomMenu.style.visibility = "visible";
 	do_scroll(0);
}

function post(i){
  document.wapplet.post(i);
  reval(document.wapplet.onenterforward());
  showCard();
}

function setCard(url){
  beginnavigation();
  //loadtimer=setTimeout('loadtimeout("loading.")',1);
  // document.wapplet.debug(true);
  document.wapplet.setCard(url);
  //clearTimeout(loadtimer);
  reval(document.wapplet.onenterforward());
  showCard();
}

function reloadDeck(){
  beginnavigation();
  document.wapplet.reloadDeck();
  reval(document.wapplet.onenterforward());
  showCard();
}

function prevCard(){
  beginnavigation();
  document.wapplet.prevCard();
  reval(document.wapplet.onenterbackward());
  showCard();
}

function nextCard(){
	beginnavigation();
	document.wapplet.nextCard();
	reval(document.wapplet.onenterforward());
	showCard();
}

function setVar(nam,val){
  return document.wapplet.setVar(nam,val);
}

function getVar(nam){
  return document.wapplet.getVar(nam);
}

function getHref(i){
  return document.wapplet.getHref(i);
}

function goHome(){
  var home = document.wapplet.homeurl();
  if(!home){
    home='http://www.applepiesolutions.com/i.wml';
  }
  setCard(home);
}

function scroll_dn(){
	do_scroll(+1);  scrollTimer = setTimeout("scroll_dn()", scrollSpeed);
}

function scroll_up(){
	do_scroll(-1);  scrollTimer = setTimeout("scroll_up()", scrollSpeed);
}

function do_scroll(dir){
	var gaugeOffset;
	if (clientHeight < scrollHeight) {
 		var scrollTop = region.scrollTop + scrollStep * dir;
 		if (scrollTop > scrollHeight - clientHeight) {
 			scrollTop = scrollHeight - clientHeight;
 		}
 		else if (scrollTop < 0) {
 			scrollTop = 0;
 		}
		gaugeOffset = (maxGaugeHeight - gaugeHeight) * scrollTop / (scrollHeight - clientHeight);
		gauge.style.top = parseInt(scrollBar.style.top) + parseInt(gaugeOffset) + "px";
		scrollBar.style.visibility = "visible";
 		gauge.style.visibility = "visible";
		region.scrollTop = scrollTop;
	}
 	else {
 		scrollBar.style.visibility = "hidden";
 		gauge.style.visibility = "hidden";
 	}
}

function no_scroll(){
	clearTimeout(scrollTimer);
}

function Save() {
	this.menu = this.form = null;
}

var leftSoftKey, leftButton, rightSoftKey, rightButton;

var leftSoftKeySave = new Save(), leftButtonSave = new Save();

var rightSoftKeySave = new Save(), rightButtonSave = new Save(), titleAreaSave;

var fontHeightSave, scrollTopSave, innerHtmlSave, cardTitleSave, scrollHeightSave;

var menuOption = 0, menuShown = 0, formShown = 0;

function showMenu() {
	if (!menuShown) {
		menuShown = 1;
		scrollTopSave = region.scrollTop;
		innerHtmlSave = region.innerHTML;
		fontSizeSave = region.style.fontSize;
		cardTitleSave = titleArea.innerHTML;
		scrollHeightSave = scrollHeight;
		titleArea.innerHTML = "==== MENU ====";
		region.innerHTML = ''
						+'<div><a href="javascript:hideMenu();do_scroll(-1000);">Początek strony<a></div>'
						+'<div><a href="javascript:hideMenu();do_scroll(+1000);">Koniec strony<a></div>'
						+'<div><a href="javascript:hideMenu();reloadDeck();">Przeładuj stronę<a></div>'
//						+'<div><a href="javascript:hideMenu(); nextCard();">Następna strona<a></div>'
						+'<div><a href="javascript:hideMenu();goHome();">Strona startowa<a></div>'
						+'<div><a href="javascript:showForm();">Inna witryna<a></div>'
						+'<div><a href="javascript:hideMenu();showSource();">Źródło w WML<a></div>'
						;
		leftSoftKey = document.getElementById('menuButton');
		leftButton = document.getElementById('lsk');
		rightSoftKey = document.getElementById('backButton');
		rightButton = document.getElementById('rsk');
		region.childNodes[menuOption].style.background = 'lightgreen';

		rightButtonSave.menu = rightButton.href;
		rightButton.href = rightSoftKey.href = 'javascript:hideMenu();';
		rightSoftKeySave.menu = rightSoftKey.innerHTML;
		rightSoftKey.innerHTML = 'Powrót';

		leftButtonSave.menu = leftButton.href;
		leftButton.href = leftSoftKey.href = 'javascript:goOption();';
		leftSoftKeySave.menu = leftSoftKey.innerHTML;
		leftSoftKey.innerHTML = 'Wybór';

		region.style.fontSize = "16px";
		scrollHeight = clientHeight;
		region.scrollTop = 0;
 		do_scroll(0);
 	}
}

function showForm() {
	if (!formShown) {
		region.style.visibility = "hidden";

		rightButtonSave.form = rightButton.href;
		rightButton.href = rightSoftKey.href = 'javascript:hideForm();';
		rightSoftKeySave.form = rightSoftKey.innerHTML;
		rightSoftKey.innerHTML = 'Menu';

		leftButtonSave.form = leftButton.href;
		leftButton.href = leftSoftKey.href = 'javascript:gotoURL();';
		leftSoftKeySave.form = leftSoftKey.innerHTML;
		leftSoftKey.innerHTML = 'Pobierz';

		document.getElementById('url').style.visibility = "visible";
		titleAreaSave = titleArea.innerHTML;
		titleArea.innerHTML = "Podaj adres witryny:";
		formShown = 1;
	}
}

function hideForm() {
	if (formShown) {
		region.style.visibility = "visible";

		rightButton.href = rightSoftKey.href = rightButtonSave.form;
		rightSoftKey.innerHTML = rightSoftKeySave.form;

		leftButton.href = leftSoftKey.href = leftButtonSave.form;
		leftSoftKey.innerHTML = leftSoftKeySave.form;

		document.getElementById('url').style.visibility = "hidden";
		titleArea.innerHTML = titleAreaSave;
		formShown = 0;
	}
}

function gotoURL(newUrL) {
	var newUrl = document.getElementById('newUrl').value;
	if (newUrl != "http://") {
		hideForm();  hideMenu();  setCard(newUrl);
	}
}

function goOption() {
//	eval(region.childNodes[menuOption].firstChild.href);
	eval(region.childNodes[menuOption].childNodes[0].href);
}


function hideMenu() {
	if (menuShown) {
		menuShown = 0;
		titleArea.innerHTML = cardTitleSave;

		rightButton.href = rightSoftKey.href = rightButtonSave.menu;
		rightSoftKey.innerHTML = rightSoftKeySave.menu;

		leftButton.href = leftSoftKey.href = leftButtonSave.menu;
		leftSoftKey.innerHTML = leftSoftKeySave.menu;

		region.style.fontSize = fontSizeSave;
		region.innerHTML = innerHtmlSave;
		region.scrollTop = scrollTopSave;
		scrollHeight = scrollHeightSave;
 		do_scroll(0);
 	}
}

function move_dn(){
	if (menuShown) move_bar(+1);
}

function move_up(){
	if (menuShown) move_bar(-1);
}

function move_bar(dir){
	var children = region.childNodes;
	var barBkgnd = children[menuOption].style.background;

	children[menuOption].style.background = '';
	if ((menuOption += dir) < 0) menuOption += children.length;
	else if (menuOption >= children.length) menuOption = 0;
	children[menuOption].style.background = barBkgnd;
}

