var carouselDiv = null;
var titleDiv = null;
var thumbs = [];
var velocity = -1;
var stageWidth = 0;
var stageHeight = 0;

//--------------------------------------------------------------------------
function start()
{
	carouselDiv = document.getElementById('carousel');
	titleDiv = document.getElementById('carousel_title');
	
	carouselDiv.onmousemove = handleMouseMove;
	stageWidth = parseInt(carouselDiv.style.width);
	stageHeight = parseInt(carouselDiv.style.height);

	var base = 'img/photos/';
	var count = data.length;
	var angle = 0;
	var angleStep = 360 / count;
	var a;
	for (a = 0; a < count; a++)
	{
		var set = data[a];
		var path = set[0];
		var title = set[1];
		var items = set[2];
		var item = items[0];
		
		var url = base + path + '/thumbs/' + item + '.jpg';
		var loader = new ImageLoader(url, imageLoad, null);
		var image = loader.image;
		image.style.position = 'absolute';
		image.style.cursor = 'pointer';
		image.onclick = thumbClick;
		image.onmouseover = handleMouseOver;
		image.onmouseout = handleMouseOut;

		image.scInfo = new Object();
		image.scInfo.degrees = angle;
		image.scInfo.title = title;
		image.scInfo.path = path;
		image.scInfo.attached = false;
		
		thumbs.push(image);
		
		angle += angleStep;
	}

	window.setInterval(update, 67); // 15 fps
}

//------------------------------------------------------------------------------
function update()
{
	var count = thumbs.length;
	var a;
	for (a = 0; a < count; a++)
	{
		var thumb = thumbs[a];
		var info = thumb.scInfo;
		info.degrees += velocity;
		if(info.degrees > 360)
			info.degrees -= 360;
		else if(info.degrees < 0)
			info.degrees += 360;
		
		if(info.attached)
			positionImage(thumb);
	}
}

//------------------------------------------------------------------------------
function positionImage(image)
{
//	var ref = findPos(carouselDiv);
	var ref = new Object();
	ref.x = (800 - stageWidth) / 2;
	ref.y = 70;
	var info = image.scInfo;
	
	var diameter = 650;
	var radius = diameter / 2;
	var p = pointFromAngle(info.degrees, radius);
	var z = (p.y + radius) / diameter;
//	p.x *= (z * 0.3) + 0.7;
	p.x += stageWidth / 2;
	p.y /= 5;
	p.y += stageHeight / 2;
	
	var minSize = 40;
	var sizeExtra = 100;
	var width = (minSize + (sizeExtra * z)) * info.widthMultiplier;
	var height = (minSize + (sizeExtra * z)) * info.heightMultiplier;
	
	image.style.left = (ref.x + (p.x - (width / 2))) + 'px';
	image.style.top = (ref.y + (p.y - (height / 2))) + 'px';
	image.style.width = width + 'px';
	image.style.height = height + 'px';
	image.style.zIndex = Math.floor(z * 100);
	setOpacity(image, (z * 0.5) + 0.5);
}

//------------------------------------------------------------------------------
function thumbClick(event)
{
	event = getEvent(event);
	var image = getEventTarget(event);
	if(image && image.scInfo)
	{
		window.location = 'gallery.php?set=' + image.scInfo.path;
	}
}

//------------------------------------------------------------------------------
function handleMouseOver(event)
{
	event = getEvent(event);
	var image = getEventTarget(event);
	if(image && image.scInfo)
		titleDiv.innerHTML = image.scInfo.title;
}

//------------------------------------------------------------------------------
function handleMouseOut(event)
{
	event = getEvent(event);
	var image = getEventTarget(event);
	if(image && image.scInfo)
		titleDiv.innerHTML = '';
}

//------------------------------------------------------------------------------
function handleMouseMove(event)
{
	event = getEvent(event);
	var p = getEventMouse(event);
	var ref = findPos(carouselDiv);
	p.x -= ref.x;
	var width = parseInt(carouselDiv.style.width);
	var halfwidth = width / 2;
//	titleDiv.innerHTML = p.x;
	velocity = ((p.x - halfwidth) / halfwidth) * 1; 
}

//--------------------------------------------------------------------------
function imageLoad(image, arg, success)
{
	if(success)
	{
		var info = image.scInfo;
		
        if(typeof(image.naturalWidth) == 'undefined')
        {
			info.width = image.width;
			info.height = image.height;
        }
        else
        {
			info.width = image.naturalWidth;
			info.height = image.naturalHeight;
        }
        
		if(info.width > info.height)
		{
			info.widthMultiplier = 1;
			info.heightMultiplier = info.height / info.width;
		}
		else
		{
			info.widthMultiplier = info.width / info.height;
			info.heightMultiplier = 1;
		}
	
		positionImage(image);
		carouselDiv.appendChild(image);
		info.attached = true;
	}
}
