var liveearthclass = Class.create();
liveearthclass.prototype = {
	initialize: function(div,hidden) {
		this.a_pushpins=new Array();
		this.map = new VEMap(div);
		
		this.map.LoadMap(new VELatLong(46.9502622421856, 2.6806640625),5, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);

		if(!hidden){
			this.map.AttachEvent("onendzoom",this.getInfo.bind(this));
			this.map.AttachEvent("onmousemove",this.getInfo.bind(this));
			this.AddMyLayer("georsstest.xml");
		}
		
		if(hidden){
			$(div).hide();
		}
	},
	AddPin:function (latLong){
		var pin = new VEShape(VEShapeType.Pushpin, latLong);
		pin.SetTitle("ici votre pushpin");
		pin.SetDescription("Description de la punaise <a href='javascript:void(0);' onclick='liveAPI.removePush("+this.a_pushpins.length+");'>delete</a>");         
		this.a_pushpins.push(pin);
		this.map.AddShape(pin); 
	},
	search:function(){ this.map.Find(null, $('adresse').value, null, null, null, null, null, null, null, null, this.displayWhere.bind(this));},
	displayWhere:function(){ setTimeout(this.AddPin.bind(this,this.map.GetCenter()),300); },
	
	searchAdresse:function(adresse,cb){
		this.map.Find(null, adresse, null, null, null, null, null, null, null, null, this.displayAdresseWhere.bind(this));
		this.callBack=cb;
	},	
	displayAdresseWhere:function(){ setTimeout(this.getInfoAdresse.bind(this,this.map.GetCenter()),300); },
	getInfoAdresse:function (latLong){
		var center =this.map.GetCenter();
		$('map_latitude').value=center.Latitude;
		$('map_longitude').value=center.Longitude;
		if(this.callBack)
			this.callBack();
	},	
	removePush:function(o){
		if(this.a_pushpins[o] && this.a_pushpins[o]!=null){
			this.map.DeleteShape(this.a_pushpins[o]);
			this.a_pushpins[o]=null;
		}
	},
	AddMyLayer:function (filename){
		var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, filename, new VEShapeLayer());
		this.map.ImportShapeLayerData(veLayerSpec, this.afterLoadRss.bind(this));
	},
	afterLoadRss:function(o){
		for(x=0;x<o.Annotations.length;x++){
			this.a_pushpins.push(o.Annotations[x]);
		}
		console.debug(this.a_pushpins);
	},
    getInfo:function (){
		if (this.map.IsBirdseyeAvailable()){
			var be = this.map.GetBirdseyeScene();
			var pixel = be.LatLongToPixel(this.map.GetCenter(), this.map.GetZoomLevel());
			$('map_zoom_level').value=this.map.GetZoomLevel();
			$('map_latitude').value=pixel.x;
			$('map_longitude').value=pixel.y;
		}else {
			var center =this.map.GetCenter();
			$('map_zoom_level').value=this.map.GetZoomLevel();
			$('map_latitude').value=center.Latitude;
			$('map_longitude').value=center.Longitude;
		}
	}
};

