
	var ngwwbook = '';
	var ngwwpage = '';
	var ngwwtocx = true;
	
	var lcfrom = '';
	var lcto = '';
	var lcflag = false;
	
	var atrace = [];
	
	// process bar
	var ngwwprobar = 0;
	var ngwwjaxgif10 = '<img id="jaxgif10" src="mod/ng1ww/img/ajax10red.gif" alt="BG">';
	var ngwwjaxgif11 = '<img id="jaxgif11" src="mod/ng1ww/img/ajax-loader_square.11.gif" alt="BG">';
	
	var tree2 = '';

	$(document).ready(function(){
	
		if (ngwwtocx) {
			if (document.getElementById('ng1wwtcnt')) {
				ngwwTreeB();
				document.getElementById('ng1wwtcnt').style.display="block";
				// ngwwtocx = false;
			}
		}
		
		if (document.getElementById('ng1wwchaps1')) { 
		// devnote: #ng1wwchaps unique true is strict required
			$('#ng1wwchaps1').treeview({
				collapsed: true,
				unique: true
			});
			
			$('.ng1wwc20click').click( function() {
				var me = '#' + $(this).attr('id');
				$(me).removeClass('ng1wwc20click'); // doesnt help
				$(me).unbind('click');
				var done = $(me).hasClass('done');
				if (!done) {
					// fetch stage 2 data
					var book = this.textContent.substr(3);
					//	#ng1ww20L...
					var b2x = me.substr(9);
					$(this).append(ngwwjaxgif10);
					ngwwCS20(b2x);
				}
			})
			
		}
		
		$("#ng1wwpageseq").sortable();
		$("#ng1wwpageseq").disableSelection();
				
		$("#ng1wwsequencer").click( function() {
			var rs = $("#ng1wwpageseq").text();
			var ul = $("#ng1wwsequencer").attr("href");
			var ula = ul + '/order/' + escape(rs);
			$("#ng1wwsequencer").attr("href", ula);
			 } );

		ngSearchView();
		
	});
	
	function ngwwTreeB() {
		$('#ng1wwtoc').treeview({
			persist: "cookie",
			cookieId: "ng1wwx",
			collapsed: true,
			unique: true,
			url: "index.php?module=ng1ww&xmit=ng1wwajx"
		});
	}
	function ngwwCS20(b1x) {
		$.ajax({
			type: "GET",
			url: "ng1ww/axop/c2s21/b1x/" + b1x,
			// url: "index.php",
			// data: "module=ng1ww&axop=c2s21" + "&book=" + book,
			success: function(reply) {
						ngwwSC20(reply);
					}
		});
	}
	function ngwwSC20(reply) {
		var ret = reply.split(':',1);
		var b1x = ret[0];
		var cnt = reply;
		var wo1 = '#ng1wwchaps1';
		// 	jquery obviously doesnt accept dots in id
		//	var wo2 = '#ng1ww20U' + book.replace(/\./g,'DOT');
		var wo2 = '#ng1ww20U' + b1x;
		var wo3 = '#ng1ww20L' + b1x;
		var wo = wo1 + ' ' + wo2;
		$(wo3).addClass('done');
		$('#jaxgif10').remove();
		var plus = $(reply).appendTo(wo);
		$('#ng1wwchaps1').treeview({
			add: plus
		});
	}
	function ng1wwc31click(me) {
		// flqrange
				var mel = '#ng1ww31L' + me;
				var meu = '#ng1ww31U' + me;
				var done = $(meu).hasClass('done');
				if (!done) {
					// fetch flqranges
					$(mel).append(ngwwjaxgif10);
					ngwwCS31(me);
				}
			}
	function ng1wwc32click(me) {
		// flq
				var mel = '#ng1ww32L' + me;
				var meu = '#ng1ww32U' + me;
				var done = $(meu).hasClass('done');
				if (!done) {
					// fetch flqs
					$(mel).append(ngwwjaxgif10);
					ngwwCS32(me);
				}
			}
	function ng1wwchaps4click(me,book,flq) {
				var mel = '#ng1ww4L' + me;
				var meu = '#ng1ww4U' + me;
				var done = $(meu).hasClass('done');
				if (!done) {
					// fetch stage 4 data
					$(mel).append(ngwwjaxgif10);
					ngwwCSchaps4(book,flq,me);
				}
			}
	function ng1ww51click(me) {
		// pagerange
		var mel = '#ng1ww51L' + me;
		var meu = '#ng1ww51U' + me;
		var done = $(meu).hasClass('done');
		if (!done) {
			// fetch stage 5 data
			$(mel).append(ngwwjaxgif10);
			ngwwCS51(me);
		}
	}
	function ng1ww52click(b3x2,me) {
				var mel = '#ng1ww52L' + me;
				var meu = '#ng1ww52U' + me;
				var done = $(meu).hasClass('done');
				if (!done) {
					$(mel).append(ngwwjaxgif10);
					ngwwCS52(b3x2,me);
				}
			}
	function ng1ww53click(b3x2,me) {
				var mel = '#ng1ww52L' + me;
				var meu = '#ng1ww52U' + me;
				var done = $(meu).hasClass('done');
				if (!done) {
					$(mel).append(ngwwjaxgif10);
					ngwwCS53(b3x2,me);
				}
			}
	function ng1ww60click(idd,ix) {
		$(idd).toggleClass('ng1wwc60sel');
		var t = $(idd).text();
		t = t.replace('(select)', '');
		if ( $(idd).hasClass('ng1wwc60sel') ) {
			$('#ng1ww99D').append(t + '<br>');
		} else {
			var u = $('#ng1ww99D').html();
			u = u.replace(t + '<br>', '');
			$('#ng1ww99D').html(u);
		}
	}
	function ng1ww60pre() {
		var sel = '';
		$('.ng1wwc60sel').each(function() {
			me = this.id;
			sel = sel + ',' + me;
		});
		window.location.href = 'ng1ww/op/wopds/xs/' + sel.slice(1);
	}
	
	function ngwwCS31(me) {
		$.ajax({
			type: "GET",
			// url: "index.php",
			// data: "module=ng1ww&axop=c2s13" + "&book=" + book + "&x=" + b3x1 + "&id=" + me,
			url: "ng1ww/axop/c2s31/b3x1/" + me,
			success: function(reply) {
						ngwwSC31(reply);
					}
		});
	}
	function ngwwCS32(me) {
		$.ajax({
			type: "GET",
			//	url: "index.php",
			//	data: "module=ng1ww&axop=c2s14" + "&book=" + book + "&flq=" + flq + "&id=" + me,
			url: "ng1ww/axop/c2s32/b3x2/" + me,
			success: function(reply) {
						ngwwSC32(reply);
					}
		});
	}
	function ngwwCS51(me) {
		$.ajax({
			type: "GET",
			//	url: "index.php",
			//	data: "module=ng1ww&axop=c2s14" + "&book=" + book + "&flq=" + flq + "&id=" + me + "&lvl2=" + me,
			url: "ng1ww/axop/c2s41/b4x1/" + me,
			success: function(reply) {
						ngwwSC51(reply);
					}
		});
	}
	function ngwwCS52(b3x2,me) {
		$.ajax({
			type: "GET",
			//	url: "index.php",
			//	data: "module=ng1ww&axop=c2s14" + "&book=" + book + "&flq=" + flq + "&id=" + me + "&lvl3=" + me,
			url: "ng1ww/axop/c2s42/b3x2/" + b3x2 + "/b5x2/" + me,
			success: function(reply) {
						ngwwSC52(reply);
					}
		});
	}
	function ngwwCS53(b3x2,me) {
		$.ajax({
			type: "GET",
			//	url: "index.php",
			//	data: "module=ng1ww&axop=c2s14" + "&book=" + book + "&flq=" + flq + "&id=" + me + "&lvl3=" + me,
			url: "ng1ww/axop/c2s43/b3x2/" + b3x2 + "/b5x2/" + me,
			success: function(reply) {
						ngwwSC53(reply);
					}
		});
	}
	function ngwwSC31(reply) {
		var ret = reply.split(':',1);
		var me = ret[0];
		var wo1 = '#ng1wwchaps1';
		var wo2 = '#ng1ww31L' + me;
		var wo3 = '#ng1ww31U' + me;
		var wo = wo1 + ' ' + wo3;
		$(wo3).addClass('done');
		$('#jaxgif10').remove();
		$(wo2).removeAttr('onclick');
		var plus = $(reply).appendTo(wo);
		$('#ng1wwchaps1').treeview({
			add: plus
		});
	}	
	function ngwwSC32(reply) {
		var ret = reply.split(':',1);
		var me = ret[0];
		var wo1 = '#ng1wwchaps1';
		var wo2 = '#ng1ww32L' + me;
		var wo3 = '#ng1ww32U' + me;
		var wo = wo1 + ' ' + wo3;
		$(wo3).addClass('done');
		$('#jaxgif10').remove();
		$(wo2).removeAttr('onclick');
		var plus = $(reply).appendTo(wo);
		$('#ng1wwchaps1').treeview({
			add: plus
		});
	}	
	function ngwwSC51(reply) {
		var ret = reply.split(':',1);
		var me = ret[0];
		var wo1 = '#ng1wwchaps1';
		var wo2 = '#ng1ww51L' + me;
		var wo3 = '#ng1ww51U' + me;
		var wo = wo1 + ' ' + wo3;
		$(wo3).addClass('done');
		$('#jaxgif10').remove();
		$(wo2).removeAttr('onclick');
		var plus = $(reply).appendTo(wo);
		$('#ng1wwchaps1').treeview({
			add: plus
		});
	}	
	function ngwwSC52(reply) {
		var ret = reply.split(':',1);
		var me = ret[0];
		var wo1 = '#ng1wwchaps1';
		var wo2 = '#ng1ww51L' + me;
		var wo3 = '#ng1ww51U' + me;
		var wo = wo1 + ' ' + wo3;
		$(wo3).addClass('done');
		$('#jaxgif10').remove();
		$(wo2).removeAttr('onclick');
		var plus = $(reply).appendTo(wo);
		$('#ng1wwchaps1').treeview({
			add: plus
		});
	}	
	function ngwwSC53(reply) {
		var ret = reply.split(':',1);
		var me = ret[0];
		var wo1 = '#ng1wwchaps1';
		var wo2 = '#ng1ww52L' + me;
		var wo3 = '#ng1ww52U' + me;
		var wo = wo1 + ' ' + wo3;
		$(wo3).addClass('done');
		$('#jaxgif10').remove();
		$(wo2).removeAttr('onclick');
		var plus = $(reply).appendTo(wo);
		$('#ng1wwchaps1').treeview({
			add: plus
		});
	}	
	
	function ngwwExpCla() {
		// expand collapse switch
		if (ngwwtocx==true) {
			ngwwtocx=false;
			document.getElementById('ng1wwboxop').textContent = 'Expand';
			document.getElementById('ng1wwtcnt').style.display="none";
		} else {
			ngwwtocx=true;
			document.getElementById('ng1wwboxop').textContent = 'Collapse';
			// ngwwTreeV();
			document.getElementById('ng1wwtcnt').style.display="block";
		}
	}
	
	function ngwwC2CpostA() {
		$('.ng1wwIms').each(function() {
			$(this).trigger('click');
		});
	}
	function ngwwC2Cpost(op,flq,subdir,n) {
		// loop task, step 1 of 3,  the cv flq click handler
		for(i = 0; i < n; i++) {
			var id = '#ng1ww' + flq + i;
			$(id).html('<img src="mod/ng1ww/img/ajax10red.gif" alt="..." />');
			switch (op) {
				case 'c2sNav':
					ngwwC2Spost('s2sNav',flq,subdir,i);
					break;
				case 'c2sImp':
					ngwwprobar++;
					ngwwC2Spost('s2sImp',flq,subdir,i);
					break;
			}
		}
	}
	function ngwwC2Spost(op,flq,subdir,n) {
		// single task, step 2 of 3, perform the ax call
		//	url: "ng1ww/op/s2cflq/flq/" + flq + "/",
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "module=ng1ww&axop=" + op + "&flq=" + flq + "&src=" + subdir + '&n=' + n,
			success: function(smsg) {
						ngwwS2Cpost(smsg);
					}
		});
	}
	function ngwwS2Cpost(smsg) {
		// smsg= flq+n &uarr; up &larr; prev &rarr; next (the arrow entities)
		// single task, step 3 of 3, showing results
		var rec = smsg;
		var nav = smsg.split('&');
		// 0 flq+n 1 uarr; 2 larr; 3 rarr; OR 0 flq+n 1 nbsp 2 imp state
		if (nav[1]=='equiv;') {
			var cnt = '    &' + nav[1] + '&' + nav[2];
			ngwwprobar--;
			$('#ngwwprobar').html(ngwwprobar);
		} else {
			var cnt = '<br />     &' + nav[1] + '<br />     &' + nav[2] + '<br />     &' + nav[3];
		}
		var id = '#ng1ww' + nav[0] ;
		$(id).html(cnt);
		if (ngwwprobar > 0 ) {
			$('#ngwwprobar').html(ngwwjaxgif11 + ' ' + ngwwprobar + ' ' + ngwwjaxgif11);
		} else {
			ngwwprobar = 0;
			$('#ngwwprobar').html(' ');
		}
	}
	
	function ngwwDocSet(book, page) {
		// nmu
		ngwwbook=book;
		ngwwpage=page;
	}

	function ngwwDocGet() {
		// nmu
		document.getElementById('ng1wwedit').href =
			'ng1ww/op/open/htm/' + ngwwbook + ',' + ngwwpage;
	}

	//
	
	function ngSel2In(e) {
		var i = document.getElementById('phpws_form_' + e + 'sel').value;
		if (i > 0) {
			document.getElementById('phpws_form_' + e).value
				= document.getElementById('phpws_form_' + e + 'sel').options[i].text;
		} else {
			document.getElementById('phpws_form_' + e).value = '';
		}
		// document.getElementById('phpws_form_' + e + 'sel').selectedIndex=null;
		document.getElementById('phpws_form_' + e).focus();
	}
	
	//
	// part ngHelp
	function ngHelpClick(hlpid) {
		var elmnt = document.getElementById(hlpid);
		var x = elmnt.previousSibling.offsetLeft;
		var y = elmnt.previousSibling.offsetTop + 20;
		elmnt.style.left = x + 'px';
		elmnt.style.top = y + 'px';
		elmnt.style.display='block';
	}

	function ngHelpOvr(hlpid) {
		var elmnt = document.getElementById(hlpid);
		if (elmnt.previousSibling.previousSibling.tagName=='A') {
			elmnt.previousSibling.style.textDecoration = 'underline';
			elmnt.previousSibling.previousSibling.style.textDecoration = 'underline';
		}
	}

	function ngHelpOff(hlpid) {
		var elmnt = document.getElementById(hlpid);
		elmnt.style.display='none';
		if (elmnt.previousSibling.previousSibling.tagName=='A') {
			elmnt.previousSibling.style.textDecoration = 'none';
			elmnt.previousSibling.previousSibling.style.textDecoration = 'none';
		}
	}

	//
	// Part search
	function ngTermsView(div) {
		var elmnt = document.getElementById(div);
		if (elmnt.style.display=='none') {
			elmnt.style.display='block';
		} else {
			elmnt.style.display='none';
		}
	}
	
	function ngSearchView() {
		if(document.getElementById('searchSelect')) {
			var lookfor = document.getElementById('searchphrase');
			hiSearchTerms(lookfor.value, false);
		}
	}
	
	function ngSearchPosit() {
		if(document.getElementById('searchSelect')) {
			var lookfor = document.getElementById('searchphrase');
			// has to work out
		}
	}

	function hiSearchTerms(searchText, treatAsPhrase, hiliteStart, hiliteEnd) {
		// treatAsPhrase parameter = search entire phrase, else take words
		if (treatAsPhrase) {
			searchArray = [searchText];
		} else {
			searchArray = searchText.split(" ");
		}
		if (!document.body || typeof(document.body.innerHTML) == "undefined") {
			return false;
		}
		var bodyText = document.body.innerHTML;
		for (var i = 0; i < searchArray.length; i++) {
			bodyText = doSearchHilite(bodyText, searchArray[i], hiliteStart, hiliteEnd);
		}
		document.body.innerHTML = bodyText;
		return true;
	}

	function doSearchHilite(bodyText, searchTerm, hiliteStart, hiliteEnd) {
		if ((!hiliteStart) || (!hiliteEnd)) {
			hiliteStart = "<font style='background-color:#FFFF00;'>";
			hiliteEnd = "</font>";
		}
		var newText = "";
		var lcSearchTerm = searchTerm.toLowerCase();
		var lcBodyText = bodyText.toLowerCase();
		var i = bodyText.indexOf('<div id="main-content">',0);
		while (bodyText.length > 0) {
			i = lcBodyText.indexOf(lcSearchTerm, i+1);
			if (i < 0) {
				newText += bodyText;
				bodyText = "";
			} else {
				// skip anything inside an HTML tag
				if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
					// skip anything inside a <script> block
					if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
						newText += bodyText.substring(0, i) + hiliteStart + bodyText.substr(i, searchTerm.length) + hiliteEnd;
						bodyText = bodyText.substr(i + searchTerm.length);
						lcBodyText = bodyText.toLowerCase();
						i = -1;
					}
				}
			}
		}  
		return newText;
	}
	
	
	function goTrans(key,srctext) {
	
		var st = $('#phpws_form').contents().find('#docsrc').html();
		var stu = $('<textarea/>').html(st).val();
		var stul = stu.length;
		var dlms = ['. ',', ','>'];
		var chnk = 1200;
		var hchunks = 0;
		var i = 0;
		var w = 0;
		var p = -1;
		var parf = [];
		var part = [];
		while (w<stul) {
			if ((w+chnk)>=stul) {
				// take rest
				parf[i]=w;
				part[i]=stul;			
				break;
			}
			for (var x in dlms) {
				p = stu.indexOf(dlms[x],w+1000);
				if ((p == -1) || ( p > w+chnk)) {
					if (x < dlms.length) {
						continue;
					}
					// not found or too far, take hard chunk
					parf[i]=w;
					part[i]=w+chnk;
					w=w+chnk;
					hchanks++;
				} else {
					// take soft chunk
					parf[i] = w;
					part[i] = p + dlms[x].length;
					w = p + dlms[x].length;
					break;
				}
			}
			i++;
		}
		// alert(parf);
		// alert(part);
		$('#awwmsg').text('Textsize=' + stul + ', translation in ' + part.length + ' chunks, hardchunks=' + hchunks);
		
		var par = '';
		lcflag=true;
		for (i=0; i<part.length; i++) {
			par = stu.substring(parf[i],part[i]);
			go2Trans(key,par);
		}
	}	
		
	function go2Trans(key,srctext) {
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		var srt = escape(srctext);
		var src = 'https://www.googleapis.com/language/translate/v2?key=' + key 
		+ '&source=' + $('#phpws_form_lcfrom').val() + '&target=' + $('#phpws_form_lcto').val()
		+ '&format=html'
		+ '&callback=backTrans&q=' + srt;
		newScript.src = src;
		// fire
		document.getElementsByTagName('head')[0].appendChild(newScript);
		
	}
	
	function backTrans(reply) {
		//alert(reply.error.errors[0].reason);
		if (typeof reply.error === 'undefined') {
			if (lcflag==true) {
				// clear docsrc before 1st chunk
				$('#docsrc___Frame').contents().find('iframe').contents().find('body').html('');
				lcflag=false;
			}
			//$('#awwtrans').append(reply.data.translations[0].translatedText);
			//$('#phpws_form').contents().find('#docsrc textarea').html(reply.data.translations[0].translatedText);
			$('#docsrc___Frame').contents().find('iframe').contents().find('body').append(
				reply.data.translations[0].translatedText
			 );
		} else {
			$('#awwmsg').append(', Feedback= <b>' + reply.error.message + '</b>');
		}
	}
	
	function checkTrans() {
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		var src = 'https://www.googleapis.com/language/translate/v2?key=' + key 
		+ '&source=' + $('#phpws_form_lcfrom').val() + '&target=' + $('#phpws_form_lcto').val()
		+ '&format=text'
		+ '&callback=backTrans&q=Test';
		newScript.src = src;
		document.getElementsByTagName('head')[0].appendChild(newScript);
	}
	
	function transChange() {
		lcfrom = $('#phpws_form_lcfrom :selected').text();
		lcto = $('#phpws_form_lcto :selected').text();	
	}


	

