function Animation(id, timeFade, timeChange, pause, callback){
    this.id = id;
    this.pause = pause || false;
    this.callback = callback || cBack();
    
    $('.preload').hide();
    
    var el = $('#' + this.id),
        bg = el.find('img.bg'),
        visual = el.find('img.visual'),
        img = el.find("div.images img"),
        count = img.length,
        current = 0,
        next = 1,
        stop = false,
        src = [],
        srcBg, srcVisual, change;
        
        if (count < 2) return false;
        
        function cBack(){
            if (typeof callback == 'function'){
                callback();
            }
        }
        
        preloadImg = function(){
            for (var i = 0; i < count; i++)
                src.push(img.eq(i).attr("src"));
        }
        
        setBg = function(){
            if (next == count){
                current = count - 1;
                next = 0;
                if (pause){
                    stop = true;
                    cBack();
                }
            }
            srcBg = src[current];
            srcVisual = src[next];
        }
        
        show = function(){
            setBg();
            current = next;

            (el.hasClass('random')) ? next = Math.floor(Math.random() * count ) : next++;

            bg.bind('load', function(){
				setTimeout(function(){
					visual.css({ "opacity" : 0 }).attr({ "src" : srcVisual });
				}, 20);
            }).attr({ "src" : srcBg });
			
			
        }
        
        showNext = function(){
            if (stop){
                clearInterval(change);
                return;
            }
			visual.fadeTo( timeFade, 1 , function(){
                show();
            });
            
        }
		
        function run(){
			if (!stop){
				showNext();
				change = setTimeout(run, timeChange);
			}
        };
        
    this.go = function(current){
        show(current);
		change = setTimeout(run, timeChange);
        //run();
    };
    preloadImg();
	window.onblur = function() { clearTimeout(change); }
	window.onfocus = function() { clearTimeout(change); change = setTimeout(run, timeChange); }
}

runSlides = function(){
    $('img.preload').hide();
    if ($('#lights').length != 0){
        showPeopleMove = function(){
            var animatePeople = new Animation('peopleMove', animateTimer, changeTimer);
                $("#peopleMove").show();
                setTimeout(function(){
					$('#lights').fadeTo(animateTimerLight, 0);
                    animatePeople.go();
                }, changeTimerLight - animateTimerLight);
        }
        var animateLight = new Animation('lights', animateTimerLight, changeTimerLight, true, showPeopleMove);
            animateLight.go();
    } else {
        if($("#peopleMove").length != 0){
            $("#peopleMove").show();
            var animatePeople = new Animation('peopleMove', animateTimer, changeTimer);
                animatePeople.go();
        }
    }
}
