var rotatorImageCount =0;
var rotatorImageWidth = 240;
var rotatorPause = 7000;
var rotatorSpeed = 10;
var rotatorJump = 0;
var rotatorPlaying = true;
var rotatorCurrentSpeed = rotatorSpeed;
var rotatorStopRequested = false;
var rotatorFirstTime = true;

function jumpToAd(jumpIndex)
{
	document.getElementById('myContent').style.left = parseInt(jumpIndex - 1) * (-1 * rotatorImageWidth) + "px";

	for (i=1;i<=rotatorImageCount;i++)
	{
		document.getElementById('rotator' + i).src = "images/" + i + ".jpg"
	}

	document.getElementById('rotator' + jumpIndex).src = "images/" + jumpIndex + "g.jpg"

	pauseRotator();
}

function toggleRotating()
{
	if (rotatorPlaying)
	{
		pauseRotator();
	}
	else
	{
		playRotator();
	}
}

function pauseRotator()
{
	rotatorPlaying = false;
	rotatorStopRequested = true;

	document.getElementById('rotatorPlayPause').src = "images/play.jpg";
}

function playRotator()
{
	rotatorPlaying = true;
	rotatorStopRequested = false;

	document.getElementById('rotatorPlayPause').src = "images/pause.jpg";
}

function slideContent()
{
	var lefty;
	var myWidth;
	var pauseTime;
	var adNumber;
	var i;

	lefty = document.getElementById('myContent').style.left;
	lefty = lefty.replace("px","");

	if ((lefty == 0) || (lefty % rotatorImageWidth == 0))
	{
		if (rotatorStopRequested)
		{
			rotatorCurrentSpeed = 0;
		}
		else
		{
			rotatorCurrentSpeed = rotatorSpeed;
		}
	}

	myWidth = document.getElementById('myContent').style.width;
	myWidth = myWidth.replace("px","");

	if ((myWidth * -1) >= (lefty - 1))
	{
		lefty = 0;
	}
	else
	{
		lefty = lefty - rotatorCurrentSpeed;	
	}

	if (rotatorFirstTime)
	{
		document.getElementById('myContent').style.left = "0px";
	}
	else
	{
		document.getElementById('myContent').style.left = lefty + "px";
	}

	if ((lefty == 0) || (lefty % rotatorImageWidth == 0))
	{
		if (parseInt(lefty) == parseInt(rotatorImageWidth) * parseInt(rotatorImageCount) * -1)
		{
			setTimeout('slideContent()',1);
		}
		else
		{
			for (i=1;i<=rotatorImageCount;i++)
			{
				document.getElementById('rotator' + i).src = "images/" + i + ".jpg"
			}

			adNumber = (lefty / rotatorImageWidth * -1) + 1;

			document.getElementById('rotator' + adNumber).src = "images/" + adNumber + "g.jpg"
			setTimeout('slideContent()',rotatorPause);
		}
	}	
	else
	{
		if (rotatorFirstTime)
		{
			rotatorFirstTime = false;
			document.getElementById('rotator1').src = "images/1g.jpg"
			setTimeout('slideContent()',rotatorPause);				
		}
		else
		{
			setTimeout('slideContent()',1);
		}
	}
}

function addItem(imgPath,linkURL,target)
{
	var myImgElement;
	var myAElement;
	var myWidth;

	myAElement=document.createElement('a');
	myImgElement=document.createElement('img');
	myImgElement.src=imgPath;

	myImgElement.border='0';
	myAElement.appendChild(myImgElement);
	myAElement.href=linkURL;

	if (trim(target) != "")
	{
		myAElement.target=target;
	}

	document.getElementById('myContent').appendChild(myAElement);

	myWidth = document.getElementById('myContent').style.width;
	myWidth = myWidth.replace("px","");
	document.getElementById('myContent').style.width = parseInt(myWidth) + parseInt(rotatorImageWidth) + "px";

	rotatorImageCount++;
}

function addImages()
{
	addItem("images/rotatorULP.jpg","default.asp?r=6",""); 
	addItem("images/rotatorProfessional.jpg","default.asp?t=60",""); 
	addItem("images/rotatorFacebook.jpg","http://www.facebook.com/pages/Canadian-Amputee-Hockey-Team/170229736394742","_blank");
	addItem("images/rotatorTHN.jpg","https://secure.thehockeynews.com/magazine-subscription.php?key=W10HCAHC&utm_campaign=CAHC&utm_medium=ad_240x150&utm_source=canadianamputeesports.ca&utm_content=Jun_10","_blank"); 
//	addItem("images/rotatorStitch.jpg","#",""); 
//	addItem("images/rotatorforEarth.jpg","http://main.forearthonline.com/","_blank"); 

}

function trim(str, chars) 
{
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

