/**
* @author    Kyle Hengst <kyle@cyberdesignworks.com.au>
*/
(function($)
{

    $.fn.jSlideshow = function(settings)
    {

        settings = jQuery.extend({
            delay: 5,
            rand: false,
            indicators: ''
        }, settings);

        this.each(function()
        {
            var scope = $(this);
            var interval = null;
            var index = 0;
            var logos = scope.children();
            var total = logos.length;
            var debug = '';
            var indicators = null;
					
            // indicators
            if(settings.indicators!='')
            {
                indicators = $(settings.indicators);
                $('li',indicators).click(function(){

                    var p = $(this).parent();
                    var i = $('li',p).index(this);
                    stop();
                    go(i);
                    loop();
                    return false;
							
                });
            }
					
            // capture indices
            var indices = new Array();
            for(i=0;i<total;i++)
                indices.push(i);
					
            // sort slides randomly
            if(settings.rand)
                indices.sort(function(){
                    return (Math.round(Math.random())-0.5);
                });


            // set all logos to transparent
            $("a",scope).hide();

            // loop
            function loop()
            {

                if(settings.delay!=0)
                interval = setInterval(
                    function(){
                        go()
                        },
                    settings.delay*1000
                    );
            }
					
            function stop()
            {
                clearInterval(interval);
            }

            // interval function
            function go(n)
            {
                hide(index);
						
                if(n!=undefined)
                {
                    index = n;
                }
                else
                {
                    index++;
                    if(index>=total)
                        index = 0;
                }
							
                show(index);
            }

            function hide(child_index)
            {
                var child = $("a",scope).eq(indices[child_index]);
                child.fadeOut('slow');
            }

            function show(child_index)
            {
                var child = $("a",scope).eq(indices[child_index]);
                child.fadeIn('slow');
						
                // indicators
                if(indicators)
                {
                    $('a',indicators).removeClass('selected');
                    var indicator = $('a',indicators).eq(indices[child_index]);
                    indicator.addClass('selected');
                }
            }
					
            function alert_test(n)
            {
                alert(n);
            }
					
            loop();
            show(0);

        })

    } // close slidingPanels

})(jQuery);
