//Positionen des Hintergrundbildes
var old_background_pos_x=0;
var old_background_pos_y=0;
var new_background_pos_x=0;
var new_background_pos_y=0;

var z;
var x,y;
var tx, ty;
var mx, my;
var mousepos_x, mousepos_x;
var isDrag=false;
var wasDraged=false;
var z=100;
var widgetpos = 0;
var widgetwidth = 200;
var widgetheight = 40;
var dragDivs = new Array();
var dragDivsData = new Array();

var aiaiCountdown;
var aiaiDivLeftPos;
var aiaiDivTopPos;

var firefox=document.getElementById&&!document.all;

document.onmousemove=mouseMove;
document.onmousedown=selectMouse;
document.onmouseup=function(){
	hideQuickInfo();
	//show menu to fly to pisiotin
	if(isDrag=="map" && wasDraged==false)
	{
		if(document.getElementById('aiaiDiv'))
		{
			/*
			document.getElementById('mapMenuDiv').style.left = tipx-5;
			document.getElementById('mapMenuDiv').style.top = tipy-5;
			document.getElementById('mapMenuKoordinateX').innerHTML = mousepos_x;
			document.getElementById('mapMenuKoordinateY').innerHTML = mousepos_y;
			*/
			
			//position for *aiai*
			document.getElementById('aiaiDiv').style.left = tipx-20;
			document.getElementById('aiaiDiv').style.top = tipy-25;
			aiaiDivLeftPos = document.getElementById('aiaiDiv').style.left;
			aiaiDivTopPos = document.getElementById('aiaiDiv').style.top;
			document.getElementById('targetmarkImg').attributes["src"].value = varGraphicRoot + '/templates/' + varTemplateDir + '/images/targetmark.gif';
			displayBlockDiv('aiaiDiv');
			clearInterval(aiaiCountdown);
			aiaiCountdown = setInterval(
										function()
										{
											clearInterval(aiaiCountdown);
											displayNoneDiv('aiaiDiv');
										}
			
								, 2000);
		}
		
		document.getElementById('map').focus();
		sendRequest('?module=map/docking','&in_out=out', 'tempdiv');						
		execJS('docking_script');
		sendRequest('?module=map/gotopos','&gotopos_x=' + mousepos_x + '&gotopos_y=' + mousepos_y, 'tempdiv');
		clearInterval(flyCountdown);
		
		if(document.getElementById("myshipinfoContent") && document.getElementById("myshipinfoContent").style.display != 'none')
		{
			openWidget('?module=myship/shipinfo_my','','myshipinfo','standard');
			exchangeStyleClassActiv('myshipinfoMenu', 'menu_' + langTextCockpit); //langTextCockpit <- from index.php $lang['COCKPIT']
		}
		getShips();
		getPlanets();
		execJS('flycountdown_script');
	}
	
	if(isDrag=="map" && 
		((mapPos_x!= "" && mapPos_x != tMapPos_x) || (mapPos_y!= "" && mapPos_y != tMapPos_y))
	){
	
		var movingShips = document.getElementsByName('movingShip');
		for(var i=0; i < movingShips.length; i++)
		{	
			if(movingShips[i].attributes.mission.value > 0)
			{
				var shipMission = movingShips[i].attributes.id.value;
				//alert(shipMission);
				//alert(document.getElementById('wespe')); //call function moveShip with username as parameter
				document.getElementById(shipMission).attributes.mission.value = 0; //call function moveShip with username as parameter
			}
		}								

		tMapPos_x = mapPos_x;
		tMapPos_y = mapPos_y;
	
		getPlanets();
		getShips();
		
		//if Minimapwidget on
		if(document.getElementById('minimap'))
		{
			getMinimap();
		}
	}
	//remove transparency from widget
	if(document.getElementById('lastDragedWidget') && document.getElementById('lastDragedWidget').innerHTML != '')
	{
		setStyleClass(document.getElementById('lastDragedWidget').innerHTML, 'widget');
		document.getElementById('lastDragedWidget').innerHTML = '';
	}
	
	isDrag=false;
	wasDraged=false;

}

function selectMouse(e) 
{
		//Position des Hintergrundes erfassen
		old_background_pos_x = new_background_pos_x;
		old_background_pos_y = new_background_pos_y;
		mx = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
		my = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
		var p = (document.all) ? window.event.srcElement : e.target;
		if (p.attributes.id && p.attributes.id.value == "map" || p.attributes.name && p.attributes.name.value == "planet"){
			
			//todo!!!if()document.getElementById('mousepos_x').value
			if(document.getElementById('gotopos_x') && document.getElementById('gotopos_y')){
				document.getElementById('gotopos_x').value = mousepos_x;
				document.getElementById('gotopos_y').value = mousepos_y;
			}
		
			isDrag="map";
			var divs = new Array();
			var temp = "";
			divs = getInnerDivs("map");
			for (var i in divs)
			{
				temp += getInnerDivs(divs[i]);
				temp += ",";
			}
			dragDivs = temp.split(",");
			for (var j in dragDivs)
			{
				if(document.getElementById(dragDivs[j]) && dragDivs[j]!="")
				{
					dragDivsData[dragDivs[j]] = new Object();
					dragDivsData[dragDivs[j]]['x']=document.getElementById(dragDivs[j]).style.left;
					dragDivsData[dragDivs[j]]['y']=document.getElementById(dragDivs[j]).style.top;
				}
			}			
		}
		else if (p.attributes.id 
			&& p.attributes.id.value 
			&& (document.getElementById(p.attributes.id.value+'Widget') || document.getElementById(p.attributes.id.value.substr(0, p.attributes.id.value.length - 4)+'Widget'))
			)
		{
			if(p.attributes.id.value.substr(p.attributes.id.value.length-4, p.attributes.id.value.length)=='left')
			{
				isDrag=p.attributes.id.value.substr(0, p.attributes.id.value.length - 4); //id without 'left'
			}else{
				isDrag=p.attributes.id.value;
			}
			x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
			y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
			tx=parseInt(document.getElementById(isDrag+'Widget').style.left);
			ty=parseInt(document.getElementById(isDrag+'Widget').style.top);
			
			document.getElementById(isDrag+'Widget').style.zIndex=z;
			document.getElementById('lastDragedWidget').innerHTML = isDrag+'Widget';
			setStyleClass(isDrag+'Widget', 'widgetDrag');
			z++;
		}
}

function mouseMove(e)
{
	tipx = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	tipy = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
	
	mousepos_x = parseInt(mapPos_x + (document.body.clientWidth/2 - document.body.clientWidth) + tipx);
	mousepos_y = parseInt(mapPos_y + (document.body.clientHeight/2 - document.body.clientHeight) + tipy);
	
	if(document.getElementById('quickInfoDiv'))
	{
		document.getElementById('quickInfoDiv').style.left = tipx+10;
		document.getElementById('quickInfoDiv').style.top = tipy+20;
	}
	
	//minimap: write the Coordinates into the Textfields
	if(document.getElementById('mousepos_x') && document.getElementById('mousepos_y')){
		document.getElementById('mousepos_x').value = mousepos_x;
		document.getElementById('mousepos_y').value = mousepos_y;
	}
	
	//minimap: move the newMapView
	/*
	if(document.getElementById('minimap') && document.getElementById('newMapView')){
		document.getElementById('newMapView').style.left = (document.all) ? window.event.clientX -document.getElementById('newMapView').style.left : e.clientX -document.getElementById('newMapView').style.left;
		document.getElementById('newMapView').style.top = (document.all) ? window.event.clientY -document.getElementById('newMapView').style.top : e.clientY -document.getElementById('newMapView').style.top;
	}
	*/
	
  if (isDrag=="map")
  {
	clearInterval(schleifeMoveShips);
	wasDraged=true; //on click fly to this position marker
	
	for (var j in dragDivs)
	{
		if(document.getElementById(dragDivs[j]) && dragDivs[j]!=""){
				document.getElementById(dragDivs[j]).style.left = (document.all) ? window.event.clientX + (parseInt(dragDivsData[dragDivs[j]]['x']) - mx) : e.clientX + (parseInt(dragDivsData[dragDivs[j]]['x']) - mx);
				document.getElementById(dragDivs[j]).style.top = (document.all) ? window.event.clientY + (parseInt(dragDivsData[dragDivs[j]]['y']) - my) : e.clientY + (parseInt(dragDivsData[dragDivs[j]]['y']) - my);
		}
	}
	//targetMarker to move
	document.getElementById('aiaiDiv').style.left = (document.all) ? window.event.clientX + (parseInt(aiaiDivLeftPos) - mx) : e.clientX + (parseInt(aiaiDivLeftPos) - mx);
	document.getElementById('aiaiDiv').style.top = (document.all) ? window.event.clientY + (parseInt(aiaiDivTopPos) - my) : e.clientY + (parseInt(aiaiDivTopPos) - my);
	//document.getElementById('aiaiDiv').style.top = tipy-25;

	mapPos_x = (document.all) ? tMapPos_x*1 - window.event.clientX*1 + mx*1 : tMapPos_x*1 - e.clientX*1 + mx*1;
	mapPos_y = (document.all) ? tMapPos_y*1 - window.event.clientY*1 + my*1 : tMapPos_y*1 - e.clientY*1 + my*1;
	//hintergrund bewegen
	new_background_pos_x = (document.all) ? (window.event.clientX - mx)/3 + (old_background_pos_x) : (e.clientX - mx)/3 + (old_background_pos_x);
	new_background_pos_y = (document.all) ? (window.event.clientY - my)/3 + (old_background_pos_y) : (e.clientY - my)/3 + (old_background_pos_y);
	document.getElementById("map").style.backgroundPosition = parseInt(new_background_pos_x) + 'px ' + parseInt(new_background_pos_y) +'px';
  }
  else if (isDrag!=false && document.getElementById(isDrag+'Widget'))	//only move the box if the user is dragging it
  {
	var box=document.getElementById(isDrag+'Widget');
	if (firefox){
		box.style.left = Math.min( Math.max(40,e.clientX + (tx - x)), document.body.clientWidth-widgetwidth);
		box.style.top  = Math.min( Math.max(1,e.clientY + (ty - y)), document.body.clientHeight-widgetheight);
	}
	else{		//IE
		box.style.left = Math.min( Math.max(40,event.clientX + (tx - x)), document.body.clientWidth-widgetwidth);
		box.style.top  = Math.min( Math.max(1,event.clientY + (ty - y)), document.body.clientHeight-widgetheight);
	}    
  }
}
function exchangeDisplay(id){
	if(document.getElementById(id).style.display == 'none')
	{
		maximizeDisplay(id);
	}
	else
	{
		minimizeDisplay(id);
	}
}
function maximizeDisplay(id){
	document.getElementById(id).style.display = 'block';
	if(id == 'chatContent')
	{
		startChatlog();
	}
}
function minimizeDisplay(id){
	document.getElementById(id).style.display = 'none';
	if(id == 'chatContent')
	{
		clearInterval(schleifeGetChatlog);
	}
}
function closeWidget(id){	
	var widgetParent=document.getElementById('widgetParent');
	if(document.getElementById(id))
	{
		var widgetId=document.getElementById(id);
		widgetParent.removeChild(widgetId);
	}
	if(id == 'chatWidget')
	{
		clearInterval(schleifeGetChatlog);
	}
}
function focusWidget(id){
	var i=0;
	var aktivfocusWidget = setInterval(
					function(){
						if(document.getElementById(id)){
							document.getElementById(id).style.zIndex=z;
							z++;
							clearInterval(aktivfocusWidget);
						}else{
							i++;
							if(i==30){clearInterval(aktivfocusWidget);}
						}
					}
				, 100);
}

function openWidget(query,post_query,widgetId,widgetTemplate, loadMenu)
{
	splitter = query.split("/");
	//widgetName = splitter[1];
	widgetName = widgetId;
	splitter = splitter[0].split("=");
	var moduleDir = splitter[1];
	if(document.getElementById(widgetId+'Widget')){
		sendRequest(query, post_query, widgetId+'Content');
		maximizeDisplay(widgetId+'Menu');
		maximizeDisplay(widgetId+'Content');
	}else{
		sendRequest('?module=widget/widget&createWidget='+widgetId+'&widgetName='+widgetName+'&widgetTemplate='+widgetTemplate, '&widgetposleft='+(100+(widgetpos*20%100))+'&widgetpostop='+(50+(widgetpos*20%100))+post_query, 'widgetParent');
		widgetpos++;
		sendRequest(query, post_query, widgetId+'Content');
	}
	//Load Menu always 
	/*
	sendRequest('?module='+moduleDir+'/menu', post_query, widgetId+'Menu');
	*/
	//Load Menu if nessesary 
	//alert(loadMenu);
	if(
		(document.getElementById(widgetId+'Menu') && document.getElementById(widgetId+'Menu').innerHTML == '') 
		|| !document.getElementById(widgetId+'Menu')
		|| loadMenu==true
	)
	{
		sendRequest('?module='+moduleDir+'/menu', post_query, widgetId+'Menu');
		//alert("mach ich");
	}
	
	/*else if(document.getElementById(widgetId+'Menu') && document.getElementById(widgetId+'Menu').innerHTML != ''){
		document.getElementById(widgetId+'Menu').innerHTML = '';
		sendRequest('?module='+moduleDir+'/menu', post_query, widgetId+'Menu');
	}*/
	
	focusWidget(widgetId+'Widget');
	
}

function getInnerDivs(id)
{
	var divs = new Array();
	var i=0;
	if(document.getElementById(id)){
		for( var x = 0; x < document.getElementById(id).childNodes.length; x++ ) {
			if( document.getElementById(id).childNodes[x].nodeName.toLowerCase() == 'div') {
				divs[i++] = document.getElementById(id).childNodes[x].id;
			}
		}
	}
	return divs;
}
function exchangeStyleClassActiv(div,id){
	var j=0;
	var aktivStyle = setInterval(
					function(){
						var divNode = document.getElementById(div);
						var idNode = document.getElementById(id);
						if(divNode && idNode){
							var tds = divNode.getElementsByTagName("td");
							
							for (var i = 0; i < tds.length; i++) {
								if(tds[i].attributes.name && tds[i].attributes.name.value == 'menu'){
									tds[i].attributes.activ.value = 'false';
									tds[i].className = 'menu_td';
								}
							}
							document.getElementById(id).attributes.activ.value = 'true';
							document.getElementById(id).className = 'menu_td_activ';
							clearInterval(aktivStyle);
						}else{
							j++;
							if(j==30){clearInterval(aktivStyle);}
						}
					}
				, 100);
}
function setBackgroundImage(id, image){
	document.getElementById(id).style.backgroundImage = image;
}

function setStyleClass(id, styleClass){
	if(	document.getElementById(id) && document.getElementById(id).attributes.activ	&& document.getElementById(id).attributes.activ.value == 'true')
	{
	}
	else
	{
		document.getElementById(id).className = styleClass;
	}
}
function hideDiv(id){
		if(document.getElementById(id)){
			document.getElementById(id).style.visibility = 'hidden';
		}
}
function showDiv(id){
		if(document.getElementById(id)){
			document.getElementById(id).style.visibility = 'visible';
		}
}
function displayNoneDiv(id){
		if(document.getElementById(id)){
			document.getElementById(id).style.display = 'none';
		}
}
function displayBlockDiv(id){
		if(document.getElementById(id)){
			document.getElementById(id).style.display = 'block';
		}
}
