// jQuery.noConflict();
function ytImgRotator(name, id, images, urls, delay)
{
    this.id     = id;       // the ID of the image rotator on the screen
    this.name   = name;     // name of the image rotator object in javascript 
    this.images = images;   // array containing the image URLs
    this.urls   = urls;     // array containing the targets for each image
    this.delay  = delay;    // slide show delay between image transitions
    
    this.hideControls = false; // hide the controls or not
    this.randomize = false;    // randomize the order of the images or not
    this.noRefresh = false;    // when set to Yes, the images will only refresh upon page load.
    this.noEffect = false;     // Turn off fade-in / fade-out effect

    this.currentImage = -1;
    this.nextImage = -1;
    this.timerSeed = 0;
    
    this.shuffle = function()
    {
        if (this.images.length > 0)
        {
            var newImages = new Array();
            var newUrls = new Array();

            while (this.images.length > 0)
            {
              var idx = Math.floor(Math.random() * this.images.length);
              var elem = this.images.splice(idx,1);
              newImages[newImages.length] = elem[0];
              var elem = this.urls.splice(idx,1);
              newUrls[newUrls.length] = elem[0];
            }
            this.images = newImages;
            this.urls = newUrls;
        }
    }
    
    this.rotate = function()
    {
        document.write("<style>");
        document.write("#" + this.id + " { position: relative }");
        document.write("#" + this.id + " img { position: absolute; left: 0px; top: 0px; visibility: hidden }");
        document.write("#" + this.id + " .ytImgRotatorControls { margin-top: 0px; margin-bottom: 0px; }");
        document.write("#" + this.id + " .ytRotatorNormal { background-color: white }");
        document.write("#" + this.id + " .ytRotatorSelected, #" + this.id + " .ytImgRotatorControls a:hover { background-color: #f68b1f; color: white; }");
        document.write("#" + this.id + " .ytImgRotatorControls a:link, #" + this.id + " .ytImgRotatorControls a:visited { font-family: arial; font-size: 12px; text-decoration: none; padding: 2px; padding-left: 5px; padding-right: 5px; border: 1px solid #c2d6dd; margin-right: 6px } ");
        document.write("</style>");

        document.write("<div id=" + this.id + ">");
        document.write("<img src=" + this.images[0] + " style='position: relative'>");
        for (var i = 0; i < this.images.length; i++)
        {
            document.write("<a id=" + this.id + "_ytImgRotatorHref" + (i) + " href=" + this.urls[i] + ">");
            document.write("<img id=" + this.id + "_ytImgRotatorImage" + (i) + " border=0 src=");
            //document.write(this.images[i]);
            document.write("></a>");
        }
        if (!this.hideControls)
        {
            document.write("<div class=ytImgRotatorControls>");
            for (var i = 0; i < this.images.length; i++)
            {
                document.write("<a href='javascript:void(0)' onclick='" + this.name + ".nextImage = " + (i) + ";" + this.name + ".ytNextImage(" + this.name + ")' id=" + this.id + "_ytImgRotatorPage" + (i) + ">" + (i + 1) + "</a>");
            }
            document.write("<a style='padding-left: 7px; padding-right: 7px;' href='javascript:void(0)' onclick='" + this.name + ".ytNextImage(" + this.name + ")'>next</a>");
            document.write("</div>");
        }
        document.write("</div>");
        
        // preload the next image
        if (this.images.length > 1)
        {
            var tmpImg = new Image();
            tmpImg.src = this.images[1];
        }

        this.ytNextImage = function(rot)
        {
            // this is a kludge because IE doesn't pass the extra arguments to the setInterval function.
            var f = function()
            {
                var previousImage = -1;
                if (rot.currentImage >= 0)
                {
                    previousImage = rot.currentImage;
                }
                
                var pg = document.getElementById(rot.id + "_ytImgRotatorPage" + rot.currentImage);
                if (pg)
                {
                    pg.className = "ytRotatorNormal";
                }
                if (rot.nextImage >= 0)
                {
                    rot.currentImage = rot.nextImage;
                    rot.nextImage = -1;
                }
                else
                {
                    rot.currentImage ++;
                }

                if (rot.currentImage >= rot.images.length)
                {
                    if (rot.randomize)
                        rot.shuffle();
                    rot.currentImage = 0;
                }

                var pg = document.getElementById(rot.id + "_ytImgRotatorPage" + rot.currentImage);
                if (pg)
                {
                    pg.className = "ytRotatorSelected";
                }
                
                document.getElementById(rot.id + "_ytImgRotatorImage" + (rot.currentImage) ).src = rot.images[rot.currentImage];
                document.getElementById(rot.id + "_ytImgRotatorHref" + (rot.currentImage) ).href = rot.urls[rot.currentImage];
                
                jQuery("#" + rot.id + "_ytImgRotatorImage" + (rot.currentImage)).css("display","none").css("visibility","visible");
                if (previousImage >= 0)
                {
                    if (rot.noEffect)
                        jQuery("#" + rot.id + "_ytImgRotatorImage" + (previousImage)).hide();
                    else
                        jQuery("#" + rot.id + "_ytImgRotatorImage" + (previousImage)).fadeOut("slow");
                }
                if (rot.noEffect)
                    jQuery("#" + rot.id + "_ytImgRotatorImage" + (rot.currentImage)).show();
                else
                    jQuery("#" + rot.id + "_ytImgRotatorImage" + (rot.currentImage)).fadeIn("slow");
                
                // preload the next image
                var im = document.getElementById(rot.id + "_ytImgRotatorImage" + (rot.currentImage + 1) );
                if (im)
                    im.src = rot.images[rot.currentImage + 1];
            }
            f();
            self.clearInterval(rot.timerSeed);
            if (!rot.noRefresh)
                rot.timerSeed = self.setInterval(f, rot.delay * 1000,rot);
        }
        if (this.randomize)
            this.shuffle();
            
        this.ytNextImage(this);
    }
}
