var RollContent = {
	currAnchor: -1,
	fullImage: null,
	textArea: null,
	timer: null,
	init: function(options){
		//added htmlWidth and htmlHeight which are the default sizes for html content blocks
		this.options = Object.extend({
			containerID: 'rollcontent',
			fullImageID: 'fullsize',
			textAreaID: 'textArea',
			navAreaID: 'rollnav',
			timerRate: 10000
		}, options || {});

		this.anchors = [];
		$$('#' + this.options.navAreaID + ' a').each( function(el) {  
			if (el.rel){
			    el.addEvent('mouseover', this.mouseover.pass(el, this));
			    el.addEvent('mouseout', this.mouseout.pass(el, this));

				el.onclick = this.click.pass(this.anchors.length, this);  
				//note: deletes any previous onclick event
				//		can't use el.addEvent('click', this.click.pass(el, this));
				//		as it does not prevent the default click action				
				this.anchors.push(el);
			}
		}, this);
		
		if (this.anchors.length > 0) { 
			this.fullImage = $(this.options.fullImageID);
			this.textArea = $(this.options.textAreaID);
			this.click.pass(0, this)();
		}
	},

	mouseover: function(link) {
		this.highlight.pass(link, this)();
	},
	
	mouseout: function(link) {		
		this.highlight.bind(this)();
	},
	
	
	click: function(index){	
		//stop any timer
		if (this.timer) { $clear(this.timer); }
		
		if (index >= 0 && index < this.anchors.length) {
			this.currAnchor = index;
			//highlight
			this.highlight.pass(this.anchors[this.currAnchor], this)();
			
			//setup content
			if (!this.textArea || !this.fullImage) {
				alert('RollContent is not setup correctly');
				return false;
			}
			var img = this.anchors[this.currAnchor].getElement('img');
			if (img) {
				this.fullImage.src = img.src.replace('.gif','_full.gif');
				this.fullImage.alt = img.src.replace('.gif','_full.gif');
			} else {
				alert('ERROR - cannot locate image!');
				return false;
			}
			var ctrl = $(this.anchors[this.currAnchor].rel);
			if (ctrl) {
				this.textArea.innerHTML = "<div class='innerArea'>" + ctrl.innerHTML + "</div>";
			} else {
				this.textArea.innerHTML = "<div class='innerArea'>Unable to locate content</div>";
				return false;
			}
			
			//setup the timer
			if (this.options.timerRate > 0) {
				var nextIndex = this.currAnchor + 1;
				if (nextIndex >= this.anchors.length) { nextIndex = 0; }
				this.timer = this.click.delay(this.options.timerRate, this, nextIndex);
			}
			
		}
		return false;
	},
	
	highlight: function(link) {
		var img = null;
		var el = null;
		
		//reset all anchors
		for (var i = 0; i < this.anchors.length; i++) {
			el = this.anchors[i];	//get a reference to the link
			img = el.getElement('img');  //get a reference to the image
			if (img) {
				if (i == this.currAnchor) {
					img.setStyle('border','solid 2px #FFF');	
				} else if (link && el.rel == link.rel){
					img.setStyle('border','solid 2px #CCC');	
				} else {
					img.setStyle('border','solid 2px #999');					
				}
			}
		}	
	}
	
};

window.addEvent('domready', RollContent.init.bind(RollContent));
