var imgCount;
imgSrc = new Array();
direction = -5;
imgs = new Array();
degrees = 0;

displaying = false;

function goto(idx)
{
        autorotate = false;
        if (active == idx)
        {
                window.top.location.href = imgProps[idx][1];
        }
        else
        {
                active = idx;

                aDegrees = imgs[idx][0] - 90;

                if (aDegrees < 0)
                {
                        aDegrees += 360;
                }

                if (aDegrees < 180)
                {
                        direction = -5;
                }
                else
                {
                        direction = 5;
                }

                setTimeout("rotateImg()", 1);
        }
}

function writeImages()
{
        imgCount = imgProps.length;
        imgSrc = new Array();

        minY = centerY - (Rad * compressY);
        maxY = centerY + (Rad * compressY);
        yDev = maxY - minY;


        for (var a = 0; a < imgCount; a++)
        {
                imgSrc[a]= new Image()
                imgSrc[a].src = imgProps[a][0];
                //alert(imgSrc[a].src);
        }

        var divStr = ""

        for (var a = 0; a < imgCount; a++)
        {
                divStr += '<div id="img' + a + '" style="position:absolute;top:0px;left:0px;">\n';
                divStr += '<img id="i' + a + '" src="' + imgSrc[a].src + '" onclick="goto(' + a + ')">\n';
                divStr += '</div>\n';
        }

        document.write(divStr);

        //initCarousel();
        window.onload = initCarousel;
}

function initCarousel()
{
        direction = initDirection * 5;
        for (var a = 0; a < imgCount; a++)
        {

                img = new Array();
                var ielem = document.getElementById("i" + a);
                img[0] = ((a + 1)/imgCount) * 360;
                img[1] = ielem.width;
                img[2] = ielem.height;
                imgs[a] = img;
        }
        setTimeout("rotateImg()", 1);
        setTimeout("RotateAuto()", autorateDelay)

}

function RotateAuto()
{
        if (autorotate)
        {
                if (direction > 0)
                {
                        active--;
                        if (active == -1)
                        {
                                active= imgProps.length - 1;
                        }
                }

                if (direction < 0)
                {
                        active++;
                        if (active == imgProps.length)
                        {
                                active=0;
                        }
                }

        }
        setTimeout("rotateImg()", 1);
        setTimeout("RotateAuto()", autorateDelay)
}

function rotateImg()
{


        if (direction < 0)
        {
                aDegrees = imgs[active][0] - 90;

                if (aDegrees < 0)
                {
                        aDegrees = aDegrees + 360;
                }

                direction = (-1 * (aDegrees / 7));
        }

        if (direction > 0)
        {
                aDegrees = imgs[active][0] - 90;

                if (aDegrees < 0)
                {
                        aDegrees = aDegrees + 360;
                }

                direction = ((360 - aDegrees) / 7);
        }



        var go = true;

        for (var a = 0; a < imgCount; a++)
        {

                var elem = document.getElementById("img" + (a));
                var ielem = document.getElementById("i" + (a));

                degrees = imgs[a][0];

                degrees = degrees + direction;

                if (degrees > 360 )
                {
                        degrees = degrees - 360;
                }
                else if (degrees < 0 )
                {
                        degrees = degrees + 360;
                }


                var Radians = (degrees / 360) * 2 * Math.PI;

                xPos = Rad * Math.cos(Radians);
                yPos = Rad * Math.sin(Radians) * compressY;



                ratio = (Math.round(((centerY + yPos - minY) / yDev) * 100)) / 100;


                elem.style.zIndex = Math.round(ratio * 100);

                if (ratio > .6)
                {
                        var trans = .15 + ((ratio -.6) * 1.91666);
                        //alert(ratio);
                }
                else
                {
                        trans = .15;
                }


                if (document.all)
                {
                        ielem.style.filter = "alpha(opacity = " + (Math.round(trans * 100)) + ")";
                }
                else
                {
                        ielem.style.opacity = trans;
                }

                imgHeight = imgs[a][2];
                imgWidth = imgs[a][1];

                zoomDiffY = imgHeight - (imgHeight * zoomHeight);
                ielem.style.height = (imgHeight - (zoomDiffY * (1 - ratio))) + "px";

                zoomDiffX = imgWidth - (imgWidth * zoomHeight);
                setWidth = (imgWidth - (zoomDiffX * (1 - ratio))) / 2;

                elem.style.top = (centerY + yPos) + "px";
                elem.style.left = (centerX + xPos - setWidth) + "px";



                imgs[a][0] = degrees;


        }


        if (!displaying)
        {
                elem = document.getElementById("images");
                elem.style.visibility = "visible";
                displaying = true;
        }

        if (Math.round(imgs[active][0]) == 90)
        {
                go = false;
        }


        if (go)
        {
                setTimeout("rotateImg()", delay);
        }
}
