HW.ContentRotator = function(o,c,opts) {
	if(!o||!c) {return;}
	this.container = o;
	this.contents = $$(c,o,'div');
	this.opts = HW.extendObject(this.opts,opts);
	this.init();
}

HW.ContentRotator.prototype = {
	current:0,
	able:true,
	buffer:[],
	opts:{
		speed:500,
		transition:'slide',
		interval:null
	},
	init:function() {
		var obj = this;
		HW.setStyle(this.container,{position:'absolute',top:0,left:0});
		if(this.opts.transition == 'slide') {
			this.positions = this.contents.each(function(a){return -a.offsetTop;});
		}
		else if(this.opts.transition == 'fade') {
			this.contents.each(function(a){HW.setStyle(a,{position:'absolute',top:0,left:0,display:'none'});HW.setFade(a,0);});
			HW.setFade(this.contents[this.current],100);
			HW.setStyle(this.contents[this.current],{display:''});
			this.move(0);
		}
		this.startInterval();
	},
	move:function(n) {
		var obj = this;
		var i = (n+this.contents.length)%this.contents.length;
		if(i == n && this.current != n) {
			if(this.able) {
				this.able = false;
				if(this.opts.transition == 'slide') {
					new HW.Animator(this.container,this.positions[this.current],this.positions[n],function(o,v){HW.setStyle(o,{top:v+'px'});},this.opts.speed,function(){obj.moved(n);});
				}
				else if(this.opts.transition == 'fade') {
					var m = this.current;
					HW.setStyle(obj.contents[n],{display:''});
					new HW.Animator(this.container,100,0,function(o,v){HW.setFade(obj.contents[m],v);HW.setFade(obj.contents[n],100-v);},this.opts.speed,function(){HW.setStyle(obj.contents[m],{display:'none'});obj.moved(n);});
				}
				// slightly hacky stuff for indicator - specific to RBS people
				if(this.indicator) {
					new HW.Animator(this.indicator,this.indicatorPositions[this.current],this.indicatorPositions[n],function(o,v){HW.setStyle(o,{top:v+'px'});},this.opts.speed);
				}
				// end slightly hacky stuff
				this.current = n;
			}
			else {
				this.buffer.push(n);
			}
		}
	},
	moved:function(n) {
		this.able = true;
		if(this.buffer.length) {
			this.move(this.buffer.shift());
		}
	},
	control:function(a,o) {
		var obj = this;
		if(o.direction && a) {
			HW.attachEvent(a,'click',function(e){HW.preventDefault(e);obj.move(obj.current + o.direction);obj.startInterval();});
		}
		else if(a) {
			HW.attachEvent(a,'click',function(e){HW.preventDefault(e);obj.move(o);obj.startInterval();});
		}
	},
	controlSet:function(a) {
		for(var i=0,j=a.length;i<j;i++) {
			this.control(a[i],i);
		}
		
		// more slightly hacky stuff to do the indicator - specific to RBS people
		this.indicatorPositions = [];
		for(var i=0,j=a.length;i<j;i++) {
			var _left = 0;
			var _top = 0;
			var o = a[i];
			while(o && o.id != 'controls') {
				_top += o.offsetTop;
				_left += o.offsetLeft;
				o = o.offsetParent;
			}
			this.indicatorPositions.push(_top);
		}
		this.indicator = HW.createNode('img',$('controls'),'',{src:'/portal/page/portal/ICG-Lanarkshire2011/IMAGE_DIRECTORY/contentStyle02ul-selected.gif'});
		HW.setStyle(this.indicator,{position:'absolute',left:_left+'px',top:this.indicatorPositions[this.current] + 'px'});
		// end slightly hacky stuff
	},
	startInterval:function() {
		if(this.opts.interval) {
			var obj = this;
			this.stopInterval();
			this.interval = setInterval(function(){obj.move((obj.current+1)%obj.contents.length);},this.opts.interval);
		}
		else {
			this.startInterval = function(){};
		}
	},
	stopInterval:function() {
		if(this.opts.interval) {
			clearInterval(this.interval);
		}
		else {
			this.stopInterval = function(){};
		}
	}
}
