if(!$chk(ROTATOR)) {
    var ROTATOR = {};
}

ROTATOR.rotators = new Array();

ROTATOR.initRotator = function(targetId, rotatorId, number) {
	window.addEvent('domready', function() {
		ROTATOR.showRotator(targetId, rotatorId, number);
	}.bind([targetId, rotatorId, number]));
}

ROTATOR.startRotator = function(targetId, timer, number) {
	banners = $$('#' + targetId + ' .banner');

	total = banners.length;
	
	if (total == 1) {
		banners[0].setStyle('display', 'block');
	} else {
		if (number >= total) {
			number = 0;
		}
		
		if (timer > 0) {
			banners.each(function(item, index) {
				item.setStyle('display', 'none');
			});
			
			banners[number].setStyle('display', 'block');
		
			number = number + 1;
			functionstring = 'ROTATOR.startRotator(\'' + targetId + '\', ' + timer + ', ' + number + ')';
		
			ROTATOR.rotators[targetId] = setTimeout(functionstring, timer * 1000);
		} else {
			//	Random banner
			var number= Math.floor(Math.random() * total);
			banners[number].setStyle('display', 'block');
		}
	}
}

ROTATOR.showRotator = function(targetId, rotatorId, type, number) {
	//Create post object for json
	var postObject = JSON.encode({
		action: 'EUROGetBannersForRotator',
		'rotatorid': rotatorId,
		'type': type,
		'number':number
	});



	//Create Json Remote object
	var completeCall = ROTATOR.showRotatorResponse.bindWithEvent(window, [targetId]);
	
	var jSonRequest = new Request({
									url:"/sbeos/ajax/JsonCall.php", 
									data: {json: postObject},
									onComplete: completeCall
		}).send();
}

ROTATOR.showRotatorResponse = function(JsonResponse, targetId) {
	$(targetId).set('html',JsonResponse.template);
	
	functionstring = 'ROTATOR.showRotator(\'' + targetId + '\', ' + JsonResponse.rotatorid + ', '
		+ '\'' + JsonResponse.type + '\', '	+ JsonResponse.number + ')';

	if (JsonResponse.timer > 0 && JsonResponse.total > 1) {
		ROTATOR.rotators[targetId] = setTimeout(functionstring, JsonResponse.timer * 1000);
	}
}

ROTATOR.stopRotator = function(targetId) {
	clearTimeout(ROTATOR.rotators[targetId]);
}
