dkanim={};
dkanim.preload=["./images/dolne/corkcomfort.jpg","./images/dolne/dekwall.jpg","./images/dolne/ipowood.jpg","./images/dolne/korek-techniczny.jpg","./images/dolne/vinylcomfort.jpg","./images/dolne/wicanders-wood.jpg","./images/banery/korektechniczny1.jpg","./images/banery/corkcomfort1.jpg","./images/banery/dekwall1.jpg",,"./images/banery/ipowood1.jpg","./images/banery/vinylcomfort1.jpg","./images/banery/wicanders1.jpg"];
dkanim.def=["banner1.jpg","banner2.jpg","banner3.jpg","banner4.jpg","banner5.jpg","banner6.jpg","banner7.jpg","banner8.jpg","banner9.jpg","banner10.jpg","banner11.jpg"];
dkanim.files=[];
dkanim.waitTime=4000;
dkanim.counter=0;
dkanim.counter2=0;
dkanim.inCollection=false;
dkanim.collection=[];
dkanim.loaded=[];

dkanim.animate = function(){
    if(dkanim.inCollection==false){
        dkanim.animateDef();
    }else{
        dkanim.animateCollection();
    }		
}


dkanim.animateDef = function(){
    this.counter++;
    if(this.counter>=this.files.length){
        this.counter=0;
    }
    var nextImg =  $("<img></img>");
    $(nextImg).attr("src","./images/"+dkanim.files[dkanim.counter]);
    $(nextImg).unbind("load");
    if(dkanim.loaded[dkanim.files[dkanim.counter]]==1){
	dkanim.animateDef2()
    }else{
	$(nextImg).bind("load",function(){
	    dkanim.animateDef2();
	});
    }
}

dkanim.animateDef2 = function(){
    dkanim.loaded[dkanim.files[dkanim.counter]]=1;
    $('#bannerImg').stop(true,true).hide(0,function(){
	    $('#bannerImg').attr('src',"./images/"+dkanim.files[dkanim.counter]);
	    $('#bannerImg').fadeIn(1000,function(){
		    $('#banner').css('background-image',"url('./images/"+dkanim.files[dkanim.counter]+"')");
		    $('#bannerImg').hide(0);
	    });

    });
    setTimeout("dkanim.animate()", dkanim.waitTime);
}


dkanim.animateCollection = function(){
    this.counter2++;
    if(this.counter2>=this.collection.length){
        this.counter2=0;
    }
    var nextImg =  $("<img></img>");
    $(nextImg).attr("src","./images/banery/"+dkanim.collection[dkanim.counter2]);
    $(nextImg).unbind("load");

    if(dkanim.loaded[dkanim.collection[dkanim.counter2]]==1){
	dkanim.animateCollection2();
    }else{
	$(nextImg).bind("load",function(){
	    dkanim.animateCollection2();
	});
    }
}

dkanim.animateCollection2 = function(){
        $('#photo2img').stop(true,true).hide(0,function(){
		dkanim.loaded[dkanim.collection[dkanim.counter2]]=1;
                $('#photo2img').attr('src',"./images/banery/"+dkanim.collection[dkanim.counter2]);
                $('#photo2img').fadeIn(1000,function(){
                        $('#photo1img').attr('src',"./images/banery/"+dkanim.collection[dkanim.counter2]);
                        $('#photo2img').hide(0);
                });

        });

        setTimeout("dkanim.animate()", dkanim.waitTime);
}


dkanim.preloadImages = function(){
	var nextImg = [];
	for(var i=0;i<dkanim.preload.length;i++){
		nextImg[i] =  $("<img></img>");
		$(nextImg[i]).attr("src",dkanim.preload[i]);
		dkanim.loaded[dkanim.preload[i]]=1;
	}
}

$().ready(function(){
	dkanim.files=dkanim.def;
	dkanim.preloadImages();
	setTimeout("dkanim.animate()", dkanim.waitTime);
	$('#menu3 a').hover(
			function(){
				if($('#banner2').length>0){
					var fileName = $(this).attr('title');
					$('#banner2').stop(true,true).hide(0).css('background-image',"url('./images/dolne/"+fileName+".jpg')").fadeIn(400);
				}
			}
			,
			function(){
				if($('#banner2').length>0){
					$('#banner2').fadeOut(400);
				}
			}
	);
});
