var Wd = 50;
var Hg = 35;
var NN = (document.layers ? true : false);
var z=0;
var uni_hide = (NN ? 'hide' : 'hidden');
var uni_show = (NN ? 'show' : 'visible');
var freq = 5;
var cfreq = freq;
var speed_f = 10;
var randPause = 100;
var incr = 1;
var maxnum = 4;
var num = 0;
var pozX = 150;
var pozY = 250;
var m_x = 0;
var m_y = 0;
var bab_x=0;
var bab_y=0;

function nop(){}

function mkLayer(nam, viz) {
	if (NN) {
		document.writeln('<LAYER NAME="' + nam + '" LEFT=' + pozX + ' TOP=' + pozY +  ' WIDTH=' + Wd + ' HEIGHT=' + Hg + ' VISIBILITY="' + viz + '"' + ' z-Index=' + (z++) + '><IMG SRC =../base-img/bfly/' + nam + '.gif width=' + Wd + ' height=' + Hg + ' border=0></LAYER>');
		}
	else {
		document.writeln('<DIV ID="' + nam + '" STYLE="position:absolute; overflow:none; left:' + pozX + 'px; top:' + pozY + 'px; width:' + Wd + 'px; height:' + Hg + 'px;' + ' visibility:' + viz + '; z-Index=' + (z++) + '"><IMG SRC="../base-img/bfly/' + nam + '.gif" width="' + Wd + '" height="' + Hg + '" border="0"></DIV>');
		}
	}

function genPage(){
	var n = 'b';
	for(var i=0; i<=maxnum; i++){
		n='b'+ i;
		mkLayer(n, uni_hide);		
		}
}

function uni_layer(nam) { if (NN) { return document.layers[nam]; } else { return eval('document.all.' + nam + '.style'); } }
	
function hideLayer(nam) { uni_layer(nam).visibility = uni_hide; }

function showLayer(nam) { uni_layer(nam).visibility = uni_show; 	}

function cycle() {
setTimeout("cycle()", speed_f);
	if(cfreq<0){
		var prev_num = num;
		num = num + incr;
		if (num == maxnum){
			incr = -1;
			cfreq=Math.floor(Math.random()*randPause)+10;
			}
		else cfreq=freq;
		if (num == 0){
			incr = 1;
			cfreq=Math.floor(Math.random()*randPause)+20;
			}
		hideLayer('b'+prev_num);
		showLayer('b'+num);
		}
	cfreq--;
}

function moveLayer(){
	var dx=(m_x-pozX)*(m_x>pozX?1:-1);
	var dy=(m_y-pozY)*(m_y>pozY?1:-1);
	if (dx>30||dy>30){	
		pozX=Math.round(pozX+(m_x-pozX)*.05);
		pozY=Math.round(pozY+(m_y-pozY)*.05);
		for(var i=0; i<=maxnum; i++){
		var layer=(document.layers)?document.eval('b'+i): eval('b'+i).style;
		layer.top=pozY;
		layer.left=pozX;
		}
	freq = 0;
	cfreq= -1;
	}
	else{
	freq = 10;
	}
setTimeout('moveLayer()',50);
}

function DetectMove(){
	if(NN){
		window.captureEvents(Event.MOUSEMOVE);
		window.onMouseMove=getM_N;
	} else   window.document.onmousemove=getM_I;
}

function  getM_N(e){ m_x = e.pageX-Wd*.5; m_y = e.pageY-Hg*.5; }

function getM_I(){ m_x = window.event.x + document.body.scrollLeft-Wd*.5; m_y = window.event.y + document.body.scrollTop-Hg*.5; }

genPage();
DetectMove();
moveLayer();
cycle();
