var iScrollLength, iScrollWidth, iScrollPx, iScrollPxMax;
var oScrollTimer;

//intialise our variable defaults
function IntialiseScroller () {
	try {
		iScrollLength	= document.getElementById("scroller").offsetWidth;
		iScrollWidth	= document.getElementById("scrollcontainer").offsetWidth - 5;
		iScrollPxMax	= iScrollLength - iScrollWidth;
		iScrollPxMax	= "-" + iScrollPxMax;
		iScrollPx		= 0;

		//check to see if our total length is less than we are already displaying
		if ( iScrollLength > (iScrollWidth+5) ) {		
			//hide both arrows
			ShowRightArrow();
			//ShowLeftArrow();
		}

		//alert("iScrollLength :: " + iScrollLength);
		//alert("iScrollWidth :: " + iScrollWidth);
		//alert("iScrollPxMax :: " + iScrollPxMax);
	}
	catch (err) {
		// do nothing
	}
}

function ShowLeftArrow() {
	//show the arrow
	document.getElementById("leftarrow").style.visibility	= "visible";
}

function ShowRightArrow() {
	//show the arrow
	document.getElementById("rightarrow").style.visibility = "visible";
}

function HideLeftArrow() {
	//hide the arrow
	document.getElementById("leftarrow").style.visibility	= "hidden";
}

function HideRightArrow() {
	//hide the arrow
	document.getElementById("rightarrow").style.visibility = "hidden";
}

function IsLeftArrowVisible () {
	if ( document.getElementById("leftarrow").style.visibility == "visible" ) {
		return true;
	}
	else {
		return false;
	}
}

function IsRightArrowVisible () {
	if ( document.getElementById("rightarrow").style.visibility == "visible" ) {
		return true;
	}
	else {
		return false;
	}
}

function ScrollRight () {
	//reduce our scroll amount (move to to the right)
	iScrollPx = iScrollPx - 2;

	//ensure that we have not scrolled too far
	if ( iScrollPx <= iScrollPxMax || iScrollPx > 0 ) {
		StopScroll();

		HideRightArrow();
	}
	else {
		//set our element to have the revised margin
		document.getElementById("scroller").style.marginLeft = iScrollPx + "px";	

		//log what we are doing
		//document.getElementById("scrollerLog").innerHTML = iScrollPx + "px";	

		//check to see if our other arrow is visible or not
		if ( !IsLeftArrowVisible() ) {
			ShowLeftArrow();
		}

		//set this function to run again in 50ms
		oScrollTimer = setTimeout("ScrollRight()",10);
	}
}

function ScrollLeft () {
	//reduce our scroll amount (move to to the right)
	iScrollPx = iScrollPx + 2;

	//ensure that we have not scrolled too far
	if ( iScrollPx <= iScrollPxMax || iScrollPx > 0 ) {	
		StopScroll();

		HideLeftArrow();
	}
	else {
		//set our element to have the revised margin
		document.getElementById("scroller").style.marginLeft = iScrollPx + "px";	

		//log what we are doing
		//document.getElementById("scrollerLog").innerHTML = iScrollPx + "px";	

		//check to see if our other arrow is visible or not
		if ( !IsRightArrowVisible() ) {
			ShowRightArrow();
		}

		//set this function to run again in 50ms
		oScrollTimer = setTimeout("ScrollLeft()",10);
	}
}

function StopScroll() {
	clearTimeout(oScrollTimer);
}

//Ensure that no matter what, the IntialiseScroller function is executed on the loading of the page
if (window.addEventListener)
window.addEventListener("load", IntialiseScroller, false)
else if (window.attachEvent)
window.attachEvent("onload", IntialiseScroller)
else if (document.getElementById)
window.onload=IntialiseScroller


/*
***************************************
* ChangePagePic
***************************************
*/

// Use this variable the time we wait before alerting the usre that their image
// is not loading correctly. Set the timer to 0 to disable the timer facility
var iTimerSeconds	= 30;

// Use this variable to set whether or not the script should attempt to look
// at the users current resolution and resize the enlarged picture so that
// it will fit on their screen
var bDynamicResize = true;

// Use this variable to set whether debug mode is on or not. It will issue many
// alerts that cover what is happening as the script is executed
var bDebugMode = false;

function ChangePagePic(sPicURL) {
	//ensure our previous picture has been hidden before loading this one
	HideChangePagePic();

	var iDivWidth, iDivHeight;
	var sImage;

	//hide our div that holds some existing images
	document.getElementById("ChangePicExistingImage").style.display = "none";

	//set up our main image details
	sImage = '<img src="' + sPicURL + '" alt="" border="0" id="ChangePagePicImage" />';

	//transfer the details to the form
	document.getElementById("ChangePagePicImageDiv").innerHTML = sImage;

	if ( bDebugMode ) alert("ChangePagePicPic :: Just set main image src"); 
	
	//show our loading image
	document.getElementById("ChangePagePicLoadingPic").style.display = "";

	if ( bDebugMode ) alert("ChangePagePicPic :: Just showed loading image"); 

	//unhide our div to show the loading image
	document.getElementById("ChangePagePic").style.display = "";

	//check to see if the image that we are showing has been fully loaded yet
	setTimeout('CheckChangePagePicPic(0)',500);

	if ( bDebugMode ) alert("ChangePagePicPic :: just 'checkeded' popup layer pic"); 
}

function CheckChangePagePicPic(iCount) {

	if ( bDebugMode ) alert("CheckChangePagePicPic :: start"); 
	if ( bDebugMode ) alert("CheckChangePagePicPic :: complete? " + document.getElementById("ChangePagePicImage").complete);
	if ( bDebugMode ) alert("CheckChangePagePicPic :: src > 0? " + (document.getElementById("ChangePagePicImage").src.length > 0));
	if ( bDebugMode ) alert("CheckChangePagePicPic :: counter :: " + iCount);

	var bStop		= false;
	var sMessage	= "";

	//Check that our user hasn't been waiting more than 30 seconds
	if ( iCount > iTimerSeconds*2 && iTimerSeconds != 0 ) {
		if ( bDebugMode ) alert("CheckChangePagePicPic :: counter too large - asking question");

		sMessage = sMessage + "The image appears to be taking a considerable amount of time to load.\n\n";
		sMessage = sMessage + "It is possible that the image path is not valid and that the image will never load successfully.\n\n";
		sMessage = sMessage + "Do you wish to continue to wait?";

		//Check to see if our user wants to exit
		if ( !confirm(sMessage) ) {
			//Remove the source from the image and hide the layer
			document.getElementById("ChangePagePicImage").src = "";
			HideChangePagePic();

			bStop = true;
		}
		else {
			iCount = 0;
		}
	}

	//Continue processing unless we have been requested to stop processing
	if ( !bStop ) {
		iCount++;

		//check to see if our main pic has loaded or not
		if ( document.getElementById("ChangePagePicImage").complete && document.getElementById("ChangePagePicImage").src.length > 0 ) {
			if ( bDebugMode ) alert("CheckChangePagePicPic :: is complete and have src"); 

			//our pic has loaded, show the image
			ShowChangePagePicPic();
		}
		else {
			//Wait and check to see if it is loaded in 0.5 seconds
			setTimeout('CheckChangePagePicPic(' + iCount + ')',500);
		}
	}
}

function ShowChangePagePicPic() {
	if ( bDebugMode ) alert("ShowChangePagePicPic :: start"); 

	//hide our loading image
	document.getElementById("ChangePagePicLoadingPic").style.display = "none";

	//show our main image and text
	document.getElementById("ChangePagePicImageDiv").style.display = "";
}

function HideChangePagePic () {
	//hide our main image and text
	document.getElementById("ChangePagePicImageDiv").style.display = "none";

	//show our loading image
	document.getElementById("ChangePagePicLoadingPic").style.display = "";

	//hide our div
	document.getElementById("ChangePagePic").style.display = "none";
}
