var bRotator = function(data) {
	this.data = data;
	this.init();
}

bRotator.prototype = {
	
	data: '',
	timer: new Array(),
	index: new Array(),
	count: new Array(),
	
	init: function() {
		this.startRotation();
	},
	
	startRotation: function() {
		for(var key in this.data) {
			this.index[key] = 0;
			this.calculateLength(key);
			this.rotateZone(key);
		}
	},
	
	calculateLength: function(key) {
		var i = 0;
		
		for(var skey in this.data[key]) {
			i++;
		}
		
		this.count[key] = i;
	},
	
	rotateZone: function(key) {
		var self = this;
		var currentIndex = this.index[key];
		
		var duration = this.data[key][currentIndex].duration * 1000;
		
		this.timer[key] = window.setTimeout(function() {
			self.changeBanner(key);
			self.rotateZone(key);
		}, duration);
	},
	
	changeBanner: function(key) {
		var debug = 0;
		
		if(debug) {
			console.groupEnd();
			console.group();
		
			console.log('change banner in zone ' + key);
		}
		
		// hide current banner
		var currentId = this.data[key][this.index[key]].id;
		
		if(debug) {
			console.log('curent banner ' +currentId);
		}
		$('#' + currentId).hide();
		
		// increase banner index in zone
		if(this.index[key] == this.count[key]-1) {
			if(debug) {			
				console.log('max banners in zone, backward (' + this.count[key] + ')');
			}
			this.index[key] = 0;
		} else {
			this.index[key] ++;
			if(debug) {
				console.log('increase banner index (' + this.index[key] + ' of ' + this.count[key]+')');
			}
		}
		
		// show next banner (or first)
		var nextId = this.data[key][this.index[key]].id;
		
		if(debug) {
			console.log('next banner ' +nextId);
		}
		
		$('#' + nextId).show();
		
		if(debug) {
			console.groupEnd();
		}
	}
	
}