function injectPopup(str,key,tgt)
{
	var url = '/wrapper';
	var moreparams = '';
	
	if(thisMovie(callbackApplication))
	{
		moreparams = '&fid=' + currentFlash;
	}
	
	if(tgt)
	{
		new Ajax.Request(url, {
			asynchronous: false,
			evalScripts: true,
			parameters:'str='+str+moreparams,
			onComplete: function(transport) {
				if (401 == transport.status)
				{
					login();
				}
				else
				{
					$(tgt).update(transport.responseText);
					callbackKey = key;
					callbackValue = '1';
					/* doCallback(key,'1'); */
			  	}
			}
		});
	}
	return true;
}

function closePopups()
{
	// Closes all Popups
	document.getElementById('popup1container').style.display ="none";
	document.getElementById('overlay').style.display ="none";
}

function closePopup()
{
	document.getElementById('popup1container').style.display ="none";
	document.getElementById('overlay').style.display ="none";
	doCloseCallback();
}

function repeatEnergytwins()
{
	closePopup();
	doReload = true;
	reload();
}

function openPopup(str, key)
{
	
	if(str)
	{

		switch(key)
		{
			
			/* Alert */
			case('0'):
				injectPopup(str,key,'popup1cnt');
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('popup1container').style.display ="block";
				break;
		
	
			/* Content */
			case('1'):
				injectPopup(str,key,'popup1cnt');
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('popup1container').style.display ="block";
				break;
		
	
			/* Error */
			case('2'):
				injectPopup(str,key,'popup1cnt');
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('popup1container').style.display ="block";
				break;
		
	
			/* Login */
			case('3'):
				injectPopup(str,key,'popup1cnt');
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('popup1container').style.display ="block";
				break;
				
				
			/* Logout */
			case('4'):
				injectPopup(str,key,'popup1cnt');
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('popup1container').style.display ="block";
				loggedOut = true;
				break;
			
			
			/* Steckbrief anzeigen */
			case('5'):
				var url = 'wrapper?str=' + str;
				new Ajax.Request(url, {
				  method: 'get',
				  onSuccess: function(transport) {
				    loadHtmlContent('steckbrief/' + transport.responseText);
				  }
				});
				break;
			
			
			/* Glossar anzeigen */	
			case('6'):
				openGlossar();
				break;
			
			/* Userpic Selector */	
			case('7'):
				selectPic(str);
				break;
			
			/* Silent Action */	
			case('8'):
				injectPopup(str,key,'nondisplay ');
				break;

				
			default:
				injectPopup(str,key,'popup1cnt');
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('popup1container').style.display ="block";
				
		}
		
	}
	updateWrapper();
	
}

function getWindowSize() {
	        
	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 revokeFriend(lookup)
{
	closePopups();
	new Ajax.Updater('friend1cnt', '/freunde/confirm/revokeFriend/'+lookup, {asynchronous:false, evalScripts:true});
	document.getElementById('overlay').style.display ="block";
	var arrayWindowSize = getWindowSize();
	$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
	document.getElementById('friend1container').style.display ="block";
}

function removeFriend(lookup)
{
	closePopups();
	new Ajax.Updater('friend1cnt', '/freunde/confirm/removeFriend/'+lookup, {asynchronous:false, evalScripts:true});
	document.getElementById('overlay').style.display ="block";
	var arrayWindowSize = getWindowSize();
	$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
	document.getElementById('friend1container').style.display ="block";
}

function login()
{
	closePopups();
	new Ajax.Updater('popup1cnt', '/wrapper/displayLogin', {asynchronous:false, evalScripts:true});
	document.getElementById('overlay').style.display ="block";
	var arrayWindowSize = getWindowSize();
	$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
	document.getElementById('popup1container').style.display ="block";	
}

function forgotPassword()
{
	closePopups();
	new Ajax.Updater('popup1cnt', '/passwortvergessen', {asynchronous:false, evalScripts:true});
	document.getElementById('overlay').style.display ="block";
	var arrayWindowSize = getWindowSize();
	$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
	document.getElementById('popup1container').style.display ="block";	
}

function displayGlossar(str)
{
	if(str)
		var url = '/glossar/' + str;
	else
		var url = '/glossar';
	new Ajax.Updater('glossarcnt', url, {asynchronous:false, evalScripts:true});
}

function closeGlossar()
{
	document.getElementById('overlay').style.display ="none";
	document.getElementById('glossarcontainer').style.display ="none";
}

function openGlossar(str)
{
	displayGlossar(str);
	document.getElementById('overlay').style.display ="block";
	var arrayWindowSize = getWindowSize();
	$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
	document.getElementById('glossarcontainer').style.display ="block";
	
}

function closeGuestbook()
{
	document.getElementById('overlay').style.display ="none";
	document.getElementById('guestbookcontainer').style.display ="none";
}

function openGuestbook(userId)
{
	new Ajax.Request('/gaestebuch/eintrag/' + userId, {
		asynchronous: true,
		evalScripts: true,
		onComplete: function(transport) {
			if (401 == transport.status)
			{
				
				login();
			}
			else
			{
				$('guestbookcnt').update(transport.responseText);
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('guestbookcontainer').style.display ="block";
		  	}
		}
	});

	
}

function deleteGuestbookEntryConfirm(mId)
{
	closePopups();
	new Ajax.Updater('popup1cnt', '/gaestebuch/eintrag/loeschenbestaetigen/'+mId, {asynchronous:false, evalScripts:true});
	document.getElementById('overlay').style.display ="block";
	var arrayWindowSize = getWindowSize();
	$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
	document.getElementById('popup1container').style.display ="block";
}

function deleteGuestbookEntry(mId)
{
	closePopups();
	new Ajax.Updater('popup1cnt', '/gaestebuch/eintrag/loeschen/'+mId, {asynchronous:false, evalScripts:true});
	document.getElementById('overlay').style.display ="block";
	var arrayWindowSize = getWindowSize();
	$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
	document.getElementById('popup1container').style.display ="block";
}

function closeAbuse()
{
	document.getElementById('overlay').style.display ="none";
	document.getElementById('guestbookcontainer').style.display ="none";
}

function openAbuse(str)
{
	new Ajax.Request('/abuse', {
		asynchronous: true,
		parameters:'reportLookup='+str,
		evalScripts: true,
		onComplete: function(transport) {
			
				$('guestbookcnt').update(transport.responseText);
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('guestbookcontainer').style.display ="block";
		}
	});

	
}

function closeFriend()
{
	document.getElementById('overlay').style.display ="none";
	document.getElementById('friend1container').style.display ="none";
}

function hideInviteFriendButton()
{
	document.getElementById('makeFriendButtonDiv').style.display ="none";
}

function openFriend(action, userId)
{
	new Ajax.Request('/freunde/' + action + '/' + userId, {
		asynchronous: true,
		evalScripts: true,
		onComplete: function(transport) {
			if (401 == transport.status)
			{
				
				login();
			}
			else
			{
				$('friend1cnt').update(transport.responseText);
				document.getElementById('overlay').style.display ="block";
				var arrayWindowSize = getWindowSize();
				$('overlay').setStyle({ width: arrayWindowSize[0] + 'px', height: arrayWindowSize[1] + 'px' });
				document.getElementById('friend1container').style.display ="block";
		  	}
		}
	});

	
}

function selectPic(str)
{
	new Ajax.Request('/user/userpic/' + str, {
		asynchronous: true,
		evalScripts: true,
		onComplete: function(transport) {
			$('userpicDiv').update(transport.responseText);
		}
	});
}
