function prepareNick(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.toLowerCase();
}

function goProfile(sNickname)
{
	var Ergebnis = sNickname.search(/@/);
	if(Ergebnis != -1)
	{
		window.location.href = '/admin/emaillookup/'+prepareNick(sNickname)+'/';
	}
	else
	{
		window.location.href = '/profile/'+prepareNick(sNickname)+'/';
	}
	return true;
}

function scrollToNick(olItemId)
{
	// fetch onlinelist contaner object
	a = $('olContainer');
	
	// fetch selected nickname
	b = $(olItemId);

	// scroll baby	
	if (typeof(b) == 'object' && b !== null)
	{
		a.scrollTop = b.offsetTop - a.offsetTop;
	}
}

function confirmURL(strMsg, strURL)
{
	var boolConfirmed = confirm(strMsg);
	if (boolConfirmed === true)
		window.location.href = strURL;
	return false;
}

function extendURL(strMsg, strURL)
{
	var strDuration = prompt(strMsg, 0);
	if ((strDuration != 0) && (strDuration != '') && (strDuration != null))
	{
		window.location.href = strURL+strDuration;
	}
	return false;
}

function toggleNavBox()
{
	oNavOLImg = $('navolimg');
	oNavOLBox = $('navolbox');
	
	if (oNavOLImg.src.search(/_open/) != -1)
	{

		oNavOLImg.src = oNavOLImg.src.replace(/_open/, '_close');
		oNavOLBox.show();
	}
	else
	{
		oNavOLImg.src = oNavOLImg.src.replace(/_close/, '_open');
		oNavOLBox.hide();
	}
}

function toggleObj(sObjId)
{
	oObj = $(sObjId);
	if (oObj.style.display == 'block')
	{
		oObj.style.display = 'none';
	}
	else
	{
		oObj.style.display = 'block';	
	}
}

var insideCore =
{
	init: function()
	{
		if ($('insideinfo'))
		{
			insideCore.initInfoBox();
		}
		if ($('insideUserAction'))
		{
			insideCore.initUserAction();
		}
	},
	
	initMsgCount: function()
	{
		insideCore.oXHR = new XHR({method: 'get'});
		insideCore.oXHR.onSuccess = function()
		{
			insideCore.setMsgCount(insideCore.oXHR.transport.responseText);
			var oDate = new Date();
			var fTimeout = (oDate.getTime()/1000)+30;
			var sXhrCookie = Cookie.get('insideMsgXhr');
			if (sXhrCookie)
			{
				var oXhrCookieJson = Json.evaluate(sXhrCookie);
				if (MESSAGESOUND > 0 && (insideCore.oXHR.transport.responseText > oXhrCookieJson.lastMsgs))
				{
					insideCore.messageSound(MESSAGESOUND);
				}
			}
			
			Cookie.set('insideMsgXhr',Json.toString(
				{
					msgs:insideCore.oXHR.transport.responseText,
					lastMsgs: insideCore.oXHR.transport.responseText,
					timeout:fTimeout
				}
			));
		};
		window.setInterval("insideCore.updateMsgCount()", 31*1000);
		insideCore.updateMsgCount();
	},
	
	updateMsgCount: function()
	{
		var sXhrCookie = Cookie.get('insideMsgXhr');
		var oDate = new Date();
		var fNow = oDate.getTime()/1000;
		var oXhrCookieJson = Json.evaluate(sXhrCookie);
		
		if (sXhrCookie && (oXhrCookieJson.timeout >= fNow))
		{
			insideCore.setMsgCount(oXhrCookieJson.msgs);
		}
		else
		{
			insideCore.oXHR.send('/post-xhr.php');
		}
	},
	
	setMsgCount: function(iCount)
	{
		$('userMsgCount').innerHTML = iCount;
		if (iCount > 0)
		{
			$('userMsgIcon').src = $('userMsgIcon').src.replace(/msg\.gif/, 'msg_new.gif'); 
		}
		else
		{
			$('userMsgIcon').src = $('userMsgIcon').src.replace(/_new/, "");
		}		
	},
	
	resetMsgCount: function()
	{
		Cookie.remove('insideMsgXhr');
		insideCore.updateMsgCount();
	},
	
	initInfoBox : function()
	{
		var sBoxCookie = Cookie.get('insideBox');
		if (!sBoxCookie || sBoxCookie != $('insideinfo').className)
		{
			$('insideinfo').style.display = 'block';
		}
	},
	
	closeInfoBox : function()
	{
		Cookie.set('insideBox', $('insideinfo').className, {duration: 345600});
		$('insideinfo').style.display = 'none';
	},
	
	initUserAction : function()
	{
		$('insideUserActionSaveBtn').onclick = function() {
			insideCore.setUserAction($('insideUserActionText').value);
		};
		
		$('insideUserActionResetBtn').onclick = function() {
			$('insideUserActionText').value = '';
			insideCore.setUserAction('');
		};

		var oInsideUserAction = $('insideUserAction');
		var oInsideUserActionText = $('insideUserActionText');
		oInsideUserActionText.onfocus = function() { this.className = 'form focus'; };
		oInsideUserActionText.onblur = function() { this.className = 'form blur'; };
	},
	
	setUserAction : function(sAction)
	{
		$('insideUserActionLoad').style.display = 'block';
		var jSonRequest = new Json.Remote("/xhr/setaction", {onComplete: function(xhrresponse){
	    	if (xhrresponse.error == true)
		    {
		    	alert('Beim übermitteln deines neuen Status ist ein Fehler aufgetreten');
		    }
		    else
		    {
				$('insideUserActionLoad').style.display = 'none';		    
		    }
		}}).send({'action': sAction});		
	},
	
	previewMessageSound: function(iSound)
	{
		$('messagePreviewBox').set('html', '');
		$('messagePreviewBox').set('html', '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="1" height="1" id="Sound1" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="http://m1.dabei.de/m/swf/msg/sound'+iSound+'.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#a7aebc" /><embed src="http://m1.dabei.de/m/swf/msg/sound'+iSound+'.swf" quality="high" bgcolor="#a7aebc" width="1" height="1" name="Sound1" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
	},
	
	messageSound: function(iSound)
	{
		var oSoundObj = new Element('object', {
				'classid': 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
				'codebase': 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
				'width': 1,
				'height': 1
		});
		
		var oSoundObjParam1 = new Element('param', {
			'name' : 'movie',
			'value' : MEDIAHOST+'/m/swf/msg/sound'+iSound+'.swf'
		});
		
		var oSoundObjEmbed = new Element('embed', {
			'height': 1,
			'width' : 1,
			'src' : MEDIAHOST+'/m/swf/msg/sound'+iSound+'.swf',
			'type' : 'application/x-shockwave-flash'
		});
		
		oSoundObj.grab(oSoundObjParam1);
		oSoundObj.grab(oSoundObjEmbed);

		$('messageSoundBox').empty().grab(oSoundObj);
	}
};

window.addEvent('domready', function() {
	insideCore.init();
});
