homerotate = {
	init: function(){
		this.activeColor	= "#fff";	// link color when hovered
		this.selectedColor = "#ddd";	// link color when is selected industry
		this.inactiveColor = "#999";	// link color normally
		this.fxDur = 750;				// how long transitions last
		this.rotateDelay = 9000;		// how long between rotation
		
		// you shouldn't need to modify anything below here
		// gives a value to delay and rotation vars, and set some other defaults
		this.rotID = this.fadeoutId = this.loadNextId = 0;
		this.loaded = 0;
		this.loading = 1;
		this.inCop		= new Array();
		this.inPic		= new Array();
		
		// remove the current div tags
		$('industrycopy').destroy();
		$('industrypic').destroy();
		// this is the top level
		this.container = $('industryblock');
		// load all of the data for the rotation into an array
		this.indCont = new Array();
		$('industysets').getElements('div').each(function(div){
			t ={};
			h = div.getElement('heading2');
			p = div.getElement('paragraph');
			a = div.getElement('anchor');
			i = div.getElement('images');
			t.h=h.getProperties('href','content');
			t.p=p.getProperties('content');
			t.a=a.getProperties('href','content');
			t.i=i.getProperties('href','content','source');
			this.indCont.push(t);
		},homerotate);
		// get the list of links, then add click & effect
		this.industryList = $('industrylist').getElements('a');
		this.industryList.each(function(a,i){
			a.addEvent('click',function(){return homerotate.clickIndustry(i);});
			a.effect = new Fx.Morph(a, {duration: this.fxDur, transition: Fx.Transitions.Sine.easeIn});
		},homerotate);
		// get a ramdom one to start with, and set the next in the list
		this.myNum = this.getRandom(this.industryList.length);
		this.myNext = ((this.myNum+1)==this.industryList.length) ? 0 : this.myNum+1;
		// create 2 sets of divs for us to switch back and forth between
		this.createSet(0);
		this.createSet(1);
		// load the ramdomly selected data into set 0, and the next into set 1
		this.loadSet(0,this.myNum);
		this.loadSet(1,this.myNext);
		// fade in set 0 and start the rotation
		this.fadeIn(0,this.myNum);		
		this.rotID = this.rotate.periodical(this.rotateDelay,homerotate);
	},
	
	rotate: function(){
		mySet = (this.loaded) ? 0 : 1;
		this.fadeOut(this.loaded,this.myNum);
		this.fadeIn(mySet,this.myNext);
		this.loading = this.loaded;
		this.loaded = mySet;
		this.myNum = this.myNext;
		this.myNext = ((this.myNum+1)==this.industryList.length) ? 0 : this.myNum+1;
		this.loadNextId = this.loadSet.delay(this.fxDur, homerotate,[this.loading,this.myNext]);
	},
	
	clickIndustry: function(num){
		if(this.rotID) this.stopEverything();
		mySet = (this.loaded) ? 0 : 1;
		this.loadSet(mySet,num);
		this.fadeOut(this.loaded,this.myNum);
		this.fadeIn(mySet,num,this.activeColor);
		this.loading = this.loaded;
		this.loaded = mySet;
		this.myNum = num;
		return false;
	},
	
	createSet: function(set){
		this.inCop[set]		= {};
		this.inCop[set].top	= new Element('div', {'class':'industrycopy'}).injectInside(this.container);
		this.inCop[set].h2		= new Element('h2').injectInside(this.inCop[set].top);
		this.inCop[set].h2a	= new Element('a').injectInside(this.inCop[set].h2);
		this.inCop[set].p		= new Element('p').injectInside(this.inCop[set].top);
		//this.inCop[set].p2		= new Element('p').injectInside(this.inCop[set].top);
		//this.inCop[set].p2a	= new Element('a').injectInside(this.inCop[set].p);
		this.inPic[set]		= {};
		this.inPic[set].top	= new Element('div',{'class':'industrypic'}).injectInside(this.container);
		this.inPic[set].a		= new Element('a').injectInside(this.inPic[set].top);
		this.inPic[set].i		= new Element('img',{'width':'330','height':'216'}).injectInside(this.inPic[set].a);
		this.inCop[set].top.effect = new Fx.Morph(this.inCop[set].top, {'duration': this.fxDur, 'transition': Fx.Transitions.Sine.easeIn});
		this.inPic[set].top.effect = new Fx.Morph(this.inPic[set].top, {'duration': this.fxDur, 'transition': Fx.Transitions.Sine.easeIn});
	},
	
	loadSet: function(set,data){
		this.inCop[set].h2a.setProperties({'href':this.indCont[data].h.href,'html':this.indCont[data].h.content});
		this.inCop[set].p.setProperties({'html':this.indCont[data].p.content});
		this.inCop[set].pa = new Element('a',{'href':this.indCont[data].a.href,'html':this.indCont[data].a.content}).inject(this.inCop[set].p,'bottom');
		//this.inCop[set].p2a.setProperties({'href':this.indCont[data].a.href,'html':this.indCont[data].a.content});
		this.inPic[set].a.setProperties({'href':this.indCont[data].i.href});
		this.inPic[set].i.setProperties({'src':this.indCont[data].i.source,'title':this.indCont[data].i.content,'alt':this.indCont[data].i.content});
	},
	
	fadeIn: function(s,n,c){
		c = (c) ? c : this.inactiveColor;
		this.industryList[n].effect.start({'color':[c,this.selectedColor]});		
		this.inCop[s].top.effect.start({'opacity':[0,1],'display':'block'});
		this.inPic[s].top.effect.start({'opacity':[0,1],'display':'block'});
	},
	
	fadeOut: function(s,n){
		this.industryList[n].effect.start({'color':[this.selectedColor,this.inactiveColor]});
		this.inCop[s].top.effect.start({'opacity':[1,0]});
		this.inPic[s].top.effect.start({'opacity':[1,0]});
		this.fadeoutId = this.hideSet.delay(this.fxDur, homerotate,[s,n]);
	},
	
	hideSet: function(s,n){
		this.inCop[s].top.style.display = this.inPic[s].top.style.display = 'none';
		this.clearColorID = this.clearColor.delay(this.fxDur, homerotate, n);
	},
	
	clearColor: function(n){
		this.industryList[n].style.color="";
	},
	
	stopEverything: function(){
		$clear(this.rotID);
		$clear(this.fadeoutId);
		$clear(this.loadNextId);
		this.inCop[0].top.effect.cancel();
		this.inPic[0].top.effect.cancel();	
		this.inCop[1].top.effect.cancel();
		this.inPic[1].top.effect.cancel();
		this.industryList.each(function(l){
			l.effect.cancel();
			l.effect.set({'color':this.inactiveColor});
		});
		this.rotID = this.fadeoutId = this.loadNextId = 0;
	},
	
	getRandom: function(num){
		myRan = Math.random();
		myRan = myRan * num;
		return parseInt(myRan);
	}
};

window.addEvent('domready', function(){
	homerotate.init();
});

