var vmin=1;
var vmax=3;
var vr=2;
var timer1;
var xcoord;
var ycoord;
var roamstate = 0; // 0 = reset 1 =ready to roam 2=start roam 3 roaming
var debug = 1;
var earthstartxx =398;
var earthstartyy=608;
var roamcount=0;
var xyi=0;
var miny=40;
var minx=44;
var maxy=185;
var maxx=780;
var dir=1;	// 1=nw, 2=ne, 3=sw, 4-se
var ballstate =0;
var odot=0;
var colors = ['assets/greendot.gif', 'assets/pinkdot.gif', 'assets/yellowdot.gif', 'assets/bluedot.gif', 
			  'assets/purpledot.gif', 'assets/reddot.gif', 'assets/turquoisedot.gif'];
var time;
var timeflag=0;
var maxcutoffx=745;
var mincutoffx=82;
var minminx=40;
var maxmaxx=793;
var score=0;
var currentpic=1;

var texton=0;

var startopc=0;
var endopc=100;
var eid;
var speed;

function opacity(id, opacStart, opacEnd, millisec) {
    speed = Math.round(millisec / 100);
	eid=id;
	endopc=opacEnd;
	document.getElementById('apDivTitleText').style.visibility='visible';
	opacitytimer();
} 

function opacitytimer() {
        
    if(startopc > endopc) {
		startopc=startopc-3;
		if (startopc < endopc)
			startopc=endopc;
		if (startopc == endopc)
			document.getElementById('apDivTitleText').style.visibility='hidden';
		changeOpac(startopc, eid);
        setTimeout("opacitytimer()", speed);
	} 
	else {
		if(startopc < endopc) {
        	startopc=startopc+3;
			if (startopc>endopc)
				startopc=endopc;
			changeOpac(startopc, eid);
		    setTimeout("opacitytimer()", speed);
        }
    }
} 

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}



function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

//var maxpic=78;
var okbacks = [14,36,37,41,44,70,72,75,76,77,81,82,90,94,96,98,99,107];
var allbacks =[3,7,8,9,11,12,14,15,16,17,20,23,24,29,31,33,34,36,37,38,39,41,42,44,48,55,58,60,64,66,69,70,72,73,74,75,65,76,77,79,80,81,82,83,84,86,90,91,94,95,96,97,98,99,66,101,102,103,104,106,107,108];
var picidx;

function randompic() {
	picidx = Math.floor(Math.random()*okbacks.length+1);
	currentpic =okbacks[picidx-1];
	for (picidx in allbacks) {
		if (allbacks[picidx] == currentpic) {
			break;
		}
	}
	picidx++;
	var imgstring="url(assets/backs/back"+currentpic+".jpg)";
	document.getElementById('apDivBacks').style.backgroundImage=imgstring;
}

function handleleft() {
//	document.leftarrow.src='assets/leftarrow_down.gif';
	picidx--;
	if (picidx < 1) {
		picidx=allbacks.length;
	}
	currentpic = (allbacks[picidx-1]);
	var imgstring="url(assets/backs/back"+currentpic+".jpg)";
	document.getElementById('apDivBacks').style.backgroundImage=imgstring;
	}
	
function handleright() {
//	document.rightarrow.src='assets/rightarrow_down.gif';
	picidx++;
	if (picidx > allbacks.length) {
		picidx=1;
	}
	currentpic = (allbacks[picidx-1]);
	var imgstring="url(assets/backs/back"+currentpic+".jpg)";
	document.getElementById('apDivBacks').style.backgroundImage=imgstring;
}

function handletexttoggle() {
	//	document.textoff.src='assets/textoff_down.gif';
	if (texton){
		texton=0;
		opacity('apDivTitleText', 100,0,1000);	
	}
	else {
		texton=1;
			opacity('apDivTitleText', 0,100,1000);	
	}
}
	
	
function replace_left() {
	document.leftarrow.src='assets/leftarrow.gif';
}
function replace_textoff() {
	document.textoff.src='assets/textoff.gif';
}
function replace_right() {
	document.rightarrow.src='assets/rightarrow.gif';
}

var newwindow;
function launchplayer()
{
	newwindow=window.open("/Music/mp3player/kozz_player.html",'name','height=300,width=280');
	if (window.focus) {newwindow.focus()}
}

function imgearth(chipname,width,height){
	this.named=chipname;
	this.vx=10;
	this.vy=9;
	this.w=width;
	this.h=height;
	this.xx=earthstartxx;
	this.yy=earthstartyy;
	this.timer1=null;
}

function imgdot(chipname,width,height){
	this.named=chipname;
	this.w=width;
	this.h=height;
	this.vy=0;
	this.down=1;
	this.up=0;
	this.sink=0;
	this.sunk=0;
	this.dy=0;
	this.ballstate =0;
	this.timer1=null;
	this.ballstate=0;
	this.opac=100;
	this.settimer=0;
}

function settimeflag() {
	timeflag=1;
}

function handleOver() {
	if(roamstate == 0) {
		time=window.setTimeout('settimeflag()', 1000);
		roamstate=1;
	}
}
function handleEClick() {
	window.clearTimeout(time);
	roamstate = 0;
	timeflag =0;
}
function handleOut() {
	window.clearTimeout(time);
	if (roamstate==1){
		window.clearTimeout(time);
		if (timeflag ==1) {
			whiteBalloon.suppress=true;
			timeflag=0;
			roamstate = 2;
			roamcount++;
			document.onmousemove = alertCoord;
			moveearth("earthimg");
		}
		if (roamstate==1) {
			whiteBalloon.suppress=false;
			roamstate=0;
		}
	}
}

function alertCoord(e) {
  if( !e ) { e = window.event; }
  if( !e ) { return; }
  if( typeof( e.pageX ) == 'number' ) {
    xcoord = e.pageX;
    ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    xcoord = e.clientX;
    ycoord = e.clientY;
    if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      xcoord += document.body.scrollLeft;
      ycoord += document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      xcoord += document.documentElement.scrollLeft;
      ycoord += document.documentElement.scrollTop;
    }
  } else { return; }
}

function moveearth(chipname){
 	if (document.getElementById){
		eval("imgearth="+chipname);
		if (window.innerWidth || window.opera){
			pageX=window.pageXOffset;
			pageW=window.innerWidth-40;
			pageY=window.pageYOffset;
			pageH=window.innerHeight-20;
	    }
	   	else if (document.body){
			pageX=iecompattest().scrollLeft;
			pageW=iecompattest().offsetWidth-40;
			pageY=iecompattest().scrollTop;
			pageH=iecompattest().offsetHeight-20;
		} 
		if (roamstate==3) {
			if ((Math.abs(imgearth.xx-earthstartxx) < 20) && (Math.abs(imgearth.yy-earthstartyy) <20)) {
				imgearth.xx=earthstartxx;
				imgearth.yy=earthstartyy;
				roamstate=0;
				// Unregister the capturing event handlers.
	        	if (document.removeEventListener) {  // DOM Event Model
		            document.removeEventListener("mousemove", alertCoord, true);
		        }
	    	    else if (document.detachEvent) {  // IE 5+ Event Model
		            document.detachEvent("onmousemove", alertCoord);
	    	    }
			}
		}
		if (roamstate > 1) {
			if (roamstate==2) {
				if ((Math.abs(imgearth.xx-earthstartxx) >200) || (Math.abs(imgearth.yy-earthstartyy) >200)) {
					roamstate=3;
					switch (roamcount) {
						case 1:
							alert('I think you should be careful with that thing. Why don\'t you just put it back and leave it alone.');
							break;
//						case 2:
//							alert('Really I must insist you stop leading the world astray, return it immediately');
//							break;
//						case 3:
//							alert('I tire of your games. DO NOT DO IT AGAIN!!');
//							break;
						case 2:
							alert('I tried to warn you, its not a toy. Now you\'ve gone and ruined it.');
							document[imgearth.named].src = 'assets/onfire.gif';
							break;
						default:
							break;
					}
				}
			}

			if (imgearth.xx < xcoord) {  //d-42
				if ((xcoord-imgearth.xx) >= imgearth.vx) { //d-42
					imgearth.xx=imgearth.xx+imgearth.vx;
				}
			}
			else {
			   if ((imgearth.xx - xcoord) >= imgearth.vx) { //d+42
				   imgearth.xx=imgearth.xx-imgearth.vx;
			   }
			}
			if (imgearth.yy < ycoord) { //d-42
				// must move down
				if ((ycoord - imgearth.yy) >= imgearth.vy) { //d-42
					imgearth.yy=imgearth.yy+imgearth.vy;
				}
			}
			else {
				// move up
				if ((imgearth.yy-ycoord) >= imgearth.vy) { //d+42
					imgearth.yy=imgearth.yy-imgearth.vy;
				}
			}
			imgearth.timer1=setTimeout("moveearth('"+imgearth.named+"')",100);
		}
		document.getElementById(imgearth.named).style.left=(imgearth.xx-(imgearth.w/2))+"px";
		document.getElementById(imgearth.named).style.top=(imgearth.yy-(imgearth.h/2))+"px";
  	}
}

function overdot() {
	odot=1;
}

function startball() {
	switch (ballstate) {
		case 0:
			document.getElementById('apDivScore').style.visibility='hidden';
			ballstate=4; //random movement
			movedot("smalldot");
			movedot("smalldot1");
			movedot("smalldot2");
			movedot("smalldot3");
			break
		case 2: // Bouncing
			document.getElementById('apDivScore').style.visibility='hidden';
			ballstate=0;
			break
		case 1:	//wall bounce
			score=0;
			document.getElementById('apDivScore').style.visibility='visible';
			Maxy = 185;
			ballstate=2 //bouncing up down
			break
		case 4: //random movement
			document.getElementById('apDivScore').style.visibility='hidden';
			ballstate=1; //random movement
			break;
	}
}

function stopball() {
}

function setminmaxx(x,y) {
	if (x < mincutoffx ) {
		ymod = Math.abs(109 - y);
		if (ymod >30) {
			minx=minminx+(ymod-30);
		}
	}
	else if (x > maxcutoffx ) {
		ymod = Math.abs(109 - y);
		if (ymod >30) {
		maxx=maxmaxx-(ymod-30);
		}
	}
}


function movedot(chipname){
 	if (document.getElementById){
		eval("imgdot="+chipname);
		if (window.innerWidth || window.opera){
			pageX=window.pageXOffset;
     		pageW=window.innerWidth-40;
     		pageY=window.pageYOffset;
     		pageH=window.innerHeight-20;
    	}
   		else if (document.body){
			pageX=iecompattest().scrollLeft;
     		pageW=iecompattest().offsetWidth-40;
     		pageY=iecompattest().scrollTop;
     		pageH=iecompattest().offsetHeight-20;
    	} 

		switch (ballstate) {
			case 2: //bouncing
				imgdot.ballstate=ballstate;
				document.getElementById(imgdot.named).style.zIndex="9";
				if(imgdot.sunk == 0) {
					if (imgdot.sink == 1) {
						imgdot.yy++;
						if(imgdot.yy>=(maxy+10)) {
							if (imgdot.xx >745)
								imgdot.xx =739;
							if (imgdot.xx < 78)
								imgdot.xx = 84;
						}


						if(imgdot.yy>=(maxy+42)){   //+16)) {
							imgdot.sink =0;
							imgdot.dy=0;
							imgdot.down=1;
							imgdot.sunk++;
	//						imgdot.yy=maxy+16;
						}
					}
					else {
						if (imgdot.down) {
							imgdot.dy++;
							imgdot.yy=imgdot.yy+imgdot.dy;
							score=score+imgdot.dy;
							if (imgdot.yy >= maxy) {
								imgdot.yy=maxy;
								imgdot.up=1;
								imgdot.down=0;
								document.getElementById('apDivScore').innerHTML = '<p>' + score +'<\/p>';
							}
							
						}
						else {
							imgdot.dy--;
							// Add some randomness to the mix
							if (Math.floor(Math.random()*10+1)  > 8) 
								imgdot.dy--;
							imgdot.yy=imgdot.yy-imgdot.dy;
							if (imgdot.dy <=0) {
								if (imgdot.yy >= maxy) {
									//we done
									imgdot.sink=1;
								}
								imgdot.down=1;
								imgdot.up=0;
							}
						}	
					}
					}
					else {
					document.getElementById(imgdot.named).style.zIndex="9";
				if (imgdot.sink == 1) {
					imgdot.sunk=2;
					imgdot.yy++;
					if(imgdot.yy>=(maxy+424)){   //+16)) {
						imgdot.sunk=2;
						imgdot.yy=maxy+424;
					}
				}
				else {
					if (imgdot.down) {
						imgdot.dy++;
						imgdot.yy=imgdot.yy+imgdot.dy;
						score=score+imgdot.dy;
						if (imgdot.yy >= (maxy+404)) {
							imgdot.yy=maxy +404;
							imgdot.up=1;
							imgdot.down=0;
							document.getElementById('apDivScore').innerHTML = '<p>' + score +'<\/p>';
						}
						
					}
					else {
						imgdot.dy--;
						// Add some randomness to the mix
						if (Math.floor(Math.random()*10+1)  > 8) 
							imgdot.dy--;
						imgdot.yy=imgdot.yy-imgdot.dy;
						if (imgdot.dy <=0) {
							if (imgdot.yy >= (maxy+404)) {
								//we done
								imgdot.sink=1;
							}
							imgdot.down=1;
							imgdot.up=0;
						}
					}	
				}
				break					
						
					}
				break

					
	
			case 1: // richochet
				if (imgdot.ballstate!=ballstate) { //reinit
					imgdot.ballstate=ballstate;
					imgdot.vx=Math.floor(Math.random()*8+1)+2; //vmin+vmax*Math.random();
					imgdot.vy=Math.floor(Math.random()*8+1)+2;//vmin+vmax*Math.random();

				//	imgdot.dotcolor=Math.floor(Math.random()*7);
					imgdot.dir=Math.floor(Math.random()*4+1);
				//	imgdot.dotcolor=Math.floor(Math.random()*7);
				//	document[imgdot.named].src = colors[imgdot.dotcolor];
					imgdot.dy=0;
					imgdot.sink=0;
				}
				minx=minminx;
				maxx=maxmaxx;
				document.getElementById(imgdot.named).style.zIndex="11";
		
			
				switch(imgdot.dir) {
					case 1: //nw
						imgdot.xx=imgdot.xx-imgdot.vx; imgdot.yy=imgdot.yy-imgdot.vy;
						setminmaxx(imgdot.xx, imgdot.yy);

						if (imgdot.xx < minx) {
							imgdot.xx=minx;
							imgdot.dir =2;
						}
						if(imgdot.yy<miny) {
							imgdot.yy=miny;
							imgdot.dir=3;
						}
						break;
				
					case 2: //ne
					
						imgdot.xx=imgdot.xx+imgdot.vx; imgdot.yy=imgdot.yy-imgdot.vy;
						setminmaxx(imgdot.xx, imgdot.yy);
						if (imgdot.xx > maxx) {
							imgdot.xx=maxx;
							imgdot.dir =1;
						}
						if(imgdot.yy<miny) {
							imgdot.yy=miny;
							imgdot.dir=4;
						}
						break;
				
					case 3: //sw
						imgdot.xx=imgdot.xx-imgdot.vx; imgdot.yy=imgdot.yy+imgdot.vy;
						setminmaxx(imgdot.xx, imgdot.yy);
						if (imgdot.xx < minx) {
							imgdot.xx=minx;
							imgdot.dir =4;
						}
						if(imgdot.yy>maxy) {
							imgdot.yy=maxy;
							imgdot.dir=1;
						}
						break;
				
				
					case 4: //se
						imgdot.xx=imgdot.xx+imgdot.vx; imgdot.yy=imgdot.yy+imgdot.vy;
						setminmaxx(imgdot.xx, imgdot.yy);
						if (imgdot.xx > maxx) {
							imgdot.xx=maxx;
							imgdot.dir =3;
						}
						if(imgdot.yy>maxy) {
							imgdot.yy=maxy;
							imgdot.dir=2;
						}
						break
				}
				break;
			case 4:		// Start ranodm movement
				if(imgdot.ballstate!=ballstate) {
				
					imgdot.ballstate = ballstate;
					imgdot.sunk=0;	
					clearTimeout(imgdot.timer1); 
					imgdot.vx=vmin+vmax*Math.random();
					imgdot.vy=vmin+vmax*Math.random();
					imgdot.opac
					imgdot.xx=400;
					imgdot.yy=120;
					imgdot.colorchange=7;
					imgdot.dotcolor=Math.floor(Math.random()*7);
//					document.getElementById(imgdot.named).style.width="77";
//					document.getElementById(imgdot.named).style.height="77";
					
					document[imgdot.named].src =  colors[imgdot.dotcolor];
				}
				
				minx=minminx;
				maxx=maxmaxx;
				document.getElementById(imgdot.named).style.zIndex="11";
				imgdot.xx=imgdot.xx+imgdot.vx;
				imgdot.yy=imgdot.yy+imgdot.vy;
   
				imgdot.vx+=vr*(Math.random()-0.5);
				imgdot.vy+=vr*(Math.random()-0.5);
				if(--imgdot.colorchange == 0) {
					var tempy;
					tempy = (Math.floor(Math.random()*6)+1) ; //1-6
					imgdot.dotcolor = ((imgdot.dotcolor + tempy) % 7)
					document.getElementById(imgdot.named).style.width="77";
					document.getElementById(imgdot.named).style.height="77";
							
					document[imgdot.named].src =  colors[imgdot.dotcolor];
					imgdot.colorchange=7;
				}
//				imgdot.opac+=8*(Math.random()-0.5);
//				if(imgdot.opac>100) 
//					imgdot.opac=100;
//				if(imgdot.opac<10)
//					imgdot.opac=10;
//				changeOpac(imgdot.opac, imgdot.named);
				if(imgdot.vx>(vmax+vmin))  imgdot.vx=(vmax+vmin)*2-imgdot.vx;
				if(imgdot.vx<(-vmax-vmin)) imgdot.vx=(-vmax-vmin)*2-imgdot.vx;
				if(imgdot.vy>(vmax+vmin))  imgdot.vy=(vmax+vmin)*2-imgdot.vy;
				if(imgdot.vy<(-vmax-vmin)) imgdot.vy=(-vmax-vmin)*2-imgdot.vy;

				setminmaxx(imgdot.xx, imgdot.yy);
				if(imgdot.xx<=minx){
					imgdot.xx=minx;
					imgdot.vx=vmin+vmax*Math.random();
				}
				if(imgdot.xx>=maxx){
					imgdot.xx=maxx;
					imgdot.vx=-vmin-vmax*Math.random();
				}
				if(imgdot.yy<=miny) {
					imgdot.yy=miny;
					imgdot.vy=vmin+vmax*Math.random();
				}
				if(imgdot.yy>=maxy) {
					imgdot.yy=maxy;
					imgdot.vy=-vmin-vmax*Math.random();
				}
				break;
		}

		imgdot.settimer=0;
		if(ballstate ==2) {
			document.getElementById(imgdot.named).style.left=(imgdot.xx - (imgdot.w/2))+"px";
			document.getElementById(imgdot.named).style.top=(imgdot.yy- (imgdot.h/2))+"px";
			if(imgdot.sunk < 2) 
				imgdot.settimer=1;
			else {
				document.getElementById(imgdot.named).style.left=-500+"px";
				imgdot.sunk=0;
			}
		}
			
		else {													  
			if (ballstate==0) {
				document.getElementById(imgdot.named).style.left=-500+"px";
			}
			else {
				document.getElementById(imgdot.named).style.left=(imgdot.xx - (imgdot.w/2))+"px";
				document.getElementById(imgdot.named).style.top=(imgdot.yy- (imgdot.h/2))+"px";
			}
			imgdot.settimer=1;
		}
		if (imgdot.settimer) {
			imgdot.timer1=setTimeout("movedot('"+imgdot.named+"')",10);
		}
  	}
}

var earthimg;
var smalldot;
var smalldot1;
var smalldot2;
var smalldot3;

function pagestart(){
	earthimg=new imgearth("earthimg",84,84);
	smalldot=new imgdot("smalldot",22,22);
	smalldot1=new imgdot("smalldot1",22,22);
	smalldot2=new imgdot("smalldot2",22,22);
	smalldot3=new imgdot("smalldot3",22,22);
	randompic();
}	

if (window.addEventListener)
window.addEventListener("load", pagestart, false)
else if (window.attachEvent)
window.attachEvent("onload", pagestart)
else if (document.getElementById)
window.onload=pagestart


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}