jQuery.Eir = function(CssClass, imageList, duration, activeImage, inactiveImage) {
    
    var currentImage = -1;
    var selector = CssClass;
    var images = imageList;           
    var displayDuration = duration;
	var first = true;

    // preload the images
    var cache = [];
    for (var i = 0; i < images.length; i++) {
      var cacheImage = document.createElement('img');
      cacheImage.src = images[i];
      cache.push(cacheImage);
    }
     

    // setup the buttons list
    var listText="";
    for(i=0;i<images.length;i++)
    {
        listText += '<li><img src="'+inactiveImage+'" alt="" /><img src="'+activeImage+'" alt="" /></li>';
    }            
    $("."+selector+" .buttons ul").html(listText);           
    
    
    // set the button mouseovers
    $("."+selector+" .buttons li").mouseenter(function () {
        highlight($(this).index());
    })
    .mouseleave(function () {
        if($(this).index() != currentImage)
        {
            lowlight($(this).index());
        }
    })
    .click(function () {
        if($(this).index() != currentImage)
        {
            interactedWith = true;
            setActive($(this).index());
        }
    });

    // animation methods
    function highlight(itemNumber) {
        $("img:eq(1)","."+selector+" .buttons li").stop().animate({ opacity: 0 }, 200);
        $("."+selector+" .buttons li:eq(" + itemNumber + ") img:eq(1)").stop().animate({ opacity: 1 }, 200);
        $("."+selector+" .buttons li:eq(" + currentImage + ") img:eq(1)").stop().animate({ opacity: 1 }, 200);
    }
    
    function lowlight(itemNumber) {
        $("."+selector+" .buttons li:eq(" + itemNumber + ") img:eq(1)").stop().animate({ opacity: 0 }, 200);
    }
    
    function setActive(itemNumber) {
        if(itemNumber!=currentImage)
        {
            if(currentImage>-1)
                lowlight(currentImage);
            currentImage = itemNumber;
            $("."+selector+" .holder img:eq(0)").attr("src", $("."+selector+" .holder img:eq(1)").attr("src"));
            if(first)
            {
            	first = false;
            	$("."+selector+" .holder img:eq(1)").attr("src", images[currentImage]).stop().animate({ opacity: 1 },0);
            }
            else
	            $("."+selector+" .holder img:eq(1)").attr("src", images[currentImage]).stop().animate({ opacity: 0 },0).animate({opacity: 1},300);

        }
    }
                
    // show the current image
    setActive(Math.floor(Math.random()*images.length));
    highlight(currentImage);
    
    var interactedWith = false;
    var autoRotate = setInterval( function() { 
            //alert("rotate");
            if(interactedWith){ 
                clearInterval(autoRotate); 
            } 
            else { 
                setActive((currentImage-1+images.length) % images.length);
                highlight(currentImage);
            } 
        } , duration*1000);                
}
