var arr_c1 = new Array(
'/image/c11.gif',
'/image/c12.gif'
);

var arr_c2 = new Array(
'/image/c21.gif',
'/image/c22.gif',
'/image/c23.gif',
'/image/c24.gif');

var arr_c3 = new Array(
'/image/c31.gif',
'/image/c32.gif',
'/image/c33.gif',
'/image/c34.gif');

var arr_c4 = new Array(
'/image/c41.gif',
'/image/c42.gif',
'/image/c43.gif',
'/image/c44.gif',
'/image/c45.gif',
'/image/c46.gif');
	
var arr_c5 = new Array(
'/image/c51.gif',
'/image/c52.gif'
);

var arr_c6 = new Array(
'/image/c61.gif',
'/image/c62.gif',
'/image/c63.gif',
'/image/c64.gif');

var arr_c7 = new Array(
'/image/c71.gif',
'/image/c72.gif',
'/image/c73.gif',
'/image/c74.gif');

var arr_c8 = new Array(
'/image/c81.gif',
'/image/c82.gif',
'/image/c83.gif',
'/image/c84.gif');

var arr_c9 = new Array(
'/image/c91.gif',
'/image/c92.gif');

img_c1_0 = new Image(476,68);
img_c1_1 = new Image(476,68);

img_c2_0 = new Image(476,68);
img_c2_1 = new Image(476,68);
img_c2_2 = new Image(476,68);
img_c2_3 = new Image(476,68);

img_c3_0 = new Image(112,68);
img_c3_1 = new Image(112,68);
img_c3_2 = new Image(112,68);
img_c3_3 = new Image(112,68);

img_c4_0 = new Image(181,293);
img_c4_1 = new Image(181,293);
img_c4_2 = new Image(181,293);
img_c4_3 = new Image(181,293);
img_c4_4 = new Image(181,293);
img_c4_5 = new Image(181,293);

img_c5_0 = new Image(476,293);
img_c5_1 = new Image(476,293);

img_c6_0 = new Image(112,293);
img_c6_1 = new Image(112,293);
img_c6_2 = new Image(112,293);
img_c6_3 = new Image(112,293);

img_c7_0 = new Image(181,112);
img_c7_1 = new Image(181,112);
img_c7_2 = new Image(181,112);
img_c7_3 = new Image(181,112);

img_c8_0 = new Image(476,112);
img_c8_1 = new Image(476,112);
img_c8_2 = new Image(476,112);
img_c8_3 = new Image(476,112);

img_c9_0 = new Image(112,112);
img_c9_1 = new Image(112,112);

var last = new Array(0,0,0,0,0,0,0,0,0);
var loaded = new Array(0,0,0,0,0,0,0,0,0);

function pageLoad()
{
	for(i=0; i<4; i++)
	{
		celle = (Math.floor(Math.random()*9+1));
		cellname = 'c' + celle;
		len = window['arr_c' + celle].length;
		num = Math.ceil(Math.floor(Math.random()*len+1)/2);		
		var ima = '/image/c'+celle + 'bg' + num + '.gif';
		document.getElementById(cellname).style.backgroundImage='url('+ima+')';
	}
	pre_load();
	setBga();
}

function pre_load()
{
	celle = Math.floor(Math.random()*9+1);
	if (fullArray(loaded) == 0)
	{
		if (inArra(loaded,celle) == false)
		{
			switch (celle){
				case 1:
					for(i=0; i < arr_c1.length; i++)
					{
						window['img_c1_'+i].src=arr_c1[i];
					}
					loaded[0] = 1;
					break;
				case 2:
					for(i=0; i < arr_c2.length; i++)
					{
						window['img_c2_'+i].src=arr_c2[i];
					}
					loaded[1] = 1;
					break;
		    case 3:
		  		for(i=0; i < arr_c3.length; i++)
					{
						window['img_c3_'+i].src=arr_c3[i];
					}
					loaded[2] = 1;
					break;
				case 4:
					for(i=0; i < arr_c4.length; i++)
					{
						window['img_c4_'+i].src=arr_c4[i];
					}
					loaded[3] = 1;
					break;
				case 5:
					for(i=0; i < arr_c5.length; i++)
					{
						window['img_c5_'+i].src=arr_c5[i];
					}
					loaded[4] = 1;
					break;
				case 6:
					for(i=0; i < arr_c6.length; i++)
					{
						window['img_c6_'+i].src=arr_c6[i];
					}
					loaded[5] = 1;
					break;
				case 7:
					for(i=0; i < arr_c7.length; i++)
					{
						window['img_c7_'+i].src=arr_c7[i];
					}
					loaded[6] = 1;
					break;
				case 8:
					for(i=0; i < arr_c8.length; i++)
					{
						window['img_c8_'+i].src=arr_c8[i];
					}
					loaded[7] = 1;
					break;
		  	case 9:
		  		for(i=0; i < arr_c9.length; i++)
					{
						window['img_c9_'+i].src=arr_c9[i];
					}
					loaded[8] = 1;
					break;
				default: 
					alert("fejl");
					break;
			}
		}setTimeout('pre_load()',500);
	}
}

function setBga()
{
	var nu = new Date();
	var is_changed = 0;
	celle = Math.floor(Math.random()*9+1);
	switch (celle){
		case 1:
			ima = nekst('c1', arr_c1.length);
			ima = ima-1;
			if ((window['img_c1_'+ima].complete) && ((last[0] + 50000) < nu.getTime()) )
			{
				is_changed = 1;
				document.getElementById('c1').style.backgroundImage='url('+window['img_c1_'+ima].src+')';
				last[0] = nu.getTime();
			}
			break;
		case 2:
			ima = nekst('c2', arr_c2.length);
			ima = ima-1;
			if ((window['img_c2_'+ima].complete) && ((last[1] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c2').style.backgroundImage='url('+window['img_c2_'+ima].src+')';
				last[1] = nu.getTime();
			}
			break;
		case 3:
			ima = nekst('c3', arr_c3.length);
			ima = ima-1;
			if ((window['img_c3_'+ima].complete) && ((last[2] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c3').style.backgroundImage='url('+window['img_c3_'+ima].src+')';
				last[2] = nu.getTime();
			}
			break;
		case 4:
			ima = nekst('c4', arr_c4.length);
			ima = ima-1;
			if ((window['img_c4_'+ima].complete) && ((last[3] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c4').style.backgroundImage='url('+window['img_c4_'+ima].src+')';
				last[3] = nu.getTime();
			}
			break;
		case 5:
			ima = nekst('c5', arr_c5.length);
			ima = ima-1;
			if ((window['img_c5_'+ima].complete) && ((last[4] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c5').style.backgroundImage='url('+window['img_c5_'+ima].src+')';
				last[4] = nu.getTime();
			}
			break;
		case 6:
			ima = nekst('c6', arr_c6.length);
			ima = ima-1;
			if ((window['img_c6_'+ima].complete) && ((last[5] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c6').style.backgroundImage='url('+window['img_c6_'+ima].src+')';
				last[5] = nu.getTime();
			}
			break;
		case 7:
			ima = nekst('c7', arr_c7.length);
			ima = ima-1;
			if ((window['img_c7_'+ima].complete) && ((last[6] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c7').style.backgroundImage='url('+window['img_c7_'+ima].src+')';
				last[6] = nu.getTime();
			}
			break;
		case 8:
			ima = nekst('c8', arr_c8.length);
			ima = ima-1;
			if ((window['img_c8_'+ima].complete) && ((last[7] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c8').style.backgroundImage='url('+window['img_c8_'+ima].src+')';
				last[7] = nu.getTime();
			}
			break;
		case 9:
			ima = nekst('c9', arr_c9.length);
			ima = ima-1;
			if ((window['img_c9_'+ima].complete) && ((last[8] + 50000) < nu.getTime()))
			{
				is_changed = 1;
				document.getElementById('c9').style.backgroundImage='url('+window['img_c9_'+ima].src+')';
				last[8] = nu.getTime();
			}
			break;
		default: 
			alert("fejl");
			break;
	}
	if (is_changed == 0)
	{setTimeout('setBga()',500);}
	if (is_changed == 1)
	{setTimeout('setBga()',7000);}
}

function inArra(arra, hide)
{
  		if (arra[hide-1] == 1)
  		{
  			return true;
  		}else { return false}
}

function fullArray(arra)
{
	full = 1;
	for (i=0; i<arra.length; i++)
	{
		if(arra[i] == 0)
		{
			full = 0;
		}
	}
	return full;	
}
	
function nekst(c, len)
{
	test = document.getElementById(c).style.backgroundImage;
	cur = test.charAt(test.length - 6);
	if (test.charAt(test.length - 7)=='g')
	{
		cur = cur * 2;
		return cur;
	}
	else if(cur % 2 == 1)
	{
		cur = parseInt(cur);
		cur = cur +1;
		return cur;
	}else{
		cur = 2*Math.floor(Math.random() * (len/2))+1;
	  return cur;
	}
}
