/* Copyright 2011 Clark Termite Control. All rights reserved. */

function animate(moveArray) { // object structure for animation sequence
	this.div_id = moveArray['divid'];
	this.ani_array = moveArray['ani'];
	this.last_move = this.ani_array.length-1;
	this.arryidx = 0; // start at first array point
	this.movex = this.ani_array[this.arryidx][0];
	this.movey = this.ani_array[this.arryidx][1];
	this.lastx = 0;
	this.lasty = 0;
	this.prefix = moveArray['prefix'];
	this.step_counter = 0;
	this.steps = 0;
	this.done = 0;
	var img_path = '';
	var x; var y;
	var deltax; var deltay;
	var count = 0;
	this.move_it = function(){
		if (!this.done) {
			this.step_counter -= 1;
			if (this.step_counter < 0) {
				if(this.arryidx <= this.last_move) {
					this.step_counter = this.ani_array[this.arryidx][2];
					this.steps = this.step_counter;
					this.lastx = this.movex;
					this.lasty = this.movey;
					this.movex = this.ani_array[this.arryidx][0];
					this.movey = this.ani_array[this.arryidx][1];
					img_path = basepath+'images/'+this.prefix + this.ani_array[this.arryidx][3]+'.gif';
					$(this.div_id).css('background-image','url("'+img_path+'")');
					this.arryidx += 1;
				}
				else
					this.done = 1;
			}
			else {
				count = this.steps - this.step_counter;
				deltax = this.movex - this.lastx;
				deltay = this.movey - this.lasty;
				x = this.lastx + parseInt((deltax/this.steps)*count);
				y = this.lasty + parseInt((deltay/this.steps)*count);
				$(this.div_id).css('left',x);
				$(this.div_id).css('top',y);
			}
			//$('#debug').html('count: '+this.step_counter+' idx: '+this.arryidx);
		}
	}
	this.reset = function(moveArray) {
		this.arryidx = -1; // initial value
		this.step_counter = -1;
		this.last_move = this.ani_array.length-1;
		this.arryidx = 0; // start at first array point
		this.movex = this.ani_array[this.arryidx][0];
		this.movey = this.ani_array[this.arryidx][1];
		this.lastx = 0;
		this.lasty = 0;
		this.steps = 0;
		this.done = 0;
	}
}

// ani: [targetx, targety, numsteps, image_seq_num]
var a = {'divid':'#rick','prefix':'rick/rick_',
	'ani':[	
[40,170,10,0],[40,170,80,1],[40,170,50,2],[40,170,60,3],[40,170,60,4],[40,170,60,5],[40,170,80,4],[40,170,80,3],[40,170,150,6],[40,170,80,7]
]};
var b = {'divid':'#termite','prefix':'termite/termite_',
	'ani':[	
[650,280,10,0],[610,280,10,0],[563,191,22,60],[537,129,17,60],[473,105,17,15],[449,169,17,285],[473,257,23,255],
[617,424,55,225],[633,481,15,255],[594,519,14,315],[474,495,31,15],[392,423,27,45],[330,425,16,0],[265,337,27,60],
[289,280,19,105],[435,280,37,180],[435,280,200,400],[600,280,54,401],[600,280,100,402]
]};
var c = {'divid':'#homehouse','prefix':'homehouse_',
	'ani':[	
[800,140,400,0],[798,140,3,1],[802,140,3,1],[802,140,3,1],[799,140,3,1],[798,140,3,1],[801,140,3,1],
[802,140,3,1],[800,140,3,1],[798,140,3,1],[801,140,3,1],[802,140,3,1],[799,140,3,1],[800,140,3,1],[798,140,3,1],
[800,140,3,1],[798,140,3,1],[802,140,3,1],[802,140,3,1],[799,140,3,1],[800,140,3,1],[798,140,3,1],[800,140,3,1],
[798,140,3,1],[802,140,3,1],[801,140,3,1],[802,140,3,1],[800,140,3,1],[798,140,3,1],[800,140,3,1],[799,140,3,1],
[800,140,3,1],[798,140,3,1],[802,140,3,1],[801,140,3,1],[802,140,3,1],[800,140,3,1],[798,140,3,1],[800,140,3,1],
[798,140,3,1],[800,140,3,1],[799,140,3,1],[802,140,3,1],[800,140,3,1],[801,140,3,1],[802,140,3,1],[799,140,3,1],
[800,140,3,1],[798,140,3,1],[800,140,3,1],[799,140,3,1],[802,140,3,1],[801,140,3,1],[799,140,3,1],[801,140,3,1],
[798,140,3,1],[800,140,3,1],[799,140,3,1],[800,140,3,0]
]};
var d = {'divid':'#boot','prefix':'boot/boot_',
	'ani':[	
[600,250,600,0],[600,260,10,1],[600,270,10,2],[600,280,10,3],[600,290,10,3],[600,290,200,4]
]};

var rick = new animate(a);
var termite = new animate(b);
var house = new animate(c);
var boot = new animate(d);
var t; // for timer

function doit() {
	if (!rick.done || !termite.done) {
		rick.move_it();
		termite.move_it();
		house.move_it();
		boot.move_it();
		t = setTimeout("doit();",10);
	}
	else $('#debug').html('done outer');
}

function preloadImg() {
	var s = '';
	var preamble = '<img src="' + basepath + 'images/';
	var postamble = '.gif" /><br />';
 	for (i=0;i<=7;i++) {
		s += preamble + 'rick/rick_'+i+postamble;
	}
	for (i=0;i<=4;i++) {
		s += preamble + 'boot/boot_'+i+postamble;
	}
	for (i=0;i<=345;i+=15) {
		s += preamble + 'termite/termite_'+i+postamble;
	}
	for (i=400;i<=402;i++) {
		s += preamble + 'termite/termite_'+i+postamble;
	}
	for (i=0;i<=1;i++) {
		s += preamble + 'homehouse_'+i+postamble;
	}
	$('#preload').html(s);
	return s;
}

$(document).ready(function(){
	$('#more1').click(function(){ $('#more1p').slideToggle(); });
	$('#more2').click(function(){ $('#more2p').slideToggle(); });
	$('#more3').click(function(){ $('#more3p').slideToggle(); });
	$('#more4').click(function(){ $('#more4p').slideToggle(); });
	$('#more5').click(function(){ $('#more5p').slideToggle(); });
	$('#termite').show();
	$('#clicktermite').show();
	$('#more1').show();
	$('#more1p').hide();
	$('#more2').show();
	$('#more2p').hide();
	$('#more3').show();
	$('#more3p').hide();
	$('#more4').show();
	$('#more4p').hide();
	$('#more5').show();
	$('#more5p').hide();
	
	$('#termite').click(function(){
		$('#clicktermite').html('play again');
		_gaq.push(['_trackEvent', 'termite', 'clicked']);
		t = setTimeout("doit();",100); // start the animation
	});
	$('#clicktermite').click(function(){
		$('#clicktermite').html('play again');
		_gaq.push(['_trackEvent', 'clicktermite', 'clicked']);
		rick.reset(a);
		termite.reset(b);
		house.reset(c);
		boot.reset(c);
		t = setTimeout("doit();",100); // start the animation
	});
	preloadImg();
	
});

