//shared scripts popups.js

tipflag=0;
col=0;
var clicks=0;
var wwidth=130;
var ob_popup=0;
var itimerRunning=0;
timer_running=false;
clicked = 0;
//Hover on a menu link shows a tooltip with text from these vars 
home_text = "Home page";
default_info = "Peter Bloxsom: sites for writers and small business, with easy content management";


var menu_sel = "";

clicktext="";
other_info = "Writing and editorial services";
publish_info = "Professional publishing services"; 
techwriting_info = "IT technical writing and editing, including online help"; 
writing_info = "Writing services, including novelization, articles, consultancy"; 
web_info = "Website service in brief";
webzine_info ="Help with setting up and maintaining a literary or other webzine site";
smallbiz_info = "Easy-maintenance websites for small businesses and professionals"; 
writer_info = "Easy websites for writers: built, hosted and maintained";


tech_info = "Coming soon: web and language issues and obsessions";
webtech_info ="Occasionally asked questions and frequently flouted principles";
langtech_info ="&ldquo;Dangling bewilderingly, I hate participles&rdquo; and other pedantic excursions";

amusements_info = "Light relief in assorted guises";
weq_info = "The quiz where all the answers are obvious"
epo_info = "Well, I&rsquo;ll eat my cardigan!";
vis_info ="Focus on images";
signs_info = "Weird and wonderful public notices";
photos_info = "Photos to wish you had taken";
fractal_info = "Some original fractal artworks";

puz_info = "Puzzles and trivia to exercise the little grey cells";
lpuz_info = "Some lateral thinking exercises, with hints and solutions"; 
ks_info = "You&rsquo;ll kick yourself when you see the answer (puzzle changes daily)";
howc_info = "Article: includes Western Sunrise and other mysteries";
ba_info = "Mental workouts with logic, words and numbers"; 
bae_info = "Warm up with gentler workouts"; 
bah_info = "A heavier workout for the keen and capable";  
commp_info = "Get puzzles and competitions for print or web";

about_info = "Who me?";

puzzles_text = "<p class='info'><b>Puzzles</b>: Kickself puzzles (whose solutions may seem fairly obvious in hindsight at least), lateral thinking or &ldquo;How come?&rdquo; problems, and a variety of logic, word and number teasers &mdash; some challenging. You can also contact us for commercial puzzle features or competitions for print or web use. At some stage there will be a regular competition here, for the keen and capable. </p><p class='info' align=right><a href='javascript:hideInf()'>Close</a></p>";

//moebius_info = "Our magazine (online and PDF)" + clicktext;
moebius_info = "Our quarterly magazine: COMING SOON!";
moebius_text = "<p class='info'><b>Moebius</b>: Moebius is our quirky, entertaining &ldquo;magazine with a twist&rdquo;. </p><p class='info' align=right><a href='javascript:hideInf()'>Close</a></p>";


metrical = "<b>Metrical</b>: As used here, the term means having a more or less identifiable and consistent rhythm. There may be variations, and the lines do not necessarily all have the same number of main &ldquo;beats&rdquo;. But what is considered metrical with variations here, might not be accepted as such by stricter &ldquo;formalists&rdquo;. <b>Free verse</b> usually has a variable or unidentifiable rhythm. But not all free verse is <i>un</i>metrical &mdash; the language may still have a rhythm. Of course, there is no rigid boundary between metrical and free verse. It&rsquo;s partly a mattter of perception and definition.<br><br> <b>Formalist</b> verse normally means metrical verse written in established forms or meters: for example (form) the sonnet of fourteen lines with Petrarchan, Shakespearean or other structure and rhyme scheme, or (meter) the four-beat de-DUM de-DUM de-DUM de-DUM iambic tetrameter that has long been common in English. <br><br><b>Rhyme</b> is common in metrical poetry but is not essential to it; rhyme is a resource the writer may choose to employ, or not. Of course, rhyme is an integral part of some formalist verse. It is also sometimes used in free verse.</p><p class='eetooltip' align=right><a href='#' onclick='hideIt();return false'>Close</a></p>"; 


showInfo(default_info);

function nowt()
{
//null function
}

function hidePopup() 
{
//timerId = setTimeout ("hideIt()", 3500)
hideIt()
}

function hideInfo()
{
//if (itimerRunning) {clearTimeout(infotimer); itimerRunning=0}
hE(infotip);
} 


tipflag=0;
infox=""; infoy="";
var clicks=0;
var wwidth=160;
var ob_popup=0;

//timer_running=false;

var popup_shown=0;
var wheight = 0; 


//function showInfo(txt,xposn,yposn) 
function showInfo(txt) 

{
if (!DomYes) {return}
//if (xposn==undefined) {xposn="" }
//if (yposn==undefined) {yposn="" }
//if (txt==ks_info) alert ("txt is " +ks_info);
//if (txt==signs_info) alert ("txt is " +signs_info);
infotip=gE("infodiv");
infotip.innerHTML = "<div class='info'>" + txt +"</div>"; 


//if (xposn=="") tipleft = tipleft_default
 //else tipleft = xposn;
//if (yposn=="") tiptop = tiptop_default 
 //else tiptop = yposn;
//sX(infotip,tipleft);xposn="";
//tiptop = 1;
//sY(infotip,tiptop);yposn="";
sE(infotip);


}


function OnHilite(txt)
{
	
	if(!DomYes) {return};
	showInfo(txt);
}

	
function showDiv(divid) 
{
 //if (!DomYes) {return}
 
 divobj=gE(divid);
sE(divobj);
}



function OnLow()
{
 //  infotip=gE("infodiv");
  // hideInfo()
   showInfo(default_info)
}

function getDocHeight()
{

var x,y;
var test1 = document.body.scrollHeight;
var test2 = document.body.offsetHeight
if (test1 > test2) // all but Explorer Mac
{
	dheight = document.body.scrollHeight;
	}
else 
{
	dheight = document.body.offsetHeight;
}

return dheight
}



function setDivtoDocHeight(divid,margins)
{
th = eval(getDocHeight() - margins);
divobj=gE(divid);
sH(divobj,th)
}



function showPopup(evt,txt,wwidth) 
{
tooltip=gE("tipdiv");shadow = gE("tipdiv_s");
	sW(tooltip,wwidth); sW(shadow,wwidth); //set width
	content='<div class="eetooltip">' + txt +'</div>';
	tooltip.innerHTML=content; shadow.innerHTML=content;
	wheight=gH("tipdiv");
	xoffset=getOffsetX(); yoffset=getOffsetY();
		
	if (window.event) 
	{
	evt=window.event; 
	if (evt.clientX > (wwidth+15)) tipleft = eval(evt.clientX - 15 - wwidth);
	else tipleft = eval(evt.clientX + 15);
	if (evt.clientY > (wheight+15)) tiptop = eval(evt.clientY +yoffset - 15 - wheight);
	else tiptop = eval(evt.clientY +yoffset + 15);
 	}
	
	else if (evt.pageX) 
	{
	leftspace = eval(evt.pageX - window.scrollX); 
	if (leftspace > (wwidth+15)) tipleft = eval(evt.pageX - 15 - wwidth);
	else tipleft = eval(evt.pageX + 15);
	topspace = eval(evt.pageY - window.scrollY); 
	if (topspace > (wheight+15)) tiptop = eval(evt.pageY - 15 - wheight);
	else tiptop = eval(evt.pageY + 15);
	}
	
	sX(tooltip,tipleft); sX(shadow,tipleft+5); 
	sY(tooltip,tiptop); sY(shadow,tiptop+5);
	sE(tooltip); sE(shadow); 
	popup_shown=1;
 }



function movePopup(evt,txt,wwidth,wheight)
{
if (popup_shown==1) showPopup(evt,txt,wwidth)
}
//
function getOffsetX()
{
if (window.scrollX)
 {
	  xoffset = window.scrollX; 
 }
	else if (document.documentElement && document.documentElement.scrollLeft)
	{
		xoffset = document.documentElement.scrollLeft
	}
		else if (document.body)
		{
			  xoffset = document.body.scrollLeft
		}
return xoffset
}	

function getOffsetY()
{
if (window.pageYOffset)
 {
	  yoffset = window.pageYOffset; 
 }
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		yoffset = document.documentElement.scrollTop
	}
		else if (document.body)
		{
			  yoffset = document.body.scrollTop
		}
return yoffset
}	


function hideIt()
{
hE(tooltip);
hE(shadow);popup_shown=0;wheight=0;
popup_shown=0;
wheight=0;
}




function getFirst(str)
{
strfirst = str.substring(0,1)
return strfirst
 }


function hideDef()
{
hidePopup();
} 

function nowt()
{
//null function
}

function hideInfo()
{
//if (itimerRunning) {clearTimeout(infotimer); itimerRunning=0}
hE(infotip);
} 


function keepIfClicked()
{
if (clicked==0)
	{
	hE(tooltip);hE(shadow);
	popup_shown=0;
	wheight=0;
	clicked = 0;
	}
}

function setClicked()
{
clicked = 1;
}


function unsetClicked()
{
clicked = 0;
}

function hideDiv(divid)
{
 //if (!DomYes) {return}
 divobj=gE(divid);
hE(divobj)
}

// Look up a term in the glossary and pop up the term and definition
function showDef(evt,term_to_define,ww)
{
if (ww==null) ww=wwidth;
lookup(term_to_define);//get text for the popup
if (poptext!="") // Do nothing if no defn found
	{
		 tooltip=gE("tipdiv");if (!tooltip.innerHTML) return;
		 showPopup(evt,poptext,ww)
	}
}  		


//Find matching definition for term 
function lookup(termx) 
{
poptext = "";
	for (var i=1; i <= (entry.length-1) ;  i++) 
		{
		if (termx.toLowerCase() == entry[i].term.toLowerCase()) 
	       //compare regardless of case	
			{
			poptext = "<b>"+ entry[i].term + ': </b> ' + entry[i].defn;
			return poptext; // still "" if no match
			}
		}
}


function assembleText(content)	
{
output="<html><head><title>Popup Text</title>";
output += "<style>body {font-family: Verdana, Geneva, Arial, Helvetica, Sans-serif; font-size: 11px; color: #004499} P {margin-top: 2px; margin-right:2px; margin-bottom:8px; margin-left:2px} H3 {font-size: 12px; margin-top: 2px; margin-right:2px; margin-bottom:7px; margin-left:2px; text-align:center}";
output += "A:link {background-color:transparent;color:#0000FF; text-decoration : underline}"
output+= "A:hover {background-color : transparent; color : #CCCC00; text-decoration : underline}</style></head>";
output +="<body bgcolor='#FFFFEE' >";
output +=  content + "<p align=right><a href='javascript:window.close()'>Close window</a></p></body></html>";
}
//onBlur='javascript:window.close()'

function popupText(output)
{
 textWindow=window.open("","popuptxt","width=250,height=200,top=120,left=200,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
// 
if (textWindow != null) //window opened successfully
	{
	textWindow.document.open();
	textWindow.document.write(output);
	textWindow.document.close();
	}
else alert("Unable to open popup window. You may need to close other applications or windows to free up some memory, then try again."); 	
}



function search (mainStr,searchStr)
{
foundOffset = mainStr.indexOf(searchStr);
//alert("In search function; foundOffset is " + foundOffset);
if (foundOffset == -1) {return 0}
return 1
}


//Extract characters after the first occurrence of the search string
function getEnd(mainStr,searchStr)
{
//alert ("in getEnd; mainStr is " +mainStr + "; searchStr is " + searchStr +"; i is " +i); 
foundOffset = mainStr.indexOf(searchStr);
if (foundOffset == -1) return null;
return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}

//Extract characters up to but not including the first occurrence of the search string
function getFront(mainStr,searchStr)
{
foundOffset = mainStr.indexOf(searchStr);
if (foundOffset == -1) return null;
return mainStr.substring(0,foundOffset)
}

//Extract characters up to but not including the last occurrence of the search string

function getFront2(mainStr,searchStr)
{
foundOffset = mainStr.lastIndexOf(searchStr);
if (foundOffset == -1) return null; //not found
return mainStr.substring(0,foundOffset)
}

var frontstr = "";
var tailstr = "";

function searchReplace(mainStr,searchStr,replaceStr)
{
front = getFront(mainStr,searchStr);
tail = getEnd (mainStr,searchStr)
if (front!=null && tail!=null) {
return front + replaceStr + tail
}
return null
}

function getFirst(str)
{
strfirst = str.substring(0,1)
return strfirst
 }

 function getLast(str)
{
strlast = str.substring(str.length-1,str.length)
return strlast
 }
 
var front = ""; var tail = "";
var entry_found=0;
var counter=0;

function searchReplace2(mainStr,searchStr)
{
counter ++;
//alert("entered searchReplace2 funct with counter = " + counter + ", mainStr = " + mainStr + " and searchStr = " + searchStr);
 front = getFront(mainStr,searchStr);
 tail = getEnd (mainStr,searchStr)
//alert("Done search; front is " + front + "; tail is " + tail);
if (front!=null && tail!=null) 
 {
	//keep the 
	
	//Ignore part-words, e.g. don't find "Java" in "Javascript"
	//end of front: >,sp or &nbsp; &mdash;  &ldquo; 
	//start of tail  sp or &nbsp; ,;.!? < &mdash;  -&rdquo; 
	tailfirst = getFirst(tail);frontlast=getLast(front);
	if (tailfirst!=" " && tailfirst!="&nbsp;" && tailfirst!="," && tailfirst!=";" && tailfirst!=":" && tailfirst!="." && tailfirst!="!" && tailfirst!="?" && tailfirst!="<" && tailfirst!="&mdash;" && tailfirst!="" && tailfirst!="-" && tailfirst!="&rdquo;" && tailfirst!="&rsquo;") //not a good find 
   {
   if (counter==1) {savedfront = front + searchStr; //alert ("counter is " + counter + "; savedfront is " +savedfront)
                             }
   else {savedfront = savedfront + front + searchStr;//alert ("counter is " + counter + "; savedfront is " +savedfront)
           }

   searchReplace2(tail,searchStr) //search for another occurrence  
   }
   else 
   { 
	  if (frontlast!=" " && frontlast!="&nbsp;" && frontlast!=">" && frontlast!="&rdquo;" && frontlast!="&mdash;" && frontlast!="" && frontlast!="'" && frontlast!="&rsquo;" && frontlast!='"') //not a good find
       {
        if (counter==1) savedfront = front + searchStr
       else savedfront = savedfront + front + searchStr;
       searchReplace2(tail,searchStr)
	   }
	else //OK    
	   {
	entry_found = 1;
	if (counter==1) frontstr = front
	else frontstr =  savedfront + front;
	tailstr=tail;
	counter=0;return entry_found; //frontstr and tailstr also available 
	   }//End else
   }//End else
 }  
 counter=0;
return entry_found;

}//End function
//Puzzles on main lateral page; solution popup rewquires a click to close
pshint= "Surfaces"; pssol= "<p class='small'><center><img src='images/ps2.gif' width=181 height=117 alt='If the image is not showing here, right-click here and select \'Show picture\'' border='0'></center><br clear='all'>Initially, the eye is fooled into thinking it sees one surface only of the original paper, except for the raised flap. Not so &mdash; there&rsquo;s a twist in it. To make the shape, cut the paper as shown at left, fold up the flap, then twist one half through 180 degrees.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

fnhint= "Nothing to do with the North Pole";
fnsol = "<p class='small'>No, this has nothing to do with the North Pole. Magnusson can justify his claim by reversing his car for 1 km. The car is pointing north but goes south.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

hihint= "Hostage";
hisol = "<p class='small'>By demanding two parachutes, the hijacker created the impression that he intended to take a hostage with him when he jumped. Had he asked for just one parachute, he might have been deliberately given a faulty one! </p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

eehint= "Maturity";
eesol = "<p class='small'>To see if the child is of an age to start school! Very young children can&rsquo;t reach the opposite ear across the top of the head. The ratio of head size to arm length is much larger in a baby (with its relatively big head and short arms) than in an adult. This ratio steadily reduces as the child grows. Typically, children fail the ear touch test until the age of five and a half to six years. In some areas of PNG where records of births are not reliably kept, the test is used as a check on children&rsquo;s ages. On passing the ear touch test he or she is ready to start school.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

gfhint= "Avoirdupois";
gfsol = "<p class='small'>Gold is measured in troy ounces and troy pounds, whereas just about everything else, if measured in pounds and ounces at all, is measured in avoirdupois. An avoirdupois pound is heavier than a troy pound.</p><p>1 lb (Av) = 16 oz (Av) =  456.3 grams (SI)<br>1 lb (T) = 12 oz (T) = 373.2 grams (SI)</p><p>The ounces are different too. A troy ounce is bigger. Grams are grams no matter what you&rsquo;re weighing.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

hahint= "Autumn";
hasol = "<p class='small'>This happened in Sydney on the night in April when the clocks go back an hour at 2 a.m, and the hour from 1:00 a.m. to 2:00 a.m. occurs twice. The 1:59 event and those at times ending in 0 (1:30, etc) happened before the change; the others happened after it. </p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

mehint= "It&rsquo;s one of these: cloud, hail, rain, sun or storm";
mesol = "<p class='small'>Rain. (Anagrams: Nepal/plane, China/chain, Algeria/regalia, Iran/rain.) </p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

lrhint= "Islands";
lrsol  = "<p class='small'>The events described took place on three different islands in the Virgins (Caribbean). They are close together. I landed at St Croix, a US Virgin where they use nearly all left-drive American cars but, surprisingly, drive on the left &mdash; or at least they did a few years ago when I was there. (The island was once a Danish possession, and Denmark originally had a left-side rule of the road.) The chief town is on the water. As soon as I arrived there I took the walk-on seaplane service to St John, also in the US Virgins, but there they drive on the right. St John didn&rsquo;t appeal on first sight, so after a quick visit to the bank I jumped on the next seaplane to Tortola, a British Virgin only a few minutes away. There they drive on the left and use US dollars, and it was there that I bought the stamp with the Queen&rsquo;s head on it.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

dshint= "Elevation";
dssol  = "<p class='small'>She lived on a west-facing coast with high cliffs. From the beach, she saw the sun set behind a small inshore island. She at once ran up a cliff path, her increasing altitude allowing her to see the sun rise again from behind the island. She then sat on the clifftop and watched the second sunset. (Similar solutions could involve a skier, a chairlift and the sun going down behind a mountain, etc.).</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

//ks puzzles are in a separate file


//Puzzles on "How Come?" page; these also require an explicit popup Close
bbsol="<p class='small'>The butcher was a woman.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";
wssol="<p class='small'>He was a Concorde Pilot. Taking off from London just after sunset and travelling west, once he exceeded the rotational speed of the earth, he &ldquo;caught up with&rdquo; the sun, and as he continued westward he saw it rise slowly above the horizon. Relative to him, the earth was rotating slowly in the opposite of the usual direction. Concorde pilots flying out of Europe just after sunset have regularly reported this experience.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

//Popup answer to Eponyms quiz; explicit Close
eposol = "<p class='small'>The phonies are: <i>busking</i>, <i>canard</i>, <i>macaw</i>, and <i>tights</i>.</p><p class='small' style='text-align:right'><a href='#' onclick='hideIt();return false'>Close</a></p>";

//Puzzles appearing elsewhere, e.g. in sidebars. Links coded to close window on mouseout 

//Dali's Brother
dbhint= "Salvadors";
dbsol  = "Salvador Dali died aged 7. Nine months later, his younger brother was born and was also named Salvador. It was this younger Salvador Dali who became the famous painter.";

//matchdog
mdhint='Lateral thinking needed here!';
mdsol='<img src="../images/dogsol.gif" alt="If image is not showing here, right-click and select \'Show picture\'" border="0" WIDTH="148" HEIGHT="106">'; 

//Gulf of Guinea
gghint = "Zero";
ggsol = "&ldquo;Where in the world is the point whose geographical coordinates are zero longitude, zero latitude, and zero altitude?&rdquo;"

//Hiker Path
hphint = "Imagine his twin starting down from the top on Saturday morning.";
hpsol = "It&rsquo;s certain that there&rsquo;s one point on the path that the hiker occupied at the same time on the two days. To see why, imagine the return trip brought forward 24 hours and undertaken by another hiker. Then on Saturday morning the original hiker started up and the second hiker started down. So long as there is some overlap in their respective periods spent on the path (and there must be, since both trips begin in the morning and end in the afternoon) the two clearly must meet at one point along the path, at some time. The same &ldquo;meeting&rdquo; (time of day and place) must equally have occurred 24 hours apart in the original scenario.";

//Screen Pairs
sppuzz ="<b>Analogy</b><br>REMOTE is to JERRY<br>as TENDERFOOT is to GINGER<br>as FOOTNOTE is to ?";
sphint = "Duos";
spsol = "The Lone Ranger! tenDERFoot: GINGER; reMOTe: JERRY; foOTNOTe: THE LONE RANGER.<br>Fred & Ginger, Tom and Jerry, Tonto and ? (FRED is concealed in reverse inTENDERFOOT, etc.)";


var picno=1;
sign = new Array(5);
sign[1] = '<img src="images/sign_shop.jpg" width=252 height=400 alt="" border="0">';
sign[2] = '<img src="images/sign_drunk.jpg" width=350 height=263 alt="" border="0">'; 
sign[3] = '<img src="images/sign_lions.jpg" width=472 height=420 alt="" border="0">'; 
sign[4] = '<img src="images/sign_water.jpg" width=236 height=240 alt="" border="0">'; 
sign[5] = '<img src="images/sign_end.jpg" width=300 height=189 alt="" border="0">'; 


function nextSign()
{
picno ++;
if (picno==sign.length) picno = 1;
signobj = gE("picture");
wH(signobj,sign[picno]);
}

function prevSign()
{
picno --;
if (picno==0) picno = sign.length-1;
signobj = gE("picture");
wH(signobj,sign[picno]);
}


var picno=1;
photo = new Array(5);
photo[1] = '<img src="images/photo_plane.jpg" width=435 height=330 alt="Plane" border="0">'; 
photo[2] = '<img src="images/photo_opera.jpg" width=460 height=310 alt="Opera" border="0">';
photo[3] = '<img src="images/photo_leaves.jpg" width=260 height=415 alt="Autumn" border="0">'; 
photo[4] = '<img src="images/photo_galaxy.jpg" width=460 height=310 alt="Galaxy" border="0">'; 
photo[5] = '<img src="images/photo_eclipse.jpg" width=270 height=415 alt="Eclipse" border="0">'; 

function changePhoto()
{
picno ++;
if (picno==photo.length) picno = 1;
photoobj = gE("picture");
wH(photoobj,photo[picno]);
}

