// jQuery.noConflict();
function ytImgRotator(name, id, images, urls, idNames, 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.idNames = idNames  // array containing the idname 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();
            var newIdNames = 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];
              var elem = this.idNames.splice(idx,1);
              newIdNames[newIdNames.length] = elem[0];
            }
            this.images = newImages;
            this.urls = newUrls;
            this.idNames = newIdNames;
        }
    }
    
    this.rotate = function()
    {
        document.write("<style>");
		if (navigator.userAgent.toLowerCase().indexOf('msie') > -1)
		{
	        document.write("#ytRotator { position: absolute !important; }");
			jQuery(document).ready(function(){
				var maxh = 0;
				jQuery("#ytRotator img").each(function(){
					if (jQuery(this).height() > maxh)
					{
						maxh = jQuery(this).height();
					}
				});
				jQuery("#ytRotator").parent().height(maxh+2);
				jQuery("#ytRotator .ytImgRotatorControls").css("cssText","padding-top: 4px !important;");
			});
		}
		else
		{
	        document.write("#" + this.id + " { position: relative !important; }");
		}
        document.write("#" + this.id + " img { position: absolute; left: 0px; top: 0px; visibility: hidden; }");
        document.write("#" + this.id + " .ytImgRotatorControls { position: absolute !important;bottom: 0px;right: 0px;background: url(/lib/yhst-81126207287951/nav-tile-piece.gif) bottom right repeat-x;margin: 0px;height: 24px;text-align: right; }");
        document.write("#" + this.id + " .ytRotatorNormal { background-color: white; }");
        document.write("#" + this.id + " .ytRotatorSelected, #" + this.id + " .ytImgRotatorControls a:hover { background: url(/lib/yhst-81126207287951/circle-on-state.png) center 3px no-repeat !important; }");
        document.write("#" + this.id + " .ytImgRotatorControls a:link, #" + this.id + " .ytImgRotatorControls a:visited { padding: 2px 1px;margin-right: 0px;background: url(/lib/yhst-81126207287951/circle-off-state.png) center 3px no-repeat;line-height: 24px; } ");
        document.write("#" + this.id + " #rotNext { background-image: none !important; }");
        document.write("#" + this.id + " .ytImgRotatorControls a#rotNext:hover { background-image: none !important; }");
        document.write("#" + this.id + " #rotPrev { background-image: none !important; }");
        document.write("#" + this.id + " .ytImgRotatorControls a#rotPrev:hover { background-image: none !important; }");
        document.write("#" + this.id + " .ytImgRotatorControls img { visibility: visible !important; position: relative !important; }");
		document.write("#" + this.id + " .ytImgRotatorControls #rotControlCorner { position: absolute !important; top: 0px; left: -20px; }");
        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>");
			// document.write("<div id=" + this.id + "_addCart_Name" + (i) + " class=addCart_Name></div><label class=rotIdName id=rotIdName" + (i) + ">" + this.idNames[i] + "</label><form method=post action='' class=rotForm id=rotForm" + (i) + "><input type=image src=/lib/yhst-81126207287951/rot-add2cart.gif class=rotCartImg></form>");
        }
        if (!this.hideControls)
        {
	        document.write("<div class=ytImgRotatorControls>");
            document.write("<a id='rotPrev' href='javascript:void(0)' onclick='" + this.name + ".ytPrevmage(" + this.name + ")'><img src=/lib/yhst-81126207287951/left-arrow.png border=0 align=absmiddle></a>");
            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) + "><img src=/lib/yhst-81126207287951/rotcontrol-link.gif border=0 align=absmiddle></a>");
            }
            document.write("<a id='rotNext' href='javascript:void(0)' onclick='" + this.name + ".ytNextImage(" + this.name + ")'><img src=/lib/yhst-81126207287951/right-arrow.png border=0 align=absmiddle></a>");
			document.write("<img src=/lib/yhst-81126207287951/corner.png id=rotControlCorner>");
			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];
                //document.getElementById("rotIdName" + (rot.currentImage) ).innerHtml = rot.idNames[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();
                        //jQuery("#" + rot.id + "_addCart_Name" + (previousImage)).hide();
                        //jQuery("#rotIdName" + (previousImage)).hide();
                        // jQuery("#rotForm" + (previousImage)).hide();
					}
                    else {
                        jQuery("#" + rot.id + "_ytImgRotatorImage" + (previousImage)).fadeOut("slow");
                        //jQuery("#" + rot.id + "_addCart_Name" + (previousImage)).fadeOut("slow");
                        //jQuery("#rotIdName" + (previousImage)).fadeOut("slow");
                        // jQuery("#rotForm" + (previousImage)).fadeOut("slow");
					}
                }
                if (rot.noEffect) {
                    jQuery("#" + rot.id + "_ytImgRotatorImage" + (rot.currentImage)).show();
                    //jQuery("#" + rot.id + "_addCart_Name" + (rot.currentImage)).show();
                    //jQuery("#rotIdName" + (rot.currentImage)).show();
                    // jQuery("#rotForm" + (rot.currentImage)).show();
				}
                else {
                    jQuery("#" + rot.id + "_ytImgRotatorImage" + (rot.currentImage)).fadeIn("slow");
                    //jQuery("#" + rot.id + "_addCart_Name" + (rot.currentImage)).fadeIn("slow");
                    //jQuery("#rotIdName" + (rot.currentImage)).fadeIn("slow");
                    // jQuery("#rotForm" + (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);
        }

        this.ytPrevImage = function(rot)
        {
        }

        if (this.randomize)
            this.shuffle();
            
        this.ytNextImage(this);
    }
}

