// There are two objects defined in this file:
// "slide" - contains all the information for a single slide
// "slideshow" - consists of multiple slide objects and runs the slideshow 
document.getElementsByClass = function (needle)
{
	var my_array = document.getElementsByTagName("*");
	var retvalue = new Array();
	var i = 0;
	var j = 0; 
	for (i = 0, j= 0; i < my_array.length; i++)
	{
		var c = " " + my_array[i].className + " ";
		if (c.indexOf(" " + needle + " ") != -1)
		retvalue[j++] = my_array[i];
	}
	return retvalue;
} 
function doNav(n_current,activate)
{
	// get all navigation buttons
	buttons = document.getElementsByClass('but'); 
	// reset all navigation buttons
	for (i=0;i<buttons.length;i++) {
		buttons[i].src = "/images/but.gif";
	}
	// activate current navigation button
	if (activate)
	{
		n_current.src = "/images/but_selected.gif";
	}
} 
function slide(src,link,text)
{
	stop = false;
	this.src = src;
	this.link = link;
	this.text = text;
	if (document.images)
	{
		this.image = new Image();
  	}
  	this.loaded = false; 
  	this.load = function()
  	{
    		if (!document.images)
    		{
    			return;
    		}
    		if (!this.loaded)
    		{
      			this.image.src = this.src;
      			this.loaded = true;
    		}
  	} 
	/*
	this.hotlink = function()
	{
	// This method jumps to the slide's link. If a window was specified for the slide, then it opens a new window. 
	var mywindow; 
	// If this slide does not have a link, do nothing
	if (!this.link) return; 
	// Open the link in a separate window?
	if (this.target)
	{ 
		// If window attributes are specified, use them to open the new window
		if (this.attr)
		{
			mywindow = window.open(this.link, this.target, this.attr);  
      		}
      		else
      		{
 			// If window attributes are not specified, do not use them (this will copy the attributes from the originating window)
			mywindow = window.open(this.link, this.target);
		}		
		// Pop the window to the front
		if (mywindow && mywindow.focus) mywindow.focus();
    		}
    		else
    		{
      			// Open the link in the current window
      			document.getElementById(this.link).style.display = 'block';
    		}
	}
	*/ 
} 
function slideshow(slideshowname)
{
	this.name = slideshowname; 
	// When we reach the last slide, should we loop around to start the slideshow again?
	this.repeat = true; 
	// -1 = preload all images.
	//  0 = load each image is it is used.
	//  n = pre-fetch n images ahead of the current image.
	this.prefetch = -1;
	this.image;
	this.textid;
	this.timeout = 2000;
	this.slides = new Array();
	this.current = 0;
	this.timeoutid = 0; 
	this.add_slide = function(slide)
	{  
		var i = this.slides.length;
		if (this.prefetch == -1)
		{
			slide.load();
		}
		this.slides[i] = slide;
	} 
	this.play = function(timeout)
	{
		if (!stop) {
			// Make sure we're not already playing
			this.pause();  
			if (timeout)
			{
				this.timeout = timeout;
			} 
			// If the current slide has a custom timeout, use it; otherwise use the default timeout
			if (typeof this.slides[ this.current ].timeout != 'undefined')
			{
				timeout = this.slides[ this.current ].timeout;
			}
			else
			{
				timeout = this.timeout;
			}
				this.timeoutid = setTimeout( this.name + ".loop()", timeout);
		}
	} 
	this.pause = function()
	{
		// This method stops the slideshow if it is automatically running.
		if (this.timeoutid != 0)
		{
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		}
	} 
	this.stop = function()
	{
		//doNav('',false);
		// This method stops the slideshow if it is automatically running.
		if (this.timeoutid != 0)
		{
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		}
		stop = true;
	} 
	this.update = function()
	{
		// This method updates the slideshow image on the page. Make sure the slideshow has been initialized correctly
		if (! this.valid_image())
		{
			return;
		}   
		// Convenience variable for the current slide
		var slide = this.slides[ this.current ]; 
		// Update the image.
		this.image.src = slide.image.src; 
		// Update the text
    		this.display_text(); 
		buts = document.getElementsByClass('but');
		doNav(buts[this.current],true); 
		// Do we need to pre-fetch images?
		if (this.prefetch > 0)
		{
			var next, prev, count; 
			// Pre-fetch the next slide image(s)
			next = this.current;
			prev = this.current;
			count = 0;
			do
			{
				// Get the next and previous slide number
				// Loop past the ends of the slideshow if necessary
				if (++next >= this.slides.length) next = 0;
				if (--prev < 0) prev = this.slides.length - 1; 
				// Preload the slide image
				this.slides[next].load();
				this.slides[prev].load(); 
				// Keep going until we have fetched the designated number of slides
			}
			while (++count < this.prefetch);
		}
	} 
	this.next = function()
	{
		if (this.current < this.slides.length - 1)
		{
			this.current++;
		}
		else if (this.repeat)
		{
			this.current = 0;
		}
		this.update();
	} 
	this.get_text = function()
	{ 
	// This method returns the text of the current slide 
	return(this.slides[ this.current ].text);
	} 
	this.get_all_text = function(before_slide, after_slide)
	{ 
	// Return the text for all of the slides.
	// For the text of each slide, add "before_slide" in front of the text, and "after_slide" after the text.
	// For example:
	// document.write("<ul>");
	// document.write(s.get_all_text("<li>","\n"));
	// document.write("<\/ul>"); 
		all_text = ""; 
		// Loop through all the slides in the slideshow 
		for (i=0; i < this.slides.length; i++)
		{
		slide = this.slides[i];
    		if (slide.text)
    			{
				all_text += before_slide + slide.text + after_slide;
			}  
		}
  		return(all_text);
	} 
	this.display_text = function(text)
	{ 
	// Display the text for the current slide  
	// If the "text" arg was not supplied (usually it isn't), get the text from the slideshow 
		if (!text)
		{
      			text = this.slides[ this.current ].text;
		}
		// If a text id has been specified, then change the contents of the HTML element 
		if (this.textid)
		{
			r = this.getElementById(this.textid);
			if (!r)
			{
				return false;
			}
			if (typeof r.innerHTML == 'undefined')
			{
				return false;
			} 
		// Update the text 
		r.innerHTML = text;
		}
	} 
  	/*
	this.hotlink = function()
	{
		// This method calls the hotlink() method for the current slide.
		this.slides[ this.current ].hotlink();
		this.slides[ this.current ].text; 
	}
	*/ 
	this.noscript = function()
	{
		// This method is not for use as part of your slideshow, but you can call it to get a plain HTML version of the slideshow images and text.
		// You should copy the HTML and put it within a NOSCRIPT element, to give non-javascript browsers access to your slideshow information.
		// This also ensures that your slideshow text and images are indexed by search engines. 
		$html = "\n"; 
		// Loop through all the slides in the slideshow for (i=0; i < this.slides.length; i++)
		{
			slide = this.slides[i]; 
			$html += '<p>'; 
			if (slide.link)
			{
				$html += '<a href="' + slide.link + '">';
			} 
			$html += '<img src="' + slide.src + '" alt="slideshow image" \/>'; 
			if (slide.link)
			{
				$html += "<\/a>";
			} 
			$html += "<\/p>" + "\n\n";
		} 
		// Make the HTML browser-safe
		$html = $html.replace(/\&/g, "&amp;" );
		$html = $html.replace(/</g, "&lt;" );
		$html = $html.replace(/>/g, "&gt;" ); 
		return('<pre>' + $html + '</pre>');
	} 
	this.loop = function()
	{
		// This method is for internal use only.
		// This method gets called automatically by a JavaScript timeout.
		// It advances to the next slide, then sets the next timeout.
		// If the next slide image has not completed loading yet, then do not advance to the next slide yet. 
		// Make sure the next slide image has finished loading
		if (this.current < this.slides.length - 1)
		{
			next_slide = this.slides[this.current + 1];
			if (next_slide.image.complete == null || next_slide.image.complete)
			{
				this.next();
			}
		}
		else
		{
			// we're at the last slide
			this.next();
		}    
		// Keep playing the slideshow
		this.play( );
	} 
	this.valid_image = function()
	{  
		if (!this.image)
		{
			return false;
		}
		else
		{
			return true;
		}
	} 
	this.getElementById = function(element_id)
	{
		if (document.getElementById)
		{
			return document.getElementById(element_id);
		}
		else if (document.all)
		{
			return document.all[element_id];
		}
		else if (document.layers)
		{
			return document.layers[element_id];
		}
		else
		{
			return undefined;
		}
	}   
	this.set_image = function(imageobject)
	{
		s.image = document.images.myimagename;
		s.update();
	}
} 
function selectBut(id)
{
	if (lastID > 0)
	{
		document.getElementById(lastID).className = "but";
	}
	//document.getElementById(id).className = "butSelected";
	lastID = id;
}
