(function($){


$.fn.JCAPslide = function(options){
    
    
    var opts = $.extend($.fn.JCAPslide.defaults,options);
    this.each(function(){
        var container, count,selector={},elements={}
        container = this;
        count = {
            images: $(container).find(">img").length,
            links:  $(container).find(">a").length
        }
        if(count.images > count.links) selector.elements = "img";
        else selector.elements = "a";
        $(container).find(selector.elements).hide();
        elements.sliding = $(container).find(selector.elements).get();
        $(container).width(opts.width).height(opts.height)
        elements.current = 0;
        container.elements = elements;
        container.selector = selector;
        container.delay = opts.delay;
        $(container).bind("slidenext",function(){
            debug("Slidenext event occurred");
            this.elements.previous = this.elements.sliding[this.elements.current];
            this.elements.current += 1;
            debug(this.elements.current);
            if(this.elements.sliding.length <= this.elements.current) this.elements.current = 0;
            debug(this.elements.current);
            this.elements.next = this.elements.sliding[this.elements.current];
            $(container.elements.previous).fadeOut(1000,function(){
                $(container.elements.next).fadeIn(1000,function(){
                    setTimeout(function(){
                        $(container).trigger("slidenext");
                    },container.delay)
                })
            })
        }).trigger("slidenext");

    });
}
$.fn.JCAPslide.defaults = {
    width: 500,
    height:300,
    delay:4000
}

function debug(deb){
    if(window.console && window.console.log)
        window.console.log(deb);
}

})(jQuery);
