var Stream = Class.create({
	file: '',
	getParams: function(){},
	callback: function(){},
	timer: null,
	allowStream: true,
	timeing: 200,
	waiting: 500,
	xhr: null,
	active: true,
	lastChar: 0,

  initialize: function(file, params, callback, options) {
    this.file  = file;
    this.getParams = (typeof(params) == 'function') ? params : function() { return params };
    this.params = params;
    this.callback = callback;
		Object.extend(this,options||{});
		this.startRequest();
  },
  stop: function() {
		this.active = false;
		window.clearTimeout(this.active);
	},
  startRequest: function() {
    this.active = true;
    this.lastChar = 0;
		if (this.allowStream) {
		  this.startStreamRequest();
		  if (typeof(this.xhr.transport.responseText) != 'string') {
        this.allowStream = false;
        this.startNormalRequest();
			}
		} else this.startNormalRequest();
	},
	startStreamRequest: function() {
		this.xhr = new Ajax.Request(this.file, {
			method:'post',
			parameters: Object.extend({stream:1},this.getParams()||{}),
			onComplete: function(){
			  if (this.allowStream && this.active) {
					window.clearTimeout(this.timer);
				  this.readStream();
			  	this.startRequest();
				}
			}.bind(this)
		});
		this.timer = window.setTimeout(this.readStream.bind(this), this.timeing);
	},
	startNormalRequest: function() {
		this.xhr = new Ajax.Request(this.file, {
			method:'post',
			parameters: Object.extend({stream:0},this.getParams()||{}),
			onComplete: function(t){
			  if (this.active) {
			    var txt = t.transport.responseText;
			    if (txt.length > 0)
			    	this.callback(txt);
					this.timer = window.setTimeout(this.startNormalRequest.bind(this), this.waiting);
				}
			}.bind(this)
		});
	},
	readStream: function() {
	  window.clearTimeout(this.active);
	  if (this.allowStream) {
	    txt = this.xhr.transport.responseText;

			var ln = txt.length;
			var i = ln -1;
			if (ln && i != this.lastChar) {
				if (this.lastChar > i)
				  this.lastChar = 0;

				this.callback(txt.substring(this.lastChar, i));
				this.lastChar = i;
			}

			if (this.active && this.allowStream) {
	      this.timer = window.setTimeout(this.readStream.bind(this), this.timeing);
			}
		}
	}
});


function getIsins() {
	var o = {};
  isins.each(function(isin){
		o[isin] = {};
		o[isin].isin = isin;
		o[isin].bid = $('isin_bid_'+isin).innerHTML.replace(/,/,'.');
		o[isin].ask = $('isin_ask_'+isin).innerHTML.replace(/,/,'.');
		o[isin].timestamp = $('isin_timestamp_'+isin).innerHTML;
	});
	return {isin:Object.toJSON(o)};
}

function setIsins(raw) {
	var lines = raw.split("\n");
	for (var i=0, ln = lines.length; i<ln; i++) {
	  if (lines[i].length < 2) continue;
	  var data = lines[i].split("_");
	  var isin = data[0];
	  
	  var bid = data[1].split('|');
	  var bid_elem = $('isin_bid_'+isin);
	  bid_elem.innerHTML = bid[0].replace(/\./,',');
	  bid_elem = $('isin_bid_color_'+isin);
		if (bid[1] != 'x') {
			if (color_timers[isin+'_bid'])
			  window.clearTimeout(color_timers[isin+'_bid']);
	    color_timers[isin+'_bid'] = window.setTimeout(function(){bid_elem.setStyle({backgroundColor:'transparent'})}, 1500);
			var col = (bid[1]=='-')?'#f66':'#6f6';
	    bid_elem.setStyle({backgroundColor:col});
		}
    
    var ask = data[2].split('|');
	  var ask_elem = $('isin_ask_'+isin);
	  ask_elem.innerHTML = ask[0].replace(/\./,',');
	  ask_elem = $('isin_ask_color_'+isin);
	  if (ask[1] != 'x') {
			if (color_timers[isin+'_ask'])
			  window.clearTimeout(color_timers[isin+'_ask']);
	    color_timers[isin+'_ask'] = window.setTimeout(function(){ask_elem.setStyle({backgroundColor:'transparent'})}, 1500);
			var col = (ask[1]=='-')?'#f66':'#6f6';
	    ask_elem.setStyle({backgroundColor:col});
		}

	  $('isin_date_'+isin).innerHTML = data[3];
	  $('isin_time_'+isin).innerHTML = data[4];
	  $('isin_timestamp_'+isin).innerHTML = data[5];
	}
}

var color_timers = {};
var isins;
Event.observe(window,'load',function(){
	isins = $$('.isin').collect(function(elem){return elem.innerHTML});
	new Stream('/_datenintegration/ls_gateway.php',getIsins,setIsins);
});


