
var Banner = Class.create({
	
	initialize: function(dirPath, images) {
		this.divs = ['banner-1', 'banner-2'];
		this.images = ['A.gif','D.gif','E.gif', 'F.gif', 'G.gif','H.gif','I.gif','L.gif','M.gif'];
		this.currentDiv = 0;
		this.currentImage = Math.floor(Math.random() * this.images.length);
		this.dirPath = dirPath;
		this.fadeDelay = 5000;
		this.fadeSpeed = 4000;
		
		// create object
	    var imageObj = new Image();

	    for(var i=0; i<this.images.length; i++){
	    	imageObj.src = this.dirPath+"/" +this.images[i];
	    }

	},
	
	init: function(){
		jQuery('#' + this.divs[this.currentDiv]).css("background-image","url("+this.dirPath+"/"+this.images[this.currentImage]+")");
		jQuery('#' + this.divs[this.currentDiv]).show();

		this.waitFadeBanner();
	},
	
	nextDiv: function(){
		this.currentDiv++;
		this.currentDiv = this.currentDiv % this.divs.length;
	},
	
	nextImage: function(){
		this.currentImage++;
		this.currentImage = this.currentImage % this.images.length;
	},
	
	fadeBanner: function(){
		jQuery('#' + this.divs[this.currentDiv]).css("background-image","url("+this.dirPath+"/"+this.images[this.currentImage]+")");
		jQuery('#' + this.divs[this.currentDiv]).fadeOut(this.fadeSpeed);
		
		this.nextDiv();
		this.nextImage();
		
		jQuery('#' + this.divs[this.currentDiv]).css("background-image","url("+this.dirPath+"/"+this.images[this.currentImage]+")");
		jQuery('#' + this.divs[this.currentDiv]).fadeIn(this.fadeSpeed, (function(){
			this.waitFadeBanner();
		}).bind(this));
	},
	
	waitFadeBanner: function(){
		setTimeout(this.fadeBanner.bind(this), this.fadeDelay);
	}
});

var dirPath = 'fileadmin/templates/main/images/banner';
//var images = ['1.gif','3.gif','13.gif','10.gif', '12.gif', '2.gif','11.gif','4.gif','5.gif','6.gif','7.gif','8.gif','9.gif'];



jQuery(window).load(function(){
	var headBanner = new Banner(dirPath);
	headBanner.init();
});


