// 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("galleryhold")) return false;
	var stripgallery=document.getElementById("motiongallery");
	var striplinks=stripgallery.getElementsByTagName("a");
	var fullgallery=document.getElementById("galleryhold");
	var gallerypics=fullgallery.getElementsByTagName("img");
	for(var i=0; i<striplinks.length; i++)
	{
		striplinks[i].thispic=gallerypics[i].getAttribute("src");
		striplinks[i].thisindex=i;
		striplinks[i].onclick=function() {
			showNewGallery(this.thispic,this.thisindex);
			return false;
			
		}
	}
	
	document.getElementById("galleryhold").style.display="none";
	document.getElementById("galleryprime").style.display="block";
	document.getElementById("slidecontain").style.visibility="visible";

}

addLoadEvent(gallerySetUp);

function showNewGallery(valuein,picindex) {
	
	var picsource=valuein;
	var thisindex=picindex;
	var galleryhold= document.getElementById("galleryhold");
	var primegallery = document.getElementById("galleryprimetext");
	var gallerypics = galleryhold.getElementsByTagName("img");
	//remove all of the prime gallery text
	while(primegallery.hasChildNodes()) {
		primegallery.removeChild(primegallery.lastChild);
	}
	//replace the picture
	for (var i=0; i<gallerypics.length; i++)
	{
			var thispic = gallerypics[i].getAttribute("src");
			if (thispic==picsource) {
				
				var ppic=document.getElementById("galleryprimeimage");
				var thebigpic=ppic.getElementsByTagName("img");
				thebigpic[0].setAttribute("src", picsource);
				//replace the title
				var titles=galleryhold.getElementsByTagName("h2");
				var thistitle=titles[i].firstChild.nodeValue;
				var newtitle=document.createElement("h2");
				
				var inserttitle=document.createTextNode(thistitle);
				newtitle.appendChild(inserttitle);
				primegallery.appendChild(newtitle);
				//step through the paragraph nodes and insert them
				var nextnode=titles[i];
				var newpara;
				var onepara;
				while (nextnode.nodeName!="A")
				{
					nextnode=moveOneNode(nextnode);
					
					if (nextnode.nodeName=="P")
					{
						newpara=document.createElement("p");
						onepara=document.createTextNode(nextnode.firstChild.nodeValue);
						newpara.appendChild(onepara);
						primegallery.appendChild(newpara);
					}
					
				}
				//insert the link to the client site
				var newlink=document.createElement("a");
				var linktext=document.createTextNode(nextnode.firstChild.nodeValue);
				newlink.appendChild(linktext);
				var setlink=nextnode.getAttribute("href");
				newlink.setAttribute("href",setlink);
				primegallery.appendChild(newlink);
			}
	
	}
	//replace the title
	
}
//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;
}

	