var charcount = 12;//16 +/-4
var countshift = 8;
var exectime = 4000;
var processtime = 3;
var timeshift = 2;
var characters = new Array();
var coords = new Array();
var used = new Array();
var pair = new Array();

var x = 0;
var y = 0;
var active = 0;
var timeout = 0;
var op = 0;
var found = false;
var timer;

var bstart = 1;
var bcount = 14;
var gstart = 21;
var gcount = 54;


//main characters
coords[1] = [7,7];

coords[2] = [5,14];
coords[3] = [9,14];

coords[4] = [7,21];
coords[5] = [2,22];
coords[6] = [12,22];

coords[7] = [4,28];
coords[8] = [10,28];

coords[9] = [2,34];
coords[10] = [12,34];
coords[11] = [7,35];

coords[12] = [5,42];
coords[13] = [9,42];

coords[14] = [7,49];




//secondary characters
coords[21] = [7,0];
coords[22] = [5,5];
coords[23] = [9,5];
coords[24] = [5,9];
coords[25] = [9,9];

coords[26] = [4,12];
coords[27] = [7,12];
coords[28] = [11,12];
coords[29] = [4,16];
coords[30] = [7,16];
coords[31] = [11,16];

coords[32] = [5,19];
coords[33] = [9,19];

coords[34] = [0,20];
coords[35] = [4,20];
coords[36] = [10,20];
coords[37] = [14,20];

coords[38] = [5,23];
coords[39] = [9,23];

coords[40] = [0,24];
coords[41] = [4,24];
coords[42] = [10,24];
coords[43] = [14,24];

coords[44] = [2,26];
coords[45] = [5,26];
coords[46] = [9,26];
coords[47] = [12,26];

coords[48] = [2,30];
coords[49] = [5,30];
coords[50] = [9,30];
coords[51] = [12,30];

coords[52] = [0,32];
coords[53] = [4,32];
coords[54] = [10,32];
coords[55] = [14,32];

coords[56] = [5,33];
coords[57] = [9,33];

coords[58] = [0,36];
coords[59] = [4,36];
coords[60] = [10,36];
coords[61] = [14,36];

coords[62] = [5,37];
coords[63] = [9,37];

coords[64] = [4,40];
coords[65] = [7,40];
coords[66] = [11,40];
coords[67] = [4,44];
coords[68] = [7,44];
coords[69] = [11,44];

coords[70] = [5,47];
coords[71] = [9,47];
coords[72] = [5,51];
coords[73] = [9,51];
coords[74] = [7,56];

pair[0] = [21,34,42,48,57,62,71];
pair[1] = [25,29,36,40,47,52,66];
pair[3] = [28,53,38,43,45,61,67,74];
pair[4] = [22,31,35,50,59,69];
pair[5] = [23,30,37,49,55,63,68];
pair[6] = [26,33,52,64,73];
pair[7] = [27,41,54,58,70];
pair[8] = [24,39,44,56,65];
pair[9] = [32,46,60,72];


function logoInit() {
	// Main chars setup
	var i;
	for(i = bstart;i<(bstart+bcount);i++) {
		//random
		x = coords[i][0] + Math.floor(Math.random()*3);
		y = coords[i][1] + Math.floor(Math.random()*3);
		//centred
		x = coords[i][0] + 1;
		y = coords[i][1] + 1;
		//main chars cant overlap so no check on positioning at init
		used[i] = [x,y];
		active = 0;
		timeout = 0;
		//op = (5 + Math.floor(Math.random()*11)/2)/10;
		op = 1;//main characters always at full brightness
		$("#char_"+i).show().css({"top":x*15+"px","left":y*15+"px","opacity":op});
		characters[i] = [x,y,active,timeout,op];
	}
	//Secondary chars setup
	for(i = gstart;i<(gstart+gcount);i++) {
		x = coords[i][0] + Math.floor(Math.random()*3);
		y = coords[i][1] + Math.floor(Math.random()*3);
		if(i>1) {
			$.each(used, function(index, value) {
				if(value != undefined && value[0]==x && value[1]==y) {
					found = true;
				}
			});
		}
		while(found) {
			found = false;
			x = coords[i][0] + Math.floor(Math.random()*3);
			y = coords[i][1] + Math.floor(Math.random()*3);
			$.each(used, function(index, value) {
				if(value != undefined && value[0]==x && value[1]==y) {
					found = true;
				}
			});
		}
		used[i] = [x,y];
		active = 0;
		timeout = processtime*1000 + Math.floor(Math.random()*(timeshift+1))*1000;
		timeout = 0;
		op = (5 + Math.floor(Math.random()*11)/2)/10;
		$("#char_"+i).show().css({"top":x*15+"px","left":y*15+"px","opacity":op});
		characters[i] = [x,y,active,timeout,op];
		setTimeout('characterChange('+i+');', Math.floor(Math.random())*1000);
	}
	/*
	for(i=0;i<10;i++) {
	  setTimeout('charactersChange('+i+');', Math.random()*1000);
	}*/
	//timer = setTimeout("logoChange()",1000);
}

function charactersChange(k) {
  var fulltime = processtime*1000 + Math.floor(Math.random()*(timeshift+1))*1000;
  $.each(pair[k], function(index, ids) {
    characters[ids][3] = fulltime;    
    if(index==(pair[k].length-1)) {      
      $("#char_"+ids).stop().animate({"opacity":0},fulltime*0.4,function(){
	var id = $(this).attr("id").split("_");
	id = id[1];
	op = (5 + Math.floor(Math.random()*11)/2)/10;
	var fulltime = characters[id][3];
	x = coords[id][0] + Math.floor(Math.random()*3);
	y = coords[id][1] + Math.floor(Math.random()*3);
	$.each(used, function(index, value) {
	    if(value != undefined && value[0]==x && value[1]==y) {
		found = true;
	    }
	});
	while(found) {
	    found = false;
	    x = coords[id][0] + Math.floor(Math.random()*3);
	    y = coords[id][1] + Math.floor(Math.random()*3);
	    $.each(used, function(index, value) {
		if(value != undefined && value[0]==x && value[1]==y) {
		    found = true;
		}
	    });
	}
	used[id] = [x,y];
	characters[id][0] = x;
	characters[id][1] = y;
	$(this).css({"top":characters[id][0]*15+"px","left":characters[id][1]*15+"px"});
	$(this).animate({"opacity":characters[id][4]},characters[id][3]*0.6,function(){
	  var id = $(this).attr("id").split("_");
	  id = id[1];
	  var l = 0;

	  for(x in pair) {
	    var z = pair[x];
	    for(j in z) {
	      if(z[j]==id) {
		l = x;
	      }
	    }
	  }
	  charactersChange(l);
	});
      });
    }
    else {
      $("#char_"+ids).stop().animate({"opacity":0},fulltime*0.4,function(){
	var id = $(this).attr("id").split("_");
	id = id[1];
	op = (5 + Math.floor(Math.random()*11)/2)/10;
	var fulltime = characters[id][3];
	x = coords[id][0] + Math.floor(Math.random()*3);
	y = coords[id][1] + Math.floor(Math.random()*3);
	$.each(used, function(index, value) {
	    if(value != undefined && value[0]==x && value[1]==y) {
		found = true;
	    }
	});
	while(found) {
	    found = false;
	    x = coords[id][0] + Math.floor(Math.random()*3);
	    y = coords[id][1] + Math.floor(Math.random()*3);
	    $.each(used, function(index, value) {
		if(value != undefined && value[0]==x && value[1]==y) {
		    found = true;
		}
	    });
	}
	used[id] = [x,y];
	characters[id][0] = x;
	characters[id][1] = y;
	$(this).css({"top":characters[id][0]*15+"px","left":characters[id][1]*15+"px"});
	$(this).animate({"opacity":characters[id][4]},characters[id][3]*0.6);
      });
    }
  });
}

function characterChange(id) {
     var fulltime = processtime*1000 + Math.floor(Math.random()*(timeshift+1))*1000;
     characters[id][3] = fulltime;
     $("#char_"+id).animate({"opacity":0},fulltime*0.4,function(){
	var id = $(this).attr("id").split("_");
	id = id[1];
	op = (5 + Math.floor(Math.random()*11)/2)/10;
	var fulltime = characters[id][3];
	x = coords[id][0] + Math.floor(Math.random()*3);
	y = coords[id][1] + Math.floor(Math.random()*3);
	$.each(used, function(index, value) {
	    if(value != undefined && value[0]==x && value[1]==y) {
		found = true;
	    }
	});
	while(found) {
	    found = false;
	    x = coords[id][0] + Math.floor(Math.random()*3);
	    y = coords[id][1] + Math.floor(Math.random()*3);
	    $.each(used, function(index, value) {
		if(value != undefined && value[0]==x && value[1]==y) {
		    found = true;
		}
	    });
	}
	used[id] = [x,y];
	characters[id][0] = x;
	characters[id][1] = y;
	$(this).css({"top":characters[id][0]*15+"px","left":characters[id][1]*15+"px"});
	$(this).animate({"opacity":characters[id][4]},characters[id][3]*0.6,function(){
	    var id = $(this).attr("id").split("_");
	    id = id[1];
	    characterChange(id);
	    //setTimeout('characterChange('+id+');', 500);
	});
     });
}

function logoChange() {
	var i,a,k;
	var change = charcount + Math.floor(Math.random()*(countshift+1));
	k=100;
	while(change>0 && k>0) {
		k--;
		i = gstart+Math.floor(Math.random()*(gcount));
		a = characters[i][2];
		if(!a) {
			found = true;
			while(found) {
				found = false;
				x = coords[i][0] + Math.floor(Math.random()*3);
				y = coords[i][1] + Math.floor(Math.random()*3);
				$.each(used, function(index, value) {
					if(value != undefined && value[0]==x && value[1]==y) {
						found = true;
					}
				});
				used[i] = [x,y];
				active = 1;
				timeout = processtime*1000 + Math.floor(Math.random()*(timeshift+1))*1000;
				op = (5 + Math.floor(Math.random()*11)/2)/10;
				characters[i] = [x,y,active,timeout,op];
				$("#char_"+i).animate({"opacity":0},timeout/2,function(){
					var z = $(this).attr("id").split("_");
					z = z[1];
					$("#char_"+z).css({"top":characters[z][0]*15+"px","left":characters[z][1]*15+"px"});
					$("#char_"+z).animate({"opacity":characters[z][4]},characters[z][3]/2);
					characters[z][2]=0;
				});
			}
			change--;
		}
	}
	timer = setTimeout("logoChange()",exectime);
}

/*


 var charcount = 7;//10 +/-3
var countshift = 6;
var exectime = 4000;
var processtime = 1;
var timeshift = 2;
var characters = new Array();
var coords = new Array();
var used = new Array();
coords[1] = [4,2];
coords[2] = [8,1];
coords[3] = [4,4];
coords[4] = [8,4];
coords[5] = [7,6];
coords[6] = [8,9];
coords[7] = [2,10];
coords[8] = [5,10];
coords[9] = [10,12];
coords[10] = [4,13];
coords[11] = [7,13];
coords[12] = [8,18];
coords[13] = [5,20];
coords[14] = [4,21];
coords[15] = [11,26];
coords[16] = [3,27];
coords[17] = [8,27];
coords[18] = [5,28];
coords[19] = [4,34];
coords[20] = [6,35];
coords[21] = [8,35];
coords[22] = [6,39];
coords[23] = [7,42];
coords[24] = [5,44];
coords[25] = [9,44];
var x = 0;
var y = 0;
var active = 0;
var timeout = 0;
var op = 0;
var found = false;
var timer;
function logoInit() {
	for(var i=1;i<26;i++) {
		$("#char_"+i).hide();
		x = coords[i][0] + Math.floor(Math.random()*3);
		y = coords[i][1] + Math.floor(Math.random()*3);
		if(i>1) {
			$.each(used, function(index, value) {
				if(value != undefined && value[0]==x && value[1]==y) {
					found = true;
				}
			});
		}
		while(found) {
			found = false;
			x = coords[i][0] + Math.floor(Math.random()*3);
			y = coords[i][1] + Math.floor(Math.random()*3);
			$.each(used, function(index, value) {
				if(value != undefined && value[0]==x && value[1]==y) {
					found = true;
				}
			});
		}
		used[i] = [x,y];
		active = 0;
		timeout = processtime*1000 + Math.floor(Math.random()*(timeshift+1))*1000;
		timeout = 0;
		op = (5 + Math.floor(Math.random()*11)/2)/10;
		$("#char_"+i).show().css({"top":x*15+"px","left":y*15+"px","opacity":op});
		characters[i] = [x,y,active,timeout,op];
	}
	timer = setTimeout("logoChange()",1000);
}
function logoChange() {
	var i,xo,yo,a,k;
	var change = charcount + Math.floor(Math.random()*(countshift+1));
	k=100;
	while(change>0 && k>0) {
		k--;
		i = 1+Math.floor(Math.random()*25);
		a = characters[i][2];
		if(!a) {
			found = true;
			while(found) {
				found = false;
				x = coords[i][0] + Math.floor(Math.random()*3);
				y = coords[i][1] + Math.floor(Math.random()*3);
				$.each(used, function(index, value) {
					if(value != undefined && value[0]==x && value[1]==y) {
						found = true;
					}
				});
				used[i] = [x,y];
				active = 1;
				timeout = processtime*1000 + Math.floor(Math.random()*(timeshift+1))*1000;
				op = (5 + Math.floor(Math.random()*11)/2)/10;
				characters[i] = [x,y,active,timeout,op];
				$("#char_"+i).animate({"opacity":0},timeout/2,function(){
					var z = $(this).attr("id").split("_");
					z = z[1];
					$("#char_"+z).css({"top":characters[z][0]*15+"px","left":characters[z][1]*15+"px"});
					$("#char_"+z).animate({"opacity":characters[z][4]},characters[z][3]/2);
					characters[z][2]=0;
				});
			}
			change--;
		}
	}
	timer = setTimeout("logoChange()",exectime);
}


 **/
