//---------------------------------------------------------------------------------------------------------------------
// NEWS SCROLLER
//

var oNews;

addLoadEvent( function() {
	oNews = new NewsScroller();
});

function NewsScroller()
{
	this.newsTop=0;
	this.newsRestart; 
	this.doubleText = ""; 
	this.newsJump = 1;
	this.newsDelay = 75;

	this.newsDiv = document.getElementById("news");
	
	if (this.newsDiv)
	{
		this.doubleText = this.newsDiv.innerHTML;							// get the contents of the news container (this.newsDiv) we may need it
		this.newsRestart = this.newsDiv.clientHeight - 12;					// the height we can see if scrolling not needed
		
		this.newsDiv.style.overflow = "hidden";
		
		if (this.newsRestart > 250)
		{																	// it's is too big to all be seen
			this.newsDiv.style.height = "250px";
			this.newsDiv.innerHTML = this.doubleText + this.doubleText;		// double the contents so when it scrolls of the top there is the start still visible
			setInterval("oNews.NewsLoop()", this.newsDelay); 				// set timer now to call the doloop function every this.newsDelay ms
		}
		
		this.newsDiv.onmouseover = function() 
		{
			oNews.newsJump = 0;
		}
		
		this.newsDiv.onmouseout = function()
		{
			oNews.newsJump = 1;
		}
	}

	this.NewsLoop = function()											// this is called every this.newsDelay ms
	{												
		this.newsTop = Math.min((this.newsTop+this.newsJump),this.newsRestart);		// take either the top with the jump adjustment or the min the top cam go to
		this.newsTop = Math.max(this.newsTop,0);							// limit it to zero
		
		if (this.newsTop >= this.newsRestart)								// once we get to the second version ie back to false start set to the real start
		{
			this.newsTop = this.newsJump;
		}	
		
		try 
		{				
			this.newsDiv.scrollTop = this.newsTop;								// set the news attributes on the div this.newsDiv;
		}
		catch (ex) { }
	}
}



//---------------------------------------------------------------------------------------------------------------------
// IMAGE CYCLER
//

var arrImageCyclers = new Array();

function ImageCycler(divId, firstImage)
{
	arrImageCyclers.push(this);
	this.cyclerIndex = arrImageCyclers.length - 1;

	var nLastSlash = 0;
	while (firstImage.indexOf('/', nLastSlash + 1) != -1)
	{
		nLastSlash = firstImage.indexOf('/', nLastSlash + 1);
	}
	
	this.cycleTime = 5000;											// Time interval to cycle at
	this.directory = escape(firstImage.substring(0, nLastSlash));	// Name of the directory to find images in
	this.divId = divId;												// Id of the div in which images are to be displayed
	this.currentImage = -1;											// Integer index of the current image

	this.xml = new ActiveXObject("Microsoft.XMLDOM");				// XML which is the directory list
	this.xml.async = false;	
	this.xml.load("ImageDirectory.aspx?Directory=" + this.directory);
	
	this.autoChange = true;											// Cycles are only performed if autoChange is true;

	this.ControlsHtml = function()
	{
		var sHtml = "";
		
		if (this.xml.selectNodes("Files/File").length > 0)
		{
			sHtml =	
				"<div id=\"" + this.divId + "_controls\" class=\"imagecycler_controls\" style=\"visibility: hidden;\">" +
					"<img src=\"Images/media_step_back.gif\" 	id=\"" + this.divId + "_back\"		class=\"back\"		alt=\"back\"	onclick=\"arrImageCyclers[" + this.cyclerIndex + "].PreviousImage();\" />" +
					"<img src=\"Images/media_pause.gif\"		id=\"" + this.divId + "_pause\"		class=\"pause\"		alt=\"pause\"	onclick=\"arrImageCyclers[" + this.cyclerIndex + "].Pause();\" />" +
					"<img src=\"Images/media_step_forward.gif\" id=\"" + this.divId + "_forward\"	class=\"forward\"	alt=\"forward\" onclick=\"arrImageCyclers[" + this.cyclerIndex + "].NextImage(); \"/>" +
				"</div>" +
				"<div class=\"imagecycler_imagecontainer\"><img id=\"" + this.divId + "_image\" src=\"\" /></div>";
		}
		else 
		{
			document.getElementById(this.divId).style.display = "none";
		}
				
		return sHtml;
	}

	document.getElementById(this.divId).innerHTML = this.ControlsHtml();

	this.Pause = function()
	{
		if (this.autoChange == true)
		{
			document.getElementById(this.divId + "_pause").attributes["src"].value = "Images/media_play.gif";
			document.getElementById(this.divId + "_pause").attributes["alt"].value = "play";
			this.autoChange = false;
		}
		else
		{
			document.getElementById(this.divId + "_pause").attributes["src"].value = "Images/media_pause.gif";
			document.getElementById(this.divId + "_pause").attributes["alt"].value = "pause";
			this.autoChange = true;
		}
	}
	
	this.PreviousImage = function()
	{	
		if (this.xml.selectNodes("Files/File").length > 0)
			{
			this.currentImage -= 1;	
				
			if (this.currentImage < 0)
			{
				this.currentImage = this.xml.selectNodes("Files/File").length - 1;
			}
			
			document.getElementById(this.divId + "_image").attributes["src"].value = this.xml.selectNodes("Files/File")[this.currentImage].text;
		}
	}
	
	
	// Cycle to the next image in the set.	
	this.NextImage = function()
	{
		if (this.xml.selectNodes("Files/File").length > 0)
		{
			this.currentImage += 1;	
				
			if (this.currentImage >= this.xml.selectNodes("Files/File").length)
			{
				this.currentImage = 0;
			}
			
			document.getElementById(this.divId + "_image").attributes["src"].value = this.xml.selectNodes("Files/File")[this.currentImage].text;
		}
	}
	
	
	this.CycleImage = function()
	{
		if (this.autoChange == true && this.xml.selectNodes("Files/File").length > 0)
		{
			this.NextImage();
		}
		setTimeout("arrImageCyclers[" + this.cyclerIndex + "].CycleImage();", this.cycleTime);
	}
	
	// Kick off with a cycle and setting the interval to continue cycling.
	this.CycleImage();
	setTimeout("arrImageCyclers[" + this.cyclerIndex + "].CycleImage();", this.cycleTime);
	


	document.getElementById(this.divId).setAttribute("cyclerID", arrImageCyclers.length - 1);
	
	document.getElementById(this.divId).onmouseover = function() 
	{	
		for (var i = 0; i < this.childNodes.length; i++)
		{
			if (this.childNodes[i].className == "imagecycler_controls")
			{
				this.childNodes[i].style.visibility = "visible";
			}
		}
	}
	
	document.getElementById(this.divId).onmouseout = function() 
	{
		for (var i = 0; i < this.childNodes.length; i++)
		{
			if (this.childNodes[i].className == "imagecycler_controls")
			{
				this.childNodes[i].style.visibility = "hidden";
			}
		}
	}
}



//---------------------------------------------------------------------------------------------------------------------
// 1 COLUMN GALLERY
//
function ImageGallery1Col(divId, basePath)
{
	// Most of what we want this to be is an image cycler... Since all buttons refer to a global array anyway,
	// we can do a sort of dodgy inheritance ¬_¬
	this.imageCycler = new ImageCycler(divId, basePath);
	this.imageCycler.cycleTime = 15000;
	this.imageCycler.autoChange = false;


	this.divId = divId;
	this.basePath = basePath;
	
	// But we will need to reassign some functions.	
	this.imageCycler.ControlsHtml = function()
	{
		var sHtml = "";
		
		if (this.xml.selectNodes("Files/File").length > 0)
		{
			sHtml =	
				"<div class=\"imagegallery1col_header\"></div>" +
				"<div class=\"imagegallery1col_imagediv\" id=\"" + this.divId + "_imagediv\">" +
					"<img src=\"Images/gallerybottom.gif\"/>" +
					"<div id=\"" + this.divId + "_controls\" class=\"imagegallery1col_controls\">" +
						"<img src=\"Images/galleryarrow1.gif\"	id=\"" + this.divId + "_back\"		class=\"back\"		alt=\"back\"	onclick=\"arrImageCyclers[" + this.cyclerIndex + "].PreviousImage();\" />" +
						"<img src=\"Images/galleryarrow2.gif\"	id=\"" + this.divId + "_forward\"	class=\"forward\"	alt=\"forward\" onclick=\"arrImageCyclers[" + this.cyclerIndex + "].NextImage(); \"/>" +
					"</div>"
				"</div>";
		}
		else 
		{
			document.getElementById(this.divId).style.display = "none";
		}
				
		return sHtml;
	}

	document.getElementById(this.divId).innerHTML = this.imageCycler.ControlsHtml();
	
	this.imageCycler.PreviousImage = function()
	{	
		if (this.xml.selectNodes("Files/File").length > 0)
		{
			this.currentImage -= 1;	
				
			if (this.currentImage < 0)
			{
				this.currentImage = this.xml.selectNodes("Files/File").length - 1;
			}
			
			document.getElementById(this.divId + "_imagediv").style.backgroundImage = 
				"url('" + this.xml.selectNodes("Files/File")[this.currentImage].text + "')";
		}
	}
	
	this.imageCycler.NextImage = function()
	{
		if (this.xml.selectNodes("Files/File").length > 0)
		{
			this.currentImage += 1;	
				
			if (this.currentImage >= this.xml.selectNodes("Files/File").length)
			{
				this.currentImage = 0;
			}
			
			document.getElementById(this.divId + "_imagediv").style.backgroundImage = 
				"url('" + this.xml.selectNodes("Files/File")[this.currentImage].text + "')";
		}
	}
	
	document.getElementById(this.divId).onmouseover = null;
	document.getElementById(this.divId).onmouseout = null;
	
	// Fire off NextImage() again because the first one will be invalid.
	this.imageCycler.NextImage();
}