var Counter = Class.create();

Counter.prototype = Object.extend(new AttachInnerHTML(), {
	initialize: function(fish, year) {
		this.fish = fish;
		this.year = year;
		this.fishArray = new Array();
		this.index = 0;
		this.loadCounter();
	},

	loadCounter: function() {
		new Ajax.Request(
			"analysis/fishsum.cgi", {
				method: 'get',
				requestHeaders: ['If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT'],
				onSuccess: this.setXML.bind(this),
				onFailure : function(error) {
					alert(error.responseText);
				}
			});
	},

	next: function() {
		this.index = (this.index + 1) % this.fishArray.length;
		this.fish = this.fishArray[this.index];
		this.attachCounter();
	},

	setXML: function(xml) {
		this.xml = xml.responseXML.documentElement;
		this.attachCounter();
	},

	attachCounter: function() {
		var total = getNode('total', this.xml);
		var text = "0";
		var cm = "--";

		if	(total.length != 0) {
			var attr = getAttributes(total[0]);
			if	(attr['-year'] == this.year) {
				var fish = getNode('fish', total[0]);
				for	(var cnt = 0; cnt < fish.length; cnt++) {
					var attr = getAttributes(fish[cnt]);
					this.fishArray.push(attr['-name']);
					if	(attr['-name'] == this.fish) {
						text = attr['-count'];
						cm = attr['-max'];
						this.index = cnt;
					}
				}
			}
		}
		text += '&nbsp;匹';
		if	(cm != undefined) {
			text += '<span class="max">（max:' + cm + 'cm）</span>';
		}
		this.attachInnerHTML('counter',  text);
		this.attachInnerHTML('fishname', this.fish);
	}
});

