/**
 * setzt die Reiter elemente
 * 
 * @param string Reiter
 * @return array
 */
function setReiterElemente(Reiter) {
	var reiterElemente = new Array();
	alleDivs = jQuery('li');
	jQuery.each(alleDivs, function (key, aktDiv) {
		if (! aktDiv.id) return false;
		if(aktDiv.id.match(Reiter+'_')){
			saveit = aktDiv.id.replace(Reiter+'_', '');
			reiterElemente[reiterElemente.length] = saveit;
		}
	});
	return reiterElemente;
}

function activatePremium(intPremiumFunction) {
	jQuery.ajax({
		type: "POST",
		url: "/incs/ajax/premium.php",
		data: {'type': intPremiumFunction},
		success: function(msg){
			window.location = "/premium.php?reiter=1";
		}
	});
}

function change_reiter(reiter_name, elternelement) {
	if (!elternelement) {
		var Container = 'container';
		var Reiter = 'reiter';
	}else {
		var myElemente = elternelement.split('|');
		var Container = myElemente[0];
		var Reiter = myElemente[1];
	}
	var Elemente = setReiterElemente(Reiter);
	jQuery.each(Elemente, function (key, Element) {
		if ($j('#'+Container+'_'+Element+':visible').length>0) {
			$j('#'+Container+'_'+Element).hide('fast');
			$j('#'+Reiter+'_'+Element).attr('class', 'reiter_inaktiv');
		}
	});
	if ($j('#'+Container+'_'+reiter_name).length>0) {
		$j('#'+Container+'_'+reiter_name).show('fast');
		$j('#'+Reiter+'_'+reiter_name).attr('class', 'reiter_aktiv');
	}
}


function getRestzeit(seconds) {
	// Stunden
	stunden = Math.floor(seconds / 3600);
	seconds = seconds - (stunden * 3600);
	// Minuten
	minuten = Math.floor(seconds / 60);
	seconds = seconds - (minuten * 60);
	// Sekunden
	sekunden = seconds;
	return ({'h' : stunden,
			'm' : minuten,
			's' : sekunden});
}

function intToString(intEingabe) {
	if (intEingabe < 10) return '0'+intEingabe;
	return intEingabe;
}

function getTimer(seconds) {
	myTimer = getRestzeit(seconds);
	return intToString(myTimer.h)+':'+intToString(myTimer.m)+':'+intToString(myTimer.s);
}

timer = {};

function printTimer(seconds, containerID) {
	// Zun�chst sofort ausf�hren
	$j('#'+containerID).html(getTimer(seconds));
	seconds--;
	// Dann im Intervall
	if (typeof timer[containerID] != 'undefined' && timer[containerID] != null) {
		
		clearInterval(timer[containerID]);
		timer[containerID] = null;
	}
	timer[containerID] = setInterval(function () {
		if (seconds <= 0) {
			clearTimeout(timer[containerID]);
			if(typeof timer[containerID.replace('kriegerausbau', 'naechsterkrieger')]!='undefined') clearTimeout(timer[containerID.replace('kriegerausbau', 'naechsterkrieger')]); 
			$j('#'+containerID).empty().addClass('gruen').append('<b>Fertig!</b>');
//			if ($j('#gebaeude_container').length>0 && typeof letzerGebaeudeKuerzel != 'undefined') ajaxGebaeudeLink(letzerGebaeudeKuerzel);
//			location.reload();
			killTimer(containerID);
		} else {
			$j('#'+containerID).html(getTimer(seconds));
			seconds--;
		}
	},1000);
}

function killTimer(containerID) {
	clearInterval(timer[containerID]);
		timer[containerID] = null;
}

function showOverlay(url) {
    url = url || null;
    
    var arrayPageSize = getPageSize();
    $j('#overlay').css({'background-color': '#000000', 'z-index': 9999999999});
    
    if (url != null) {
        $j('#overlay').css({position: 'absolute', 'margin-left': '225px', 'width': '556', 'height': arrayPageSize[1] + 'px'});
		if($j.browser.msie && $j.browser.version < 7) $j('#overlay').css('margin-left', '-562px');
        if($j.browser.msie && $j.browser.version >= 7) $j('#overlay').css('margin-left', '-512px');
        $j('#overlay').html('<iframe width="650" border="0" height="570" src="'+url+'" style="position: absolute; margin-left: 0px; margin-top:50px; border:none; opacity: 1 !important; -moz-opacity: 1!important; filter: alpha(opacity=100) !important; "></iframe><br /><a href="javascript:void(0);" onclick="hideOverlay();" style="position: absolute; top: 25px; left: 0px; padding: 10px; background: #1C1E1C;">[x]&nbsp;Fenster&nbsp;schlie&szlig;en</a>').css('text-align: center;');
    } else {
        $j('#overlay').css({opacity: 0.7, '-moz-opacity': 0.7, filter: 'alpha(opacity=70)', position: 'absolute', 'margin-left': '225px','width': 556 + 'px', 'height': arrayPageSize[1] + 'px'});
        if($j.browser.msie && $j.browser.version < 7) $j('#overlay').css('margin-left', '-562px');
        if($j.browser.msie && $j.browser.version >= 7) $j('#overlay').css('margin-left', '-512px');
    }
    $j('#overlay').show();
}

function hideOverlay() {
	$j('#overlay').hide();
	$j('#overlay').html('<img src="/img_allg/loading.gif" style="position: absolute; left: 50%; margin-left: -64px; top: 50%;" />');
}

function bistDuSicher(msg) {
	if(msg==null) { msg = 'Bist du Sicher?'; }
	return confirm(msg);
}

function getPageSize() {
        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

 function refreshRst(str) {
  	$j('#rsterr').css('borderColor', '#353C3A');
  	if (!str.match('Error:')) {
  		h = str.split(',');
  		jQuery.each(h, function(key, pair) {
  			pair=pair.split(':');
  			
  			// Einheiten Updaten
  			if (pair[0] == 'einheiten') {
  				verteilbareEinheiten = pair[1];
  				$j('#einheiten').html(pair[1]);
  			}

  			if (pair[0].match('stufe_')) {
          arrValue = pair[1].split('|');
          pair[1] = arrValue[0];
          intAddTurm = arrValue[1];
  				// Punkte anzeigen
  				$j('#'+pair[0]).html(pair[1]);
  				// Punkte setzen
  				rstEinheiten[pair[0].replace('stufe_', '')] = pair[1];
  				// Erzeugung neu setzen
          intErzProStufe = Math.round(3600*parseInt(pair[1])*erzProStufe['erz'+pair[0]]);
          intErzProStufe = parseInt(intErzProStufe)+parseInt(intAddTurm);
          //alert('rohstoff:'+pair[0]+' - '+intErzProStufe+' + '+intAddTurm+'!');
  				subElem = $j('#'+pair[0]+'_erzeugung');
  				if(subElem) subElem.text(arrValue[1]);
  				subElemHead = $j('#'+pair[0]+'_head');
  				if(subElemHead) subElemHead.text(arrValue[1]);
  			}
  			
  			/*
  			
  			if (pair[0].match('stufe_')) {
         	// Punkte anzeigen
  				$j('#'+pair[0]).html(pair[1]);
  				// Punkte setzen
  				rstEinheiten[pair[0].replace('stufe_', '')] = pair[1];
  				// Erzeugung neu setzen
          intErzProStufe = Math.round(3600*parseInt(pair[1])*erzProStufe['erz'+pair[0]])+intAddTurm;
  				subElem = $j('#'+pair[0]+'_erzeugung');
  				if(subElem) subElem.text(intErzProStufe);
  				subElemHead = $j('#'+pair[0]+'_head');
  				if(subElemHead) subElemHead.text(intErzProStufe);
  			}
  			
  			*/
  		});
  	}else {
  		$j('#rsterr').css({borderColor: '#CC0000'});
  		//alert(str.replace('Error:', ''));
  	}
  }
  
  function refreshAktRst(str) {
  	$j('#rsterr').css('borderColor', '#353C3A');
  	if (!str.match('Error:')) {
  		h = str.split(',');
  		jQuery.each(h, function(key, pair) {
  			pair=pair.split(':');
			headElem = $j('#aktuell_'+pair[0]);
			if(headElem) headElem.text(parseInt(pair[1]));
  		});
  	}else {
  		$j('#rsterr').css({borderColor: '#CC0000'});
  		//alert(str.replace('Error:', ''));
  	}
  }
  var letzerGebaeudeKuerzel = null;
  
function ajaxGebaeudeLink(strKuerzel, strData) {
	strData = strData || '';
  	showOverlay();
  	letzerGebaeudeKuerzel = strKuerzel;
  	$j('#div_neu_errichten').remove();
  	$j('#gebaeude_container').hide();
  	$j.ajax({
  		type:	'POST',
  		data:	strData,
  		url:	'gebaeude/'+strKuerzel+'.inc.php',
  		success:function(gebHTML) {
  			$j('#gebaeude_container').html(gebHTML);
  			$j('#gebaeude_container').show();
  			hideOverlay();
  		}
  	});
}


// Nachrichten-Check fuer alle Seiten
function countNew() {

  jQuery.ajax({
		type: "POST",
		url: "/incs/ajax/messages.php",
		data: "action=checkCount",
		success: function(msg){
      
      arrCount = msg.split('|');
      intGesamt = parseInt(arrCount[0])+parseInt(arrCount[1]);
      
      // Inbox = arrCount[0]
      // Report = arrCount[1]
      // Gesamt = intGesamt
      
      $j("#newInbox").empty();
      if (parseInt(arrCount[0]) > 0) $j("#newInbox").append('('+arrCount[0]+')');
      
      $j("#newReport").empty();
      if (parseInt(arrCount[1]) > 0) $j("#newReport").append('('+arrCount[1]+')');
      
      $j(".posteingang_count").empty();
      if (parseInt(intGesamt) > 0) $j(".posteingang_count").append('('+intGesamt+')');
      if (parseInt(intGesamt) > 0) $j(".posteingang_count_ohneklammern").empty().append(''+intGesamt+'');
      
		}
	});
	
	countTimer = window.setTimeout('countNew()', 35000); // alle 35 Sekunden check auf neue Nachrichten!

}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function urlaubbeenden() {
showOverlay();
 $j.ajax({
   type: "POST",
   url: "/incs/ajax/einstellungen.php",
   data: "action=urlaubbeenden",
   success: function(msg){
	   	$j("#urlaubsbox").html('<h1>Willkommen zur&uuml;ck!</h1>Dein Urlaub wurde erfolgreich beendet!<br /><br />Bitte beachte, dass du erst in 4 Wochen, den n&auml;chsten Urlaub beantragen kannst!<br /><br /><a href="station.php">&raquo; weiterspielen!</a>');
		hideOverlay();
   }
 });
}

wmtt = null;

document.onmousemove = updateWMTT;

function updateWMTT(e) {
	x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
	if (wmtt != null) {
		wmtt.style.left = (x + 20) + "px";
		wmtt.style.top 	= (y + 20) + "px";
	}
	$j("#werte").css('left',x+20);
	$j("#werte").css('top',y+20);
}

function showWMTT(id) {
	wmtt = document.getElementById(id);
	wmtt.style.display = "block"
}

function hideWMTT() {
	wmtt.style.display = "none";
}


function rohstoffupdate() {
 $j.ajax({
   type: "POST",
   url: "/incs/ajax/station.php",
   data: "action=rohstoffe&user="+user_info.userid+"&kuppel="+user_info.kuppel,
   dataType: "json",
   success: function(msg){
		$j('#aktuell_stahl').text(msg.stahl);
		$j('#aktuell_fluorid').text(msg.fluorid);
		$j('#aktuell_toberit').text(msg.toberit);
		$j('#aktuell_demantoid').text(msg.demantoid);
   }
 });
}

-->
