//-------------------------------------------------------------------------------------------//
// JavaScript Document
// Macromedia scripts, renamed, commented, correctly formatted -- Chris Allsopp

function isIE()
{
	return (navigator.appVersion.indexOf("MSIE")!=-1);
}

function isMozilla()
{
	return (navigator.appName == "Netscape");
}

function ReplaceAll(str, from, to) 
{
	var index = str.indexOf( from );

    while ( index > -1 ) 
	{
        str = str.replace( from, to );
        index = str.indexOf( from );
    }
    return str;
}

function MakeURLSafe(str)
{
	str = ReplaceAll(str, " ", "%20");
	str = ReplaceAll(str, "'", "");
	return str;
}

function OpenPopupWindow(URL) {

	var popup = window.open(URL, '', 'width=200px,height=200px,resizable=yes,statusbar=no');
	popup.focus(); 
	return false;
}

function preloadImages() { 
 	
	if(document.images){ // check if their are any images on the page
		if(!document.preloadImgs) {
			document.preloadImgs=new Array(); // create the document wide variable if not already done so
		}
				
	    var i = 0; //loop variable
		var j = document.preloadImgs.length; // number of items in array
		var	args = preloadImages.arguments;  // args is the list of image names to be preloaded
		
		for(i=0; i < args.length; i++) {
		    if (args[i].indexOf("#") !=0 ) { // ignore if image name starts with #
				// add the image to the global (document) list of images
				document.preloadImgs[j] = new Image; 
				document.preloadImgs[j++].src = args[i]; // assign to the src= property off the image tag
			}
		}
	}
}

function swapImage() { //v3.0
   
   var i, j = 0;
   var foundObj = null;
   var args = swapImage.arguments; 
   document.swappedImgs = new Array; 
   
	// loop index increment by 3 because MM stores rollover data as triples
	// {image to be swapped out, <not used>, image to be swapped in}
   for(i=0; i < (args.length-2); i+=3) {
	   // ensure the object can be found in the document model
	   if ((foundObj=findObj(args[i]))!=null)
	   {  
   		 document.swappedImgs[j++] = foundObj; // assign foundobject to the global array
		 // make sure the oldSrc property of the object is not set
		 if(!foundObj.oldSrc) 
		 {
			 foundObj.oldSrc = foundObj.src; 
		 }
		 var arg_src = args[i+2];
		// if(foundObj.src != arg_src)
		 //{
		 	foundObj.src = arg_src; // set new source name to the 'image to be swapped in' arg
		// }
	  }
   }
}


function swapImgRestore() { 
	
	var i = 0;
	var obj = null; 
	var sImgs = document.swappedImgs;
	// Check to make sure document.swappedImgs is setup.
	// Checks to ensure loop counter isn't out of bounds.
	// Set and check object to equal the value in the document.swappedImgs;
	// Check to see if the image has the oldSrc property set from the swap image function.
	
	for(i=0; sImgs && (i < sImgs.length) && (obj = sImgs[i]) && obj.oldSrc; i++) {
		obj.src = obj.oldSrc; // restore source name to the old name
	}
}


function findObj(name, doc) { //v4.01
	
	var i = 0;
	var pos = name.indexOf("?"); // find the index of '?' character in 'name'
	var obj = null; 
	if(!doc) doc=document; // set up variable 'doc' to be document if not defined

	// I think the name of the frame containing the object is appended to the
	// object name and prefixed with a '?' delimiter. If a '?' is found, the
	// doc variable is set to the correct frames document. 
	// Finally the '?<name>' portion of the string is removed.
	if((pos > 0) && parent.frames.length) {
	    doc = parent.frames[n.substring(pos+1)].document;
		name = name.substring(0,pos);
	}
	
	// assign the obj variable to the correct element of the document
	obj = doc[name];
  	if(!obj && doc.all) { 
		obj = doc.all[name]; 
	}
	// if still no object has been found, look in forms
	for(i=0; !obj && (i < doc.forms.length); i++) {
		obj = doc.forms[i][name];
	}
    // again, if no object was found in the forms or dox, look in the layers
	for(i=0; !obj && doc.layers && (i < doc.layers.length); i++) {
		obj = findObj(name, doc.layers[i].document); // recursive call to function
	}
	// finally if no object has been found, call the document's getElementById method if it exists
	if(!obj && doc.getElementById) {
		obj = doc.getElementById(name); 
	}
	return obj;
}


// arg[0] = "down" | "over" | "out" | "init"
// arg[1] = group name if event=="init" otherwise = nav button name??
// arg[2] = image name
// arg[3] = alternate image name (based on type of event) 
// e.g. if event="down", arg[3] = " image name of 'down' nav button
function navbarGroup(event, grpName) { //v6.0
	
	var i = 0;
	var img = null;
	var navbarArr = null;
	var args=navbarGroup.arguments;


	// "init" specifies which button in the navbar is initially set to the 'down' (pushed) state
	if(event == "init" && args.length > 2) {
		img = findObj(args[2]);
		if((img != null) && !img.init) { // if image is found and has not been initialised
		    img.init = true; 
			img.up   = args[3]; 
			img.dn   = img.src;
			navbarArr = document[grpName];

			if(navbarArr == null) {
				navbarArr = document[grpName] = new Array();
			}
			
			navbarArr[navbarArr.length] = img; // add img to the end of the array

			for (i=4; i < args.length-1; i+=2) {
				img = findObj(args[i]);
				if(img != null) {
			        if (!img.up) {
						img.up = img.src;
					}
					img.src = img.dn = args[i+1];
			        navbarArr[navbarArr.length] = img; // add img to the end of the array
				}
    		}
		}
	// event "over" specifies hover behaviour
	} else if (event == "over") {
		document.navbarOver = navbarArr = new Array();
     	
		for (i=1; i < args.length-1; i+=3) {
			img = findObj(args[i]);
			if(img != null) {
		       if (!img.up) {
				   img.up = img.src;
			   }
		       img.src = (img.dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img._up);
 		   	   navbarArr[navbarArr.length] = img; // add img to the end of the array
			}
	     }
	// event "out" specifies behaviour on mouseout
	} else if (event == "out" ) {
	    
		for (i=0; i < document.navbarOver.length; i++) {
		    img = document.navbarOver[i]; 
			img.src = (img.dn) ? img.dn : img.up; 
		}
	// event "down" specifies behaviour on click
	} else if (event == "down") {
    	
		navbarArr = document[grpName];
	    if(navbarArr) {
		   for(i=0; i < navbarArr.length; i++) { 
			  img = navbarArr[i]; 
			  img.src = img.up; 
			  img.dn  = null; 
		   }
		}
		
		document[grpName] = navbarArr = new Array();
			 
		for (i=2; i < args.length-1; i+=2) {
			img = findObj(args[i]);
			if (img != null) {
			   	if(!img.up) {
					img.up = img.src;
				}
			    img.src = img.dn = (args[i+1]) ? args[i+1] : img.up;
	     		navbarArr[navbarArr.length] = img; // add img to the end of the array
			}
		}
	} // end event switch
}

//Reads thumbnail images from ini file and place into motion gallery
function GenerateThumbnails(imgData, topmostImg) {
	
	GenerateThumbnail(topmostImg, imgData);
	
	for(var key in imgData) {
		if(!(key === topmostImg)) {
			GenerateThumbnail(key, imgData);
		}
	}
}

function GenerateThumbnail(key, imgData) {

	if(key === 'topmost_image') return;

	document.write("<a href=\"#\" onClick=\"GalleryChange('" + key + "'," + " imgData['" + key + "'] )\" >" +
 				   "<img src=images/thumbs/" + key + " width=\"100\" height=\"100\" " +
				   "id=\"" + key + "thumb\"></a>\n");	
}

// Changes an image
function ChangeImageSrc(imgIdentifier, src) {
	var image = findObj(imgIdentifier);
    image.src = src;
}

//************************************************
// DOM Manipulation Functions


// Nests a div inside a DOM element
function NestDivClass(parentName, nestedDivName) {
	var parent    = findObj(parentName);
	var nestedDiv = document.createElement('div');
	nestedDiv.setAttribute('class', nestedDivName);
	parent.appendChild(nestedDiv);
	return nestedDiv;
}

function NestDivId(parentName, nestedDivName) {
	var parent    = findObj(parentName);
	var nestedDiv = document.createElement('div');
	nestedDiv.setAttribute('id', nestedDivName);
	parent.appendChild(nestedDiv);
	return nestedDiv;
}

// Nests a div inside a DOM element
function NestImg(parentName, src) {
	var parent  = findObj(parentName);
	var img     = document.createElement('img');
	img.setAttribute('src', src);
	parent.appendChild(img);
	return img;
}

// Removes a div from a DOM element
function RemoveDiv(parentName, nestedDivName) {
	var parent    = findObj(parentName);
	var nestedDiv = findObj(nestedDivName);
	parent.removeChild(nestedDiv);
	return nestedDiv;
}

// Removes all child divs from a DOM element
function ClearDivIncludingTextNodes(divName) {
	
	var div = findObj(divName);

	while(parseInt(div.childNodes.length) > 0){
		div.removeChild(div.lastChild);
    }   
	
	return div;
}

// Removes all child divs from a DOM element excluding text nodes
function ClearDiv(divName) {
	var div = findObj(divName);
	var textChildCount = parseInt(0);
	
	while((parseInt(div.childNodes.length) - textChildCount) > 0)
	{
		if(div.lastChild.nodeName != "#text") {
			div.removeChild(div.lastChild);
		} else {
			textChildCount++;
		}
    }   
	return div;
}

