/**
 * jQuery random background changer
 * @name Random Background Changer
 * @author Charles Harvey - http://www.charles-harvey.co.uk
 * @version 0.1
 * @date September 4 2009
 * @category jQuery plugin
 * @copyright (c) 2009 Charles Harvey
 */


(function($) {

    $.cycleThru = {
        defaults: {
            delay: 3000
        }
    }

    $.fn.extend({
        cycleThru:function(config) {
            var config = $.extend({}, $.cycleThru.defaults, config);
            return this.each(function() {

                var delay = config.delay;
                var ulid = $(this).attr("id");
                var j = 0;
                var k = 0;
                var n = 0;
                var jmax = $(this).children("li").length -1;

                function cyclee(){
                    $("ul#" + ulid + " li:eq(" + j + ")")
                    .animate({"opacity" : "1"}, 600)
                    .animate({"opacity" : "1"}, 4500)
                    .animate({"opacity" : "0"}, 600, function(){
                        (j == jmax) ? j=0 : j++;
                        cyclee();
                    });
                }
                function cyclee2(){
                    n = n + 1;
                    $("ul#" + ulid + "2" + " li:eq(" + k + ")")
                    .animate({"opacity" : "1"}, 600)
                    .animate({"opacity" : "1"}, (k==0 && n==1) ? 2550 : 0)
                    .animate({"opacity" : "1"}, 4500)
                    .animate({"opacity" : "0"}, 600, function(){
                        (k == jmax) ? k=0 : k++;
                        cyclee2();
                    });
                }
                cyclee();
                cyclee2();
            })
        }
    })
})(jQuery);
