(function($) {
    $.fn.slider = function(options) {

        var defaults = {
			speed: 			1200,
			pause:			5000
		};

        var options = $.extend(defaults, options); 
        var numChildren;
        var current;
        var timeout;

        var toLeftComplete = $.proxy (function () {
            var o = $("ul", this).find('li:first-child').detach();
            o.css('margin-left', 0);
            o.css('opacity', .2);
            o.appendTo($("ul", this));
            o.find('.slider-label').css('opacity', 0);
            timeout = setTimeout(toLeft, options.pause);
        }, $(this));

        var toLeft = $.proxy(function () {
            
            var o = $("ul", this).find('li:first-child');
            var n = $("ul", this).find('li:nth-child(2)');
            o.animate({
                marginLeft: -o.width()
               },
               options.speed,
               toLeftComplete
            );
            n.find('.slider-label').animate({
                opacity: 1
               },
               options.speed
            );
            n.animate({
                opacity: 1
               },
               options.speed
            );
        }, $(this));


        var init = function () {
            this.each(function () {
                $(this).css('opacity', 1);
                var obj = $(this);
                var s = $("li", obj).length;
                var w = $("li", obj).width();
                var h = $("li", obj).height();
                obj.width(w);
                obj.height(h);
                obj.css("overflow","hidden");
                var ts = s-1;
                var t = 0;
                $("ul", obj).css('width',s*w);
                $("li", obj).css('opacity', .2);
                $("li:first-child", obj).css('opacity', 1);
                $(".slider-label").css('opacity', 0);
                $("li:first-child", obj).find(".slider-label").css('opacity', 1);
                numChildren = s;
                setTimeout(toLeft, options.pause);
            });
            $(this).css('opacity', 1);
        };
        $(this).css('opacity', 0);

        var interval = setInterval($.proxy(function () {
            if ($(this).find('img').height() > 100) {
                clearInterval(interval);
                $.proxy(init, $(this))();
            }
        }, $(this)), 30)

        //$(this).imagesLoaded($.proxy(init, $(this)));
        
    }
})(jQuery);
