function IRadar()
{
	return this;
}

IRadar.prototype =
{
	last_ts: 0,
	is_working: false,
	plane_callback: null,
	ready_callback: null,
	mode: 'raw',
	iframe: null,
	base_url: '',
	
	fix_domain: function ()
	{
		var tmp = null;
		try {
			tmp = (document.location + '').substr(7).split('/')[0].split('.');
			tmp = tmp[tmp.length - 2] + '.' + tmp[tmp.length - 1];
			document.domain = tmp;
		}catch(e){}
	},
	
	init: function(_mode, done_callback, line_callback, _base_url, _debug)
	{
		this.plane_callback = line_callback;
		this.ready_callback = done_callback;
		this.base_url = _base_url;
		this.mode = _mode;
		
		this.fix_domain();
		
		if( _mode == 'iframe' ) {

			if( !document.getElementById('iradar-f') ) {
		
				this.iframe = document.createElement('iframe');
				this.iframe.style.width  = '200px';
				this.iframe.style.height = '100px';
				this.iframe.style.position = 'fixed';
				this.iframe.style.right = '0px';
				this.iframe.style.bottom = '0px';
				this.iframe.id = 'iradar-f';
				this.iframe.name = 'iradar-f';
			
				if( typeof(_debug) != 'string' ) {
					this.iframe.style.display='none';
				}

		                var self = this;

	                        this.iframe.onload = function() {
                                	self.update_callback_iframe();
                        	}

				document.body.appendChild(this.iframe);

			} else {
				this.iframe = document.getElementById('iradar-f');
			}
			
			this.iframe.src = _base_url + '/index.html';
		}
	},
	
	update: function( min_alt, max_alt, top_left_lat, top_left_lon, bot_right_lat, bot_right_lon)
	{
		var self = this;
		var date = new Date();
		
		if( self.is_working ) {
			return false;
		}
		
		top_left_lat = parseInt(top_left_lat * 100000);
		top_left_lon = parseInt(top_left_lon * 100000);
		bot_right_lat = parseInt(bot_right_lat * 100000);
		bot_right_lon = parseInt(bot_right_lon * 100000);
		
		self.is_working = true;
		
		if( this.mode == 'raw' ) {
		
			BinaryAjax
			(
				this.base_url + '/data?mina=' + parseInt(min_alt) + '&maxa=' + parseInt(max_alt), 
				function (oHTTP) {
					self.update_callback(oHTTP);
				}
			);
		
		} else {
			this.iframe.src = this.base_url + '/data?mina=' + parseInt(min_alt) + '&maxa=' + parseInt(max_alt) + '&ts=' + date.getTime() + '&txt=1&html=1';
		}

		return true;
	},
	
	update_callback: function(oHTTP)
	{
		var bin = oHTTP.binaryResponse;
		var num_records = bin.getShortAt(0);

		if( this.plane_callback == null || this.ready_callback == null) {
			return false;
		}
		
		var plane  = [];
		var base_offset = 32;
		var latest_update = 0;
		var show_limit = num_records;
		
		last_update = bin.getLongAt(2, false);
		show_limit  = this.ready_callback(num_records, last_update);
		show_limit  = show_limit < num_records ? show_limit : num_records;
		
		/* 
		 1. Record 0x20 bytes
		 ID|HEX|ALT|LLAT|LLON|TRK|SPD|SQW|VSPD|CALL
		 
		 0x00: R-ID - 1byte
		 0x01: HEX  - 3byte
		 0x04: ALT  - 2byte unsigned short, alt + 5000
		 0x06: LLAT - 4byte float/long
		 0x0A: LLON - 4byte float/long
		 0x0E: TRK  - 2byte short
		 0x10: SPD  - 2byte short
		 0x12: SQW  - 2byte short
		 0x14: VSPD - 1byte (vspd / 64)
		 0x15: CALL - 8byte
		 0x1E: ---- - 2byte reserved
		 */
		
		for( var i=0; i<show_limit; i++, base_offset += 32)
		{
			plane = { 
				rdr_id: bin.getByteAt(base_offset),
				hex: ( bin.getLongAt(base_offset) >> 8).toString(16),
				alt: ( bin.getShortAt(base_offset + 0x04, false) - 5000 ),
				lat: ( bin.getSLongAt(base_offset + 0x06, false) / 100000.0),
				lon: ( bin.getSLongAt(base_offset + 0x0A, false) / 100000.0),
				trk: ( bin.getShortAt(base_offset + 0x0E, false) ),
				spd: ( bin.getShortAt(base_offset + 0x10, false) ),
				sqw: ( bin.getShortAt(base_offset + 0x12, false).toString(16) ),
				vspd:( bin.getSByteAt(base_offset + 0x14) * 64),
				call:( bin.getStringAt(base_offset + 0x15, 8 ) )
			};
			
			
			this.plane_callback(plane, i);
		}
		
		this.is_working = false;
	},
	
	update_callback_iframe : function () 
	{
		var ifdoc = window.frames['iradar-f'].document;
		var ifdata = '';
		var num_records = 0;

		if( this.plane_callback == null || this.ready_callback == null) {
			return false;
		}
		
		var plane  = [];
		var record = [];
		var latest_update = 0;
		var show_limit = num_records;
		
		if( (ifdata = ifdoc.getElementById('iradar-data')) != null )
		{
			ifdata = ifdata.innerHTML.split('\n');
			
			num_records = ifdata.length - 2;
			last_update = parseInt(ifdata[num_records]);
			show_limit  = this.ready_callback(num_records, last_update);
			show_limit  = show_limit < num_records ? show_limit : num_records;
			
			
			for( var i=0; i<show_limit; i++)
			{
				//;     1;     2;    3;       5;       6;  6;  7;   8;  9;10;
				//;4442fe;LOT302;18950;49.12500;20.51100;418;255;1234;-18;5;$
				record = ifdata[i].split(';');
				
				plane = { 
					rdr_id: record[10],
					hex: record[1],
					alt: parseInt(record[3]) - 5000,
					lat: parseFloat(record[4]),
					lon: parseFloat(record[5]),
					trk: parseInt(record[7]),
					spd: parseInt(record[6]),
					sqw: record[8],
					vspd:( parseInt(record[9]) * 64),
					call:record[2]
				};
			
				this.plane_callback(plane, i);
			}
            
		}
        
        this.is_working = false;
	}
}
