onFlash = 0;

function so_clearInnerHTML(obj) {
	// so long as obj has children, remove them
	while(obj.firstChild) obj.removeChild(obj.firstChild);
}

function addText(node, text)
{
	var label = document.createTextNode(text);
	node.appendChild(label);
}

function addLink(node, text, href, onclickimage, onclickg)
{
	var l = document.createElement("a");
	l.setAttribute("href", href); 
	if(onclickimage)
	{
		l.onclick = function(){return swap(onclickimage, onclickg);};
	}
	addText(l, text);
	node.appendChild(l);
}

function swap(image,g,lnk)
{
	if(onFlash)
	{
		onFlash = 0;
		pDiv = document.createElement("p");
		eDIV = document.createElement("img");
		eDIV.setAttribute("src","paintings/med_"+image);
		
		addText(pDiv, 'To explore the surface of this image, click ');
		addLink(pDiv, 'here' ,"paintings/"+image, image, g);
		addText(pDiv, ', or ');
		if(lnk=='e')
		{
			addLink(pDiv, 'return to the exhibition', "exhibitions.php?g="+g, null);
		}
		else
		{
			addLink(pDiv, 'return to the gallery', "galleries.php?g="+g, null);
		}
		addText(pDiv, '.');
		
		obj = document.getElementById("swap");
		so_clearInnerHTML(obj);
		obj.appendChild(pDiv);
		obj.appendChild(eDIV);
	}
	else
	{
		d = document.getElementById("swap");
		thelink = (lnk=='e') ? '<a href="exhibitions.php?g='+g+'">return to the exhibition</a>' :  '<a href="galleries.php?g='+g+'">return to the gallery</a>';
		d.innerHTML = '<p>To see the whole painting, click <a onclick="return swap(\''+image+'\', \''+g+'\', \''+lnk+'\');" href="paintings/'+image+'">here</a>, or '+thelink+'.</p><object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle" height="300" width="550"><param allowscriptaccess="sameDomain"><param movie="imageviewer.swf?image=paintings/'+image+'"><param quality="high"><param bgcolor="#ffffff"><param name="SRC" value="imageviewer.swf?image=paintings/'+image+'" /><embed pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="sameDomain" name="imageviewer" bgcolor="#ffffff" quality="high" src="imageviewer.swf?image=paintings/'+image+'" align="middle" height="300" width="550"></object>';
		onFlash = 1;
	}

  	return false;
}

function ingrandire()
{
		i = document.getElementById("toswap");
		var instr = document.getElementById('instructions');
		src = i.getAttribute('src');
	if(onBig)
	{
		onBig = 0;
		i.setAttribute('src', src.replace('photos/','photos/med_'));
		instr.innerHTML = 'To see this image full-size, click <a onClick="ingrandire()" style="cursor:pointer;">here</a>.';
	}
	else
	{
		onBig = 1;
		i.setAttribute('src', src.replace('med_',''));
		instr.innerHTML = 'To see this image at a smaller size, click <a onClick="ingrandire()" style="cursor:pointer;">here</a>.';
	}

  	return false;
}

function doSwap(image)
{
	d = document.getElementById("swap");
	d.innerHTML = '<p id="instructions" style="margin: 15px 0;">To see this image full-size, click <a onClick="ingrandire()" style="cursor:pointer;">here</a>.</p><img id="toswap" src="photos/med_'+image+'" />';
	onBig = 0;
	location.href = '#swapanch';
  	return false;
}
onBig = 0;
