

var Site = {

	start: function()

	{

		if ($('CircleAnimImg'))

		{

			circleAnim = new CircleAnim();

			circleAnim.loadNextSegment();

		}

		$("CircleAnimImg").addEvent('mouseenter', circleAnim.stopAnim.bindWithEvent(circleAnim));

 		$("CircleAnimImg").addEvent('mouseleave', circleAnim.startAnim.bindWithEvent(circleAnim));

	}

};



var circleAnim;

var CircleAnim = new Class({

	initialize: function()

	{

		this.numFrames = 75;

		this.frameImages = [];

		this.frameToLoadIndex = 1;

		this.framesLoaded = 0;

		this.frameIndex = 0;

		this.loadSegmentSize = 5;

		this.segmentFramesLoaded = 0;

		this.animating = false;

		this.animTimer = 0;

	},



	loadNextSegment: function()

	{

		var intloop = this.loadSegmentSize;

		if ((this.frameToLoadIndex + this.loadSegmentSize) > this.numFrames) intlooop = this.numFrames - this.frameToLoadIndex;



		while(intloop > 0)

		{

			var frameNum = String(this.frameToLoadIndex);

			var frameNumLength = frameNum.length;

			while(frameNumLength < 3)

			{

				frameNum = "0" + frameNum;

				frameNumLength++;

			}

			var image = new Element('img');

			image.addEvent('load', this.frameLoaded.bindAsEventListener(this));

			image.src = "/images/splash/circleAnim/" + frameNum + ".png";

			this.frameImages.push(image);

			this.frameToLoadIndex++;

			this.currentFrameLoading++;

			intloop--;

		}

	},



	frameLoaded: function()

	{

		this.segmentFramesLoaded++;

		if (this.segmentFramesLoaded == this.loadSegmentSize) this.segmentLoaded();

	},

	

	segmentLoaded: function()

	{

		this.segmentFramesLoaded = 0;

		this.framesLoaded += 5;

		if (!this.animating) this.startAnim();

		if (this.frameToLoadIndex < this.numFrames) this.loadNextSegment();

	},

	

	startAnim: function()

	{

		if (this.framesLoaded > 0)

		{

			this.animating = true;

			this.animTimer = this.animateFrame.periodical(50, this);

		}

		else this.startAnim.delay(500, this);

	},



	stopAnim: function()

	{

		$clear(this.animTimer);

		this.frameIndex = 0;

		$("CircleAnimImg").src = this.frameImages[this.frameIndex].src;

	},



	animateFrame: function()

	{

		this.frameIndex++;

		if (this.frameIndex >= this.framesLoaded) this.frameIndex = 0;

		$("CircleAnimImg").src = this.frameImages[this.frameIndex].src;

	}

});



window.addEvent('load', Site.start);




