function Odds_moves(params) {
	var self = this;
	Changes.apply(this, [
		[
		new Omoves_ajax_handler({
			'get_avg_data' : function (data) {
				self._handle_data(data);
			}
		}),
		new Omoves_ajax_handler()
		]
	]);
	this._params = params;
}

extend(Odds_moves, Changes);

Odds_moves.prototype.update = function () {
	var ot = this._params['ot'] || null;
	var p = { 'ot' : ot,  'minmov' : 5, 'last' : this.get_last_update(), 'timeout' : 10  };
	this.get_async_srv().get_avg_data(p);
}

Odds_moves.prototype._update_data = function (data) {
	var newavgdata = data;
	this._update_missing_pevts(Util.hash_keys(newavgdata));
	
	var avgdata = this.get_data();
	for (peid in newavgdata) {
		for (ot in newavgdata[peid]) {
			var nd = newavgdata[peid][ot];
			if (!avgdata[peid]) {
				avgdata[peid] = {};
			}
			avgdata[peid][ot] = nd;
		}
	}
}


function Odds_moves_screen(omoves, opts, edata) {
	var self = this;
	var defaults = { 'sflags' : 0, 'minn' : 0, 'max_rows' : 400 };
	$.extend(defaults, opts);
	Peid_ot_changes_screen.apply(this, [ omoves, defaults ]);

	this._books = edata['books'];
	this._pevts = omoves.get_pevts();
	this._avgdata = omoves.get_data();
	this._oamounts = edata['oamounts'];
	
	this._updqueue = [];	
	setInterval(function () { self._process_upd_queue() }, 250);
		
	this._sortord = [ 3, 0 ];
	this._ttdata = {};
	this._tts = new Tooltips(this._ttdata, { 
		'create_tt' : function (pos, data) {
			var t = self._get_peid_ot(data);
			return Util.create_tooltip(pos, self._create_tooltip_text(t[0], t[1]));
		}
	});
	this._ttids = {};
}

extend(Odds_moves_screen, Peid_ot_changes_screen);

Odds_moves_screen.prototype.attach_table = function (table) {
	this._oct = table;
	this._create_oct_event_handlers();

	// alustetaan tooltip id:t	
	var ttids = {};
	for (i=2; i < this._oct.rows.length; ++i) {
		var id = this._oct.rows[i].id;
		ttids[id] = id;
	}
	this._ttids = ttids;
	this._tts.set_data(this._ttids);
}

Odds_moves_screen.prototype.create = function () {
	var oct = document.createElement("table");
	this._oct = oct;
	oct.className = "omvt";
	cont.appendChild(oct);

	var r = oct.insertRow(0);
	var c;
	c = r.insertCell(r.cells.length);
	c = r.insertCell(r.cells.length);
	c = r.insertCell(r.cells.length);
	c.colSpan = 5;
	c.className = "omvth1";
	c.innerHTML = "1";
	c = r.insertCell(r.cells.length);
	c.colSpan = 5;
	c.className = "omvthx";
	c.innerHTML = "X";
	c = r.insertCell(r.cells.length);
	c.colSpan = 5;
	c.className = "omvth2";
	c.innerHTML = "2";

	r = oct.insertRow(1);
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "date";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "match";

	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "n";
	c.title = "total number changes";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "up";
	c.title = "changes up";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "dn";
	c.title = "changes down";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "ioavg";
	c.title = "initial odds average";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "coavg";
	c.title = "current odds average";
	
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "n";
	c.title = "total number changes";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "up";
	c.title = "changes up";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "dn";
	c.title = "changes down";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "ioavg";
	c.title = "initial odds average";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "coavg";
	c.title = "current odds average";
	
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "n";
	c.title = "total number changes";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "up";
	c.title = "changes up";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "dn";
	c.title = "changes down";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "ioavg";
	c.title = "initial odds average";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "coavg";
	c.title = "current odds average";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "oci";
	c.title = "odds change index: aggregate changes vs. starting odds";
	c = r.insertCell(r.cells.length);
	c.className = "omvtht";
	c.innerHTML = "bfamnt";
	c.title = "Betfair matched amount";
	this._create_oct_event_handlers();
}

Odds_moves_screen.prototype._create_oct_event_handlers = function () {
	var self = this;
	var r = this._oct.rows[1];
	r.cells[2].onclick = function () { self._sortord = [ 3, 0]; self.refresh(); }
	r.cells[3].onclick = function () { self._sortord = [ 3, 1]; self.refresh(); }
	r.cells[4].onclick = function () { self._sortord = [ 3, 2]; self.refresh(); }
	r.cells[5].onclick = function () { self._sortord = [ 2, 0]; self.refresh(); }
	r.cells[6].onclick = function () { self._sortord = [ 1, 0]; self.refresh(); }
	r.cells[7].onclick = function () { self._sortord = [ 3, 3]; self.refresh(); }
	r.cells[8].onclick = function () { self._sortord = [ 3, 4]; self.refresh(); }
	r.cells[9].onclick = function () { self._sortord = [ 3, 5]; self.refresh(); }
	r.cells[10].onclick = function () { self._sortord = [ 2, 1]; self.refresh(); }
	r.cells[11].onclick = function () { self._sortord = [ 1, 1]; self.refresh(); }
	r.cells[12].onclick = function () { self._sortord = [ 3, 6]; self.refresh(); }
	r.cells[13].onclick = function () { self._sortord = [ 3, 7]; self.refresh(); }
	r.cells[14].onclick = function () { self._sortord = [ 3, 8]; self.refresh(); }
	r.cells[15].onclick = function () { self._sortord = [ 2, 2]; self.refresh(); }
	r.cells[16].onclick = function () { self._sortord = [ 1, 2]; self.refresh(); }
	r.cells[17].onclick = function () { self._sortord = [ 6, 1]; self.refresh(); }
	r.cells[18].onclick = function () { self._sortord = [ -1 ]; self.refresh(); }
}

Odds_moves_screen.prototype.refresh = function () {
	var oct = this._oct;	
	while (oct.rows.length > 2)
		oct.deleteRow(oct.rows.length-1);
	this._updqueue = [];
	this.update(this._avgdata);
}
	
Odds_moves_screen.prototype._update_row = function (peid, ot, nd) {
	var opts = this.get_opts();
	var max_rows = opts.max_rows;
	var id = this._get_row_id(peid, ot);
	var oct = this._oct;	

	var i;	
	for (i=2; i < oct.rows.length; ++i) {
		if (oct.rows[i].id == id) {
			oct.deleteRow(i);
			break;
		}
	}
	
	var sflags = opts.sflags;
	var minn = opts.minn;
	var inrunnings = sflags & 1;
			
	var oamount = this._oamounts[peid] != undefined && this._oamounts[peid][ot] != undefined ? this._oamounts[peid][ot] : 0;

	do {	
		var pe = this._pevts[peid];
		if (!pe) continue;
		var petime = pe[0];

		// XXX server time?
		var inrunning = new Date().getTime() > petime*1000;
		if (!inrunnings && inrunning) {
			continue;
		}

		var so = this._sortord;
		if (so[0] != -1) {
			for (i=2; i < oct.rows.length && i < max_rows; ++i) {
				var omov = this._get_row_data(oct.rows[i].id);
				if (parseInt(nd[so[0]][so[1]]) > parseInt(omov[so[0]][so[1]])) {
					break;
				}
			}
		} else if (so[0] == -1) {
			for (i=2; i < oct.rows.length && i < max_rows; ++i) {
				var rd = $(oct.rows[i])[0];
				var oamount2 = parseInt(rd.cells[18].innerHTML);
				if (oamount > oamount2) {
					break;
				}
			}
		}
		
		if (i < max_rows) {
			if (minn && nd[3][0] < minn) break;
			this._create_row(i, id, peid, ot, nd);
			if (oct.rows.length > max_rows) {
				oct.deleteRow(oct.rows.length-1);
			}
		}
	} while (0);
}

Odds_moves_screen.prototype._create_row = function (i, id, peid, ot, omov) {
	var r = this._oct.insertRow(i);
	r.id = id;

	var c;
	var pevt = this._pevts[peid];
	c = r.insertCell(r.cells.length);
	c.className = "omvtdate";
	c.innerHTML = Util.date_str2(new Date(pevt[0]*1000));
	c = r.insertCell(r.cells.length);
	c.className = "omvtteams";
	c.innerHTML = "<a href=\"/cgi-bin/kvtest?type=ochtml&peid=" + peid + "&topbar=0\" target=\"_blank\">" + pevt[4] + "-" + pevt[5] + "</a>";
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][0];
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][1];
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][2];
	c = r.insertCell(r.cells.length);
	c.innerHTML = Oc_util.odd_str(omov[2][0], 0);
	c = r.insertCell(r.cells.length);
	c.innerHTML = Oc_util.odd_str(omov[1][0], 0);
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][3];
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][4];
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][5];
	c = r.insertCell(r.cells.length);
	c.innerHTML = Oc_util.odd_str(omov[2][1], 0);
	c = r.insertCell(r.cells.length);
	c.innerHTML = Oc_util.odd_str(omov[1][1], 0);
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][6];
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][7];
	c = r.insertCell(r.cells.length);
	c.innerHTML = omov[3][8];
	c = r.insertCell(r.cells.length);
	c.innerHTML = Oc_util.odd_str(omov[2][2], 0);
	c = r.insertCell(r.cells.length);
	c.innerHTML = Oc_util.odd_str(omov[1][2], 0);
	c = r.insertCell(r.cells.length);
	c.innerHTML = (omov[6][1]/1000.0).toFixed(2);
	c = r.insertCell(r.cells.length);
	var oamount = this._oamounts[peid] != undefined && this._oamounts[peid][ot] != undefined ? this._oamounts[peid][ot] : 0;
	c.innerHTML = oamount;

	// uusi tooltip id	
	this._ttids[id] = id;
}

Odds_moves_screen.prototype.update = function (data) {
	for (var peid in data) {
		for (var ot in data[peid]) {
			this._updqueue.push([peid, ot]);
		}
	}
	var so = this._sortord;
	var oct = this._oct;
	var avgdata = this._avgdata;
	var oamounts = this._oamounts;
	this._updqueue.sort(function (a, b) {
		var peid1 = a[0];
		var ot1 = a[1];
		var peid2 = b[0];
		var ot2 = b[1];
		var aomov = avgdata[peid1][ot1];
		var bomov = avgdata[peid2][ot2];
		if (so[0] != -1) {
			return bomov[so[0]][so[1]] - aomov[so[0]][so[1]];
		} else if (so[0] == -1) {
			var a1 = oamounts[peid1] != undefined && oamounts[peid1][ot1] != undefined ? oamounts[peid1][ot1] : 0;
			var a2 = oamounts[peid2] != undefined && oamounts[peid2][ot2] != undefined ? oamounts[peid2][ot2] : 0;
			return a2 - a1;
		}
		return 0;
	});
}

Odds_moves_screen.prototype._process_upd_queue = function () {
	var len = this._updqueue.length;
	var max = 20;
	var peidots = this._updqueue.splice(0, max);
	this._do_update(peidots);
}

Odds_moves_screen.prototype._do_update = function (peidots) {
	for (var i=0; i < peidots.length; ++i) {
		var peidot = peidots[i];
		var peid = peidot[0];
		var ot = peidot[1];
		this._update_row(peid, ot, this._avgdata[peid][ot]);
	}
}
	
Odds_moves_screen.prototype._create_tooltip_text = function (peid, ot) {
	var pe = this._pevts[peid];
	var s = "<b>" + pe[4] + "-" + pe[5] + "</b><br>";
	s += "initial / current / book<br>\n";
	for (bid in this._avgdata[peid][ot][4]) {
		var oc = this._avgdata[peid][ot][4][bid];
		s += Oc_util.odd_str(oc[3], 0);
		s += " ";
		s += Oc_util.odd_str(oc[4], 0);
		s += " ";
		s += Oc_util.odd_str(oc[5], 0);
		s += " - ";
		s += Oc_util.odd_str_s(oc[0], -1, oc[3], 0);
		s += " ";
		s += Oc_util.odd_str_s(oc[1], -1, oc[4], 0);
		s += " ";
		s += Oc_util.odd_str_s(oc[2], -1, oc[5], 0);
		s += " ";
		s += this._books[bid][0];
		s += "<br>\n";
	}	

	return s;
}

