/*
 Binh Nguyen Ha
 * version 1.1
 * - add css
 *      - position relative (ul)
 *      - position absolute (li)
 * - add nav (not yet)
 * version 1.2
 * - Fix slide
*/
(function($) {
    $.fn.junoSlider = function(speed) {
        
        var slider = this;
        
        var vars = {
            currentSlide: 0,
            totalElement : 0,
            running: false,
            paused: false,
            stop:false
        };
        
        var tempHeight = 0;
        var maxHeight = 0
        var thisHeight = 0;
        var totalElement = slider.length;
        //add css to ul
        slider.css('postion', 'relative');
        slider.addClass('junoSlider');
        
        
        
        //Find children
        var kids = slider.children();
        kids.each(function(i, j){            
            var child = $(this).children('li > img');
            
            //get child width and height
            var childWidth = child.width();
            var childHeight = child.height();
            if(childWidth == 0) childWidth = child.attr('width');
            if(childHeight == 0) childHeight = child.attr('height');
            
            if(childHeight > tempHeight){
                maxHeight = childHeight;
            }
            
            $(this).css({
                position : 'absolute',
                display : 'none'
            })
        });
        slider.parent().css('background', 'none');
        $(slider).height(maxHeight);      
        
        
        $(slider).children('li').eq(0).show().addClass('active');
        
        
        setTimeout(autoSlider, 4000); 
        
        function autoSlider(){
            var autoKid = slider.children();
            var next = autoKid.filter('.active').next();
            if(next.length == 0){
                autoKid.eq(totalElement - 1).fadeOut('slow').removeClass().end().eq(0).fadeIn('slow').addClass('active');            
            }else{
                autoKid.filter('.active').fadeOut('slow').removeClass().next().fadeIn('slow').addClass('active');
            }
            setTimeoutID = setTimeout(autoSlider, speed);        
        }
        
    };
})(jQuery);
