//colour transitions.js										 *
//based on mootools OO javascript framework*
//(c)sample oak records. morespecifically..*
//risch.																	 * 
//******************************************

function Colourobject(){
	
	this.killobj = function(){
		this.terminate = true;
	}
	
	this.setidstr = function(val){
		this.idstr  = val;
	}
	
	this.liveobj = function(){
		this.terminate = false;
	}
	
	this.settest = function(val){
		this.test = val;		
	}
	
	this.testcol = function(){
		soak.debugVar(this.test);
	}
	
	
	this.initialize	= function(){
		this.isGo = soak.options.isGo;
		this.isTrue = soak.options.isTrue;
		this.cc1start = soak.options.cc1start;
		this.cc2start = soak.options.cc2start;
		this.cc1end = soak.options.cc1end;
		this.cc2end = soak.options.cc2end;
		this.prime	= soak.options.prime;
		//soak.debugEvent(this.options.test);
	}
	this.primeCycle = function (calledfrom){
	 if(!this.prime){
	 	
	 	this.prime	= true;
		}else{
			this.prime = false;
		}
		soak.debugEvent('*event[:::: primeCycle ' + this.prime.toString());	
		soak.cccount	= 0;	
	}
	
	this.triggerCycle =	function(){
		soak.debugEvent('*event[:::: triggerCycle ' + this.prime.toString());
		if(this.prime){
			this.isGo=true;
			this.isTrue=true;
			
		}
	}
	
	this.stopCycle =	function	(){
		this.isGo=false;
		this.isTrue=false;
	}
	// The Oh-so-awesome colourCycle chains :D.	
		this.colourCycle = function(calledfrom){
		
		if(!this.terminate && !soak.override){	
		
			soak.debugFunction('colCycle1 - CO - ' + this.idstr,1)
			soak.debugEvent('*event[COLOUR OBJECT = ' + this.idstr +']*' );
			soak.debugEvent('*event[colCycle CALLED :::: call from - ' + calledfrom + ']*' );
			soak.debugEvent('*event[colCycle DEBUG  :::: cc2end = ' + this.cc2end + ' cc1start = ' +this.cc1start +']*' );
			soak.debugEvent('*event[colCycle DEBUG  :::: isTrue = ' + this.isTrue + ' isGo = ' +this.isGo +']*' );
			performTransition(soak.gotocolour);
			if(this.isGo && this.isTrue && this.cc2end && !this.cc1start && !this.terminate && !soak.override){
					this.cc1start = true;
					this.cc1end 	= false;
					soak.debugEvent('*event[colCycle START :::: ]*' );																																																																																																																																																				
					var colours  = soak.colours;
					
					var myChain = new Chain();
					if(this.idstr==1){
						colours.each(function(colour) {  myChain.chain( function(){ if(colman.currentobj==1){performTransition(colour);}else{soak.debugEvent('transition failed as object no longer active idstr= 1. colman.co = ' + colman.currentobj);} } ); });
					}else{
						colours.each(function(colour) {  myChain.chain( function(){ if(colman.currentobj==2){performTransition(colour);}else{soak.debugEvent('transition failed as object no longer active idstr= 2. colman.co = ' + colman.currentobj);} } ); });
					}
					var runChain = function() {
						myChain.callChain();
						if (myChain.chains.length == 0) { runChain = $clear(timer); }
					}
					var timer = runChain.periodical(soak.periodical);
				
				if(this.idstr==1){
					setTimeout("soak.debugEvent('*event[colCycle ENDED OK :::: ]*' );soak.debugFunction('colCycle1 - CO - ' + " + this.idstr +",0);colman.obj1.cc1end = true;colman.obj1.cc1start=false;if(colman.currentobj==" + this.idstr +"){colman.activeco.colourCycle2('colcycle1')}else{soak.debugEvent('COLCYCLE1 WAS TERMINATED AS OBJECT NO LONGER IN USE -> object =" +this.idstr+" CO - ' + colman.currentobj);}",soak.delay);	
				}else{
					setTimeout("soak.debugEvent('*event[colCycle ENDED OK :::: ]*' );soak.debugFunction('colCycle1 - CO - ' + " + this.idstr +",0);colman.obj2.cc1end = true;colman.obj2.cc1start=false;if(colman.currentobj==" + this.idstr +"){colman.activeco.colourCycle2('colcycle1')}else{soak.debugEvent('COLCYCLE1 WAS TERMINATED AS OBJECT NO LONGER IN USE -> object =" +this.idstr+" CO - ' + colman.currentobj);}",soak.delay);	
				}
				
			}else{
				if(!this.cc1start){
					this.cc1end = false;
					soak.debugFunction('colCycle1',0);
				}
				soak.debugEvent('*event[**ERROR**colCycle CO=' + this.idstr + ' ENDED EARLY :::: ]*' );	
				this.cc1start=false;
				soak.debugFunction('colCycle1  CO - ' + this.idstr,0)
			}
		}else{
			soak.debugEvent('colCycle1 - CO - ' + this.idstr + ' TERMINATED');
		}
	}
	
	this.colourCycle2 = function(calledfrom){
	if(!this.terminate && !soak.override){	
			soak.debugFunction('colCycle2 - CO - ' + this.idstr,1);
			soak.debugEvent('*event[COLOUR OBJECT = ' + this.idstr +']*' ); 
			soak.debugEvent('*event[colCycle2 CALLED 2:::: call from - ' + calledfrom + ']*' );
			soak.debugEvent('*event[colCycle2 DEBUG  :::: cc2end = ' + this.cc1end + ' cc1start = ' +this.cc2start +']*' );
			performTransition(soak.gotocolour);
			if(this.isGo && this.isTrue && this.cc1end && !this.cc2start && !this.terminate && !soak.override){
					this.cc2start = true;
					this.cc2end		= false;		
					soak.debugEvent('*event[colCycle2 START :::: ]*' );																																																																																																																																																		
					var colours  = soak.colours;
					var myChain = new Chain();
					if(this.idstr==1){
						colours.each(function(colour) {  myChain.chain( function(){ if(colman.currentobj==1){performTransition(colour);}else{soak.debugEvent('transition failed as object no longer active idstr= 1. colman.co = ' + colman.currentobj);this.cc2start=false;} } ); });
					}else{
						colours.each(function(colour) {  myChain.chain( function(){ if(colman.currentobj==2){performTransition(colour);}else{soak.debugEvent('transition failed as object no longer active idstr= 2. colman.co = ' + colman.currentobj);this.cc2start=false;} } ); });
					}
					var runChain = function() {
						myChain.callChain();
						if (myChain.chains.length == 0) { runChain = $clear(timer); }
					}
					var timer = runChain.periodical(soak.periodical);
					
					if(this.idstr==1){
						setTimeout("soak.debugEvent('*event[colCycle2 ENDED OK :::: ]*' );colman.obj1.cc2end = true;colman.obj1.cc2start=false;soak.debugFunction('colCycle2 - CO - ' + " + this.idstr +",0);if(colman.currentobj==" + this.idstr +"){colman.activeco.colourCycle('colCycle2')}else{soak.debugEvent('COLCYCLE2 WAS TERMINATED AS OBJECT NO LONGER IN USE -> object = " + this.idstr +" CO - ' + colman.currentobj);}",soak.delay);
					}else{
						setTimeout("soak.debugEvent('*event[colCycle2 ENDED OK :::: ]*' );colman.obj2.cc2end = true;colman.obj2.cc2start=false;soak.debugFunction('colCycle2 - CO - ' + " + this.idstr +",0);if(colman.currentobj==" + this.idstr +"){colman.activeco.colourCycle('colCycle2')}else{soak.debugEvent('COLCYCLE2 WAS TERMINATED AS OBJECT NO LONGER IN USE -> object = " + this.idstr +" CO - ' + colman.currentobj);}",soak.delay);
					}
				
			}else{
				if(!this.cc2start){
					this.cc2end = true;
				}
				soak.debugEvent('*event[**ERROR**colCycle2 ENDED EARLY :::: ]*' );
				soak.debugFunction('colCycle2 - CO - ' + this.idstr,0);
				this.cc2start = false;
			}
		}
	}
	
}

var Colourmanager = ({
	
	//call after setobj1+2
	init : function(){
		this.currentobj = 2;
		this.obj1 = new Colourobject();
		this.obj2 = new Colourobject();
		this.obj1.initialize();
		this.obj2.initialize();
		this.setACO();
		this.hasrun	= false;
		this.oldco = false;
	},
	

			
	setACO : function(){
		if(this.currentobj==1){
			this.currentobj = 2;
			this.activeco = this.obj2;
			this.oldco = this.obj1;
		}else{
			this.currentobj = 1;
			soak.debugEvent('SET TO CURRENTOBJ1');
			this.activeco = this.obj1;
			this.oldco = this.obj2;
		}
		//so important...
		this.activeco.setidstr(this.currentobj);
		soak.debugFunction('COLOUR OBJECT ' + this.currentobj,1);
		soak.debugEvent('HERE');
		if(this.hasrun){
			if(this.currentobj==1){
				soak.debugFunction('COLOUR OBJECT 2',0);
				//this.obj1.killobj();
				//this.obj2.liveobj();
			}else{
				soak.debugFunction('COLOUR OBJECT 1',0);
				//this.obj2.killobj();
				//this.obj1.liveobj();
			}
			
		}
		this.hasrun=true;
	},
});
