//defines the possible texts
var bubbleText=new Array();
bubbleText[0]="8-11 April";       
bubbleText[1]="Dirty deals?";
bubbleText[2]="Power Politics?";
bubbleText[3]="Forced Freedom?";
bubbleText[4]="Times of Trial";       
bubbleText[5]="Economic Crises";
bubbleText[6]="Climate Change";
bubbleText[7]="Parallel truths";
bubbleText[8]=" Tolerance ";       
bubbleText[9]="Human Rights";
bubbleText[10]="Time to Act!";


//defines for each bubble what text is loaded
var bubble=new Array();
bubble[0]=0;       
bubble[1]=0;
bubble[2]=0;
bubble[3]=0;

//defines bubbleSize to adjust font size with
var bubbleSize = new Array();
bubbleSize[0]=1.2;       
bubbleSize[1]=1.0;
bubbleSize[2]=1.2;
bubbleSize[3]=1.1;

var defaultFontSize = 12;

function calculateFontSize (bubbleNr, textNr) {
	return defaultFontSize * bubbleSize[bubbleNr] * 15 / bubbleText[textNr].length;
}

function isVisible(number) {
	var i=0;
	for (i=0;i<4;i++)
	{
		if (bubble[i] == number)
			return true;
	}
	return false;
}

jQuery.fn.doFadeIn = function() {
	$(this).animate({
    opacity: 'show'
  }, 5000, function() {
    // Animation complete.
  });
}

jQuery.fn.doFadeOut = function(callback) {
	$(this).animate({
    opacity: 'hide'
  }, 5000, callback);
}

function updateBubble (jBubble, bubbleNr) {
	jBubble.doFadeOut(function () {
		jBubble.loadText (bubbleNr);
		jBubble.doFadeIn();

		setTimeout (function() {
			updateBubble(jBubble,bubbleNr);
		}, getRandomTime());
	});
}

function getRandomText () {
	var bottom = 0;
	var top = bubbleText.length;

	var number = 0;
	while (isVisible(number))
  {
		number = Math.floor(Math.random()*(top-bottom)) + bottom;
  }
  return number;
}

function getRandomTime() {
	var bottom = 4000;
	var top = 24000;

	return randomnumber=Math.floor(Math.random()*(top-bottom)) + bottom;
}

jQuery.fn.loadText = function(bubbleNr) {
	var textNr = getRandomText();
	bubble[bubbleNr] = textNr;
	$(this).html(bubbleText[textNr]);
	$(this).css('font-size',calculateFontSize (bubbleNr, textNr)+'px');
	
	return $(this);
}

$(document).ready(function() 
{

	var bubble0 = $('#bubble0 .text');
	var bubble1 = $('#bubble1 .text');
	var bubble2 = $('#bubble2 .text');
	var bubble3 = $('#bubble3 .text');
	
	
	bubble0.hide().loadText(0).fadeIn('slow');
	bubble1.hide().loadText(1).fadeIn('slow');
	bubble2.hide().loadText(2).fadeIn('slow');
	bubble3.hide().loadText(3).fadeIn('slow');

setTimeout (function() {
	updateBubble(bubble0,0);
}, getRandomTime());

setTimeout (function() {
	updateBubble(bubble1,1);
}, getRandomTime());

setTimeout (function() {
	updateBubble(bubble2,2);
}, getRandomTime());

setTimeout (function() {
	updateBubble(bubble3,3);
}, getRandomTime());

});
