
/*************************** Multiple onload's function *****************************************************************/

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

	//addLoadEvent(hideList);
	addLoadEvent(prepareGallery);
	//addLoadEvent(theMarquee);
	addLoadEvent(setDefaultImage);
	addLoadEvent(pause);
	addLoadEvent(scrollLeft);
	addLoadEvent(calendarPopup);
	addLoadEvent(championshipTablePopup);
	addLoadEvent(webPopup);
	addLoadEvent(favPopup);
	addLoadEvent(addPopups);
	addLoadEvent(stadardImagePop);

/************************************************* Photo gallery *********************************************************/


function prepareGallery() {
		
	// Test for DOM capabilities & ensure elements exist
	if ( !document.getElementById || !document.getElementsByTagName || !document.getElementById("secondary-content")  ) return false;

	//Generate the slideshow div, as this function will only work with JavaScript enabled
		
		// Create container div for image
		var slideshowMovie = document.createElement("div");
		slideshowMovie.setAttribute("id", "slideshow");
		
		var placeholder = document.createElement("img");
		placeholder.setAttribute("src", "/img/rollover-gallery/newSlideshow4.jpg");
		placeholder.setAttribute("width", "2400");
		placeholder.setAttribute("height", "300");
		placeholder.setAttribute("id", "swap");
		placeholder.setAttribute("alt", "");
		
		// Swap the static image for the slideshow movie if JavaScript is enabled.
		var slideshowParent = document.getElementById("gallery");
		var slideshowStatic = document.getElementById("slideshow");
		slideshowParent.replaceChild(slideshowMovie ,slideshowStatic);
		
		// Append image to containg div
		slideshowMovie.appendChild(placeholder);
		
		// Get element that I want to append the div to and use the insertAfter function to enter it at the end of the element.
		var parent = document.getElementById("gallery");
		var getContainer = document.getElementById("window");
		parent.insertBefore(slideshowMovie, getContainer);
		
		
	// Set up event listener
		var photos = document.getElementById("nav-primary");
		var links = photos.getElementsByTagName("li");

				links[0].onmouseover = function() {
				moveElement("swap", -0, 0, 10)
				}
											
				links[1].onmouseover = function() {
				moveElement("swap", -300, 0, 10)
				}
				
				links[2].onmouseover = function() {
				moveElement("swap", -600, 0, 10)
				}
				
				links[3].onmouseover = function() {
				moveElement("swap", -900, 0, 10)
				}
				
				links[4].onmouseover = function() {
				moveElement("swap", -1200, 0, 10)
				}
								
				links[5].onmouseover = function() {
				moveElement("swap", -1500, 0, 10)
				}
						
				links[6].onmouseover = function() {
				moveElement("swap", -1800, 0, 10)
				}
				
				links[7].onmouseover = function() {
				moveElement("swap", -2100, 0, 10)
				}

	}


/* This function sets the image in the rollover window */
	function setDefaultImage() {
				
		var currentPosition = document.getElementById("swap");
		var currenturl = window.location.href;
		
		if ( (currenturl.indexOf("biography.php") != -1 ) || (currenturl.indexOf("/biography/") != -1) ) {
			var image = document.getElementById("swap");
			image.style.left = "-0px";
			return;
		}
		
		if ( (currenturl.indexOf("media.php") != -1) || (currenturl.indexOf("/news/") != -1 )  ) {
			var image = document.getElementById("swap");
			image.style.left = "-300px";
			return;
		}
		
		if ( (currenturl.indexOf("results.php") != -1) || (currenturl.indexOf("/results/") != -1 )  ) {
			var image = document.getElementById("swap");
			image.style.left = "-600px";
			return;
		}

		if ( currenturl.indexOf("carspec.php") != -1  ) {
			var image = document.getElementById("swap");
			image.style.left = "-900px";
			return;
		}

		if ( (currenturl.indexOf("theteam.php") != -1) || (currenturl.indexOf("/team/") != -1 )  ) {
			var image = document.getElementById("swap");
			image.style.left = "-1200px";
			return;
		}
		
		if ( (currenturl.indexOf("gallery.php") != -1) || (currenturl.indexOf("/gallery/") != -1 )  ) {
			var image = document.getElementById("swap");
			image.style.left = "-1500px";
			return;
		}
		
		if ( (currenturl.indexOf("supporters.php") != -1) || (currenturl.indexOf("/companies/") != -1 )  ) {
			var image = document.getElementById("swap");
			image.style.left = "-1800px";
			return;
		}
		
		if ( currenturl.indexOf("contact.php") != -1  ) {
			var image = document.getElementById("swap");
			image.style.left = "-2100px";
			return;
		}	
	}

/************************************************* Text Marquee *********************************************************/
	
function theMarquee() {

		
		if ( !document.getElementById || !document.getElementsByTagName || !document.getElementById("static-marquee") || !document.createElement ) return false;
		
		// Get existing static list, as the text within each item will be placed in the dynamic marquee
			var staticMarquee = document.getElementById("static-marquee");
			var items = staticMarquee.getElementsByTagName("a");
			
		// Create dynamic marquee
			var dynamicMarquee = document.createElement("div");
			dynamicMarquee.setAttribute("id", "marquee-container");
			
			var ul = document.createElement("ul");		
			ul.setAttribute("id", "ticker");
			
			// Create list items for the dynamic marquee.  This text stored in the static list are taken out and placed into the dynamic
			//marquee
			for ( var i = 0; i < items.length; i++ ) {
				
				/* Create all the dynamic links */
					var linksSource = items[i].getAttribute("href");
					var listText = items[i].lastChild.nodeValue;				
					var li = document.createElement("li");
					var a = document.createElement("a");
					a.setAttribute("href", linksSource);
					var bullet = document.createElement("img");
					bullet.setAttribute("src", "/img/marquee-bullet.gif")
					var point1 = document.createTextNode(listText);

				/* Append all the dynamic links */
					//a.appendChild(point1);
					li.appendChild(bullet);
					li.appendChild(point1);
					//li.appendChild(a);
					ul.appendChild(li);
			}

			dynamicMarquee.appendChild(ul);

		// Swap the static image for the slideshow movie if JavaScript is enabled.
			var marqueeParent = document.getElementById("secondary-content");
			var marqueeStatic = document.getElementById("static-marquee");
			marqueeParent.replaceChild(dynamicMarquee ,marqueeStatic);

}

	
// -------------------------
// Tickertape script to move a div left across a page creating scroll effect
// Seems to work on Win IE5+, firefox/Moz, Opera7, NN6+, Mac IE, Safari, Mac Netscape
// IE6 seems to require a hack to make scrolling work properly. Add a comment line as first line of HTML file
// ** Author: Marky Wong - 26 May 2004 **
// -------------------------

var xpos = 0;                	// init position of the ticker tape

function scrollLeft(){

	var displayWidth = 335;      // change to width of the display for ticker tape
	var tickerWidth = 4180;      // change to width of the ticker tape
	var decrement = 1;           // size of movement  
	var timedelay = 25;          // time delay for next step in milliseconds
	
	var ticker = document.getElementById("ticker");
	
	// wrap round when tickertape gets to end
    if (xpos > -tickerWidth){
        xpos -= decrement;
		ticker.style.left = xpos + "px";
    }else{
        xpos = displayWidth;
    }
    
    // move the ticker tape left
    loop = window.setTimeout("scrollLeft();", timedelay); 
	
}

function pause(){
		var ticker = document.getElementById("ticker")
		
		ticker.onmouseover = function() {
		window.clearTimeout(loop);
		}
		
		ticker.onmouseout = function() {
		scrollLeft();
		}
}


/******************* Popup window for race date calendar *************************/

function calendarPopup() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if ( links[i].className && (" " + links[i].className + " ").indexOf(" calander-link ") != -1 ) {
      links[i].onclick = function() {
        window.open(this.href, 'mypop', "height=400, width=500, status, resizable, scrollbars");
        return false;
      }
    }
  }
}

function championshipTablePopup() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if ( links[i].className && (" " + links[i].className + " ").indexOf(" champ-table ") != -1 ) {
      links[i].onclick = function() {
        window.open(this.href, 'mypop', "height=400, width=500, status, resizable, scrollbars");
        return false;
      }
    }
  }
}

function webPopup() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if ( links[i].className && (" " + links[i].className + " ").indexOf(" web-image ") != -1 ) {
      links[i].onclick = function() {
        window.open(this.href, 'mypop', "height=750, width=690, status, resizable, scrollbars");
		return false;
      }
    }
  }
}

function favPopup() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if ( links[i].className && (" " + links[i].className + " ").indexOf(" fav-image ") != -1 ) {
      links[i].onclick = function() {
        window.open(this.href, 'mypop', "height=405, width=570, status, resizable, scrollbars");
		return false;
      }
    }
  }
}

function stadardImagePop() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if ( links[i].className && (" " + links[i].className + " ").indexOf(" standard-pop ") != -1 ) {
      links[i].onclick = function() {
        window.open(this.href, 'mypop', "height=650, width=900, status, resizable, scrollbars");
		return false;
      }
    }
  }
}

/******************* Gallery Popup window *************************/

POPUP_WIN = 'my_image';
POPUP_FEATURES = 'width=570, height=405, status, resizable, scrollbars';

function addPopups(){

    // check that the functions we want to use are available
    if(document.getElementById && document.getElementsByTagName){
	if(!document.getElementById('thumb-gallery')) return false;
	// Get the gallery list
	var gallery = document.getElementById('thumb-gallery');

	// get all the anchors
	var thumbs = gallery.getElementsByTagName('a');
	var i;
	var num_thumbs = thumbs.length;

	// Assign popup function to all anchors in the gallery list
	for(i=0; i<num_thumbs; i++){
	    thumbs[i].onclick = popup;
	}
    }
}


// pop up window and set focus on it
// return false so page doesn't do the default action 
// and take to href location when user clicks on anchor
function popup(){
    var win = window.open(this.href, POPUP_WIN, POPUP_FEATURES);
    win.focus();
    return false;

}

	function popupWindowImageScroll(){		
	
		var currentImage = document.getElementsByTagName("img");


		// Create the links
		var parent = document.getElementById("link-navigation"); //Get parent container
		// Create previous link
			var previousLink = document.createElement("a");
			previousLink.className = "prev";
			prevText = document.createTextNode("Previous");
			previousLink.appendChild(prevText);
			parent.appendChild(previousLink);
		
		// Create Next link
			var nextLink = document.createElement("a");
			nextLink.className = "next";
			nextText = document.createTextNode("Next");
			nextLink.appendChild(nextText);
			parent.appendChild(nextLink);
		
		// Create Close Window link, replace existing back link with close window link
			var closeLink = document.getElementById("closewindow");
			currentCloseLinkText = closeLink.firstChild; 
			var newCloseLinkText = document.createTextNode("Close Window");
			closeLink.replaceChild(newCloseLinkText, currentCloseLinkText);
			closeLink.onclick = function() {
				self.close();
				return false;
			}
	
		// Get the current image source, then get the staring potition of the image number (not all images should be in the format 
		//mike60, mike59 etc.  Once this is obtained removed the .jpg from the remaining string to leave a number
			var createId =  currentImage[0].getAttribute("src");
			var startPos = createId.indexOf("large/mike");
			var restOfString = createId.substr(startPos +10);
			var imgNumber = parseInt(restOfString);
			
			//Set next and previous images
			var previousImageNumber = imgNumber - 1;
			var nextImageNumber = imgNumber + 1;
			var previous = ("image_popup.php?img=" + "mike" + previousImageNumber + ".jpg");
			var next = ("image_popup.php?img=" + "mike" + nextImageNumber + ".jpg");
		
		
		//replace the # in the link with the number obtained from the image, thus allowing us to 
		//get the previous and next images
			var links = document.getElementsByTagName("a");
			for (var i = 0; i < links.length; i++) {
				//Previous
				if ( links[i].className && (" " + links[i].className + " ").indexOf(" prev ") != -1 ) {	
					links[i].setAttribute("href", previous)
					
				}
				//Next
				if ( links[i].className && (" " + links[i].className + " ").indexOf(" next ") != -1) {	
					links[i].setAttribute("href", next)					
				}		
				
			}
	}

