var mouseX,mouseY;

function MouseDown(event){	
	/* Click  on Map */
	mapWidth=910;
	mapHeight=455;	
	mapOffset = GetAbsPosition(document.getElementById("pointer_div"));
	
	/* Get mouse position */
	getMouseXY(event);
	if (document.all) {
	 	/* Bei IE (nicht) 2px abziehen...Weiss nicht warum Abweichung */
		if (window.opera) {
 		} else {
	 		mouseX = mouseX-2;	
	 		mouseY = mouseY-2;
		}
	} 

	/* Calculate Lat and Lon */			
	posX = mouseX - mapOffset.x - 2;
	posY = mouseY - mapOffset.y - 1;	
	lon = (posX-(mapWidth/2))*360/mapWidth;
	lat = -1*(posY-(mapHeight/2))*180/mapHeight;

	document.getElementById("form_x").style.visibility = "hidden";						
	document.getElementById("form_y").style.visibility = "hidden";							
	document.pointform.form_x.value = lon;
	document.pointform.form_y.value = lat;
	
	/* Calculate position for pinpoint */
	// Marker an gegebene Position setzen	 
	mapOffset = GetAbsPosition(document.getElementById("pointer_div"));	
 	posX = ((parseFloat(lon)+180) * mapWidth / 360);
 	posY = ((90-parseFloat(lat)) / 180 * mapHeight);	 	
	document.getElementById("cross").style.left = mapOffset.x + Math.round((posX-5)/4)*4;
	document.getElementById("cross").style.top = mapOffset.y + Math.round((posY-3)/4)*4-8;	
	document.getElementById("cross").style.visibility = "visible" ;	
	
	// Fotoanzeige neu aufrufen
	document.pointform.submit();
}


function getMouseXY(e) { 
	// works on IE6,FF,Moz,Opera7
 	if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e) { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mouseX = e.pageX;
      mouseY = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      // Note: I am adding together both the "body" and "documentElement" scroll positions
      //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
      //         (example: IE6 in compatibility mode or strict)
      //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
      //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
      //         (from info at http://www.quirksmode.org/js/doctypes.html)
      mouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}


function MouseMove(event) {
	/* Process Mouse Movement */
	mapWidth=910;
	mapHeight=455;
	
	getMouseXY(event);
	if (document.all) {
	 	/* Bei IE (nicht) 2px abziehen...Weiss nicht warum Abweichung */
		if (window.opera) {
 		} else {
	 		mouseX = mouseX-2;	
	 		mouseY = mouseY-2;
		}
	} 

	/* Process */
	mapOffset = GetAbsPosition(document.getElementById("pointer_div"));
	if (mouseX>=mapOffset.x && mouseX<=mapWidth+mapOffset.x && mouseY>=mapOffset.y && mouseY<=430+mapOffset.y) { // mapHeight+mapOffset.y) {
		document.getElementById("vline").style.left=mouseX;
		document.getElementById("vline").style.top=mapOffset.y;	
		document.getElementById("vline").style.visibility = "visible" ;						
		document.getElementById("hline").style.left=mapOffset.x;	
		document.getElementById("hline").style.top=mouseY;
		document.getElementById("hline").style.visibility = "visible" ;
		document.getElementById("form_x").style.left=mouseX;
		document.getElementById("form_x").style.top=mapOffset.y+mapHeight-20;	
		document.getElementById("form_x").style.visibility = "visible" ;		
		document.getElementById("form_y").style.left=mapWidth+mapOffset.x;
		//document.getElementById("form_y").style.top=mouseY;	
		//document.getElementById("form_y").style.visibility = "visible" ;		
		
		// Calculate Lat and Lon 
		lon = Math.round(((mouseX-mapOffset.x)-(mapWidth/2))*360/mapWidth*10)/10;
		lat = Math.round(-1*((mouseY-mapOffset.y)-(mapHeight/2))*180/mapHeight*10)/10;
				
		document.pointform.form_x.value = "Lat. "+lat+document.getElementById("degsign").value+", Lng. "+lon+document.getElementById("degsign").value;
		document.pointform.form_y.value = lat+document.getElementById("degsign").value;		
	} else {
 		document.getElementById("vline").style.visibility = "hidden" ;
 		document.getElementById("hline").style.visibility = "hidden" ; 	
 		document.getElementById("form_x").style.visibility = "hidden" ;
 		//document.getElementById("form_y").style.visibility = "hidden" ; 				 	
	}
}

function GetAbsPosition(object) {
	var position = new Object;
	position.x = 0;
	position.y = 0;
	
	if( object ) {
		position.x = object.offsetLeft;
		position.y = object.offsetTop;
	
		if( object.offsetParent ) {
			var parentpos = GetAbsPosition(object.offsetParent);
			position.x += parentpos.x;
			position.y += parentpos.y;
		}
	}
	
	position.cx = object.offsetWidth;
	position.cy = object.offsetHeight;
	
	return position;
}

function MouseInit() {
	// Initialen Marker setzen
	mapWidth=910;
	mapHeight=455;	
	mapOffset = GetAbsPosition(document.getElementById("pointer_div"));	

 	// Marker an gegebene Position setzen	 	
 	posX = ((parseFloat(document.getElementById("initlong").value)+180) * mapWidth / 360);
 	posY = ((90-parseFloat(document.getElementById("initlat").value)) / 180 * mapHeight);	 	

	// Calculate position for pinpoint
	document.getElementById("cross").style.left = mapOffset.x + Math.round((posX)/4-1)*4;
	document.getElementById("cross").style.top = mapOffset.y + Math.round((posY)/4-1)*4-8;	
	document.getElementById("cross").style.visibility = "visible" ;	
	
	/* Init Mouse Movement Processing */
	/* MSIE, Konqueror, Opera */
	if (document.all)
    	window.onmousemove=MouseMove;
	/* Netscape, Mozilla */
  	else {
		/* Netscape6, Mozilla */
		if (typeof(document.addEventListener)=="function")
	    	document.addEventListener("mousemove",MouseMove,true);
		/* Netscape4 */
    	else {
      		window.captureEvents(Event.MOUSEMOVE);
      		window.onmousemove=MouseMove;
	    }	    	    
	}	
	
	document.getElementById("vline").style.left=mapOffset.x;	
	document.getElementById("vline").style.top=mapOffset.y;					
	document.getElementById("hline").style.left=mapOffset.x;	
	document.getElementById("hline").style.top=mapOffset.y;
}

function setRandomMarker() {  
 	// Set marker at random position	
	mapWidth=910;
	mapHeight=455;
	mapOffset = GetAbsPosition(document.getElementById("pointer_div"));		
						 	
	posX = GetRandom(0,mapWidth);
	posY = GetRandom(mapHeight*0.1,mapHeight*0.9);
	
	// Calculate Lat and Lon
	lon = (posX-(mapWidth/2))*360/mapWidth;
	lat = -1*(posY-(mapHeight/2))*180/mapHeight;
	
	// Calculate position for pinpoint
	document.getElementById("cross").style.left = mapOffset.x + Math.round((posX-3)/4)*4;
	document.getElementById("cross").style.top = mapOffset.y + Math.round((posY-3)/4)*4-8;	
	document.getElementById("cross").style.visibility = "visible";			 		
}

function initAnim(counter,direction) {
	/* Intro Animation */		
	mapWidth=910;
	mapHeight=455;
	mapOffset = GetAbsPosition(document.getElementById("pointer_div"));	
				
	if (document.getElementById("form_x").style.visibility != "visible") {
		document.getElementById("vline").style.left=counter+direction + mapOffset.x;
		document.getElementById("hline").style.top=(counter+direction)/2 + mapOffset.y;
	
		if (counter <= (mapWidth-direction)) {
		 	if (counter >= 0)
				setTimeout("initAnim("+(counter+direction)+","+direction+")", 10);
			else {
				document.getElementById("hline").style.visibility = "hidden" ;
				document.getElementById("vline").style.visibility = "hidden" ;	
			}
			
		} else {
		 	//setRandomMarker();	 
		 	
	 		if (document.getElementById("playintro").value == "1") {
			 	// Marker setzen	 	
			 	posX = ((parseFloat(document.getElementById("form_x").value)+180) * mapWidth / 360);
			 	posY = ((90-parseFloat(document.getElementById("form_y").value)) / 180 * mapHeight);	 	
		
				// Calculate position for pinpoint
				document.getElementById("cross").style.left = mapOffset.x + Math.round((posX)/4)*4;
				document.getElementById("cross").style.top = mapOffset.y + Math.round((posY)/4)*4-8;	
				document.getElementById("cross").style.visibility = "visible" ;	
		 	}
		
			direction = -20;
			setTimeout("initAnim("+(counter+direction)+","+direction+")", 10);					
		}
	} else
		if (document.getElementById("cross").style.visibility != "visible")
		 	setRandomMarker();	 
} 
  

function GetRandom( min, max ) {
	if(min > max)
        return(-1);
        
	if(min == max)
		return(min);
    var r = parseInt(Math.random()*(max+1));
	return(r + min <= max ? r + min : r);
} 

// private method for UTF-8 encoding
function _utf8_encode(string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }

    }

    return utftext;
}