var currentElement = {div:null, img:null};
var currentFleet = {id:0, smid:0, fromfid:0, count:0};

function ShowMoveShipsDiv()
{
  var el = null;
  el = document.getElementById('moveShipsDiv');
  if (el != null)
  {
    el.style.visibility = "visible";
    el.style.display = "block";
    //el.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.body.scrollLeft)-parseFloat(el.clientWidth)/2)+'px';
    //el.style.top = Math.floor(Math.round((document.documentElement.offsetHeight/3)+document.body.scrollTop)-parseFloat(el.clientHeight)/2)+'px';
    
	var xy = getCenteredElement(el);
    el.style.left = xy[0] + 'px';
    el.style.top = xy[1] + 'px';
    
    var id = document.getElementById("fromfid");
    if (id) id.value = currentFleet.fromfid;
    
    id = null;
    id = document.getElementById("tofid");
    if (id) id.value = currentFleet.id;
    
    id = null;
    id = document.getElementById("smid");
    if (id) id.value = currentFleet.smid;

    id = null;
    id = document.getElementById("shipscount");
    if (id) id.value = currentFleet.count;
  } 	
}

function ShowRenameDiv(elementname, fid, sname)
{

  var el = null;
  el = document.getElementById(elementname);
  if (el != null)
  {
    el.style.visibility = "visible";
    el.style.display = "block";
    //el.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.body.scrollLeft)-parseFloat(el.clientWidth)/2)+'px';
    //el.style.top = Math.floor(Math.round((document.documentElement.offsetHeight/3)+document.body.scrollTop)-parseFloat(el.clientHeight)/2)+'px';

	var xy = getCenteredElement(el);
    el.style.left = xy[0] + 'px';
    el.style.top = xy[1] + 'px';
    
    var id = document.getElementById("fid");
    if (id) id.value = fid;
    
    id = null;
    id = document.getElementById("newname");
    if (id) id.value = sname;
  }
 
}

function ShowTacticsDiv(elementname, fid, sourceobj)
{
  var el = null;
  el = document.getElementById(elementname);
  if (el != null)
  {
    el.style.visibility = "visible";
    el.style.display = "block";
    var pos = findPos(sourceobj);
    el.style.left = pos[0]+'px';
    el.style.top = pos[1]+parseFloat(sourceobj.offsetHeight)+'px';
    
    var id = document.getElementById("tddfid");
    if (id) id.value = fid;
    
    HideDiv('formationsDiv');
    HideDiv('rangesDiv');
  }
}

function ShowRangeDiv(elementname, fid, sourceobj)
{
  var el = null;
  el = document.getElementById(elementname);
  if (el != null)
  {
    el.style.visibility = "visible";
    el.style.display = "block";
    var pos = findPos(sourceobj);
    el.style.left = pos[0]+'px';
    el.style.top = pos[1]+parseFloat(sourceobj.offsetHeight)+'px';
    
    var id = document.getElementById("rddfid");
    if (id) id.value = fid;

    HideDiv('formationsDiv');
    HideDiv('tacticsDiv');
  }
}

function ShowFormationsDiv(elementname, fid, sourceobj)
{
  var el = null;
  el = document.getElementById(elementname);
  if (el != null)
  {
    el.style.visibility = "visible";
    el.style.display = "block";
    var pos = findPos(sourceobj);
    el.style.left = pos[0]+'px';
    el.style.top = pos[1]+parseFloat(sourceobj.offsetHeight)+'px';
    
    var id = document.getElementById("fddfid");
    if (id) id.value = fid;

    HideDiv('tacticsDiv');
    HideDiv('rangesDiv');
  }
}

function ShowCommandsDiv(elementname, wpid, fid, sourceobj)
{
  var el = null;
  el = document.getElementById(elementname);
  if (el != null)
  {
    el.style.visibility = "visible";
    el.style.display = "block";
    el.style.zIndex = 8000;
    
    var pos = findPos(sourceobj);
    el.style.left = pos[0]+'px';
    el.style.top = pos[1]+parseFloat(sourceobj.offsetHeight)+'px';
    
    var id = document.getElementById("pathcmd");
    if (id) id.value = wpid;

    var id = document.getElementById("pathfid");
    if (id) id.value = fid;
  }
}

function ShowPlanetsDiv(elementname, wpid, fid, sourceobj)
{
  var el = null;
  el = document.getElementById(elementname);
  if (el != null)
  {
    el.style.visibility = "visible";
    el.style.display = "block";
    el.style.zIndex = 8000;
    var pos = findPos(sourceobj);

    el.style.left = pos[0]+'px';
    el.style.top = pos[1]+parseFloat(sourceobj.offsetHeight)+'px';
    
    var id = document.getElementById("pathplntwpid");
    if (id) id.value = wpid;

    var id = document.getElementById("pathplntfid");
    if (id) id.value = fid;

    DrawReloadHTML('pathplanetslist');
    xajax_FleetsModule_GetPlanetsList('pathplanetslist', wpid);
  }
}

function SetHelpAlly(val, fid)
{
	if (isNaN(val)) val = 0;
	if (val > 1) val = 1;
	var el = document.getElementById("ha"+fid);
	if (el)
	{
		el.src = "/img/check_"+val+".png";
	}
}

function SetTactics(tactics, fid)
{
	var el = document.getElementById("tt"+fid);
	if (el)
	{
		el.innerHTML = tactics;
	}
}

function SetRange(range, fid)
{
	var el = document.getElementById("rng"+fid);
	if (el)
	{
		el.innerHTML = range;
	}
}

function SetFormation(frmt, fid)
{
	var el = document.getElementById("frm"+fid);
	if (el)
	{
		el.innerHTML = frmt;
	}
}

function SetPathCommand(cmd, wpid)
{
	var el = document.getElementById("pathcmd"+wpid);
	if (el)
	{
		el.innerHTML = cmd;
	}
}

function OpenCloseFleetDiv(senderID, imgID, imgOpen, imgClose, nearFleetsID, fid, sid)
{
	var el = null; 
	if (currentElement.div != null && currentElement.div.id != senderID)
	{
		if (currentElement.div.style.visibility != "hidden")
		{
			OpenCloseDiv(currentElement.div.id);
			if (currentElement.img != null)
			{
				el = document.getElementById(currentElement.img);
				if (el)
					SetOpenCloseImage(el,currentElement.div.id,imgOpen,imgClose);
				el = null;
			}
		}
	}
	
    HideDiv('tacticsDiv');
    HideDiv('rangesDiv');
    HideDiv('formationsDiv');
	OpenCloseDiv(senderID); 
	el = document.getElementById(imgID);
	if (el)
		SetOpenCloseImage(el,senderID,imgOpen,imgClose);

	if (document.getElementById(senderID))
	{
		if (document.getElementById(senderID).style.visibility == "visible")
		{
			DrawReloadHTML(nearFleetsID);
			xajax_FleetsModule_GetNearFleets(nearFleetsID, fid, sid);
		}
	}
	
	currentElement.div = document.getElementById(senderID);
	currentElement.img = imgID;
}

function SetCurrentSystem(sid, fid)
{
	var el = document.getElementById("newfleetsid");
	if (el)
	{
		el.value = sid;
	}
	el = document.getElementById("newfleetid");
	if (el)
	{
		el.value = fid;
	}
}

function SetCurrentFleet(fid)
{
	currentFleet.id = fid;
//	document.getElementById("tmp").innerHTML = fid;	
}

function startShipsDragging(e, smname, smid, fromfid, count)
{
	var continer = document.getElementById('fleetsContiner');
	if (continer)
	{
		continer.innerHTML = smname;
		continer.style.visibility = "visible";
		if (e == null) { e = window.event;}
		xcoor = GetMousePos(e)[0];
		ycoor = GetMousePos(e)[1];
		continer.style.left = xcoor + 3 + "px";
		continer.style.top = ycoor + "px";
		pleft = parseInt(continer.style.left);
		ptop = parseInt(continer.style.top);
		//pleft = 0;
		//ptop = 0;
			
		document.onmousemove = draggingShipsElement;
		document.onmouseup = stopShipsDragging;
		document.onmousedown = disabletext; 
		
		currentFleet.smid = smid;
		currentFleet.fromfid = fromfid;
		currentFleet.count = count;
	}
	
	return false;
}

function draggingShipsElement(e)
{
	if (e == null) { e = window.event; }
	var sender = (window.event) ? e.srcElement : e.target;
	
	var continer = document.getElementById('fleetsContiner');
	if (continer)
	{
		mpos = GetMousePos(e);
		continer.style.left=pleft+mpos[0]-xcoor+"px";
		continer.style.top=ptop+mpos[1]-ycoor+"px";
	}
	
	return false;
}

function stopShipsDragging(e)
{
	if (e == null) { e = window.event; }
	var sender = (window.event) ? e.srcElement : e.target;
	
	document.onmousemove = null;
	document.onmouseup = null;
	document.onmousedown = null;

	dropShips();
		 
	return false;
}

function dropShips()
{
	var continer = document.getElementById('fleetsContiner');
	if (continer)
	{
		continer.style.visibility = "hidden";
	}
	if (currentFleet.id > 0)
	{
		ShowMoveShipsDiv();
	}
}

function SetPathFleet(elname, elvalue)
{
	var el = document.getElementById(elname);
	//alert("name|"+elname + "|value|" + elvalue+"|"+el.value);
	if (el)
	{
		el.value = elvalue;
	}
	//alert("name|"+elname + "|value|" + elvalue+"|"+el.value);
	
	return false;	
}

function SetWayPoint(elname, elvalue)
{
	var el = document.getElementById(elname);
	if (el)
	{
		el.value = elvalue;        
             
        xajax_FleetsModule_AddWaypoint(
            '#fleetPath_dialog','fleetPath_dialog_content',        
            'addWaypointDivResult',        
            xajax.getFormValues('waypointForm',false)
        );
                
        ClearDiv('addWaypointDivResult');		
        $("#addWaypointDiv").dialog("close");        
	}
	
	return false;
}

