var resstmout;
var lastmode = 0;
var infohidden = 0;

function allowNumbers(field, event)
{
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

  if ((keyCode > 47 && keyCode < 58) || (keyCode > 95 && keyCode < 106) || keyCode == 8 || keyCode == 9 || (keyCode > 34 && keyCode < 50))
    return true;

  return false;
}

function getRanking(mode)
{
	$.getJSON('./_ajax/ajax.get_ranking.php', {mode:mode} ,function(data){

        var mode1 = 0;
        var mode2 = 0;
        
		switch(mode)
		{
			case 1: $('#rankhead').html('Gesamt').attr("href","./index.php?site=stat&start="+data['rank']);
					mode1 = 2;
					mode2 = 5;
					break;
			case 2: $('#rankhead').html('Flotten').attr("href","./index.php?site=stat&type=flt&start="+data['rank']);
					mode1 = 3;
					mode2 = 1;
					break;
			case 3: $('#rankhead').html('Geb&auml;ude').attr("href","./index.php?site=stat&type=bld&start="+data['rank']);
					mode1 = 4;
					mode2 = 2;
					break;
			case 4: $('#rankhead').html('Forschung').attr("href","./index.php?site=stat&type=res&start="+data['rank']);
					mode1 = 5;
					mode2 = 3;
					break;
			case 5: $('#rankhead').html('Allianz').attr("href","./index.php?site=stat&who=ally&start="+data['rank']);
					mode1 = 1;
					mode2 = 4;
					break;
		}

		if ( data['loin'] == false )
			$('#rankhead').attr("href","#");
		
		
        $('#rankbox').html(data['html']);

        $('#rank_left').attr("href","javascript:getRanking("+mode2+");");
        $('#rank_right').attr("href","javascript:getRanking("+mode1+");");
	});
}

function showFleet(id,zl)
{
	var mode;

	var b1 = new Image(); b1.src = "./_img/plus.png";
	var b2 = new Image(); b2.src = "./_img/minus.png";

	if ( $('#'+id+'1').css("visibility") == "collapse" )
	{
		$('#id'+id).attr('src',b2.src);
		
		mode = "visible";
	}
	else
	{
		$('#id'+id).attr('src',b1.src);
		
		mode = "collapse";
	}
	
	for(i=1;i<zl;i++)
	{
		$('#'+id+i).css("visibility",mode);
	}
	
	var x = $('#bigbox').height()-$('#leftbox').height()-$('#leftbox1').height()-$('#leftbox2').height()-$('#rankbox').height()-$('#topbox').height()+80;
	
	if ( x > 0 )
	{
		x = x + $('#test').height() + 20;
		
		$('#test').css('height',x);
	}
}

function showPMFeat(id)
{
	var b1 = new Image(); b1.src = "./_img/plus.png";
	var b2 = new Image(); b2.src = "./_img/minus.png";

	if ( $('#pm'+id+"id1").css("visibility") == "collapse" )
	{
		$('#pm'+id).attr('src',b2.src);
		$('#pm'+id+"id1").css("visibility","visible");
	}
	else
	{
		$('#pm'+id).attr('src',b1.src);
		$('#pm'+id+"id1").css("visibility","collapse");
	}
}

function setUeber(u) 
{
	/*if ( $(u).html() != '' )
	{
		$.cookie("galaxika_head", $(u).html());
	}
	else
	{
		$.cookie("galaxika_head", null);
	}*/
}

function getUeber() 
{
	/*if ( $.cookie("galaxika_head") != null )
	{
		$('#content_ueberschrift').html($.cookie("galaxika_head"));
	}*/
}

function countInput(box,max)
{	
 var txtcnt = box.value.length;

 if (txtcnt > max)
 {
   var txt = box.value;
   box.value = txt.substring(0, max);

   txtcnt = box.value.length;
 }
 
 $('#cntChars').html(txtcnt);
}

function rankrotate()
{
	if ( lastmode < 5 )
	{
		lastmode++;
	}
	else
	{
		lastmode = 0;
	}

	getRanking(lastmode);
	
	resstmout = window.setTimeout("rankrotate()", 20000);
}

function getNews()
{
	$.post('./_ajax/ajax.get_news.php' ,function(data){

 	$('#newstag').html(data);
	});
}

function showNews(id)
{
	if ( infohidden == 0 )
	{
		infohidden = 1;
		
		$.post('./_ajax/ajax.show_news.php', {id:id} ,function(data){

			var html = '<div id="layer_header"></div><div id="layer_mitte"><div style="margin-left: 15px; width: 190px;">'+data+'</div></div><div id="layer_footer"></div>';
			
			if ( infohidden == 1 )
			{
				Tip(html,BGCOLOR,'transparent',BORDERCOLOR,'transparent',FONTCOLOR,'#000000');
			}
		});
	}
}

function shFields()
{
	if ( infohidden == 0 )
	{
		infohidden = 1;
		
		$.post('buildings.ajax_feldinfo.php' ,function(data){

			var html = '<div id="layer_header"></div><div id="layer_mitte"><div style="margin-left: 15px; width: 190px;">'+data+'</div></div><div id="layer_footer"></div>';
			
			if ( infohidden == 1 )
			{
				Tip(html,BGCOLOR,'transparent',BORDERCOLOR,'transparent',FONTCOLOR,'#000000');
			}
		});
	}
}

function hideInfo()
{
	infohidden = 0;
	
	UnTip();
}

function saveTarget(target,from)
{
	$.post('./_ajax/ajax.save_target.php', {target:target,frommsg:from} ,function(data){
		$('#fleet_info').html(data);
	});

	$.post('./_ajax/ajax.fleet_shortcut.php', function(data){
		$('#saved_fleet').html(data);
	});
}

function delTarget(g,s,p)
{
	$.post('./_ajax/ajax.del_target.php', {galaxy:g,system:s,planet:p} ,function(data){
		$('#fleet_info').html(data);
	});

	$.post('./_ajax/ajax.fleet_shortcut.php', function(data){
		$('#saved_fleet').html(data);
	});
}

function getflotte(g,s,p,typ) {
	if ( typ == 1 )
	{
		$('#attack' + last_planet_id).css("display","block");
		
		$('#tile_info').css('top', getTopPos(cur_tile));
	}
	
	$.post('./_ajax/ajax.fleet.php', {galaxy:g,system:s,planet:p,ajaxtyp:typ} , function(data){

		$('#fleetcentre').html(data);
	});
}

function calcInfo()
{
	$.post('./_ajax/ajax.fleet_info.php', $("#fleetform").serialize() ,function(data){

		$('#mission').html(data['optionen']);
		$('#remainingresources').html(data['storage']);
		$('#verbrauch').html(data['consumption']);
		$('#consumption_php').val(data['consumption']);
		$('#capacityALL').val(data['capacityALL']);

		var sekunden = data['fly_time'];
	    var Zeit='';

	    var d = Math.floor(sekunden / (3600*24));
	    var h = Math.floor((sekunden - (3600*24*d)) / 3600);
	    var m = Math.floor((sekunden - (3600*24*d) - (3600*h)) / 60);
	    var s = Math.floor((sekunden - (3600*24*d) - (3600*h) - (60*m)));
	    
	    if ( d > 0 )
	    {
	    	Zeit += d + ':';
	    }

	    if ( h >= 10 )
	    {
	    	Zeit += h + ':';
	    }
	    else
	    {
	    	Zeit += '0' + h + ':';
	    }
	    
	    if ( m >= 10 )
	    {
	    	Zeit += m + ':';
	    }
	    else
	    {
	    	Zeit += '0' + m + ':';
	    }
	    
	    if ( s >= 10 )
	    {
	    	Zeit += s;
	    }
	    else
	    {
	    	Zeit += '0' + s;
	    }
	    
		$('#duration').html(Zeit);
		
	},'json');
}

function sendFleet()
{
	$.post('./flotenajax.php', $("#fleetform").serialize() ,function(data){
		var mldg = data.split(';');

        var color = 'green';
        
        if ( mldg[1] == 'error')
        {
        	color = 'red';
        }

        $('#fleet_info').html('<b style="color:'+color+';">'+mldg[0]+'</b><br>');
        $('#fleetcentre').html('');
	});
}

function maxResourceA(res) 
{
	var capacity = $("#capacityALL").val();
	
	capacity -= $("#consumption_php").val();
	capacity -= $("#resource1").val();
	capacity -= $("#resource2").val();
	capacity -= $("#resource3").val();

	if (capacity > 0) 
	{
		if($("#thisresource"+res).val() >= capacity) 
		{
			$("#resource"+res).val(Math.floor(capacity));
			capacity = 0;
		} 
		else 
		{
			$("#resource"+res).val(Math.floor($("#thisresource"+res).val()));
			capacity -= $("#resource"+res).val();
		}
	} 
	else 
	{
		$("#resource"+res).val(0);
	}
	
	$("#remainingresources").html(((capacity > 0) ? numSeparator(capacity) : 0));
}

function doit(order, galaxy, system, planet, planettype, shipcount)
{
	var ship = '';

	if(order==6) {
		ship = '210';
	} 
	else if(order==8) 
	{
		ship = '209';
	}
	else if(order==9) 
	{
		ship = '208';
	}

	$.post('./flotenajax.php', {mission:order,resource1:"0",resource2:"0",resource3:"0",thisgalaxy:thisgalaxy,thissystem:thissystem,thisplanet:thisplanet,thisplanettype:thistype,galaxy:galaxy,system:system,planet:planet,speedfactor:"10",speed:"100",planettype:planettype,shiptype:ship,shipcount:shipcount} ,function(data){
		var mldg = data.split(';');

        var color = 'green';
        
        if ( mldg[1] == 'error')
        {
        	color = 'red';
        }

        $('#fleet_info').html('<b style="color:'+color+';">'+mldg[0]+'</b><br>');
	});
}

function getrocket() {
	$('#rock' + last_planet_id).css("display","block");
  }

function gorocket(galaxy, system, planet)
{
	$.post('./flotenajax.php', {mission:"5",resource1:"0",resource2:"0",resource3:"0",thisgalaxy:thisgalaxy,thissystem:thissystem,thisplanet:thisplanet,thisplanettype:thistype,galaxy:galaxy,system:system,planet:planet,speedfactor:"10",speed:"100",planettype:"1",ship503:$('#gr' + last_planet_id).val()} ,function(data){
		var mldg = data.split(';');

        var color = 'green';
        
        if ( mldg[1] == 'error')
        {
        	color = 'red';
        }
        else
        {
        	rockstop = mldg[2];
        }

    	$('#rock' + last_planet_id).css("display","none");

        $('#fleet_info').html('<b style="color:'+color+';">'+mldg[0]+'</b><br>');
	});
}

function getkolo() {
	$('#koloplname').css("display","block");
  }

function gokolo(galaxy, system, planet)
{
	var raid = ($("input:checked").length > 0) ? 1 : 0;
	
	$.post('./flotenajax.php', {mission:"9",resource1:"0",resource2:"0",resource3:"0",thisgalaxy:thisgalaxy,thissystem:thissystem,thisplanet:thisplanet,thisplanettype:thistype,galaxy:galaxy,system:system,planet:planet,speedfactor:"10",speed:"100",planettype:"1",shiptype:"208",shipcount:"1",koloname:$('#koloname').val(),raidplanet:raid} ,function(data){
		var mldg = data.split(';');

        var color = 'green';
        
        if ( mldg[1] == 'error')
        {
        	color = 'red';
        }

    	$('#koloplname').css("display","none");
        $('#fleet_info').html('<b style="color:'+color+';">'+mldg[0]+'</b><br>');
	});
}

function showRW(id)
{
	if ( $('#'+id).html() == '' )
	{
		$.post('./_ajax/ajax.get_rw.php', {rid:id} ,function(data){
			$('#'+id).html(data);
			$('#'+id).css('margin-top','15px').css('margin-bottom','15px');			
		});
	}
	else
	{
		$('#'+id).html('');
		$('#'+id).css('margin-top','0px').css('margin-bottom','0px');			
	}
}

function makeMessage(id) {
	$('#attack' + last_planet_id).css("display","block");
	
	$.post('./_ajax/ajax.send_message.php', {owner:id} , function(data){

		$('#fleetcentre').html(data);
	});
  }

function sendAjaxMessage()
{
	$.post('./_ajax/ajax.send_message.php', $("#msgform").serialize() ,function(data){
        $('#fleet_info').html('<b>'+data+'</b><br>');
        $('#fleetcentre').html('');
	});
}

function makeBuddy(id) {
	$('#attack' + last_planet_id).css("display","block");
	
	$.post('./_ajax/ajax.send_buddy.php', {owner:id} , function(data){

		$('#fleetcentre').html(data);
	});
  }

function sendAjaxBuddy()
{
	$.post('./_ajax/ajax.send_buddy.php', $("#msgform").serialize() ,function(data){
        $('#fleet_info').html('<b>'+data+'</b><br>');
        $('#fleetcentre').html('');
	});
}

function setIgnore(usr)
{
	$.post('./_ajax/ajax.set_ignore.php', {ignouser:usr} ,function(data){
		$('#fleet_info').html(data);
	});
}

function toclpb(box)
{
	box.select();
	
	if(window.clipboardData) 
	{
		window.clipboardData.setData('Text',box.value);
	}
}

function showSpioBericht()
{
	var mode;
	
	if ( $('#spiopm').html() == '+' )
	{
		$('#spiopm').html('-');
		$('#spiobericht').css("visibility","visible");
	}
	else
	{
		$('#spiopm').html('+');
		$('#spiobericht').css("visibility","collapse");
	}
}

function checkAngriff()
{
	$.post('./_ajax/ajax.check_angriff.php', function(data){
		
		if ( data == 1 )
		{
			$('#angriff').css('visibility','visible');
		}
		else
		{
			$('#angriff').css('visibility','hidden');
		}
	});
}

function Sofortbau(id,mode)
{
	$.post('./_ajax/ajax.sofortbau.php', {buildid:id} ,function(data){
		if ( data == 1 )
		{
			if ( mode == 0 )
			{
				 window.location.href = './index.php?site=buildings';  
			}
			else
			{
				 window.location.href = './index.php?site=buildings&mode=research';  
			}
		}
	});
}

function showRWSim(id)
{
	if ( $('#tr'+id).css("visibility") == 'collapse' )
	{
		$.post('./_ajax/ajax.showRWsim.php', {id:id} ,function(data){
			$('#td'+id).html(data);
			$('#tr'+id).css("visibility",'visible');
		});
	}
	else
	{
		$('#tr'+id).css("visibility",'collapse');
	}
}

function showFAQ(id)
{
	var b1 = new Image(); b1.src = "./_img/plus.png";
	var b2 = new Image(); b2.src = "./_img/minus.png";

	if ( $('#id'+id+"1").css("visibility") == "collapse" )
	{
		$('#id'+id).attr('src',b2.src);
		$('#id'+id+"1").css("visibility","visible");
	}
	else
	{
		$('#id'+id).attr('src',b1.src);
		$('#id'+id+"1").css("visibility","collapse");
	}
}

function getHRoute(g,s,p,typ) {
	$.post('./_ajax/ajax.fleet.php', {galaxy:g,system:s,planet:p,ajaxtyp:typ} , function(data){

		$('#fleetcentre').html(data);
	});
}

function toggleHRoute(g,s,p) {
	$("#galaxy").val(g);
	$("#system").val(s);
	$("#planet").val(p);

	shortInfo();
	
	if ( $('#zielwahl').css("visibility") == 'collapse' )
	{
		$('#zielwahl').css("visibility",'visible');
	}
	else
	{
		$('#zielwahl').css("visibility",'collapse');
	}
}

function getBericht()
{
	$.post('./_ajax/ajax.getBericht.php', function(data){
		$('#bbtn').html('&nbsp;Bericht angefordert');
	});
}

function searchSpy()
{
	$.post('./_ajax/ajax.searchSpy.php', function(data){
		if ( data != '' )
		{
			$('#drone').css("visibility","hidden");
		}
	});

}

function ServerUhr()
{
	var Datum = new Date()
	
	Datum.setTime(timestamp * 1000);
	timestamp++;

	var std = Datum.getHours()
	var min = Datum.getMinutes()
	var sec = Datum.getSeconds()
	Zeit = ((std < 10) ? " 0" : " ") + std
	Zeit += ((min < 10) ? ":0" : ":") + min
	Zeit += ((sec < 10) ? ":0" : ":") + sec
	
	$('#uhrzeit').html(Zeit);
	
	timerid = setTimeout("ServerUhr()",1000)
}

function scrollGalaxy(mode)
{
	var g = parseInt($('#gscroll').val());
	var s = parseInt($('#sscroll').val());
	var p = parseInt($('#pscroll').val());
		
	switch(mode)
	{
		case 0:	g = g - 1; break;
		case 1:	s = s - 1; break;
		case 2:	p = p - 1; break;
		case 3:	p = p + 1; break;
		case 4:	s = s + 1; break;
		case 5:	g = g + 1; break;
	}
	
	window.location.href="./index.php?site=galaxy&g="+g+"&s="+s+"&p="+p+"&scroll=1";
}

function showVideo(vid)
{
	info = window.open("./tutorial.html?video="+vid, "Infovideo", "width=640,height=480,location=no");
}