// JavaScript Document

function addLoadEvent(func) {
	var oldonload=window.onload;
	if(typeof(window.onload) != 'function')
	   {
		   window.onload = func;
	   }
	   else
	   {
		   window.onload = function() {
			   oldonload();
			   func();
		   }
	   }
}

function gallerySetUp() {
	
	if(!document.getElementsByTagName) return false;	
	if(!document.getElementById) return false;	
	if(!document.getElementById("motiongallery")) return false;	
	if(!document.getElementById("galleryshow")) return false;
	var stripgallery=document.getElementById("motiongallery")
	var gallerylinks=stripgallery.getElementsByTagName("a");
	for(var i=0; i<gallerylinks.length; i++)
	{	
		
		gallerylinks[i].onclick=function() {
			showNewGallery(this);
			return false;
		}
	}
	document.getElementById("galleryshow").style.display="none";
	document.getElementById("primeideabookcontain").style.display="block";
	document.getElementById("filmstripgallery").style.visibility="visible";

}

addLoadEvent(gallerySetUp);

function showNewGallery(valuein) {
	var picsource=valuein.firstChild.getAttribute("src");
	var idbook = document.getElementById("contentideabook");
	var primegallery = document.getElementById("primegallerytext");
	var idbookpics = idbook.getElementsByTagName("img");
	for (var i=0; i<idbookpics.length; i++)
	{
			var thispic = idbookpics[i].getAttribute("src");
			if (thispic==picsource) {
				var ppic=document.getElementById("primepiccontain");
				var thebigpic=ppic.getElementsByTagName("img");
				thebigpic[0].setAttribute("src", picsource);
				//now load the thumbnails
				var thmbpos = document.getElementById("primethmbcontain");
				var primethmbs = thmbpos.getElementsByTagName("img");
				thmbstart=i-8;
				picstart=i-7;
				for (j=0; j< primethmbs.length/2; j++)
				{
					var newthmb = idbookpics[thmbstart+(j*2)].getAttribute("src");
					var thisnewpic = idbookpics[picstart+(j*2)].getAttribute("src");
					primethmbs[(j*2)].setAttribute("src", newthmb);
					primethmbs[(j*2)+1].setAttribute("src", thisnewpic);

				}
			}
	}
	var putingallery = document.getElementById("primegallerytext");
	var primetitle = putingallery.getElementsByTagName("h3");
	var primetext = putingallery.getElementsByTagName("p");
	var newtitle=valuein.firstChild.getAttribute("title");
	var idbooktitles= idbook.getElementsByTagName("h3");
	//replace the title
	for (var j=0; j<idbooktitles.length; j++)
	{
		var thistitle = idbooktitles[j].firstChild.nodeValue;
		if (thistitle==newtitle)
		{
			currenttitletag = idbooktitles[j];
			primetitle[0].firstChild.nodeValue=thistitle;	
		}
		
	}
	//Step through the paragraph nodes, replace the prime nodes with the current ones
	//use the image make an appointment to define where we stop
	var currentbooktag=moveOneNode(currenttitletag);
	var currentprimetag=moveOneNode(primetitle[0]);
	//put in sub-title
	currentprimetag.firstChild.nodeValue=currentbooktag.firstChild.nodeValue;

	
		var stopflag=0;
	do {
			currentbooktag=moveOneNode(currentbooktag);
			//when we reach the make an appointment link, we are done
			if (currentbooktag.nodeName=="A")
			{
				stopflag=1;  //stop the loop
				//clean up any leftover text nodes in the prime space
				while (!checkForImg(currentprimetag))
				{
					currentprimetag=moveOneNode(currentprimetag);
					if (currentprimetag.nodeType==1)
					{
						currentprimetag.firstChild.nodeValue=" ";
					}
				}
				
			}
			else
			{
				//replace this prime node with the idea book text
				if (!checkForImg(currentprimetag))
				{
					currentprimetag=moveOneNode(currentprimetag);
					currentprimetag.firstChild.nodeValue=(currentbooktag.firstChild.nodeValue);

				}
				else
				{
					//we are out of prime nodes, create one then place ideabook text in there
					var newp = document.createElement("P");
					var newprimenode=document.createTextNode(currentbooktag.firstChild.nodeValue);
					newp.appendChild(newprimenode);
					primegallery.insertBefore(newp,currentprimetag.nextSibling);
					currentprimetag=moveOneNode(currentprimetag);
				}

			}
	} while (stopflag==0);
}
//function to ensure IE6 moves to the next element node
function moveOneNode(intag) {
	intag=intag.nextSibling;
	while(intag.nodeType!=1) 
	{
		intag=intag.nextSibling;
		if (intag.nodeName=="A") return intag;
	}
	return intag;
}
//function to check and see if we are at the image node link yet
function checkForImg(intag) {
	intag=intag.nextSibling;
	while(intag.nodeType!=1) 
	{
		intag=intag.nextSibling;
	}
	if (intag.nodeName=="A") 
	{
	return true;
	}
	else
	return false;
}

	