SPEED=100;
goblin={
	state: "door",
	anim_c: 0,
	anim_max: 0,
	min_left: 0,
	max_left: 0,
	active: 0,
	maxsquig: 0,
	maxboom_y: 0,
	direction: 1,
	init: function(){			
		if(this.active) return;			
		this.active=1;
		var offset=$("#header").offset();
		this.min_left=offset.left+5;		
		this.max_left=offset.left+900;
		this.maxsquig=$("body").width();	
		this.maxboom_y=$(document).height()-50;	
		var opening=document.createElement("img");
		$(opening).css({position:"absolute",top:"68px",left:(offset.left+47)+"px",zIndex:100});		
		$(opening).attr("src","/war/gobbo/img/opendoor.gif");	
		$(opening).attr("id","door_opening");		
		var door=document.createElement("img");
		$(door).css({position:"absolute",top:"68px",left:(offset.left+47)+"px",zIndex:103});		
		$(door).attr("src","/war/gobbo/img/door1.png");
		$(door).attr("id","door");
		this.gy=50;
		this.gx=offset.left+33;
		var g=document.createElement("img");
		$(g).css({position:"absolute",top:this.gy+"px",left:this.gx+"px",zIndex:102});
		$(g).attr("src","/war/gobbo/img/smallgoblin1.png");
		$(g).attr("id","goblin");	
		this.gimg=g;		
		
		$(g).click(function(){ goblin.remove();});
	
		$("#body_content").append(opening);		
		$("#body_content").append(g);		
		$("#body_content").append(door);		
		this.state="door";		
		this.anim_c=1;		
		if(!window.paused){
			window.goblin.interval=setInterval("goblin.animate()",SPEED);
		}		
	},
	
	remove: function(){		
		for(var i=0;i<this.zaps.length;i++){
			this.zaps[i].cleanup();
		}
		clearInterval(this.interval);
		$(this.gimg).remove();		
	},
	moveGoblin: function(x,y){
		this.gy+=y;
		this.gx+=x;
		this.gimg.style.top=this.gy+"px";
		this.gimg.style.left=this.gx+"px";
	},
	
	pause: function(){
			
		clearInterval(window.goblin.interval);
		window.paused=1;
		return true;		
	},	
	unpause: function(){		
		if(window.paused && window.goblin.active){
		  window.goblin.interval=setInterval("goblin.animate()",SPEED);		  
		  window.paused=0;
		}	
		return true;
	},
	animate: function(){
	    this.anim_c++;	   
		if(this.state=="door"){
			if(this.anim_c>4){		
				$("#door").css("zIndex",101);			
				this.anim_c=1;				
				this.state="grow";				
			}else{			
				$("#door").attr("src","/war/gobbo/img/door"+this.anim_c+".png");							
			}	
		}else if(this.state=="grow"){		
			if(this.anim_c>14){							
				this.moveGoblin(0,4);				
				this.state="walk";
				this.anim_c=1;
			}else if(this.anim_c>4){
				this.gimg.src="/war/gobbo/img/goblin1.png";
				this.moveGoblin(0,4);
				$("#door_opening").remove();			
				$("#door").remove();			
			}else{	
				this.gimg.src="/war/gobbo/img/smallgoblin"+this.anim_c+".png";
				$("#door").attr("src","/war/gobbo/img/door"+(5-this.anim_c)+".png");						
			}
		}else if(this.state=="walk"){						
			if(Math.random()*50<1){
			   this.direction=-this.direction;
			}
			
			dx=this.direction*4;				
			if(this.gx+dx<this.min_left) dx=0;
			else if(this.gx+dx>this.max_left) dx=0;
			else{ 
				if(this.anim_c>2){
					this.anim_c=1;
				}
				
				this.gimg.src="/war/gobbo/img/goblin"+this.anim_c+".png";
				this.moveGoblin(dx,0);				
			}	
			
			if(Math.random()*20<1){
				this.anim_c=1;
				this.state="zap";	
				this.gimg.src="/war/gobbo/img/goblinzap"+this.anim_c+".png";		
			}		
		}else if(this.state=="zap"){
			if(this.anim_c>4){
				this.anim_c=0;
				this.state="dance";				
				this.addZap();				
			}else{
				this.gimg.src="/war/gobbo/img/goblinzap"+this.anim_c+".png";
			}	    
		}else if(this.state=="dance"){
			if(this.anim_c>4 && Math.random()*2<1){
				this.anim_c=1;
				// keep dancing
			}
		
			if(this.anim_c>4){
				this.anim_c=0;
				this.state="walk";				
			}else{				
				this.gimg.src="/war/gobbo/img/goblindance"+this.anim_c+".png";
			}		
		}
		
		for(var i=0;i<this.zaps.length;i++){
			if(!this.zaps[i].boom) this.zaps[i].animate();
		}
	},
	
	addZap:function(){
		z=new Zap();
		z.x=this.gx+25;
		z.y=this.gy-10;		
		var w=$(window).width();
		z.dx=Math.random()*w/50-w/100;
		z.dy=15;
		z.init();	
		this.zaps[this.zaps.length]=z;
		if(this.zaps.length>20){
			var z2=this.zaps.shift();
			z2.cleanup();
		}
	},
	
	zaps: [],
	
	dummy: 0
};

window.goblin=goblin;

function Zap(){}

Zap.prototype={
	x:0,
	y:0,
	dx:0,
	dy:0,
	img:0,
	boom: 0,
	anim_c:1,
	life: 0,
	chomp: 0,
	chompwidth:0,
	chompx: 0,
	state:"move",
	init:function(){
		this.img=document.createElement("img");
		this.img.style.top=this.y+"px";
		this.img.style.left=this.x+"px";
		this.img.style.position="absolute";				
		this.img.src="/war/gobbo/img/bolt1.png";
		var t=this;
		this.img.onmouseover=function(){ t.mousehit();}
		$("#body_content").append(this.img);
	},
	cleanup: function(){
		$(this.img).remove();
	},
	mousehit: function(){
		if(this.state=="move"){
			this.state="boom";
			this.anim_c=0;
		}
	},
	animate:function(){
		this.anim_c++;		
		this.life++;
		if(this.state=="move"){
			if(this.anim_c>10) this.anim_c=1;
			this.x+=this.dx;
			this.y+=this.dy;
			this.img.style.top=this.y+"px";
			this.img.style.left=this.x+"px";
			this.img.src="/war/gobbo/img/bolt"+this.anim_c+".png";
			if((this.life>4 && (Math.random()*20<1)) || this.y>goblin.maxboom_y || this.x<0 || this.x>goblin.maxsquig){			
				this.state="boom";
				this.anim_c=0;
			}
		}else if(this.state=="boom"){
			if(this.anim_c>3){		
				if(Math.random()*20<1){					
					this.squig=1;
					this.state="mushroom";
					this.anim_c=0;
				}else{
					this.img.src="/war/gobbo/img/burn"+Math.floor(1+Math.random()*3)+".png";
					this.boom=1;
				}				
			}else{
				this.img.src="/war/gobbo/img/boltboom"+this.anim_c+".png";
			}
		}else if(this.state=="mushroom"){		
			if(this.anim_c>5){
				this.state="squig";
				this.dx=-2;
				if(Math.random()*2<1){
					 this.state="squigr";
					 this.dx=2;
				}
				this.anim_c=0;				
				
				this.chomp=document.createElement("div");
				this.chomp.style.position="absolute";
				this.chomp.style.top=this.y+15+"px";
				this.chomp.style.left=this.x+15+"px";
				if(this.state=="squig"){
					this.chomp.style.background="url('/war/gobbo/img/squig_chewed.png') top right repeat-x;";
				}else{
					this.chomp.style.background="url('/war/gobbo/img/squig_chewed.png') top left repeat-x;";
				}
				this.chomp.style.height="20px";
				this.chomp.style.display="block";
				this.chomp.style.width="0px";
				this.chompx=this.x+15;
				$(this.chomp).insertBefore(this.img);
				this.chompwidth=0;
			}else{
				this.img.src="/war/gobbo/img/mushroom"+this.anim_c+".png";
			}
		}else if(this.state=="squig"){
			if(this.anim_c>4){
				this.anim_c=1;
			}
			this.x=this.x+this.dx;
			this.img.style.left=this.x+"px";
			if(this.x<-40){
				this.boom=1;
				this.cleanup();
			}
			this.chompx=this.chompx+this.dx;
			this.chompwidth+=Math.abs(this.dx);
			this.chomp.style.width=this.chompwidth+"px";			
			this.chomp.style.left=this.chompx+"px";
			this.img.src="/war/gobbo/img/squig"+this.anim_c+".png";
		}else if(this.state=="squigr"){
			if(this.anim_c>4){
				this.anim_c=1;
			}
			this.x=this.x+this.dx;
			this.img.style.left=this.x+"px";
			if(this.x>goblin.maxsquig){
				this.boom=1;
				this.cleanup();
			}			
			this.chompwidth+=Math.abs(this.dx);
			this.chomp.style.width=this.chompwidth+"px";			
			this.chomp.style.left=this.chompx+"px";
			this.img.src="/war/gobbo/img/squigr"+this.anim_c+".png";
		}
	},
	dummy: 0
};

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages("/war/gobbo/img/door1.png","/war/gobbo/img/door2.png","/war/gobbo/img/door3.png","/war/gobbo/img/door4.png","/war/gobbo/img/opendoor.gif",
				"/war/gobbo/img/smallgoblin1.png","/war/gobbo/img/smallgoblin2.png","/war/gobbo/img/smallgoblin3.png","/war/gobbo/img/smallgoblin4.png",
				"/war/gobbo/img/mushroom1.png","/war/gobbo/img/mushroom2.png","/war/gobbo/img/mushroom3.png","/war/gobbo/img/mushroom4.png","/war/gobbo/img/mushroom5.png",
				"/war/gobbo/img/squig1.png","/war/gobbo/img/squig2.png","/war/gobbo/img/squig_chewed.png","/war/gobbo/img/squigr1.png","/war/gobbo/img/squigr2.png",
				"/war/gobbo/img/squig3.png","/war/gobbo/img/squig4.png","/war/gobbo/img/squigr3.png","/war/gobbo/img/squigr4.png",
				"/war/gobbo/img/goblin1.png","/war/gobbo/img/goblin2.png","/war/gobbo/img/bolt1.png","/war/gobbo/img/bolt2.png","/war/gobbo/img/boltboom1.png","/war/gobbo/img/boltboom2.png",
				"/war/gobbo/img/boltboom3.png","/war/gobbo/img/burn1.png","/war/gobbo/img/burn2.png","/war/gobbo/img/burn3.png");

$(document).ready(function(){
	if(!window.browserIe6){
		setTimeout("goblin.init()",30000+Math.random()*60000);
		//goblin.init();
	}
	$('#doorlink').click(function(){ if(window.paused) window.paused=0; goblin.init(); return false;});
});

$(window).blur(function(){ window.goblin.pause();});
$(window).focus(function(){ window.goblin.unpause();});