/*
	Character Viewer Class
	Requires Prototype 1.6
*/
var CharacterViewerClass = Class.create({
	initialize: function(characterinfo) {
		this.current = 0;
		this.next = 0;
		this.total = 0;
		this.oldclass = '';
		this.characterinfo = characterinfo;
		
		//update viewer on first load
		this.firstLoad();
		
		var parent = this;
		//set events
		$('cvNavUp').observe("click", function() {
			parent.setNextCharacter('up');		
		});
		$('cvNavDown').observe("click", function() {
			parent.setNextCharacter('down');		
		});
	},
	
	firstLoad: function() {
		this.total = (this.characterinfo.length)-1;
		this.updateCharacterViewer();
	},
	
	updateCharacterViewer: function() {
		$('characterViewer').removeClassName(this.oldclass);
		this.oldclass = 'character-'+(this.next+1);
		$('characterViewer').addClassName(this.oldclass);
		$('cvHeader').innerHTML = "Meet "+unescape(this.characterinfo[this.next].name);
		$('cvText').innerHTML = unescape(this.characterinfo[this.next].text);
		this.current = this.next;	
	},
	
	setNextCharacter: function(direction) {
		if(direction=='up') {
			if(this.current==0) this.next = this.total;
			else this.next = this.current - 1;
		} else {
			if(this.current==this.total) this.next = 0;
			else this.next = this.current + 1;
		}
		this.updateCharacterViewer();	
	},
	
	manualSwitch: function(index) {
		if(index<(this.total+1)) {
			this.next = index;
			this.updateCharacterViewer();
		}
		
		//useless eyecandy
		var pageAnchor = $('characterViewer');
		var delta = pageAnchor.offsetTop-document.viewport.getScrollOffsets().top+pageAnchor.getHeight()+50;
		new Effect.Scroll(window,{y:delta,duration:0.25});	
	}
});

/* --------------------------------------------------- */

var CharacterViewer = new CharacterViewerClass(characterinfo);