﻿function startHomePageIPod()
{
	var homeBar = document.getElementById('home-bar');
	var ipodImages = document.getElementById('ipod-images');
	if (homeBar != null && ipodImages != null)
	{
		var firstRun = true;
		var homeBarImg = new Image();
		homeBarImg.onload= function()
		{
			var ipodImagesImg = new Image();
			ipodImagesImg.onload= function()
			{
				homeBar.style.backgroundImage = "url(" + homeBarImg.src + ")";
				homeBar.style.visibility = 'visible';
				var loadIPodTrack = function(num)
				{
					ipodImages.style.visibility = 'visible';
					if (firstRun)
					{
						ipodImages.style.backgroundPosition = (num * -149) + "px 0px";
						new OpacityTween(ipodImages, Tween.regularEaseIn, 0, 100, .4).start();
					}
					else
					{
						var otwn = new OpacityTween(ipodImages, Tween.regularEaseOut, 100, 0, .2);
						otwn.onMotionFinished = function()
						{
							ipodImages.style.backgroundPosition = (num * -149) + "px 0px";
							new OpacityTween(ipodImages, Tween.regularEaseIn, 0, 100, .5).start();
						}
						otwn.start();
					}
					
					firstRun = false;
				}
				homeBar.style.scrollLeft = "-558px";
				var homeBarMoveCount = 0;
				new OpacityTween(homeBar,Tween.regularEaseIn,0,100,.5).start();
				loadIPodTrack(7);
				var homeBarSync = function() { homeBar.style.backgroundPosition = homeBar.style.scrollLeft + " 0px"; }
				
				var homeBarOnFinished = function()
				{
					loadIPodTrack(homeBarMoveCount);
					homeBarMoveCount++;
					if (homeBarMoveCount == 8)
					{
						homeBar.style.scrollLeft = "-558px";
						homeBarMoveCount = 0;
					}
					//setTimeout(function()
					//{
						var ex1 = 0;
						if (homeBarMoveCount == 7)
						{
							ex1 = 5;
						}
						var homeBarTween = homeBarTweenObj(ex1);
						homeBarTween.start();
					//}, 0); //7500);
				};
				var homeBarTweenObj = function(ex) 
				{ 
					var twn = new Tween(homeBar.style, 'scrollLeft', Tween.linear, parseInt(homeBar.style.scrollLeft), parseInt(homeBar.style.scrollLeft)-(79+ex), 8.5, 'px'); 
					twn.onMotionChanged = function() { homeBarSync(); }
					twn.onMotionFinished = homeBarOnFinished;
					return twn;
				};
				var homeBarTween = homeBarTweenObj(0);
				homeBarTween.start();
			}
			ipodImagesImg.src = "images/ipod-images.jpg";
		}
		homeBarImg.src = "images/home-images.jpg";
	}
	else
	{
		setTimeout(startHomePageIPod, 1000);
	}
}
setTimeout(startHomePageIPod, 0);
