/**
 * @package		jQuery
 * @access		public
 * @copyright	Eigen & Wijze Communicatie BV
 */
(function($) {
    $.fn.innerfade = function(options)
    {
        var settings;
        var timeout;
        
        return this.each(function(){
            $.innerfade(this, options);
        });
    }
    
    $.innerfade = function(container, options)
    {
        var children;
        
        settings = {
            animationtype   : 'fade',
            speed           : 'normal',
            type            : 'sequence',
            timeout         : 5000,
            containerheight : 'auto',
            timer           : 1,
            items           : '.items',
            navigation      : '.nav',
            hasNumbers      : false,
            paused          : false,
			pauseButton		: null
        }
        
        if(options)
        {
            $.extend(settings, options)
        }
        
        // Add the container to the settings
        // Doing this after $.extend makes it unable to be overridden
        settings._container = container;
        
        // Get the child elements
        children = $(settings.items, container).children();
        
        // Check if the number of child elements is greater than 1
        if(children.length > 1)
        {
            // Fill the navigation
            if(settings.navigation != null)
            {
                for(var i = children.length-1; i >= 0; i--)
                {
                    var item = $('<li></li>')
                        .attr('id', 'innerfade-navigation-' + i);
                    
                    if(settings.hasNumbers == true)
                    {
                        item.text(i+1);
                    }
                    
                    $(settings.navigation, container).prepend(item);
                }
            }
            
            // Add styles
            $(settings.items, container).css('position', 'relative')
                .css('height', settings.containerheight);
            
            for (var i = 0; i < children.length; i++) 
			{
                $(children[i]).css('z-index', String(children.length-i))
                    //.css('position', 'absolute')
                    .hide();
            }
            
            // Navigation handling
            var nav = $(settings.navigation + ' li', container).get();
            $.each(nav, function(){
                $(this).mouseover(function(){
                    $.each($(this).parent().children(), function(){
                        $(this).removeClass('active');
                    });
                    
                    $(this).addClass('active');
                    
                    var visible = 0;
                    var id      = $(this).attr('id');
                    var current = id.split('innerfade-navigation-').join('');
                    current     = parseInt(current);
                    next        = current + 1 < children.length
                        ? current + 1
                        : 0;
					
                    // Get the current visible image
                    for(var i = 0; i < children.length; i++)
                    {
                        if($(children[i]).hasClass('active'))
                        {
                            visible = i;
                            continue;
                        }
                    }
                    
                    if(visible != current)
                    {
                        $(children[visible]).fadeOut(settings.speed)
                            .removeClass('active');
                        $(children[current]).fadeIn(settings.speed)
                            .addClass('active');
                        clearTimeout(timeout);
                        
                        //$.innerfade.skip();
                        timeout = setTimeout(function(){
                            $.innerfade.next(children, settings, settings._container, next, current);
                        }, settings.timeout);
                    }
                });
            });
            
            // Fade / cycle method
            switch(settings.type)
            {
                case 'sequence' :
                    timeout = setTimeout(function(){
                        $.innerfade.next(children, settings, container, 1, 0);
                    }, settings.timeout);
                    
                    $(children[0]).show()
                        .addClass('active');
                    
                    if(settings.navigation)
                    {
                        // Activate the first item
                        var nav = $(settings.navigation + ' li', container).get();
                        $(nav[0]).addClass('active');
                    }
                    
                    
                    break;
            }
			
			// Controls handling
			if(settings.pauseButton)
			{
				$(settings.pauseButton).click(function(){
					$.innerfade.pause($(this));
				});
			}
        }
    }
	
	// Pause the script
	$.innerfade.pause = function(pauseButton)
    {
        var children = $(settings.items, settings._container).children();
        if(!settings.paused)
        {
            settings.timer = 0;
            settings.paused = true;
			
			pauseButton.removeClass(settings.playClass);
			pauseButton.addClass(settings.pauseClass);
        } else
        {
            settings.timer = 1;
            settings.paused = false;
            
            // Get the current screen
            for(var i = 0; i < children.length; i++)
            {
                if($(children[i]).hasClass('active'))
                {
                    current = i;
                    continue;
                }
            }
			
			pauseButton.removeClass(settings.pauseClass);
			pauseButton.addClass(settings.playClass);
            
            setTimeout(function(){
                $.innerfade.next(children, settings, settings._container, current+1, current);
            }, 0);
        }
    }
    
	// Show next child
    $.innerfade.next = function(children, settings, container, next, current)
    {
        if(settings.timer == 'on' || settings.timer == 1)
        {
            switch(settings.animationtype)
            {
                case 'slide' :
                    
                    
                    break;
                
                case 'fade' :
                    $(children[current]).fadeOut(settings.speed)
                        .removeClass('active');
                    
                    $(children[next]).fadeIn(settings.speed)
                        .addClass('active');
                    
                    if(settings.navigation)
                    {
                        var nav = $(settings.navigation + ' li', container).get();
						for(var i = 0; i < nav.length; i++)
						{
							$(nav[i]).removeClass('active');
						}
						
                        $(nav[next]).addClass('active');
                    }
                    
                    break;
            }
            
            switch(settings.type)
            {
                case 'sequence' :
                    if(next + 1 < children.length)
                    {
                        current = next;
                        next    = next + 1;
                    } else
                    {
                        current = next;
                        next    = 0;
                    }
                    
                    break;
            }  
            
        	timeout = setTimeout((function() {
                $.innerfade.next(children, settings, container, next, current);
        	}), settings.timeout);
        }
    }
})(jQuery);
