// Wrapper to get a cross-browser XMLHttp object
function GetXmlHTTPObject()
{
	var xmlhttp = null;

	if (window.XMLHttpRequest) // Firefox/Mozilla
		xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject) // IE Version
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	
	return xmlhttp;
}

function GetImages(element, transform)
{
	var http = GetXmlHTTPObject();
	
	if(http)
	{
		var url = 'imagescroller.aspx?r=' + new Date().getTime();
		
		http.open('GET', url, true); 
		
		// Wire up the event handler and send the request
		http.onreadystatechange = function() { HandleHttpResponse(http, element, transform); }; 
		http.send(null);

		return false;
	}
	else
	{
		return true;
	}
}

// Code to handle the RSS XML response
function HandleHttpResponse(http, element, transform) 
{ 				
	// If the request is complete
	if(http.readyState == 4)
	{
		element.innerHTML = (transform) ? transform(http.responseText) : http.responseText;
		InitImageScroller(document.getElementById('scrollcontainer'));
	}
	
	// Kill the XMLHttp object
	http = null;
}

var scroll1;
var scroll2;
var timeout;
var img;
var pause;
var pauseindex = 6;
var containeroffset;
var imageopen = false;

function InitImageScroller(container)
{	
	containeroffset = FindX(container);
	
	scroll1 = document.getElementById('scroll1');
	scroll1.style.left = '0';
	
	img = container.getElementsByTagName('img');
	pause = img[pauseindex];
	
	scroll2 = document.getElementById('scroll2');
	scroll2.style.left = scroll1.offsetWidth + 'px';
	
	document.getElementById('scrollcontrol').getElementsByTagName('a')[0].onclick = function() { return false; } // function() { imageopen = false; ScrollImages(); return false; };
	
	// Cannot use mouseover pausing in Safari because it fires both events on mouseover (?!!)
	if(navigator.vendor && navigator.vendor.indexOf('Safari'))
	{
		scroll1.speed = 2;
		scroll2.speed = 2;
	}
	else
	{
		var a = container.getElementsByTagName('a');
		
		for(var x=0; x<a.length; x++)
		{
			a[x].onmouseover = function() { clearTimeout(timeout); }
			a[x].onmouseout = function() { ScrollImages() };
			
			a[x].onclick = function() { imageopen = true; clearTimeout(timeout); return hs.expand(this); }
		}
		
		scroll1.speed = 3;
		scroll2.speed = 3;
	}
	
	scroll1.active = false;
	scroll2.active = true;

	timeout = setTimeout(ScrollImages, 3000);
}

function FindX(obj)
{
	var curleft = 0;
	
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
		
	return curleft;
}

function ScrollImages()
{
	if(!imageopen)
	{
		if(scroll2.active)
		{
			if(parseInt(scroll2.style.left, 10) <= 0)
			{
				scroll1.style.left = scroll2.offsetWidth + 'px';
				scroll1.active = true;
				scroll2.active = false;
			}
		}
		else if(scroll1.active)
		{
			if(parseInt(scroll1.style.left, 10) <= 0)
			{
				scroll2.style.left = scroll1.offsetWidth + 'px';
				scroll2.active = true;
				scroll1.active = false;
			}
		}
		
		scroll1.style.left = (parseInt(scroll1.style.left, 10) - 3) + 'px';
		scroll2.style.left = (parseInt(scroll2.style.left, 10) - 3) + 'px';
		
		var pausex = parseInt(FindX(pause), 10) - containeroffset;
		
		// If the item to pause on has reached the left of the container
		if(pausex <= 3)
		{
			// Tidy up and get it to the left
			while(pausex > 0)
			{
				scroll1.style.left = (parseInt(scroll1.style.left, 10) - 1) + 'px';
				scroll2.style.left = (parseInt(scroll2.style.left, 10) - 1) + 'px';
				pausex--;
			}
			
			// Move the pause index on 3
			pauseindex += 6;
			
			if(pauseindex < img.length)
			{
				pause = img[pauseindex];
			}
			else
			{
				// We've gone past the end, reset the index
				pauseindex = 3;
				pause = img[pauseindex];
			}
			
			timeout = setTimeout(ScrollImages, 3000);
		}
		else
		{
			timeout = setTimeout(ScrollImages, 35);
		}
	}
}