/**
 * Copyright David Olah - Projekt: Atlantica
 * JS - global.js
 * Javascript Funktionen, die überall gebraucht werden können
 *
*/

/**
 * Füllt die Contentbox mit Inhalt
 *
 * return false: Damit man nicht bei ausgeschaltetem Javascript weitergeleitet wird
*/
function getContent(external_source, speed)
{
	var source_parts = external_source.split('/');
	if (source_parts.length > 2)
	{
		external_source = source_parts[0]+'/'+source_parts[1];
	}
	$('div.contentbox').hide();
	$('div.contentbox').load('/index.php/'+external_source).fadeIn(speed);
	return false;
}

/**
 * Countdown, der HTMl-Elemente automatisch aktualiusiert
 * 
 * @param id string: HTML-Element´
*/
function countDown(id)
{
	var str_time_old = $('#'+id).html();
	var str_parts = str_time_old.split(':');
	var minutes = trim(str_parts[0]);
	var seconds = trim(str_parts[1]);
	
	// Wenn der Countdown vorbei ist, zeigen wir --:-- an
	if (minutes <= 0 && seconds <= 0)
	{
		minutes = '--';
		seconds = '--';
	}
	else
	{
		if (minutes > 0 && seconds == 0)
		{
			minutes--;
			if (minutes < 10) minutes = '0' + minutes;
			seconds = 59;
		}
		else
		{
			if (minutes != '--' && seconds != '--')
			{
				seconds--;
			}
		}
	}
	
	if (seconds < 10)
	{
		seconds = '0' + seconds;
	}
	
	var str_time_new = minutes + ':' + seconds;
	$('#'+id).html(str_time_new);
	window.setTimeout('countDown(\''+id+'\')', 1000);
}

/**
 * Sucht einen Benutzer und füllt ein Div-Element mit Werten
 *
 * @param user_name string: Benutzername, nach dem gesucht wird
 * @param fill_element string: Element, das gefüllt wird
*/
function findUser(user_name, fill_element)
{
	$(fill_element).html(user_name);
}

function nl2br(str) 
{
	if(typeof(str)=="string") 
		return str.replace(/(\r\n)|(\n\r)|\r|\n/g,"<BR>");
 	else 
		return str;
}

/**
 * Zeigt eine Confirm-Box und gibt als Resultat das Resultat der Confirmbox zurück
 *
 * @param text text: Anzuzeigender Text in der Box
 * @return bool
*/
function showConfirm(text)
{
	return window.confirm(text+'?');
}

/**
 * Zeigt eine Confirmbox an, wenn es neue Nachrichten gibt
 *
 * param part_amount string: Der Teil des Textes, welcher Auskunft über die Anzahl der neuen Nachrichten ausgibt (z.B.: 4 Nachrichten)
*/
function showConfirm_NewMessages(text)
{
	var response = window.confirm(text);
	if (response)
	{
		document.location = '/index.php/user/messages';
	}
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function twoDigits(n) {
	s=""
	if(n<10) s="0"
	return s+n.toString();
}