var OUTPT_USER_DISABLED =  "\n\n\nDer Benutzer ist <b>gesperrt</b>";
var OUTPT_USER_ENABLED =  "\n\n\nDer Benutzer ist <b>aktiv</b>";
var OUTPT_WORLD_DISABLED =  "\n\nDie Welt ist <b>gesperrt</b>";
var OUTPT_REG_DISABLED =  "\n\nDie Registrierung ist <b>gesperrt</b>";
var VALUE_ENABLE = "Aktivieren";
var VALUE_DISABLE = "Sperren";
var MAIN_URL = "http://rbucksch.homelinux.org:8080/";

var PLACEHOLDER_HALLO = "$HELLO$";
var PLACEHOLDER_TITLE_LOGIN = "$TITLE_LOGIN$";
var PLACEHOLDER_USERNAME = "$LOGIN$";
var PLACEHOLDER_WORLD = "$WORLD$";
var PLACEHOLDER_PASSWORD = "$PASSWORD$";
var PLACEHOLDER_EMAIL = "$EMAIL$";
var PLACEHOLDER_DIRECTION = "$DIRECTION$";
var PLACEHOLDER_ATTITUDE = "$ATTITUDE$";
var PLACEHOLDER_LAST_LOGIN_DAY = "$LAST_LOGIN_DAY$";
var PLACEHOLDER_LAST_LOGIN_TIME = "$LAST_LOGIN_TIME$";
var PLACEHOLDER_TOWN = "$TOWN$";


var START_VIA_SID = false;


var topicFocused = false;

var gameWindowWidth = 1024;
var gameWindowsHeight = 768;

//--> Adapt to old game
var oldWorldLinks = new Array(	"http://www.kingdom-of-legends.de/game2",
								"http://www.kingdom-of-legends.de/game3", 
								"http://www.kingdom-of-legends.de/game4", 
								"http://www.kingdom-of-legends.de/game5");
//<--

var pUsername;
var pPassword;

//--> Adapt to old game
function isOldGame(worldLink){
	
	for (var i=0; i<oldWorldLinks.length; i++ ){
	
		if (worldLink.substring(0, oldWorldLinks[i].length)  == oldWorldLinks[i]){
			return true;
		}
	}
	
	return false;	
}
//<--

function getLink(link) {
	
	//--> Adapt to old game
	if(isOldGame(link)) {
		return link+"/?name=" + pUsername + "&" + "pass=" + pPassword;	
	} 
	//<--
	return link+"/";
	
}

function getSidLink(link, sid) {
	
	return link + "?sid=" + sid;
	
}

function shouldOpenInPopup(){
	
	if((screen.width == 1024) && (screen.height == 768)) {
		
	 return true;	
	}
	
	return false;
	
}


function startGameViaSid(link, sessionId) {
	
	var top = (screen.height - gameWindowsHeight) / 2;
	var left = (screen.width - gameWindowWidth) / 2;
	
	
		
	if(shouldOpenInPopup()) {
		
	  var myWindow;
		
	  //Open game in PopUp
	  myWindow = window.open(getSidLink(link, sessionId), "KingdomOfLegends",
			    "width="+ gameWindowWidth +",height="+ gameWindowsHeight 
			  + ",top="+top+", left="+left 
			  + ",status=yes,resizeable=yes" );

	  myWindow.focus();

	  //Return to homepage
	  setTimeout("self.location.href='/'",0);

	} else {
		
	  //Forward to game
	  setTimeout("self.location.href='"+getSidLink(link, sessionId)+"'",0);
		
	} 
	
}


function startGame(link, username, password) {

	document.cookie = "login_data="+username+"&"+password+";  path=/ ";
	
	var top = (screen.height - gameWindowsHeight) / 2;
	var left = (screen.width - gameWindowWidth) / 2;
	
	//--> Adapt to old game
		pUsername = username;
		pPassword = password;
	//<--
	
		
		
	if(shouldOpenInPopup()) {
		
	  var myWindow;
		
	  //Open game in PopUp
	  myWindow = window.open(getLink(link), "KingdomOfLegends",
			    "width="+ gameWindowWidth +",height="+ gameWindowsHeight 
			  + ",top="+top+", left="+left 
			  + ",status=yes,resizeable=yes" );

	  myWindow.focus();

	  //Return to homepage
	  setTimeout("self.location.href='/'",0);
 

	} else {
		
	  //Forward to game
	  setTimeout("self.location.href='"+getLink(link)+"'",0);
		
	} 
	
	
}

function emailAdminAdd(text) {
	
	if(topicFocused) {
		addToValue('iEmailTopic', text);
	} else {
		addToValue('iEmailContent', text);
	}
	
}

function addToValue(id, text) {
	$("#"+ id).attr("value", $("#"+ id).attr("value") + text ) ;
	$("#"+ id).focus();
	
}

function selectAll(name, count) {
	
	for(var i = 0; i < count; i++){
		document.getElementById(name + "Selected["+ i +"]").checked = "checked";
	} 
}

function showExportWindow(){
	setTimeout("self.location.href='/KoL_Website/export.csv'", 1000);
}
	 
function setNewsCtAttr(attr, value){
	
	if(attr == "width"){
		document.getElementById("newsCt").style.marginLeft = (value / (-2))+"px";
		document.getElementById("newsCt").style.width = value+"px";
	}

	
	
	if(attr == "height"){
		document.getElementById("newsCt").style.marginTop = (value / (-2))+"px";
		document.getElementById("newsCt").style.height = value+"px";
	}
	
	if(attr == "top"){
		document.getElementById("newsBox").style.marginTop = value+"px";
	}

	if(attr == "left"){
		document.getElementById("newsBox").style.marginLeft = value+"px";
	}
	
}

function setNewsBgImage(url){
	
	
	bgImage = new Image();
	
	bgImage.onload = function() {
	
		document.getElementById("newsBox").style.width = (bgImage.width-20) + "px";
		document.getElementById("newsBox").style.height = (bgImage.height-10) + "px";
		
	};
	
	bgImage.src = url;
	//bgImage.
	
	document.getElementById("newsBox").style.backgroundImage = "url('"+ url +"')";
	
	
	
	//alert(document.getElementById("news").style.height);
	//alert(document.getElementById("news").style.backgroundImage);
	
}

function setAttr(id, attr, value){
	$("#"+ id).attr(attr, value) ;
}

function changeVisibility(div) {
	
	if (document.getElementById(div).style.visibility == 'visible'){
		document.getElementById(div).style.visibility='hidden';
	} else {
		document.getElementById(div).style.visibility='visible';
	}
}

function hideElements(div){
	
	for(var i=0; i<div.length; i++) {
		document.getElementById(div[i]).style.visibility='hidden' ;
	}
}


function showElements(div){
	
	for(var i=0; i<div.length; i++) {
		document.getElementById(div[i]).style.visibility='visible' ;
	}
}

function hideElement(div) {
	document.getElementById(div).style.visibility='visible';
}

function hideElement(div) {
	document.getElementById(div).style.visibility='hidden';
}



$(document).ready(function() {
	$("#btpUsername").keyup(function() {
		
		$.ajax({
				type: 'POST',
				url: 'username_proplist?username='+ $('#btpUsername').val()
						+ '&world=' + $('#suWorld').val()	,
				success: function(result) {
					if(result != "false") {
						$("#oProposal").html(result);
						$("#oProposali").html(result);
					} 
				}
		  });	
	});
	}); 
	






function changeUserStatusOutput(){
	
//	alert($('#btpUsername').val());
	
	$("#suChangeStatus").attr('disabled', "disabled") ;
	
	$.ajax({
		type: 'POST',
		url: 'userIsDisabled?username='+$('#btpUsername').val()+'&world='+$('#suWorld').val(),
		success: function(result) {
			if(result != "false") {

				var userExists = true;
			
				
				if(result == OUTPT_USER_DISABLED){
					$("#suChangeStatus").attr('value', VALUE_ENABLE) ;
				} else if (result == OUTPT_USER_ENABLED){
					$("#suChangeStatus").attr('value', VALUE_DISABLE) ;
				} else { 
					userExists = false;
				}
				
				if(userExists) {
					$("#suChangeStatus").attr('disabled', "") ;
					
					$("#userIsDisabled").html(result);
				}
				
				
			} 
		}
  });
	
}

var  WORLD_ATTRIBUTE_NAME = "name";
var  WORLD_ATTRIBUTE_TITLE = "title";
var  WORLD_ATTRIBUTE_LINK = "link";
var  WORLD_ATTRIBUTE_DISABLED_MESSAGE = "disabled_message";
var  WORLD_ATTRIBUTE_REG_DISABLED_MESSAGE = "registration_disabled_message";


function removeFirstSpaces(str) {
	
	result = str;

	while((result.substring(0,1,result) == " ")  ) {
		result = result.substring(1,result.length,result);
	}
	
	while( (result.substring(0,1,result) == "\n") ) {
		result = result.substring(1,result.length,result);
	}
	
	return result;
	
}

function getAndWriteWorldValues(attribute, writeTo) {
	
	$.ajax({
		type: 'POST',
		url: 'getWorldValue?world='+$('#swWorld').val()+'&attribute='+attribute,
		success: function(result) {
			if(result != "false") {
				$("#"+writeTo).attr("value", removeFirstSpaces(result));
			} 
		}
  });	
	
}


function setDefaults() {
	
	$("#worldIsDisabled").html(result);
	$("#swChangeStatus").attr('disabled', "") ;
	$("#swTitle").attr('disabled', "") ;
	$("#swDelete").attr('disabled', "") ;
	$("#swRename").attr('disabled', "") ;
	$("#swChangeMessage").attr('disabled', "") ;
	$("#swDisabledMessage").attr('disabled', "") ;	
	$("#swName").attr('disabled', "") ;	
	$("#swLink").attr('disabled', "") ;	
}

function updateWorldValues() {
	
	getAndWriteWorldValues(WORLD_ATTRIBUTE_TITLE, "swTitle");
	getAndWriteWorldValues(WORLD_ATTRIBUTE_LINK, "swLink");
	getAndWriteWorldValues(WORLD_ATTRIBUTE_DISABLED_MESSAGE, "swDisabledMessage");
	getAndWriteWorldValues(WORLD_ATTRIBUTE_REG_DISABLED_MESSAGE, "swRegistrationDisabledMessage");
}

function changeWorldStatusOutput(){
	
	$.ajax({
		type: 'POST',
		url: 'worldIsDisabled?world='+$('#swWorld').val(),
		success: function(result) {
			if(result != "false") {
			
				
				
				$("#worldIsDisabled").html(result);
				$("#swChangeStatus").attr('disabled', "") ;
				$("#swTitle").attr('disabled', "") ;
				$("#swDelete").attr('disabled', "") ;
				$("#swRename").attr('disabled', "") ;
				$("#swChangeMessage").attr('disabled', "") ;
				$("#swDisabledMessage").attr('disabled', "") ;	
				$("#swName").attr('disabled', "") ;	
				$("#swLink").attr('disabled', "") ;	
				$("#swChangeTitle").attr('disabled', "") ;	
				$("#swChangeLink").attr('disabled', "") ;	
				$("#swRegistrationDisabledMessage").attr('disabled', "") ;
				$("#swChangeRegMessage").attr('disabled', "") ;
				if(result == OUTPT_WORLD_DISABLED){
					$("#swChangeStatus").attr('value', VALUE_ENABLE) ;
				} else {
					$("#swChangeStatus").attr('value', VALUE_DISABLE) ;
				}
				
				
				
				
			} 
		}
  });	
	
}


function changeWorldRegStatusOutput(){
	
	$.ajax({
		type: 'POST',
		url: 'worldIsRegDisabled?world='+$('#swWorld').val(),
		success: function(result) {
			if(result != "false") {
			
				
				
				$("#worldRegIsDisabled").html(result);
				$("#swChangeRegStatus").attr('disabled', "") ;
				
				if(result == OUTPT_REG_DISABLED){
					$("#swChangeRegStatus").attr('value', VALUE_ENABLE) ;
				} else {
					$("#swChangeRegStatus").attr('value', VALUE_DISABLE) ;
				}
				
				
				
				
			} 
		}
  });	
	
}


$(document).ready(function() {
	$("#swWorld").change(function() {
		
		changeWorldStatusOutput();
		changeWorldRegStatusOutput();
		updateWorldValues();
		
	}); 
});


$(document).ready(function() {
	$("#sUser").change(function() {
		
		changeUserStatusOutput();
		
	}); 
});


	

