//*** START GLOBAL FUNCTION ***

function Int(to_integer){
	to_integer=parseInt(to_integer);
	if (isNaN(to_integer))
		to_integer=0;
	return to_integer;
}

function numchk(this_number){
	this_number=trim(this_number)
	punkt=this_number.indexOf(".");
	komma=this_number.indexOf(",");
	if (komma > -1){
		if (punkt==-1)
			this_number=this_number.replace(',','.');
		else if(komma<punkt)
			this_number=this_number.replace(',','');
		else if(punkt<komma){
			this_number=this_number.replace('.','');
			this_number=this_number.replace(',','.');
		}
	}
	return this_number;
}

function GreateMarker(){
	var ii=0;
	for(ii=0;ii<all_markers_type.length;ii++){
		AddMarker(ii)
	}
}

function AddMarker(i){
	if (all_markers_size[i]==34){
		marker34Icon.image=''+all_markers_type[i];
		var marker=new GMarker(all_markers_point[i], marker34Icon);
	}
	else{
		marker20Icon.image=''+all_markers_type[i];
		var marker=new GMarker(all_markers_point[i], marker20Icon);
	}
	
	if (all_markers_title[i]!='' || all_markers_img[i].length>12 || all_markers_info[i]!=''){
		GEvent.addListener(marker, "click", function() {
			OpenInfoWindow(i);
		});
		/*GEvent.addListener(marker, "mouseover", function() {
			OpenInfoWindow(i);
		});*/
	}
	all_markers[i]=marker;
	map.addOverlay(marker);
}


function AddType(element,subelement,size){
	elementtoadd=element;
	subinfo=subelement;
	markersize=size;
	if (help_polyline!=''){
			help_polyline.remove();	
	}
	if (element=='polyline'){
		if (typeof all_polylines[curr_polyline_pos]!='undefined')
			curr_polyline_pos=curr_polyline_pos+1
	}
}


function ArrayElementFind(this_array,element){
	var i=0;
	for (i=0;i<this_array.length;i++){
		if (this_array[i]==element)
			return i;
	}
	return -1;
}

function ArrayDeleteAt(this_array,pos){
	var new_array=[];
	var i=0;
	for (i=0;i<this_array.length;i++){
		if (i!=pos)
			new_array[new_array.length]=this_array[i];
	}
	return new_array;
}

//*** END GLOBAL FUNCTION ***

//*** START MARKER ***
function OpenInfoWindow(this_pos){
	var this_content='';
	this_content+='<table cellpadding="1" cellspacing="1" border="0" width="220" bgcolor="#ffffff">';
	if (all_markers_title[this_pos]!=''){
		this_content+='<tr><td style="font-weight:bold; font-size:11px;"><b>'+all_markers_title[this_pos]+'</b></td></tr>';
	}
	
	if (all_markers_img[this_pos].length>12){
		this_content+='<tr><td align="center"><img src="'+all_markers_img[this_pos]+'" height="150" border="0" align="center" /></td></tr>';
	}
	if (all_markers_info[this_pos]!=''){
		this_content+='<tr><td>'+all_markers_info[this_pos]+'</td></tr>';
	}
	this_content+='</table>';
	all_markers[this_pos].openInfoWindowHtml(this_content);
}

//*** END MARKER ***


//*** START POLYLINE ***
function DrawPolyLine(this_pos){
	var points=[];
	for (i=0;i<all_polylines_marker[this_pos].length;i++){
		points[points.length]=all_polylines_marker[this_pos][i].getPoint();
	}
	var new_polyline=new GPolyline(points,'#0000FF',3,1);
	if (all_polylines[this_pos]!=""){
		all_polylines[this_pos].remove();
	}
	all_polylines[this_pos]=new_polyline;
	map.addOverlay(new_polyline);
}

function ComputeArrowHead(p1,p2) {
	var dir=0;
	var lat1 = p1.latRadians();
	var lon1 = p1.lngRadians();
	var lat2 = p2.latRadians();
	var lon2 = p2.lngRadians();
	var angle = - Math.atan2( Math.sin( lon1 - lon2 ) * Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) - Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
	if ( angle < 0.0 )
	angle  += Math.PI * 2.0;
	angle = angle * 180.0 / Math.PI;
	dir = angle.toFixed(1);
	dir = Math.round(dir/3) * 3;
	while (dir >= 120) {dir -= 120;}
	return "http://www.google.com/intl/en_ALL/mapfiles/dir_"+dir+".png"
}

function PolyLineMarker(point, this_polyline_pos, this_marker_pos, line_type){
	if (line_type=='arrow'){
		arrowIcon.image=ComputeArrowHead(all_polylines_marker[this_polyline_pos][this_marker_pos-1].getPoint(),point);
		var marker=new GMarker(point, arrowIcon);
	}
	else if (line_type=='point'){
		var marker=new GMarker(point, pointIcon);
	}
	else{
		var marker=new GMarker(point, transparentIcon);
	}

	all_polylines_marker[this_polyline_pos][this_marker_pos]=marker;
	map.addOverlay(marker);
}

//*** END POLYLINE ***


//*** START LABEL ***
function TLabel(){}
TLabel.prototype.initialize=function(a){
	var this_label=document.createElement('span');
	var this_pos=this.this_pos;
	this_id='wmlabel_'+this_pos;
	this_label.setAttribute('id',this_id);
	document.body.appendChild(this_label);
	LabelShowContent(this_pos);
	this_label.style.position='absolute';
	this_label.style.zIndex=100+this_pos*1;
	this.mapTray=map.getPane(G_MAP_MAP_PANE);
	this.mapTray.appendChild(this_label);
	if(!this.markerOffset){this.markerOffset=new GSize(0,0);}
	SetLabelPosition(this_pos)
	GEvent.bind(a,"zoomend",this,function(){SetLabelPosition(this_pos)});
}

function LabelShowContent(this_pos){
	var this_id='wmlabel_'+this_pos;
	var this_content='';
	this_content+='<table cellpadding="1" cellspacing="1" border="0" width="200" bgcolor="#C0C0C0">';
	if (all_labels_title[this_pos]!=''){
		this_content+='<tr><td style="font-weight:bold; font-size:11px;">'+all_labels_title[this_pos]+'</td></tr>';
	}
	if (all_labels_img[this_pos].length>12){
		this_content+='<tr><td align="center"><img src="'+all_labels_img[this_pos]+'" border="0" /></td></tr>';
	}
	if (all_labels_content[this_pos]!=''){
		this_content+='<tr><td>'+all_labels_content[this_pos]+'</td></tr>';
	}
	this_content+='</table>';
	SetLabelOpacity(this_pos,all_labels_opacity[this_pos]);
	document.getElementById(this_id).innerHTML=this_content;
}
	
function SetLabelOpacity(this_pos,po){
	if(po<0)
		po=0; 
	if(po>100)
		po=100;
	var c=po/100;
	var d=document.getElementById('wmlabel_'+this_pos)
	if(typeof(d.style.filter)=='string'){d.style.filter='alpha(opacity:'+po+')';}
	if(typeof(d.style.KHTMLOpacity)=='string'){d.style.KHTMLOpacity=c;}
	if(typeof(d.style.MozOpacity)=='string'){d.style.MozOpacity=c;}
	if(typeof(d.style.opacity)=='string'){d.style.opacity=c;}
}

function SetLabelPosition(this_pos){
	//alert(all_labels[this_pos].id)
	var b=map.fromLatLngToDivPixel(all_labels_point[this_pos]);
	var xx=Int(parseInt(b.x)-5);
	var yy=Int(parseInt(b.y)-5);
	var d=document.getElementById('wmlabel_'+this_pos)
	this_width=xx-all_labels[this_pos].markerOffset.width;
	this_height=yy-all_labels[this_pos].markerOffset.height;
	if (this_width<0)
		this_width=0;
	if (this_height<0)
		this_height=0;
	d.style.left=this_width+'px';
	d.style.top=this_height+'px';
}

GMap2.prototype.addTLabel=function(a){
	a.initialize(this);
}

function createTLabel(point,title,img,content,opacity) {
	var this_pos=all_labels.length;
	var label = new TLabel();
	label.this_pos=this_pos;

	all_labels[this_pos]=label;
	all_labels_point[this_pos]=point;
	all_labels_title[this_pos]=title;
	all_labels_img[this_pos]=img;
	all_labels_content[this_pos]=content;
	all_labels_opacity[this_pos]=opacity;

	map.addTLabel(label);
}


//*** END LABEL ***


if (GBrowserIsCompatible()) {

	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	
	var arrowIcon = new GIcon();
	arrowIcon.iconSize = new GSize(24,24);
	arrowIcon.shadowSize = new GSize(1,1);
	arrowIcon.iconAnchor = new GPoint(12,12);
	arrowIcon.infoWindowAnchor = new GPoint(0,0);
	
	var marker34Icon = new GIcon();
	marker34Icon.iconSize = new GSize(20,34);
	marker34Icon.shadowSize = new GSize(1,1);
	marker34Icon.iconAnchor = new GPoint(10,34);
	marker34Icon.infoWindowAnchor = new GPoint(0,0);
	
	var marker20Icon = new GIcon();
	marker20Icon.iconSize = new GSize(12,20);
	marker20Icon.shadowSize = new GSize(1,1);
	marker20Icon.iconAnchor = new GPoint(6,20);
	marker20Icon.infoWindowAnchor = new GPoint(0,0);
	
	
	var transparentIcon = new GIcon();
	transparentIcon.image="";
	transparentIcon.transparent="";
	transparentIcon.iconSize = new GSize(10,10);
	transparentIcon.shadowSize = new GSize(1,1);
	transparentIcon.iconAnchor = new GPoint(5,5);
	
	var pointIcon = new GIcon();
	pointIcon.image="";
	pointIcon.iconSize = new GSize(10,10);
	pointIcon.shadowSize = new GSize(1,1);
	pointIcon.iconAnchor = new GPoint(5,5);
	
	all_polylines=[];
	all_polylines_type=[];
	all_polylines_marker=[];
	
	curr_polyline_pos=0;
	
	help_polyline="";
	
	all_markers=[]
	all_markers_point=[];
	all_markers_type=[];
	all_markers_size=[];
	all_markers_title=[];
	all_markers_img=[];
	all_markers_info=[];
	
	all_labels=[];
	all_labels_point=[];
	all_labels_title=[];
	all_labels_img=[];
	all_labels_content=[];
	all_labels_opacity=[];
	
	elementtoadd="";
	subinfo="";
	markersize="";
	
	marker_to_move="";
	polyline_to_move_pos=-1;
	label_to_move='none';
	
}
else{
	alert("Sorry, the Google Maps API is not compatible with this browser");
}
