var HX_XHREQ_STAT_UNINITIALIZED  = 0;
var HX_XHREQ_STAT_LOADING        = 1;
var HX_XHREQ_STAT_LOADED         = 2;
var HX_XHREQ_STAT_INTERACTIVE    = 3;
var HX_XHREQ_STAT_COMPLETE       = 4;


var arfangmaps=new Array();

function hxArfangMap_StartAll(){
	for(var i=0;i<arfangmaps.length;i++)
		arfangmaps[i].refresh();
}

function hxArfangMap(div){
	this.debug=0;
	this.div=div;	
	this.viewport=null;	
	this.statusbar=null;	
	this.img=null;	
	this.imgbf=new Image();	
	

	this.params=null;
	this.xhr=null;
	
	this.mouse_x=0;
	this.mouse_y=0;	

	
	this.level=0;	
	this.level_count=0;		
	this.showevents=0
	this.directview=0
	this.protractor=0
	this.station='';	
	this.title='';
	this.description='';
	this.longitude_rate=0.0;
	this.latitude_rate=0.0;	
	this.longitude=0.0;
	this.latitude=0.0;
	this.cursor_x=-1;
	this.cursor_y=-1;
	this.cursor_altitude=0;	
	this.raystr='';
	this.beg_ts='';
	this.end_ts='';
	
	if(window.XMLHttpRequest){
		this.xhr = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
		
	arfangmaps[arfangmaps.length]=this;
	this.viewport=_(this.div + '-viewport');		
	this.statusbar=_(this.div + '-statusbar');		
	this.img=_(this.div + '-img');
	
	
	this.set_debug=function(enabled){
		this.debug=enabled;
	}
	
	this.set_station=function(station){
		this.station=station;
	}
	
	this.set_title=function(title){
		this.title=title;
	}
	
	this.set_level=function(level){
		this.level=level;
	}
	
	this.set_showevents=function(showevents){
		this.showevents=showevents;
	}
	
	this.set_directview=function(directview){
		this.directview=directview;
	}
	
	this.set_protractor=function(protractor){
		this.protractor=protractor;
	}

	// Step 2B
	this.set_beg_ts=function(beg_ts){
		this.beg_ts=beg_ts;
	}
	this.set_end_ts=function(end_ts){
		this.end_ts=end_ts;
	}
	
	this.set_raystr=function(rays){
		this.raystr=rays;	
	}
	
	
	this.refresh=function(){			
		this.img.src='images/arfangmap/loading.gif';		
		var obj=this;
		setTimeout(function() {obj.load();}, 250);
	}
	
	this.build_url_params=function(){		
		return 	'div=' + this.div +
				'&station=' + this.station +				
				'&level=' + this.level +									
				'&showevents=' + this.showevents +									
				'&directview=' + this.directview +									
				'&protractor=' + this.protractor +									
				'&cursor_x=' + this.cursor_x +				
				'&cursor_y=' + this.cursor_y +
				'&debug=' + this.debug +
				'&title=' + this.title +
				// Step 7 : retour vers le PHP
				'&begts=' + this.beg_ts +
				'&endts=' + this.end_ts +
				'&raystr=' + this.raystr;
	}
	
	this.load=function(){						
		//Img
		this.imgbf.src='arfangmap.php?cmd=img&' + this.build_url_params();				
		this.onloadimage();		
		//Data
		var obj=this;		
		this.xhr.open('GET', 'arfangmap.php?cmd=data&' + this.build_url_params() );		
		this.xhr.onreadystatechange = function(){ obj.onloadparams.call( obj ); };		
		this.xhr.send(null);
	}
	
	this.onloadimage = function(){		
		if(!this.imgbf.complete){
			var objt=this;
			setTimeout(function() {objt.onloadimage();}, 250);
		}else{
			this.img.src=this.imgbf.src;						
		}
	}
	
	this.onloadparams = function( ){
		if ( !this.xhr ) return;		
		if (this.xhr.readyState == HX_XHREQ_STAT_COMPLETE ){					
			if ( this.xhr.status == '200' ) {						
				this.params=eval('(' + this.xhr.responseText + ')');					
							
				this.cursor_altitude=parseFloat(this.params.cursor_altitude);	
				this.debug=this.params.debug;	
				this.level_count=parseInt(this.params.level_count);					
				this.level=parseInt(this.params.level);					
				this.showevents=this.params.showevents;	
				this.directview=this.params.directview;	
				this.protractor=this.params.protractor;									
				this.title=this.params.title;	
				this.description=this.params.description;		
				this.latitude=parseFloat(this.params.latitude);	
				this.longitude=parseFloat(this.params.longitude);					
				this.latitude_rate=parseFloat(this.params.latitude_rate);					
				this.longitude_rate=parseFloat(this.params.longitude_rate);
				// Step 6 : recuperation via json
				this.beg_ts=parseFloat(this.params.begin_timestamp);
				this.end_ts=parseFloat(this.params.end_timestamp);
				this.raystr=this.params.raystr;	
				
				_(this.div + '-title').innerHTML=this.params.title;	
				_(this.div + '-level').innerHTML='Level: ' +this.params.level;	

				//Cursor
				if ((this.cursor_y>=0) && (this.cursor_x>=0)){
				
					var mask =_(this.div + '-mask');
					if (!mask){
						mask=document.createElement('div');
						this.viewport.appendChild(mask);
					}								
					mask.name=this.div + '-mask';
					mask.id=mask.name;					
					var pos=this.getpos(this.img);		
					mask.style.left=(parseInt(pos[0])+parseInt(this.cursor_x)-14);		
					mask.style.top=(parseInt(pos[1])+parseInt(this.cursor_y)-14);				
					mask.className='arfangmap_selection';					
					var lat=this.latitude+(this.cursor_y/this.latitude_rate);
					var lng=this.longitude+(this.cursor_x/this.longitude_rate);
					var alt=this.cursor_altitude;
					_(this.div + '-statusbar-cursor').innerHTML='[' + this.cursor_x + ' x ' + this.cursor_y + ']   -   latitude: ' +  lat.toFixed(6) + ' - longitude: ' + lng.toFixed(6) + ' - altitude: ' + alt + ' m';					
				
				}
				
				//Bar
				var map =_(this.div + '-map-btn');
				if (!map){			
					var pos=this.getpos(this.viewport);	
					map=document.createElement('div');
					this.viewport.appendChild(map);
					map.id=this.div + '-map-btn';		
					map.style.position='absolute';
					map.style.zIndex=100;					
					map.style.left=pos[0];
					map.style.top=parseInt(pos[1])+2;
					map.style.height='20px';
					map.style.width=parseInt(this.viewport.style.width)-10;
					
				}
				
				
				//Btns
				var obj=this;		
				
				//Info
				if (this.description!=''){
					btn =_(this.div + '-btn-info');
					if (!btn){			
						btn=document.createElement('a');
						map.appendChild(btn);
						btn.id=this.div + '-btn-info';					
						btn.className='arfangmap_big_btn';										
						btn.onclick=function() {obj.oninfo();}
						btn.innerHTML=this.params.btn_description_caption;
						btn.title=this.params.btn_description_desc;							
					}
				}
				
				//Save
				btn =_(this.div + '-btn-save');
				if (!btn){			
					btn=document.createElement('a');
					map.appendChild(btn);
					btn.id=this.div + '-btn-save';					
					btn.className='arfangmap_big_btn';										
					btn.onclick=function() {obj.onsave();}
					btn.innerHTML=this.params.btn_save_caption;
					btn.title=this.params.btn_save_desc;							
				}
				

				//protractor
				btn =_(this.div + '-btn-protractor');
				if (!btn){			
					btn=document.createElement('a');
					map.appendChild(btn);
					btn.id=this.div + '-btn-protractor';					
					btn.className='arfangmap_big_btn';										
					btn.onclick=function() {obj.onprotractor();}
					btn.innerHTML=this.params.btn_protractor_caption;
					btn.title=this.params.btn_protractor_desc;							
				}
				if (this.protractor==1)
					btn.className='arfangmap_big_btn';											
				else
					btn.className='arfangmap_big_btn_off';				


				//directview
				btn =_(this.div + '-btn-directview');
				if (!btn){			
					btn=document.createElement('a');
					map.appendChild(btn);
					btn.id=this.div + '-btn-directview';					
					btn.className='arfangmap_big_btn';										
					btn.onclick=function() {obj.ondirectview();}
					btn.innerHTML=this.params.btn_direct_view_caption;
					btn.title=this.params.btn_direct_view_desc;							
				}
				if (this.directview==1)
					btn.className='arfangmap_big_btn';											
				else
					btn.className='arfangmap_big_btn_off';
				
				
				//showevents
				btn =_(this.div + '-btn-showevents');
				if (!btn){			
					btn=document.createElement('a');
					map.appendChild(btn);
					btn.id=this.div + '-btn-showevents';					
					btn.className='arfangmap_big_btn';										
					btn.onclick=function() {obj.onshowevents();}
					btn.innerHTML=this.params.btn_show_event_caption;
					btn.title=this.params.btn_show_event_desc;							
				}				
				if (this.showevents==1)
					btn.className='arfangmap_big_btn';											
				else
					btn.className='arfangmap_big_btn_off';											
					
				
				

				//Unzoom
				btn =_(this.div + '-btn-unzoom');
				if (!btn){			
					btn=document.createElement('a');
					map.appendChild(btn);
					btn.id=this.div + '-btn-unzoom';					
					btn.innerHTML=this.params.btn_zoom_out_caption;
					btn.title=this.params.btn_zoom_out_desc;			
					
				}
				if (this.level<=0){
					btn.className='arfangmap_big_btn_disabled';						
					btn.onclick=null;
				}else{
					btn.className='arfangmap_big_btn';						
					btn.onclick=function() {obj.onunzoom();}
				}				
						
				//Zoom
				var btn =_(this.div + '-btn-zoom');
				if (!btn){			
					btn=document.createElement('a');
					map.appendChild(btn);
					btn.id=this.div + '-btn-zoom';					
					btn.innerHTML=this.params.btn_zoom_in_caption;
					btn.title=this.params.btn_zoom_in_desc;					
				}								
				if ((this.level+1)>=this.level_count){
					btn.className='arfangmap_big_btn_disabled';						
					btn.onclick=null;
				}else{
					btn.className='arfangmap_big_btn';						
					btn.onclick=function() {obj.onzoom();}
				}
				
				
			
				
			}else{				
				//this.setError(HX_XREQ_ERR_CONNEXION);				
			}
		}		
	}
		
	

	
	
	this.ontoggle=function(){
		var btn=_(this.div + '-btn-toggle');		
		if (this.viewport.style.display!='table-cell'){
			this.viewport.style.display='table-cell';
			this.statusbar.style.display='block';
			btn.src='images/arfangmap/toolbar_hide.png';
		}else{
			this.viewport.style.display='none';
			this.statusbar.style.display='none';
			btn.src='images/arfangmap/toolbar_show.png';
		}
	}
	
	this.oninfo=function(){		
		if (this.description=='') return;
		alert(this.description);
	}
	
	
	this.onzoom=function(){	
		this.level++;
		this.refresh();
	}
	this.onunzoom=function(){	
		if (!this.level) return;
		this.level--;
		this.refresh();
	}
	
	this.onshowevents=function(){	
		var btn=_(this.div + '-btn-showevents');				
		if (this.showevents==1){
			this.showevents=0;		
			btn.className='arfangmap_big_btn_off';
		}else{
			this.showevents=1;		
			btn.className='arfangmap_big_btn';
		}				
		this.refresh();
	}
	
	this.ondirectview=function(){	
		var btn=_(this.div + '-btn-directview');				
		if (this.directview==1){
			this.directview=0;		
			btn.className='arfangmap_big_btn_off';
		}else{
			this.directview=1;		
			btn.className='arfangmap_big_btn';
		}		
		this.refresh();
	}
	
	this.onprotractor=function(){	
		var btn=_(this.div + '-btn-protractor');				
		if (this.protractor==1){
			this.protractor=0;		
			btn.className='arfangmap_big_btn_off';
		}else{
			this.protractor=1;		
			btn.className='arfangmap_big_btn';
		}		
		this.refresh();
	}
	
	

	this.onfull=function(){		
		window.open('arfangmap.php?cmd=full&' + this.build_url_params() );
	}
	this.onsave=function(){		
		window.open('arfangmap.php?cmd=force&' + this.build_url_params() );
	}
	this.onimg=function(){		
		window.open('arfangmap.php?cmd=img&' + this.build_url_params() );
	}
	
	
		
	
	this.getpos=function(obj){
	  var curleft = 0;
	  var curtop = 0;
	  if ((obj)&&(obj.offsetParent)) {
		do {
		  curleft += obj.offsetLeft;
		  curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	  }
	}
	
	this.onclick=function(){
		this.cursor_x=this.mouse_x;
		this.cursor_y=this.mouse_y;		
		//Data
		var obj=this;		
		this.xhr.open('GET', 'arfangmap.php?cmd=data&' + this.build_url_params() );		
		this.xhr.onreadystatechange = function(){ obj.onloadparams.call( obj ); };		
		this.xhr.send(null);
	}
	
	
	
	this.onmove=function(event){
		if (document.all){
			this.mouse_x = event.x+document.body.scrollLeft;
			this.mouse_y = event.y+document.body.scrollTop;
		}else{
			this.mouse_x = event.pageX;
			this.mouse_y = event.pageY; 
		}
		var pos=this.getpos(this.img);	
		this.mouse_x-=(pos[0]+1);
		this.mouse_y-=(pos[1]+1);				
		var lat=this.latitude+(this.mouse_y/this.latitude_rate);
		var lng=this.longitude+(this.mouse_x/this.longitude_rate);
		_(this.div + '-statusbar-current').innerHTML='[' + this.mouse_x + ' x ' + this.mouse_y + ']   -   latitude: ' +  lat.toFixed(6) + ' - longitude: ' + lng.toFixed(6);		
		
		
		
	}
	
}



