var BIGOIL = null;

/* 
Animation Class
*/
function Animation() {
	this.divname;
	this.div;
	this.position = new Array();
	this.rate = new Array();
	this.active = new Array();
	this.min_val = new Array();
	this.max_val = new Array();
}
/* 
Animation Class Method
Initialize animation
*/
Animation.prototype.init = function( settings ) {
		this.divname = settings[0];
		this.div = document.getElementById(settings[0]);
		this.rate["top"] = settings[1];
		this.rate["left"] = settings[2];
		this.position["top"] = settings[3];
		this.position["left"] = settings[4];
		this.min_val["top"] = settings[5];
		this.min_val["left"] = settings[6];
		this.max_val["top"] = settings[7];
		this.max_val["left"] = settings[8];
		this.active["top"] = settings[9];
		this.active["left"] = settings[10];
};
/* 
Animation Class Method
check each item if it should be moved and call the move function.
*/
Animation.prototype.move = function () {
//$("testDiv").innerHTML += this.divname + this.active["left"] + this.active["top"] + "move<br>"
	if (this.active["left"]) {
		this.moveItem("left");
	} 
	if (this.active["top"]) {
		this.moveItem("top");
	}	
}

/* 
Animation Class Method
move the item
*/
Animation.prototype.moveItem = function ( direction ) {

	var pos = this.position[direction];
	var rate = this.rate[direction];
	var maxV = this.max_val[direction];
	var minV = this.min_val[direction];
	pos += rate;
	if (pos >= maxV) {
		pos = maxV - 2;
		rate = -rate;
	} else if (pos <= minV) {
		pos = minV + 2;
		rate = -rate;
	}
	if (this.div != null ) {
		if (direction == "left")
			this.div.style.left = pos + "px";
		if (direction == "top")
			this.div.style.top = pos + "px";
	}
	this.position[direction] = pos;
	this.rate[direction] = rate;
}

/* 
ANIMATE Class Method
sets up and runs the animations;
*/
function Animate() {
	this.animationList = new Array();
	this.animationInterval = null;
	this.spillInterval = null;
	this.count = 0;
	this.currentSpill;
	this.currentTweet;
	this.adjusted = new Array();
	this.r = 10;
	this.g = 176;
	this.b = 198;
}
Animate.prototype.init = function(alist) {
	var animation;
	for (var a = 0; a < alist.length; a++) {
		animation = new Animation();
		animation.init( alist[a] )
		this.animationList[a] = animation;
	}
}
Animate.prototype.run = function() {
	for (var a = 0; a < this.animationList.length; a++) {
		this.animationList[a].move();
	}
	this.count++;
	
	if (this.count%4==0) {
		if (this.r > 0)	this.r--;
		if (this.g > 0)	this.g--;
		if (this.b > 0)	this.b--;
	}
	this.animationList[5].div.style.backgroundColor = "rgb(" + this.r + "," + this.g + "," + this.b + ")"
	if (this.count > 10000) {
		stopAnimation();
	}

}
Animate.prototype.stop = function() {
	clearInterval(this.animationInterval);
	clearTimeout(this.spillInterval);
	this.spillInterval = null;
	this.animationInterval = null;
}


function animate() {
	BIGOIL.run();
}

function stopAnimation() {
	if (BIGOIL != null && typeof BIGOIL != "undefined" ) BIGOIL.stop();
}

function init() { 


	/***************************************************************
	 *	file:	prototype.fix.js
	 *	title:	Prototype Fix
	 *
	 *	description:	This fixes
	 */
	Element.addMethods({
		show: function(element,override){
			if(!override || override == "undefined") override = "block";
		
			element = $(element);
	    	element.style.display = override;
	    	return element;
		},
	
		setOpacity: function(element,value){
			element = $(element);
		    element.style.opacity = (value < 0.00001) ? 0 : value;
		    return element;	
		}
	});

	//"div" , 	ratet,	ratel,	post,	posl,	mint,	minl,	maxt,	maxl 
	var AnimationList = { items: [ 
			["water1", 0, -1,	0, -41, 0, -93, 0, -7, false, true],
			["water2", 0,  4, 0, -45, 0, -97, 0, -3, false, true],
			["water3", 0, -2, 0, -46, 0, -98, 0, -2, false, true],
			["water4", 0,  1, 0, -47, 0, -99, 0, -1, false, true],
			["rig", -1,  0, 50, 0, 42, 0, 58, 0, true, false],
			["horizonwater", 1,  0, 250, 0, 242, 0, 258, 0,	true, false],
			["spill1", -1,  0, 370, 0, 362, 0, 362, 0, false, false],
			["spill5", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill3", -1,  0, 370, 0, 362, 0, 362, 0, false, false],
			["spill2", -1,  0, 370, 0, 362, 0, 962, 0, false, false],
			["spill4", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill7", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill8", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill9", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill10", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill14", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill12", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill11", -1,  0, 370, 0, 378, 0, 962, 0, false, false],
			["spill13", -1,  0, 370, 0, 378, 0, 962, 0, false, false]
		] }

	BIGOIL = new Animate();
	BIGOIL.init(AnimationList.items);
	if (BIGOIL.animationInterval == null) {
		BIGOIL.animationInterval = setInterval("animate()", 200);
		//  delay and then run spill cycle.
		getTwitterFeed();
	}

}


function getTwitterFeed() {
	new Ajax.Request('/paidforbybigoil/getTwitter.aspx', { 
		evalJSON: true,
		onSuccess: function(transport) {
		if (transport.responseText.indexOf("PAIDFOR") >= 0) {
			startCycle(transport.responseText);
		}
	  }
	});
}

function startCycle(responsetext) {
	responsetext = responsetext.replace("\n", " ");
	responsetext = responsetext.replace("\r", " ");
	eval(responsetext)
	BIGOIL.spillInterval = setTimeout("spillCycle()", 2000);
}

function spillCycle( ) {
	var startOil = 6;
	var totalOil = 10;

	//change next spill in cycle ()
	var currentTweet = BIGOIL.currentTweet;
	var currentSpill = BIGOIL.currentSpill;
	// hide current spill
	var currentDiv = null;
	var midSpill = 0;

	if (currentSpill != null) {
		currentDiv = document.getElementById(BIGOIL.animationList[currentSpill].divname);
		switch(currentSpill) {
			case 6:
			case 9:
				midSpill = 	11;
				break;
			case 8:
				midSpill = 	12;
				break;
			case 7:
			case 10:
				midSpill = 	13;
				break;
		}

		//if (typeof document.getElementById("testDiv").filters != "undefined") {
			//currentDiv.style.filter = "progid:DXImageTransform.Microsoft.Fade(Duration=3)"
			//document.getElementById(BIGOIL.animationList[currentSpill+8].divname).style.filter = "progid:DXImageTransform.Microsoft.Fade(Duration=2)"
			//document.getElementById(BIGOIL.animationList[midSpill].divname).style.filter = "progid:DXImageTransform.Microsoft.Fade(Duration=2)"
		//} else {
			Effect.Fade(currentDiv, {duration: 3.0});
			Effect.Fade(document.getElementById(BIGOIL.animationList[currentSpill+8].divname), {duration: 2.0});	
			Effect.Fade(document.getElementById(BIGOIL.animationList[midSpill].divname), {duration: 2.0});	
		//}		
	} else {
		currentTweet = 0;
		currentSpill = startOil-1;
	}
		
	// loop back to first spill display if necessary.
	currentSpill += 1
	if (currentSpill > totalOil) currentSpill = startOil;
	currentDiv = document.getElementById(BIGOIL.animationList[currentSpill].divname)
	
	// show new spill
	var tweet = window.PAIDFOR.tweets[currentTweet];
	var reName = /\@([\S]*)/g
	var reLink = /http:\/\/([\S]*)/gi
	var reHash = /(\#[\S]*)/g
	
	if (BIGOIL.adjusted[currentTweet] != true ) {
		tweet = tweet.replace(reLink, '<a href="http://$1">$1</a>')
		tweet = tweet.replace(reName, '@<a href="http://twitter.com/$1">$1</a>')
		tweet = tweet.replace(reHash, '<a href="http://twitter.com/#search?q=%23$1">$1</a>')
		BIGOIL.adjusted[currentTweet] = true;
		window.PAIDFOR.tweets[currentTweet] = tweet;
	}
	
	currentDiv.innerHTML = "<p>" + tweet + "</p>"

	switch(currentSpill) {
		case 6:
		case 9:
			midSpill = 	11;
			break;
		case 8:
			midSpill = 	12;
			break;
		case 7:
		case 10:
			midSpill = 	13;
			break;
	}

	//if (typeof document.getElementById("testDiv").filters != "undefined") {
		//currentDiv.style.filter = "progid:DXImageTransform.Microsoft.Fade(Duration=3)"
		//currentDiv.style.filter[0].run();
		//document.getElementById(BIGOIL.animationList[currentSpill+8].divname).style.filter = "progid:DXImageTransform.Microsoft.Fade(Duration=2)"
	//	document.getElementById(BIGOIL.animationList[midSpill].divname).style.filter = "progid:DXImageTransform.Microsoft.Fade(Duration=2)"
	//} else {
		Effect.Appear(currentDiv, {duration: 3.0});
		Effect.Appear(document.getElementById(BIGOIL.animationList[currentSpill+8].divname), {duration: 2.0});	
		Effect.Appear(document.getElementById(BIGOIL.animationList[midSpill].divname), {duration: 2.0});	
	//}
	currentTweet++;
	
	if (currentTweet == window.PAIDFOR.tweets.length-1 ) currentTweet = 0;
	
	BIGOIL.currentTweet = currentTweet;
	BIGOIL.currentSpill = currentSpill;
	// after 2 minute run through message, get latest twitter feed.
	BIGOIL.spillInterval = setTimeout("spillCycle()", 6000);
}



