/*****************************************************************************************************************/
jQuery(document).ready(function(){
	bindTitle();
	zeitAnzeigen();
	
	jQuery(".accordion").accordion({ autoHeight: false });
	jQuery( ".resizable" ).resizable({ containment: 'parent' });

	if( jQuery('#ajaxMap') )
	{
		doSystemMapMouseActions();
		setInterval('refreshSystemView()', (1000*60));	
	}
	
	if( jQuery.countdown )
	{
		startCountdown();
	}
					
	if( jQuery.cookie && jQuery.cookie("chatstatus") == 'close' )
	{
		jQuery( "#chatOutput" ).animate({
			height: 'toggle'
		  }, 0, function() {
			// Animation complete.
		  });
	}
	else if( jQuery.cookie && jQuery.cookie("chatstatus") != 'close')
	{
		setInterval('getChatMessages( "start", 0 )', (1000*7));	
	}
	
	jQuery( "#chatMinimizer" ).click( function(){ 
		toggleChatWidget();
	});
});
function startCountdown()
{
	jQuery('.countdown').each( function(){
		var tempTime = jQuery(this).attr('rel');
		jQuery(this).countdown({
			until: +tempTime,
			//until: +5,
			compact: true,
			onExpiry: function(){
				setTimeout(function(){
					location.reload();
				}, 1000);					
			}
		});
	});
	jQuery('.progressbar').each( function(){
		var progressBarDiv = this;
		var leftTime = jQuery(progressBarDiv).parent().children('.countdown').attr('rel');
		var durationTime = jQuery(progressBarDiv).attr('rel');
		var pastTime = durationTime - leftTime;
		var pastPercent = 100 / durationTime * pastTime;
		jQuery(progressBarDiv).progressbar( { "value" : pastPercent });
		setInterval( function()
						{
							leftTime--;
							updateProgressBar( jQuery(progressBarDiv), leftTime, durationTime )  
						}, 1000 );					
	});
}
function updateProgressBar( prograssBarDiv, leftTime, durationTime )
{
	var pastTime = durationTime - leftTime;
	var pastPercent = 100 / durationTime * pastTime;
	jQuery(prograssBarDiv).progressbar( 'option', 'value', pastPercent );
}
function bindTitle()
{
	jQuery('*[title]').tooltip({
		delay: 0,
		showURL: false,
		track: true,
		showBody: " - "//, 
		//fade: 250 
	});
	
}
function doSystemMapMouseActions()
{
	bindTitle();
	jQuery('#systemMap').mousemove(function(e){
		var x = e.pageX - this.offsetLeft + parseInt(jQuery('#systemX').html()) *600-600;
		var y = e.pageY - this.offsetTop + parseInt(jQuery('#systemY').html()) *450-450;
		
		jQuery('#mapMouseCoordinatesX').html(x);
		jQuery('#mapMouseCoordinatesY').html(y);
		jQuery('#new_target_x').html(x);
		jQuery('#new_target_y').html(y);
	}); 
	jQuery('#systemMap').click(function(e){
		var x = e.pageX - this.offsetLeft + parseInt(jQuery('#systemX').html()) *600-600;
		var y = e.pageY - this.offsetTop + parseInt(jQuery('#systemY').html()) *450-450;
		
		jQuery('#new_target_x_input').val(x);
		jQuery('#new_target_y_input').val(y);
	});
	startMoveConvois();
}
function startMoveConvois()
{
	jQuery('.flying').each( function() {
			var jsonObj = jQuery.parseJSON( jQuery(this).attr('rel') );
			jQuery(this).animate({
					'top': jsonObj.new_y,
					'left': jsonObj.new_x
				  }, jsonObj.m , function() {
					// Animation complete.
				  });
	});
}
function getObjectInfo( objectType, objectId )
{
	jQuery('#objectInfo').empty();
	getAjaxContent('index.php?module=map&option=object', 'objectType='+ objectType +'&objectId='+ objectId +'&ajax=true','#objectInfo', 'bindTitle(), startCountdown();');
}
function setNewConvoiTarget( objectType, objectId, objectCommand, coordinate_x, coordinate_y )
{
	jQuery('#objectInfo').empty();
	getAjaxContent('index.php?module=map&option=object', 'objectType='+ objectType +'&objectId='+ objectId +'&objectCommand='+ objectCommand +'&coordinate_x='+ coordinate_x +'&coordinate_y='+ coordinate_y +'&ajax=true','#objectInfo', 'refreshSystemView()');
}
function convoiDockIn( objectType, objectId, objectCommand )
{
	jQuery('#objectInfo').empty();
	getAjaxContent('index.php?module=map&option=object', 'objectType='+ objectType +'&objectId='+ objectId +'&objectCommand='+ objectCommand +'&ajax=true','#objectInfo', 'refreshSystemView()');
}
function convoiColonize( objectType, objectId, objectCommand, freePlanetId )
{
	jQuery('#objectInfo').empty();
	getAjaxContent('index.php?module=map&option=object', 'objectType='+ objectType +'&objectId='+ objectId +'&objectCommand='+ objectCommand +'&enemyPlanetId='+ freePlanetId +'&ajax=true','#objectInfo', 'refreshSystemView()');
}
function raidPlanet( objectType, objectId, objectCommand, enemyPlanetId )
{
	jQuery('#objectInfo').empty();
	getAjaxContent('index.php?module=map&option=object', 'objectType='+ objectType +'&objectId='+ objectId +'&objectCommand='+ objectCommand +'&enemyPlanetId='+ enemyPlanetId +'&ajax=true','#objectInfo', 'refreshSystemView()');
}
function fightConvoi( objectType, objectId, objectCommand, enemyConvoiId )
{
	jQuery('#objectInfo').empty();
	getAjaxContent('index.php?module=map&option=object', 'objectType='+ objectType +'&objectId='+ objectId +'&objectCommand='+ objectCommand +'&enemyConvoiId='+ enemyConvoiId +'&ajax=true','#objectInfo', 'refreshSystemView()');
}
function refreshSystemView()
{
	if( jQuery('#systemX').html() && jQuery('#systemY').html() )
	{
		getAjaxContent('index.php?module=map&option=system&sysx='+ parseInt(jQuery('#systemX').html()) +'&sysy='+ parseInt(jQuery('#systemY').html()) +'', 'ajax=true','#ajaxMap', 'doSystemMapMouseActions()');
	}
}
function showSystem( sysx, sysy )
{
	getAjaxContent('index.php?module=map&option=system&sysx='+ sysx +'&sysy='+ sysy +'', 'ajax=true','#ajaxMap', 'doSystemMapMouseActions()');
}
function showGalaxy( galax, galay )
{
	getAjaxContent('index.php?module=map&option=galaxy&galax='+ galax +'&galay='+ galay +'', 'ajax=true','#ajaxMap', 'doSystemMapMouseActions()');
}
function showQuadrant( quadx, quady )
{
	getAjaxContent('index.php?module=map&option=quadrant&quadx='+ quadx +'&quady='+ quady +'', 'ajax=true','#ajaxMap', 'doSystemMapMouseActions()');
}
function toggleChatWidget( )
{
	jQuery( "#chatOutput" ).animate({
		height: 'toggle'
	  }, 500, function() {
		// Animation complete.
		if( jQuery.cookie("chatstatus") == 'close' )
		{
			jQuery.cookie("chatstatus", "open");
		}
		else if( !jQuery.cookie("chatstatus") || jQuery.cookie("chatstatus") == 'open' )
		{
			jQuery.cookie("chatstatus", "close");
		}
	  });
}
function sendChatMessage( gameModule, gameId, chatMessage )
{
	if( jQuery.cookie("chatstatus") == 'close' )
	{
		toggleChatWidget();
	}
	getAjaxContent('index.php?module=chat&option=chat', 'gameModule='+ gameModule +'&sub='+ gameId +'&chatMessage=' + chatMessage + '&submit=submit&ajax=true','#chatOutput', '');
}
function getChatMessages( gameModule, gameId )
{
	if( jQuery.cookie("chatstatus") != 'close' )
	{
		getAjaxContent('index.php?module=chat&option=chat', 'gameModule='+ gameModule +'&sub='+ gameId +'&ajax=true','#chatOutput', '');
	}
}
function getAjaxContent(url, data, container, doAfterSuccess)
{
	jQuery.ajax({
			type: 'POST',
			url: url,
			data: data,
			success: function(output) {
				jQuery( container ).html( output );
				if( doAfterSuccess != '' )
				{
					eval(doAfterSuccess);
				}
			}
	});
}

execJS = function (div_id)
{
	var div_id = div_id;
	var j=0;
	clearInterval(schleife);	
	var schleife = setInterval(
					function()
					{
						var node = document.getElementById(div_id);	
						if(node  &&  node != null)
						{
							// Element auf Javascript überprüfen, und falls nötig ausführen 
							var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
							var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
							var bMoz = (navigator.appName == 'Netscape');
							var st = node.getElementsByTagName('script');//hier muss die getInnerDiv funktion rein!!!
							var strExec;
						
							for(var i=0;i<st.length; i++)
							{
							  if (bSaf) {
								strExec = st[i].innerHTML;
							  } else if (bOpera) {
								strExec = st[i].text;
							  } else if (bMoz) {
								strExec = st[i].textContent;
							  } else {
								strExec = st[i].text;
							  }
							  try
							  {
								eval(strExec);
							  }
							  catch(e)
							  {
								alert(e);
								alert(strExec);
							  }
							}
							clearInterval(schleife);
						}else{
							j++;
							if(j>=30){clearInterval(schleife);}
							//if(j>=0 && j<5){		alert(j); }
						}
					}
				, 100);
}

function runde(x, n) {
	if (n < 1 || n > 14) return false;
	var e = Math.pow(10, n);
	var k = (Math.round(x * e) / e).toString();
	if (k.indexOf('.') == -1) k += '.';
	k += e.toString().substring(1);
	return k.substring(0, k.indexOf('.') + n+1);
}

function zeitAnzeigen()
{
    var Stunden = Serverzeit.getHours();
    var Minuten = Serverzeit.getMinutes();
    var Sekunden = Serverzeit.getSeconds();
    Serverzeit.setSeconds(Sekunden+1);

    if(Stunden <= 9)
    {
        Stunden = "0" + Stunden;
    }

    if(Minuten <= 9)
    {
        Minuten = "0" + Minuten;
    }

    if(Sekunden <= 9)
    {
        Sekunden = "0" + Sekunden;
    }

    Uhrzeitanzeige = Stunden + ":" + Minuten + ":" + Sekunden;

	jQuery( '#uhr' ).html( Uhrzeitanzeige );

    setTimeout("zeitAnzeigen()", 1000);
}

