
//object construction function, creates an object called KaAddObjectTool 
//with the Parameter oKaMap and the method this.addObjectGeo = _addObjectGeo

function KaAddObjectTool( oKaMapXMLOverlay )  {
    
	this.kaMapXmlOverlay = oKaMapXMLOverlay;

	kaTool.apply( this, [oKaMapXMLOverlay.kaMap] );
    //manual inheritance
    for (var p in kaTool.prototype)
    {
	        if (!KaAddObjectTool.prototype[p])
	            KaAddObjectTool.prototype[p]= kaTool.prototype[p];
    }

    this.name = 'KaAddObjectTool';

    this.bMouseDown = false;
    this.addObjectGeo= _addObjectGeo;
	this.addObjectByXY = _addManually;
    this.onmousedown = _onmousedown;
    this.activate = _activate;
	this.save=_save;
	this.setSymbol=_setSymbol;
	this.isNew = true;
	this.isManual=false;

}


function _onmousedown(e) {
	e = (e)?e:((event)?event:null);
	if (this.isNew){
		this.kaMapXmlOverlay.session_pid++;
		this.isNew= false;		
	}
	isManual=false;
	if (e) this.addObjectGeo(e);
}

function _addManually() {
	if (this.isNew){
		this.kaMapXmlOverlay.session_pid++;
		this.isNew= false;		
	}
	isManual=true;
	this.addObjectGeo(null);
}


function _addObjectGeo (e){
        var LatLon;
		
		if (e != null){
			LatLon = this.adjustPixPosition( e.clientX, e.clientY );
     		LatLon = this.kaMap.pixToGeo( LatLon[0] , LatLon[1] );
			var inp = document.getElementById('northing');
			inp.value= LatLon[0].toFixed(3);
			inp = document.getElementById('easting');
			inp.value= LatLon[1].toFixed(3);
		}
		else {
			LatLon= new Array(2);
			var inp = document.getElementById('northing');
			LatLon[0]= inp.value;
			inp = document.getElementById('easting');
			LatLon[1]=inp.value;
		}

		var allmonths=new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
		var tdate=new Date();
		var wholedate;
		wholedate=tdate.getDate() + "." + allmonths[tdate.getMonth()] + "." + tdate.getFullYear();

        var xmlStr = "<overlay><point id=\""+this.kaMapXmlOverlay.session_pid+"\" x=\""+LatLon[0]+"\" y=\""+LatLon[1]+"\" sid=\""+this.kaMapXmlOverlay.sid+"\" text=\"\" nick1=\"\" nick2=\"\" picture=\"blank\" tag=\"\" date=\""+wholedate+"\"><icon w=\"30\" h=\"30\" src=\"images/cross.png\"/></point></overlay>";
		//this.kaMap.addObjectGeo( canvas, LatLon[0] , LatLon[1] , img );
		this.kaMapXmlOverlay.loadXml(xmlStr, false);
}

function _save (){

	var np = this.kaMapXmlOverlay.getPointObject(this.kaMapXmlOverlay.session_pid);

	var nick1 = document.getElementById("nick1_100");
	var nick2 = document.getElementById("nick2_100");
	var gettagStr = document.getElementById("tag_str");
	var tagStr = gettagStr.value;
	var tagStrtrimmed = noWhiteSpace(tagStr);
	np.tag = tagStrtrimmed;
	np.nick1 = nick1.value;
	np.nick2 = nick2.value;					

	var text = document.getElementsByName("textarea");
	np.comment = text[0].value;
	np.isManual=this.isManual;

	var pointattribute = document.getElementsByName("Pointattribute");
	var image = pointattribute[0].getElementsByTagName("img");
		if (image.length > 44){
			np.pictureURL = image[44].name;
		}
    	else {
			np.picturURL = '/images/default.jpg';
         }  

	np.save('php/insert.php',this.kaMapXmlOverlay,this);
    this.isNew=true;
}


function _setSymbol(symid){
	var np = this.kaMapXmlOverlay.getPointObject(this.kaMapXmlOverlay.session_pid);
	np.sid = "images/symbole/"+symid;
		
	var images = np.div.getElementsByTagName("img");	
	images[0].src=np.sid;

	var symbol_src = "<img src=\""+np.sid+"\"/>";
	var symbol_text = "<br /><small>Sie haben dieses Karten-Symbol gew&auml;hlt.</small>";
	var gewSym = document.getElementById('gewaehltesSymbol').innerHTML = symbol_src+symbol_text;
}

function noWhiteSpace(s){
	if((s==null)||(typeof(s)!='string')||!s.length)return'';
	return s.replace(/\s+/g,'');
}

function _activate () {
	
    if (this.kaMap.currentTool == this) {
	//	this.kaMap.deactivateTool ( this );
    //		document.kaCurrentTool = null;
       }
    else {
		this.kaMap.activateTool ( this );
    	document.kaCurrentTool = this;
     }  
}


