var currentColDropZone = {pid:0, dtype:0}
var currentDragOrigin = {pid:0, dtype:0}
var transferCount = 0;
var currentPlanet = {pid:0, obj:null, cname:''};
var prefixDIV   = 'd';
var refreshColonyInfo = true;
var refreshEmpireInfo = true; 
var refreshUpdateColonyRowInfo = true;
var currentMenu;	

//------ SelectAllCheckbox ---
 var selVal = true;
 function SelectAllCheckbox(id,txtCheck,txtUncheck,search)
 {   
   $("INPUT[type='checkbox']").attr('checked',selVal);
   AddRemoveCalss(search, selVal);
   if (selVal)
   {      
      selVal = false;
      $("#"+id).attr("src","/img/check_1.png");
      $("#"+id).attr("title",txtUncheck);                  
   }
   else
   { 
      selVal = true;
      $("#"+id).attr("src","/img/check_0.png");
      $("#"+id).attr("title",txtCheck);      
   }   
 } 
   
 function AddRemoveCalss(search, selType)
 {
   if (selType)  
   {      
     $(search).removeClass('borderblack');
     $(search).addClass('bordergrey');
   }   
   else  
   {  
     $(search).removeClass('bordergrey');
     $(search).addClass('borderblack');
   }     
 }
 
 function SelectOnCheckbox(id)
 { 
   if ( $(id).is(':checked') )  
     AddRemoveCalss('#col_'+id.value, true);     
   else 
     AddRemoveCalss('#col_'+id.value, false);
 } 
 
 function ResetVal () {selVal = true;}
//---end--- SelectAllCheckbox ---

function ChangeColonyInfoHtml(id, colInfo)
{  
  $('.food'+id).html(colInfo.food);
  $('.fooduse'+id).html(colInfo.fooduse);
  $('.production'+id).html(colInfo.production);
  $('.pollution'+id).html(colInfo.pollution);
  $('.research'+id).html(colInfo.research);
  $('.income'+id).html(colInfo.income);
  $('.maintenance'+id).html(colInfo.maintenance);
}

function SetCurrColDropZone(pid, dtype)
{
	currentColDropZone.pid = pid;
	currentColDropZone.dtype = dtype; 
//	document.getElementById("tmp").innerHTML = currentColDropZone.pid+':'+currentColDropZone.dtype+
//	'--'+currentDragOrigin.pid+':'+currentDragOrigin.dtype;
}

function SetDragOrigin(pid, dtype)
{    
	currentDragOrigin.pid = pid;
	currentDragOrigin.dtype = dtype; 
//	document.getElementById("tmp").innerHTML = currentColDropZone.pid+':'+currentColDropZone.dtype+
//	'--'+currentDragOrigin.pid+':'+currentDragOrigin.dtype;
}

function StartPopDragging(e, draggedID, divID, refreshColony, refreshEmpire, refreshUpdateColonyRow, current_menu)
{
    if (divID != '') prefixDIV = divID;
    refreshColonyInfo = refreshColony;
    refreshEmpireInfo = refreshEmpire;
    refreshUpdateColonyRowInfo = refreshUpdateColonyRow;
    currentMenu = current_menu;
         
	var continer = document.getElementById('dragContiner');
	if (continer)
	{
		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";
		continer.style.zIndex  = 7500;
		pleft = parseInt(continer.style.left);
		ptop = parseInt(continer.style.top);
			
		document.onmousemove = DraggingPopContiner;
		document.onmouseup = StopPopDragging;
		document.onmousedown = disabletext; 
		
		MovePopToContiner(draggedID);
	}
	return false;
}

function DraggingPopContiner(e)
{
	if (e == null) { e = window.event; }
	var sender = (window.event) ? e.srcElement : e.target;
	
	var continer = document.getElementById('dragContiner');
	if (continer)
	{
		mpos = GetMousePos(e);
		continer.style.left=pleft+mpos[0]-xcoor+"px";
		continer.style.top=ptop+mpos[1]-ycoor+"px";
	}
	
	return false;
}

function StopPopDragging(e)
{
	if (e == null) { e = window.event; }
	var sender = (window.event) ? e.srcElement : e.target;
	
	document.onmousemove = null;
	document.onmouseup = null;
	document.onmousedown = null;

	DropPop();
		 
	return false;
}

function DropPop()
{
	var continer = document.getElementById('dragContiner');
	var el = null;

	el = document.getElementById(prefixDIV+currentColDropZone.pid+'_'+currentColDropZone.dtype);
	if (el == null || (prefixDIV+currentColDropZone.pid+'_'+currentColDropZone.dtype) == 
		(prefixDIV+currentDragOrigin.pid+'_'+currentDragOrigin.dtype))
	{
		return CancelPopTransfer();
	}
	if (el)
	{
		var tmpColDropZone = {pid:0, dtype:0};
		tmpColDropZone.pid = currentColDropZone.pid;
		tmpColDropZone.dtype = currentColDropZone.dtype;
		if (currentDragOrigin.pid != currentColDropZone.pid)
		{
            return CancelPopTransfer();            
		}
		else
		{
			currentColDropZone.pid = tmpColDropZone.pid;
			currentColDropZone.dtype = tmpColDropZone.dtype;
			var img = document.createElement("IMG");
			img.src = "/img/loading.gif";
			img.className = "lpadding10";
			img.id = "loadingimg";
		
			continer.appendChild(img);
			xajax_ColoniesModule_TransferPopulation(
				'errorDialog', 
				currentDragOrigin.pid, 
				currentColDropZone.pid, 
				transferCount, 
				currentDragOrigin.dtype, 
				currentColDropZone.dtype, 
				prefixDIV+currentColDropZone.pid+'_'+currentColDropZone.dtype);
			
			return false;
		}
	}
}

function MovePopToContiner(draggedID)
{
	var continer = document.getElementById('dragContiner');
	var dragged = document.getElementById(draggedID);
	
	var owner = dragged.parentNode;
	if (owner.nodeType == 1 && owner.tagName == "DIV")
	{
		SetDragOrigin(GetDraggedInfo(owner.id)[0],GetDraggedInfo(owner.id)[1]);
	}
		
	var from = document.getElementById(prefixDIV+currentDragOrigin.pid+'_'+currentDragOrigin.dtype);
	if (from && continer && dragged)
	{
		var startMove = false;
		var i = 0;
		transferCount = 0;
		while (i < from.childNodes.length)
		{
			if (from.childNodes[i] == dragged)
			{
				startMove = true;
			}	
			if (startMove && from.childNodes[i].nodeType == 1 && from.childNodes[i].tagName == "DIV")
			{
				var el = from.childNodes[i];
				from.removeChild(el);
				continer.appendChild(el);
				el.clasName = "popgrey";
				transferCount++;
			}
			else
			{
				i++;
			}
		} 
	}
	return false;	
}

function MovePopFromContiner(elid, dtype)
{
	var continer = document.getElementById('dragContiner');
	var to = document.getElementById(elid);
	
	var i = 0;
	while (i < continer.childNodes.length)
	{
		if (continer.childNodes[i].nodeType == 1 && continer.childNodes[i].tagName == "DIV")
		{
			var el = continer.childNodes[i];
			continer.removeChild(el);
			to.appendChild(el);
			if (dtype == 1)
				el.className = "farmer";
			if (dtype == 2)
				el.className = "worker";
			if (dtype == 3)
				el.className = "researcher";
		}
		else
		{
			i++;
		}
	} 
	return false;	
}

function CancelPopTransfer()
{
	transferCount = 0;
	var continer = document.getElementById('dragContiner');
	var img = document.getElementById("loadingimg");
	if (img) continer.removeChild(img);
	MovePopFromContiner(prefixDIV+currentDragOrigin.pid+'_'+currentDragOrigin.dtype, currentDragOrigin.dtype);
	continer.style.visibility = "hidden";
	continer.style.zIndex = 1;	
	continer.innerHTML = "";	 
	return false;	
}

function SameSystemPopTransfer(elid, droptype)
{
	transferCount = 0;

	var continer = document.getElementById('dragContiner');
	var img = document.getElementById("loadingimg");
	if (img) continer.removeChild(img);

	MovePopFromContiner(elid, droptype);
	
	continer.style.visibility = "hidden";
	continer.innerHTML = "";
	
	if (refreshEmpireInfo) 
	    xajax_EmpireComponent_GetEmpireInfo('empire_info');
	if (refreshUpdateColonyRowInfo)	
	    xajax_ColoniesModule_UpdateColonyRowInfo('', '', '', 'b'+currentDragOrigin.pid, currentDragOrigin.pid, currentMenu);	
	if (refreshColonyInfo)
	{		   
	    xajax_ColoniesModule_GetColonyInfo('colonyInfo', GetDraggedInfo(elid)[0] );	    
	    xajax_ColoniesModule_GetColonyInfo2( GetDraggedInfo(elid)[0] );
	    xajax_EmpireScreenModule_GetColonyQueue('colonyQueueInfo', GetDraggedInfo(elid)[0]);
	}
	 
	img = null;
	img = document.getElementById('i'+GetDraggedInfo(elid)[0]);
	if (img)
	  SelectPlanet(GetDraggedInfo(elid)[0], img, 'tmiddle rpadding5 cpointer icon');

	return false;
}

function SystemsPopTransfer(elid, droptype)
{  
	transferCount = 0;

	var continer = document.getElementById('dragContiner');
	var img = document.getElementById("loadingimg");
	if (img) continer.removeChild(img);

	continer.style.visibility = "hidden";
	continer.innerHTML = "";
	xajax_ColoniesModule_GetColonyInfo(
		'colonyInfo',
		GetDraggedInfo(elid)[0]);
	xajax_EmpireComponent_GetEmpireInfo('empire_info');
		
	UpdatePopulationBlocks(GetDraggedInfo(elid)[0],'');
	UpdatePopulationBlocks(currentDragOrigin.pid,'');
	  		
	img = null;
	img = document.getElementById('i'+GetDraggedInfo(elid)[0]);
	if (img)
	  SelectPlanet(GetDraggedInfo(elid)[0], img, 'tmiddle rpadding5 cpointer icon');

	return false;
}

function GetDraggedInfo(elid)
{
	var sp = elid.substr(1).split("_"); 
	return sp;	
}

function ShowTransferInfo(elid, srcelid)
{
	var el = document.getElementById(elid);
	var src = document.getElementById(srcelid);
	if (el && src)
	{
		var mp = findPos(src);
		el.style.left = mp[0]+"px";
		el.style.top = mp[1]+src.offsetHeight+"px";
    	el.style.display = "block";
	    el.style.visibility = "visible";
	}
}

function SelectPlanet(pid, obj, cname)
{
	if (currentPlanet.obj != null)
	{
		currentPlanet.obj.className = currentPlanet.cname;
		var sp = document.getElementById('i'+currentPlanet.pid+'span');
		if (sp)
		{
			sp.className = 'tleft size12 arial yellow1';
		}
	}
	currentPlanet.pid = pid;
	currentPlanet.obj = obj;
	currentPlanet.cname = obj.className;
	obj.className = cname;
	
	var sp = document.getElementById('i'+pid+'span');
	if (sp)
	{
		sp.className = 'tleft size12 arial yellow3';
	}
}

function UpdatePopulationBlocks(pid,current_menu)
{    
	xajax_ColoniesModule_UpdateColonyRowInfo(
		'd'+pid+'_1',
		'd'+pid+'_2',
		'd'+pid+'_3',
		'b'+pid,
		pid,
		current_menu);
}

function SelectFirstPlanet(pid)
{
	if (currentPlanet.obj == null)
	{
		var el = document.getElementById('i'+pid);
		if (el)
		{
			SelectPlanet(pid, el, 'tmiddle rpadding5 cpointer icon');
			xajax_ColoniesModule_GetColonyInfo('colonyInfo',pid);
		}
	}
}

function HideAddedBuilding(building)
{
	var el = document.getElementById(building);
	if (el)
	{
		el.style.visibility = "hidden";
		el.style.display = "none";
	}
}

function ShowAbandonColony(elname, planet, qid, text, ok, cancel)
{	
    var buttons = {
    		cancel : function() { $(this).dialog("close"); $("#main_confirm_dialog_content").html(""); }, 
    		ok : function() { xajax_EmpireScreenModule_AbandonColony(elname, planet, qid); $(this).dialog("close"); } 
    };
    $("#main_confirm_dialog").dialog("option", "buttons", buttons);    
	$("#main_confirm_dialog_content").html("<span class='size14 arial white'>"+text+"<br></span>");
	$("#main_confirm_dialog").dialog("open").parents(".ui-dialog:eq(0)").wrap("<div class='confirmScope'></div>");
}

function ShowCancelAbandonColony(elname, planet, qid, text, ok, cancel)
{	
    var buttons = {
    		cancel : function() { $(this).dialog("close"); $("#main_confirm_dialog_content").html(""); }, 
    		ok : function() { xajax_EmpireScreenModule_CancelAbandon(elname, planet, qid); $(this).dialog("close"); } 
    };
    $("#main_confirm_dialog").dialog("option", "buttons", buttons);    
	$("#main_confirm_dialog_content").html("<span class='size14 arial white'>"+text+"<br></span>");
	$("#main_confirm_dialog").dialog("open").parents(".ui-dialog:eq(0)").wrap("<div class='confirmScope'></div>");
}

//Alliance

function ShowApply(elname, errelementname, allid, empinfo, text, ok, cancel)
{	
    var buttons = {
    		cancel : function() { $(this).dialog("close"); $("#main_confirm_dialog_content").html(""); }, 
    		ok : function() {xajax_AlliancesModule_Apply(elname, errelementname, allid, empinfo); $(this).dialog("close"); } 
    };
    $("#main_confirm_dialog").dialog("option", "buttons", buttons);    
	$("#main_confirm_dialog_content").html("<span class='size14 arial white'>"+text+"<br></span>");
	$("#main_confirm_dialog").dialog("open").parents(".ui-dialog:eq(0)").wrap("<div class='confirmScope'></div>");
}

function DissolveAlliance( elname , text, ok, cancel)
{	
    var buttons = {
    		cancel : function() { $(this).dialog("close"); $("#main_confirm_dialog_content").html(""); }, 
    		ok : function() {xajax_AlliancesModule_DissolveAlliance(elname); $(this).dialog("close"); } 
    };
    $("#main_confirm_dialog").dialog("option", "buttons", buttons);    
	$("#main_confirm_dialog_content").html("<span class='size14 arial white'>"+text+"<br></span>");
	$("#main_confirm_dialog").dialog("open").parents(".ui-dialog:eq(0)").wrap("<div class='confirmScope'></div>");
}

function LeaveAlliance( elname , text, ok, cancel)
{	
    var buttons = {
    		cancel : function() { $(this).dialog("close"); $("#main_confirm_dialog_content").html(""); }, 
    		ok : function() {xajax_AlliancesModule_LeaveAlliance(elname); $(this).dialog("close"); } 
    };
    $("#main_confirm_dialog").dialog("option", "buttons", buttons);    
	$("#main_confirm_dialog_content").html("<span class='size14 arial white'>"+text+"<br></span>");
	$("#main_confirm_dialog").dialog("open").parents(".ui-dialog:eq(0)").wrap("<div class='confirmScope'></div>");
}

function ExpelMember( elname , expempireid , text, ok, cancel)
{	
    var buttons = {
    		cancel : function() { $(this).dialog("close"); $("#main_confirm_dialog_content").html(""); }, 
    		ok : function() { xajax_AlliancesModule_ExpelMember( elname , expempireid ); $(this).dialog("close"); } 
    };
    $("#main_confirm_dialog").dialog("option", "buttons", buttons);    
	$("#main_confirm_dialog_content").html("<span class='size14 arial white'>"+text+"<br></span>");
	$("#main_confirm_dialog").dialog("open").parents(".ui-dialog:eq(0)").wrap("<div class='confirmScope'></div>");
}

function MapSendToColonize( elname , planetid , text, ok, cancel)
{	
    var buttons = {
    		cancel : function() { $(this).dialog("close"); $("#main_confirm_dialog_content").html(""); }, 
    		ok : function() { xajax_EmpireScreenModule_FMapSendToColonize( elname , planetid ); $(this).dialog("close"); } 
    };
    $("#main_confirm_dialog").dialog("option", "buttons", buttons);    
	$("#main_confirm_dialog_content").html("<span class='size14 arial white'>"+text+"<br></span>");
	$("#main_confirm_dialog").dialog("open").parents(".ui-dialog:eq(0)").wrap("<div class='confirmScope'></div>");
}
