jQuery.fn.imagefader = function (options) {
    
    /*
     * Created by www.TIM-CARTER.com 2011
     * 
     * ImageFader is a nice little function. You specify a container as the selector. Like a DIV. Then ImageFader will take all the images inside
     * this DIV and preload them into an array. It will the remove the images from the DOM and make 1 new one. It then fades the pictures in and out
     * You can specify interval, fadespeed, startimage, waitforpreload, randomize and even say if it only should show the picture you say and then do nothing more. 
     * This is done with the activated option.
     * 
     */
    options = jQuery.extend({
        waitForPreload : true,
        interval : 3000, //HOW LONG TO WAIT BEFORE CHANGING THE IMAGE
        fadeSpeed : 1000,
        startImage : 0,
        randomize : false,
        activated : true,
        fixedDimensions : true,
        
        selectorClass : "imagefader_currentimg" //THIS IS TO REFER TO THE IMAGE, YOU CAN CHANGE THE CLASS IF YOU WANT TO
    }, options);
    var c = this; //CONTAINER
    var cHeight = $(c).height();
    var cWidth = $(c).width();
    
    var images = new Array();
    var imagesTitles = new Array();
    var imageObj = new Image();
    var counter = 0;
    var currentImage = 0;
    
    $(c).find("img").each(function(){
        
        images[counter] = $(this).attr("src");
        imagesTitles[counter] = $(this).attr("title");
        if(options.waitForPreload){ //THIS IS A MANUAL PRELOAD FIX, DUE TO SOME BROWSERS STILL NOT LOADED IMAGES PROPERLY, YOU CAN DISABLE IT IN OPTIONS IF YOU DONT WANT IT
            var loaded = false;
            imageObj.onload = function(){
                loaded = true;
            };
        }
        imageObj.src=images[counter];
        if(options.waitForPreload){ //THIS IS A MANUAL PRELOAD FIX, DUE TO SOME BROWSERS STILL NOT LOADED IMAGES PROPERLY, YOU CAN DISABLE IT IN OPTIONS IF YOU DONT WANT IT
            for(i=0;i<1;i++){
                if(!loaded) i=0;
            }
        }
        $(this).remove();
        counter++;
    });
    
    var firstImage = (options.randomize ? Math.floor(Math.random()*images.length) : options.startImage);
    var img = "<img class='"+options.selectorClass+"' title='"+imagesTitles[firstImage]+"' src='"+images[firstImage]+"' "+(options.fixedDimensions ? "style='height:"+cHeight+"px;width:"+cWidth+"px;'" : "")+">";
    $(img).appendTo(c);
    currentImage = firstImage;
    if(options.activated){ //WE ONLY CYCLE THE IMAGES IF ACTIVATED IS TRUE ELSE IT WONT DO ANYTHING ELSE
        setTimeout(function(){imageCycle();},options.interval);
    }
    function imageCycle(){
        var nextImage = 0;
        if(options.randomize){
            for(i=0;i<1;i++){ //WE DO NOT WANT THE IMAGE TO BE THE SAME AS THE LAST IMAGE SO WE HAVE TO CHECK FOR IT
                nextImage = Math.floor(Math.random()*images.length);
                if(nextImage == currentImage)
                    i=0;
            }
        }else{
            if(currentImage == (images.length - 1)){
                nextImage = 0;
                currentImage=0;
            }else{
                nextImage = currentImage + 1;
                currentImage++;
            }
        }
        $("."+options.selectorClass).fadeOut(options.fadeSpeed,function(){
            $("."+options.selectorClass).attr({src:images[nextImage],title:imagesTitles[nextImage]}).fadeIn(options.fadeSpeed,function(){
                setTimeout(function(){imageCycle()},options.interval);
            });
        });
        
    }
}
