//Handles autogrow/autoshrink Copyright N.P.Johnstone 9/Oct/05

var oEnlarge;
var oChosen;
var bSet = false;
var bSetEnlarge=false;
var xEnlarge;
var yEnlarge;
var	sBigImage;
var imgEnlarge;
var sShrinkPath;
var oShrinkChosen;
var oShrink;
var sPath;
var iSize=1.0;
var iShrinkSize = 1.0;
var xShrink;
var yShrink;
var imgShrink;
var bTimerSet=false;
var	oShrinkDate = new Date();

var oPauseImage, sPauseBigURL,sPauseTarget;
var iFrame;
var iGlobal =1;

var oAdFrame;
var oTitleDiv;
var tKillTimer;

function onExpandContract(idname, imgname)
{
	var objSpan = document.getElementById(idname);
	var objImg =  document.getElementById(imgname);
	if (objSpan.style.display=="none")
	{
		var rowCollect = objSpan.childNodes[0].getElementsByTagName("tbody").item(0).getElementsByTagName("img");
		for (rowCnt = 0; rowCnt<rowCollect.length; rowCnt++)
		{
			var sPath = rowCollect.item(rowCnt).id;
			rowCollect.item(rowCnt).src = sPath;
		}
		objSpan.style.display="block";
		objImg.src = "hide.gif";
	}
	else
	{
		objSpan.style.display="none";
		objImg.src = "show.gif";
		var rowCollect = objSpan.childNodes[0].getElementsByTagName("tbody").item(0).getElementsByTagName("img");
		for (rowCnt = 0; rowCnt<rowCollect.length; rowCnt++)
		{
			rowCollect.item(rowCnt).src = "a.gif";
			
		}
	}
}

// Position finder courtesy of excellent quirksmode.org
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function GoPage(sTarget)
{
	window.location=sTarget;
}

function st()
{
	iFrame = iGlobal;
	iGlobal++;
	return;
}

function ChooseThis(oImage,sBigURL,sTarget)
{
	if (!oImage.complete || (typeof oImage.naturalWidth != "undefined" && oImage.naturalWidth == 0))
	{
		iFrame = iGlobal;
		iGlobal++;
		return;
	}

	oPauseImage = oImage;
	sPauseBigURL = sBigURL;
	sPauseTarget = sTarget;
	iFrame = iGlobal;
	setTimeout("ChooseThis2("+iFrame+")", 500);
	iGlobal++;
}

function ChooseThis2(iWhichFrame)
{
	if (iWhichFrame!=iFrame) return;
	var oImage = oPauseImage;
	var sBigURL =sPauseBigURL;
	var sTarget=sPauseTarget;
	iFrame=-1;

	if (oChosen == oImage) return;
	if (oShrinkChosen == oImage) 
	{
		var oNow = new Date();
		if (oNow.getTime() - oShrinkDate.getTime() <2000) return;	// don't zoom for 2 secs
	}
	
	if (bSetEnlarge)
	{
		if (bSet)
		{
			oShrink.width = 80;
			oShrink.height = 60;
			oShrink.src = sShrinkPath;
			document.body.removeChild(oShrink);
		}
		
		oShrink=oEnlarge;
		oShrinkChosen=oChosen;
		oShrinkDate = new Date();
		iShrinkSize=iSize;
		if (iShrinkSize>4) iShrinkSize=4;
		sShrinkPath = sPath;
		bSet = bSetEnlarge;
		xShrink = xEnlarge;
		yShrink = yEnlarge;
		imgShrink = imgEnlarge;
	}

	xEnlarge = findPosX(oImage);
	yEnlarge = findPosY(oImage);
	bSetEnlarge = true;
	oChosen = oImage;
	sPath = oImage.src;
	iSize = 1.0;
	sBigImage = sBigURL;

	oEnlarge = document.createElement('div');
	oEnlarge.style.position = "absolute";
	oEnlarge.style.left = xEnlarge+"px"; 
	oEnlarge.style.top = yEnlarge+"px";
	oEnlarge.style.width = "80px";
	oEnlarge.style.height = "60px";
	imgEnlarge =new Image();
	imgEnlarge.src = sPath;
	imgEnlarge.width=80;
	imgEnlarge.height=60;
	imgEnlarge.onclick = function() {GoPage(sTarget);};
	imgEnlarge.onmouseout = function() {CancelGrow(this);};
	oEnlarge.appendChild(imgEnlarge);
	document.body.appendChild(oEnlarge);
	if (!bTimerSet) setTimeout("ChangeSizes2()", 20);
	bTimerSet = true;
}

function CancelGrow(obj)
{
	if (oShrinkChosen == obj) return;
	if (bSet)
	{
		oShrink.width = 80;
		oShrink.height = 60;
		oShrink.src = sShrinkPath;
		document.body.removeChild(oShrink);
		RemoveAdFrames();	
	}
	
	oShrink=oEnlarge;
	oShrinkChosen=oChosen;
	oShrinkDate = new Date();
	iShrinkSize=iSize;
	if (iShrinkSize>4) iShrinkSize=4;
	sShrinkPath = sPath;
	bSet = bSetEnlarge;
	xShrink = xEnlarge;
	yShrink = yEnlarge;
	imgShrink = imgEnlarge;
	bSetEnlarge = false;
	oChosen = null;
	if (!bTimerSet) setTimeout("ChangeSizes2()", 20);
	bTimerSet = true;
}

function ChangeSizes2()
{
	bTimerSet =false;
	if (iSize<4)
	{
		iSize+=0.125;
		var w = 80*iSize;
		var h = 60*iSize;
		imgEnlarge.width = w;
		imgEnlarge.height = h;
		var x = xEnlarge - (w-80)/2;
		var y = yEnlarge - (h-60)/2;
		oEnlarge.style.left=x;
		oEnlarge.style.top=y;
		oEnlarge.style.width = w+"px";
		oEnlarge.style.height = h+"px";
		if (iSize>3.99) 
		{
			imgEnlarge.src = sBigImage;
			MakeLeftRightDiv(x,y);
		}
	}
	if (iSize>=4)
	{
		iSize+=0.125;
		if (iSize > 36)
		{
			if (bSet)
			{
				oShrink.width = 80;
				oShrink.height = 60;
				oShrink.src = sShrinkPath;
				document.body.removeChild(oShrink);
			}
			
			oShrink=oEnlarge;
			oShrinkChosen=oChosen;
			oShrinkDate = new Date();
			iShrinkSize=4;
			iSize=1;
			sShrinkPath = sPath;
			bSet = bSetEnlarge;
			xShrink = xEnlarge;
			yShrink = yEnlarge;
			imgShrink = imgEnlarge;
			bSetEnlarge = false;
			oChosen = null;
		}
	}

	if (bSet && iShrinkSize>1)
	{
		iShrinkSize-=0.125;
		var w = 80*iShrinkSize;
		var h = 60*iShrinkSize;
		imgShrink.width = w;
		imgShrink.height = h;
		oShrink.style.width = w+"px";
		oShrink.style.height = h+"px";
		oShrink.style.left=xShrink - (w-80)/2;
		oShrink.style.top=yShrink - (h-60)/2;
		if (iShrinkSize<1.1)
		{
			document.body.removeChild(oShrink);
			tKillTimer = setTimeout("RemoveAdFrames()",600);
			bSet = false;
		}
	}

	if (bSetEnlarge || (bSet && iShrinkSize>1))  
	{
		bTimerSet = true;
		setTimeout("ChangeSizes2()", 20);
	}
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

var iLeftRight = 0;

function MakeAdFrame(x,y, w,h, src)
{
	if (getInternetExplorerVersion() >-1)
	{
		x--;
		y--;
		w+=2;
		h+=2;
	}
	var ifr = document.createElement("iframe");
	ifr.src = src;
	ifr.frameborder = "0";
	ifr.framemargin = 0;
	ifr.width = w;
	ifr.height= h;
	ifr.scrolling = "no";
	ifr.align="left";
	ifr.marginwidth=0;
	ifr.marginheight=0;
	ifr.style.position="absolute";
	ifr.style.left = x+"px";
	ifr.style.top = y+"px";
	ifr.style.border = "0px none";
	ifr.style.background = "white";
	return ifr;
}

function MakeRecipeNameFrame(x,y,w,h)
{
	var rnf = document.createElement('div');
	rnf.style.position = "absolute";
	rnf.style.left = x+"px"; 
	rnf.style.top = y+"px";
	rnf.style.width = w+"px";
	rnf.style.height = h+"px";
	rnf.style.visibility = "visible";
	rnf.style.background = "white";

	var atbl = document.createElement('table');
	var abdy = document.createElement('tbody');
	atbl.appendChild(abdy);
	atbl.height = h;
	atbl.width = w;
	var atr = document.createElement('tr');
	var atd = document.createElement('td');
	atd.valign = "middle";
	atd.align = "center";
	atd.height = h;
	atr.appendChild(atd);
	abdy.appendChild(atr);
	rnf.appendChild(atbl);

	atbl.onclick = function() {GoPage(sPauseTarget);};

	var sTxt = oPauseImage.alt;

	var iWhere = sTxt.indexOf('(');
	if (iWhere)
	{
		var iTo = sTxt.lastIndexOf(')');
		if (iTo==-1) iTo=sTxt.length;
		sTxt = "<p>"+sTxt.substring(0,iWhere)+"</p><p>"+sTxt.substring(iWhere+1, iTo)+"</p>";
	}

	var aspn = document.createElement('span');
	aspn.innerHTML="<h3>"+sTxt+"</h3>";
	atd.appendChild(aspn);
	return rnf;
}

function MakeLeftRightDiv(x, y)
{
	RemoveAdFrames();

	// adframe removed
	iLeftRight=0;
	oTitleDiv = MakeRecipeNameFrame	(x,y+240,320,80);
	document.body.appendChild(oTitleDiv);
	return;

	if (iLeftRight ==0)
	{
		oAdFrame = MakeAdFrame(x-120,y,120,240,"/adshim.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x-120,y+240,440,80);
		document.body.appendChild(oTitleDiv);
	}
	else if (iLeftRight == 1)
	{
		oAdFrame = MakeAdFrame(x-120,y+240,468,60,"/adshim2.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x-120,y,120,240);
		document.body.appendChild(oTitleDiv);

	}
	if (iLeftRight ==2)
	{
		oAdFrame = MakeAdFrame(x-120,y,120,240,"/adshim.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x-120,y-80,440,80);
		document.body.appendChild(oTitleDiv);
	}
	else if (iLeftRight == 3)
	{
		oAdFrame = MakeAdFrame(x-120,y-60,468,60,"/adshim2.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x-120,y,120,240);
		document.body.appendChild(oTitleDiv);
	}
	if (iLeftRight ==4)
	{
		oAdFrame = MakeAdFrame(x+320,y,120,240,"/adshim.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x,y+240,440,80);
		document.body.appendChild(oTitleDiv);
	}
	else if (iLeftRight == 5)
	{
		oAdFrame = MakeAdFrame(x-28,y+240,468,60,"/adshim2.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x+320,y,120,240);
		document.body.appendChild(oTitleDiv);

	}
	if (iLeftRight ==6)
	{
		oAdFrame = MakeAdFrame(x+320,y,120,240,"/adshim.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x,y-80,440,80);
		document.body.appendChild(oTitleDiv);
	}
	else if (iLeftRight == 7)
	{
		oAdFrame = MakeAdFrame(x-28,y-60,468,60,"/adshim2.html");
		document.body.appendChild(oAdFrame);

		oTitleDiv = MakeRecipeNameFrame	(x+320,y,120,240);
		document.body.appendChild(oTitleDiv);
	}

	iLeftRight++;
	if (iLeftRight>7) iLeftRight=0;
}

function RemoveAdFrames()
{
	if (tKillTimer!=null)
	{
		clearTimeout(tKillTimer);
		tKillTimer = null;
	}
	if (oAdFrame != null)
	{
		document.body.removeChild(oAdFrame);
		oAdFrame = null;
	}
	if (oTitleDiv != null)
	{
		document.body.removeChild(oTitleDiv);
		oTitleDiv = null;
	}
}

