// JavaScript Document
var tmpdiv=null;
var bublesarray = Array();
var tick = 0;
var order = 0;
var next = 0;
var screenw;
var screenh;
var bublescount = 0;
var bublesimage = Array();
var isLoad = 0;
var chet = 0;
var timer=null;
var enot1 = null;
var enot2 = null;
function preload(){
   for(i=0;i<4;i++){
	  bublesimage[i] = new Image();
	  bublesimage[i].src = '/templates/miranda/images/buble'+i+'.png';
   }
   enot1 = new Image();
   enot1.src = '/templates/miranda/images/enot.gif';
   enot2 = new Image();
   enot2.src = '/templates/miranda/images/enot2.gif';
   var tmp = document.cookie.split(';');
   var tmp1;
   var cookie=Array();
   for(x in tmp){
	   tmp1 = tmp[x].split('=');
	   tmp1[0]=tmp1[0].replace(' ','');
	   tmp1[1]=tmp1[1].replace(' ','');
	   cookie[tmp1[0]]=tmp1[1];
   }
   try{
       if(cookie['stopping']=='1'){
	      if(document.getElementById('enot')) document.getElementById('enot').src = enot2.src;
       }else{
          gobuble();
       }
   }catch(e){
	   gobuble();
   }
}
function setStopingCookie(val){
	var expire = new Date();
	expire.setTime(expire.getTime()+(7*24*60*60*1000));
	document.cookie = "stopping=" + val + "; expires=" + expire.toGMTString();
}
function stopbuble(){
	if(timer){
		clearTimeout(timer);
		if(document.getElementById('enot')) document.getElementById('enot').src = enot2.src;
		timer = null;
		setStopingCookie(1);
	}else{
		if(document.getElementById('enot')) document.getElementById('enot').src = enot1.src;
		setStopingCookie(0);
		if(!bublescount){
		   gobuble();
		}else{
		   changeposition();
		}
	}
}
function gobuble(){
	if(document.getElementById('bubles')){
	   screenw = 960;
	   screenh = document.body.clientHeight;
	   if(document.body.scrollHeight){
		   screenh = document.body.scrollHeight;
	   }else{
		   if(document.height){
			   screenh = document.height;
		   }else{
			   if(document.body.style.pixelHeight){
				   screenh = document.body.style.pixelHeight;
			   }
		   }
	   }
	   screenh -= 70;
	   var maxj = Math.ceil(screenh/80);
	   next = Math.ceil(screenh/8);
	   maxj = (maxj>12)?12:maxj;
	   tmpdiv = document.getElementById('bubles');
	   for(var i=0; i<4; i++){
		   var k=0;
		   for(var j=0; j<maxj; j++){
		      tmpdiv.innerHTML +='<div id="buble'+(i*maxj+j)+'" class="bublecover"><img id="bimg'+(i*maxj+j)+'" src="" width="36px" height="36px"></div>';
			  document.getElementById('bimg'+(i*maxj+j)).src = bublesimage[k].src;
			  bublesarray[i*maxj+j] = Array();
			  bublesarray[i*maxj+j][0]=screenh;
			  k=(k==3)?0:k+1;
		   }
	   }
	   bublescount = bublesarray.length;
	   changeposition();
	}
}
function changeposition(){
	for(var i=0; i<bublescount; i++){
		var obj = document.getElementById('buble'+i);
		if(i==order && (obj.style.visibility == 'hidden' || !obj.style.visibility)){
			bublesarray[i][0] = screenh;
			obj.style.top = bublesarray[i][0];
			if(chet){
				bublesarray[i][1] = screenw - Math.ceil(100*Math.random());
			}else{
				bublesarray[i][1] = Math.ceil(180*Math.random());
			}
			obj.style.left = bublesarray[i][1];
			obj.style.visibility = 'visible';
			chet = (chet)?0:1;
		}else{
			if(obj.style.visibility == 'visible'){
				bublesarray[i][0]-=1;
				obj.style.top = bublesarray[i][0];
				if(bublesarray[i][0]<100 || (bublesarray[i][0]<160 && bublesarray[i][1]<65) || (bublesarray[i][0]<220 && bublesarray[i][1]>170)) obj.style.visibility = 'hidden';
			}
		}
	}
	tick++;
	if(tick == next*10) tick=0;
	if(tick%next == 0) order++;
	if(order==bublescount) order=0;
	timer = setTimeout('changeposition()',10);
}
