var g_iCurrentUniverse = 0;
var g_bIsOwnerUniverse = false;
var g_ListArray = new Array();
var g_ListArrayRedirect = new Array();
var g_changingUniverseId = 0;
var timeOutNotifCount = null;
var iRefreshTimeNotifCount = 60000;
var g_universeList = new Array();
var g_yourUniverseList = new Array();
var g_currentPrivacy = 0;

function initUniverseManagement(bLogged, listName, iCurrentUniverse, _isOwnerUniverse, _bAllowBgUpdate, _bRedirectOnChange)
{
	g_iCurrentUniverse = iCurrentUniverse;
	g_bIsOwnerUniverse = _isOwnerUniverse;
	g_bIsLogged = bLogged;
	var list = document.getElementById(listName);
	g_ListArray[listName] = list;
	g_ListArrayRedirect[listName] = _bRedirectOnChange;
	list.onchange = onUniverseListChange;
	if (_bAllowBgUpdate)
	{
		var bgUniverse = document.getElementById("menulinkUniverseBackground");
		bgUniverse.onclick = function (){
			COMMON_show_AddFormRequest('./home/layout_uploadBg.php', callbackUniverseBackground, null);
		}
	}
	// Notifications
	if (bLogged)
	{
		if (timeOutNotifCount)
			clearTimeout(timeOutNotifCount);
		timeOutNotifCount = setTimeout(refreshNotifCount, iRefreshTimeNotifCount);		
	}
}

function ShowUniverseOptionsMenu(obj)
{
	if ($("#universe_options_menu").hasClass('hidden')) {
		//$("#box_menu").fadeIn('fast').removeClass('hidden');
		document.getElementById("universe_options_menu").style.top = COMMON_GetTopPos(obj) + obj.offsetHeight;
		document.getElementById("universe_options_menu").style.left = COMMON_GetLeftPos(obj) + obj.offsetWidth;
		$("#universe_options_menu").animate({height: 'toggle', opacity: 1}, 300).removeClass('hidden');
		LINK_HideMenu();
		BOX_HideMenu();
		TAB_HideMenu();
	} else {
		$("#universe_options_menu").animate({height: 'toggle', opacity: 0}, 100).addClass('hidden');		
	}		
}

function UNIVERSE_HideOptionsMenu()
{
	if (!$("#universe_options_menu").hasClass('hidden')) {
		$("#universe_options_menu").animate({height: 'toggle', opacity: 0}, 100).addClass('hidden');
	}	
}

function UNIVERSE_HideMenu()
{
	if (!$("#universe_menu").hasClass('hidden')) {
		$("#universe_menu").animate({height: 'toggle', opacity: 0}, 100).addClass('hidden');
	}	
}
function UNIVERSE_ImportFromMenu(e, inputObj)
{
	COMMON_show_AddFormRequest('./home/layout_importFav.php?id=' + g_iCurrentUniverse, UNIVERSE_ImportFromMenuCallback, null);
	UNIVERSE_HideOptionsMenu();	
}

function UNIVERSE_ImportChangePrivacy()
{
	var optionList = document.getElementsByName("addTypeRadio");
	var privacyValue = 0;
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].checked)
		{
			privacyValue = optionList[i].value;
			break;
		}
	}
	new AjaxUpload(importLinkUniverseButton,{
		//action: 'upload-test.php', // I disabled uploads in this example for security reasons
		action: './common/utilities/fileuploader.php?importLink&iduniverse=' + g_iCurrentUniverse + '&privacy=' + privacyValue, 
		name: 'file',
		onSubmit : function(file, ext){
			var optionList = document.getElementsByName("addTypeRadio");
			var privacyValue = 0;
			for (var i = 0; i < optionList.length; i++)
			{
				if (optionList[i].checked)
				{
					privacyValue = optionList[i].value;
					break;
				}
			}
			this.action = './common/utilities/fileuploader.php?importLink&iduniverse=' + g_iCurrentUniverse + '&privacy=' + privacyValue; 
			
			// change button text, when user selects file			
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
		},
		onComplete: function(file, response){					
			// enable upload button
			this.enable();
			if (response != "")
			{
				window.location = "./home/" + g_iCurrentUniverse;
			}
			COMMON_hideAddForm();			
		}
	});
}

function UNIVERSE_ImportFacebookLikesFromMenuCallback(e, inputObj)
{
	var loginButton = document.getElementById('facebookConnectButton');
	if (loginButton)
		loginButton.onclick = display_facebook_prompt;
	if (!document.getElementById("importFacebookLikeUniverseButton"))
		return;
	if (g_currentPrivacy == 1)
		document.getElementById("addInputPrivate").checked = true;
	else if (g_currentPrivacy == 2)
		document.getElementById("addInputFriend").checked = true;		
	var importLinkUniverseButton = document.getElementById("importFacebookLikeUniverseButton");		
	importLinkUniverseButton.onclick = UNIVERSE_ValidFacebookImport;
}

function UNIVERSE_ValidFacebookImport()
{
	var optionList = document.getElementsByName("addTypeRadio");
	var privacyValue = 0;
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].checked)
		{
			privacyValue = optionList[i].value;
			break;
		}
	}
	var universeList = document.getElementById("universeListImportFacebook").getElementsByTagName('option');;
	var universeId = 0;
	for (var i = 0; i < universeList.length; i++)
	{
		if (universeList[i].selected)
		{
			universeId = universeList[i].value;
			break;
		}
	}
	AJAX_SendRequest("./ajax/facebook.php?importLinks&universeId=" + universeId + "&privacy=" + privacyValue);
}

function UNIVERSE_ImportFromMenuCallback(e, inputObj)
{
	if (!document.getElementById("importLinkUniverseButton"))
		return;
	if (g_currentPrivacy == 1)
		document.getElementById("addInputPrivate").checked = true;
	else if (g_currentPrivacy == 2)
		document.getElementById("addInputFriend").checked = true;		
	var importLinkUniverseButton = document.getElementById("importLinkUniverseButton");
	var optionList = document.getElementsByName("addTypeRadio");
	var privacyValue = 0;
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].checked)
		{
			privacyValue = optionList[i].value;
			break;
		}
	}
	new AjaxUpload(importLinkUniverseButton,{
		//action: 'upload-test.php', // I disabled uploads in this example for security reasons
		action: './common/utilities/fileuploader.php?importLink&iduniverse=' + g_iCurrentUniverse + '&privacy=' + privacyValue, 
		name: 'file',
		onSubmit : function(file, ext){
			var optionList = document.getElementsByName("addTypeRadio");
			var privacyValue = 0;
			for (var i = 0; i < optionList.length; i++)
			{
				if (optionList[i].checked)
				{
					privacyValue = optionList[i].value;
					break;
				}
			}
			this.action = './common/utilities/fileuploader.php?importLink&iduniverse=' + g_iCurrentUniverse + '&privacy=' + privacyValue; 
			
			// change button text, when user selects file			
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
		},
		onComplete: function(file, response){					
			// enable upload button
			alert(response);
			this.enable();
			if (response != "")
			{
				window.location = "./home/" + g_iCurrentUniverse;
			}
			COMMON_hideAddForm();			
		}
	});
}

function UNIVERSE_ImportFacebookLikesFromMenu(e, inputObj)
{
	COMMON_show_AddFormRequest('./home/layout_importFacebookLikes.php?id=' + g_iCurrentUniverse, UNIVERSE_ImportFacebookLikesFromMenuCallback, null);
	UNIVERSE_HideOptionsMenu();	
}

function UNIVERSE_DeleteFromMenu(e, inputObj)
{
	if (confirm(g_lang_universe_delete))
		AJAX_SendRequest('./ajax/universe.php?deleteUniverse&id=' + g_iCurrentUniverse);	
	UNIVERSE_HideOptionsMenu();	
}

function UNIVERSE_RenameFromMenu(e, inputObj)
{
	COMMON_show_AddFormRequest('./home/layout_modifyUniverse.php?id=' + g_iCurrentUniverse, UNIVERSE_RenameFromMenuCallback);
	UNIVERSE_HideOptionsMenu();
}

function UNIVERSE_RenameFromMenuCallback(e, inputObj)
{
	var universeName = "";
	var universeList = document.getElementById("universeList").getElementsByTagName('option');;
	for (var i = 0; i < universeList.length; i++)
	{
		if (universeList[i].selected)
		{
			universeName = universeList[i].getAttribute('realname');
			break;
		}
	}
	document.getElementById("addInput").value = universeName;
	document.getElementById("addInput").focus();
	document.getElementById("addInput").select();
	if (g_currentPrivacy == 1)
		document.getElementById("addInputPrivate").checked = true;
	else if (g_currentPrivacy == 2)
		document.getElementById("addInputFriend").checked = true;		
}

function refreshNotifCount()
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = './common/notifications.php?refreshnotifcount';	// Specifying which file to get
	ajaxObjects[ajaxIndex].onCompletion = function(){ DB_responseRefreshNotifCount(ajaxObjects[ajaxIndex].response);};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
	if (timeOutNotifCount)
		clearTimeout(timeOutNotifCount);
	timeOutNotifCount = setTimeout(refreshNotifCount,  iRefreshTimeNotifCount);
}

function DB_responseRefreshNotifCount(_sResponse)
{
	document.getElementById("infoRequestCount").innerHTML = _sResponse;
	if (document.getElementById("idNotifCountOnNotif"))
		document.getElementById("idNotifCountOnNotif").innerHTML = _sResponse;
	
	if (_sResponse != "0")
	{
		document.getElementById("infoRequestCount").parentNode.style.visibility = "visible";
		if (document.getElementById("idNotifCountOnNotif"))
			document.getElementById("notifNewNotifDiv").style.display = "block";
		window.document.title = "(" + _sResponse + ") EasyFav Home Page";
	}
	else
	{
		document.getElementById("infoRequestCount").parentNode.style.visibility = "hidden";
		if (document.getElementById("idNotifCountOnNotif"))
			document.getElementById("notifNewNotifDiv").style.display = "none";
		window.document.title = "EasyFav Home Page";
	}	
}

function refreshNewNotifOnNotif()
{
	/*var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = '../common/notifications.php?refreshnotif';	// Specifying which file to get
	ajaxObjects[ajaxIndex].onCompletion = function(){ DB_responseRefreshNewNotifOnNotif(ajaxObjects[ajaxIndex].response);};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function*/
	AJAX_SendRequest('./ajax/notifications.php?notif_general');
	AJAX_SendRequest('./ajax/notifications.php?notif_important');
	document.getElementById('notifNewNotifDiv').style.display = "none";
	refreshNotifCount();
}

function DB_responseRefreshNewNotifOnNotif(_sResponse)
{
	if (document.createTextNode)
	{
		if (document.getElementById("notifMainDiv"))
		{
			window.document.title = "EasyFav Home Page";
			var mainDiv = document.getElementById("notifMainDiv"); 
			document.getElementById("notifNewNotifDiv").style.display = "none";
			document.getElementById("infoRequestCount").style.display = "none";
			mainDiv.innerHTML = _sResponse + mainDiv.innerHTML;
			
			var alertDiv = document.getElementById("notifNewNotifDiv");
			alertDiv = mainDiv.removeChild(alertDiv);
			mainDiv.insertBefore(alertDiv, mainDiv.getElementsByTagName("div")[0]);
			initLightbox();
			initLightboxForVideo();
		}
	}
	else
	{
		window.location.reload();
	}
}

function onClickUniverseListChangeHomepage(e)
{
	if (document.getElementById('homepageUniverse').checked)
		return;
	document.getElementById('homepageUniverse').checked = true;
	updateUniverseChangeHomepage();
}
function onUniverseListChangeHomepage(e)
{
	updateUniverseChangeHomepage();
}

function Universe_UpdateBgColorDefault()
{
	backgroundId = 0;
	{
		var optionList = document.getElementById("universeListChangeBg").getElementsByTagName('option');
		for (var i = 0; i < optionList.length; i++)
		{
			if (optionList[i].selected)
			{
				backgroundId = optionList[i].value;
				break;
			}
		}
		if (backgroundId == 0)
			return false;
	}
	document.body.style.backgroundColor = "#E8EDFF";
	document.getElementById('colorPicker').style.backgroundColor = "#E8EDFF";
	document.getElementById('colorPicker').value = "E8EDFF";
	AJAX_SendRequest('./ajax/universe.php?updateBgColor=default&id=' + backgroundId);
}

function onColorPickerChange(e)
{
	//selectorList, idStyleSheet, cssToChange, idColorPicker
	target = COMMON_GetTarget(e);
	// TOPPANEL
	if (target == document.getElementById('colorPickerTopPanel'))
	{
		selectorList = new Array(".tabTopPanelRequest", "#topMenuDiv");
		idStyleSheet = 0;
		cssToChange = "background-color";
		idColorPicker = "colorPickerTopPanel";
	}
	else if (target == document.getElementById('colorPickerLittleTitleTXT'))
	{
		selectorList = new Array(".settingLittleTitle");
		idStyleSheet = 0;
		cssToChange = "color";
		idColorPicker = "colorPickerLittleTitleTXT";
	}
	else if (target == document.getElementById('colorPickerLittleTitleBG'))
	{
		selectorList = new Array(".settingLittleTitle");
		idStyleSheet = 0;
		cssToChange = "background-color";
		idColorPicker = "colorPickerLittleTitleBG";		
	}
	else if (target == document.getElementById('colorPickerNotifTXT'))
	{
		selectorList = new Array(".notifTopTitle");
		idStyleSheet = 0;
		cssToChange = "color";
		idColorPicker = "colorPickerNotifTXT";
	}
	else if (target == document.getElementById('colorPickerNotifBG'))
	{
		selectorList = new Array(".notifTopTitle");
		idStyleSheet = 0;
		cssToChange = "background-color";
		idColorPicker = "colorPickerNotifBG";		
	}
	else if (target == document.getElementById('colorPickerCurrentTab'))
	{
		selectorList = new Array("#tabheader #current");
		idStyleSheet = 1;
		cssToChange = "background-color";
		idColorPicker = "colorPickerCurrentTab";
	}
	else if (target == document.getElementById('colorPickerOthersTabs'))
	{
		selectorList = new Array("#tabheader li");
		idStyleSheet = 1;
		cssToChange = "background-color";
		idColorPicker = "colorPickerOthersTabs";
	}
	else if (target == document.getElementById('colorPickerOthersTabsTXT'))
	{
		selectorList = new Array("#tabheader li a");
		idStyleSheet = 1;
		cssToChange = "color";
		idColorPicker = "colorPickerOthersTabsTXT";
	}
	else if (target == document.getElementById('colorPickerCurrentTabTXT'))
	{
		selectorList = new Array("#tabheader #current a");
		idStyleSheet = 1;
		cssToChange = "color";
		idColorPicker = "colorPickerCurrentTabTXT";
	}
	else if (target == document.getElementById('colorPickerHoverTabBG'))
	{
		selectorList = new Array("#tabheader li:hover");
		idStyleSheet = 1;
		cssToChange = "background-color";
		idColorPicker = "colorPickerHoverTabBG";
	}
	else if (target == document.getElementById('colorPickerHoverTabTXT'))
	{
		selectorList = new Array("#tabheader li:hover a");
		idStyleSheet = 1;
		cssToChange = "color";
		idColorPicker = "colorPickerHoverTabTXT";
	}
	else if (target == document.getElementById('colorPickerLink'))
	{
		selectorList = new Array("a");
		idStyleSheet = 0;
		cssToChange = "color";
		idColorPicker = "colorPickerLink";	
	}
	else if (target == document.getElementById('colorPickerLinkHover'))
	{
		selectorList = new Array("a:hover");
		idStyleSheet = 0;
		cssToChange = "color";
		idColorPicker = "colorPickerLinkHover";	
	}
	else if (target == document.getElementById('colorPickerBoxInnerBG'))
	{
		selectorList = new Array(".dragableBoxHeader", ".dragableBoxSpecialHeader");
		idStyleSheet = 3;
		cssToChange = "background-color";
		idColorPicker = "colorPickerBoxInnerBG";	
	}
	else if (target == document.getElementById('colorPickerBoxBG'))
	{
		selectorList = new Array(".dragableBoxContent", ".dragableBoxSpecialContent");
		idStyleSheet = 3;
		cssToChange = "background-color";
		idColorPicker = "colorPickerBoxBG";	
	}
	else if (target == document.getElementById('colorPickerBoxBorder'))
	{
		selectorList = new Array(".dragableBoxInner");
		idStyleSheet = 3;
		cssToChange = "border-color";
		idColorPicker = "colorPickerBoxBorder";	
	}
	else if (target == document.getElementById('colorPickerBoxTXT'))
	{
		selectorList = new Array(".dragableBoxHeader");
		idStyleSheet = 3;
		cssToChange = "color";
		idColorPicker = "colorPickerBoxTXT";	
	}
	else if (target == document.getElementById('colorPickerTXT'))
	{
		document.body.style.color = "#" + document.getElementById("colorPickerTXT").value;
		return;
	}
	else if (target == document.getElementById('colorPickerLI1'))
	{
		selectorList = new Array(".li_1");
		idStyleSheet = 4;
		cssToChange = "background-color";
		idColorPicker = "colorPickerLI1";	
	}
	else if (target == document.getElementById('colorPickerLI2'))
	{
		selectorList = new Array(".li_2");
		idStyleSheet = 4;
		cssToChange = "background-color";
		idColorPicker = "colorPickerLI2";	
	}
	else if (target == document.getElementById('colorPickerBoxStatusBar'))
	{
		selectorList = new Array(".dragableBoxStatusBar");
		idStyleSheet = 3;
		cssToChange = "background-color";
		idColorPicker = "colorPickerBoxStatusBar";	
	}
	else if (target == document.getElementById('colorPickerLIFriend'))
	{
		selectorList = new Array(".li_friend");
		idStyleSheet = 4;
		cssToChange = "background-color";
		idColorPicker = "colorPickerLIFriend";	
	}
	else if (target == document.getElementById('colorPickerLIPrivate'))
	{
		selectorList = new Array(".li_private");
		idStyleSheet = 4;
		cssToChange = "background-color";
		idColorPicker = "colorPickerLIPrivate";	
	}
	else if (target == document.getElementById('colorPickerLIHover'))
	{
		selectorList = new Array(".easyfav_tree ul li:hover, li:focus, li:active, #floatingSearchContainer li:hover, li:focus, li:active");
		idStyleSheet = 4;
		cssToChange = "background-color";
		idColorPicker = "colorPickerLIHover";	
	}
	else if (target == document.getElementById('colorPickerLITXT'))
	{
		selectorList = new Array(".easyfav_tree li a, #floatingSearchContainer li a");
		idStyleSheet = 4;
		cssToChange = "color";
		idColorPicker = "colorPickerLITXT";	
	}
	else if (target == document.getElementById('colorPickerMenuTXT'))
	{
		selectorList = new Array(".box_menu_container");
		idStyleSheet = 0;
		cssToChange = "color";
		idColorPicker = "colorPickerMenuTXT";	
	}
	else if (target == document.getElementById('colorPickerMenuBG'))
	{
		selectorList = new Array(".box_menu_container");
		idStyleSheet = 0;
		cssToChange = "background-color";
		idColorPicker = "colorPickerMenuBG";	
	}
	
	
	cssRulesList = document.styleSheets[idStyleSheet].cssRules;
	for (var i = 0; i < cssRulesList.length; i++)
	{
		for (var j = 0; j < selectorList.length; j++)
		{
			if (cssRulesList[i].selectorText == selectorList[j])
			{
				styleList = cssRulesList[i].style;
				if (!styleList)
					continue;
				for (var j = 0; j < styleList.length; j++)
				{
					if (cssToChange == "border-color")
					{
						styleList.borderColor = "#" + document.getElementById(idColorPicker).value;
						break;						
					}
					else if (styleList[j] == cssToChange)
					{
						switch (cssToChange)
						{
							case "background-color":
								styleList.backgroundColor = "#" + document.getElementById(idColorPicker).value;
								// cas spécial
								if (target == document.getElementById('colorPickerCurrentTab'))
								{
									styleList.borderBottomColor = "#" + document.getElementById(idColorPicker).value;
									document.getElementById('tabcontainer').style.backgroundColor = "#" + document.getElementById(idColorPicker).value;
								}
								break;
							case "color":
								styleList.color = "#" + document.getElementById(idColorPicker).value;
								break;
						}
						//document.getElementById('tabcontainer').style.backgroundColor = "#" + document.getElementById("colorPickerCurrentTab").value;
					}
				}
	 		}			
		}
	}	
}

function onColorPickerChangeValue()
{
	backgroundId = 0;
	{
		var optionList = document.getElementById("universeListChangeBg").getElementsByTagName('option');
		for (var i = 0; i < optionList.length; i++)
		{
			if (optionList[i].selected)
			{
				backgroundId = optionList[i].value;
				break;
			}
		}
		if (backgroundId == 0)
			return false;
	}
	document.body.style.backgroundColor = "#" + document.getElementById('colorPicker').value;
	AJAX_SendRequest('./ajax/universe.php?updateBgColor=' + document.getElementById('colorPicker').value + '&id=' + backgroundId);
}
function updateUniverseChangeHomepage()
{
	if (document.getElementById('homepageCom').checked)
	{
		AJAX_SendRequest('./ajax/settings.php?homepage=0');		
	}
	else
	{
		var option = document.getElementById('universeListChangeHomepage').getElementsByTagName('option');
		for (var i = 0; i < option.length; i++)
		{
			if (option[i].selected)
			{
				AJAX_SendRequest('./ajax/settings.php?homepage=' + option[i].value);
				break;
			}
		}		
	}
}
function onUniverseBackgroundChange(e)
{
	var t = COMMON_GetTarget(e);
	var optionList = t.getElementsByTagName('option');
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].selected)
		{
			// Récupération des infos background
			AJAX_SendRequest('./ajax/universe.php?universeBGInfo=' + optionList[i].value);
			return;
		}
	}
}
function onUniverseListChange(e)
{
	var t = COMMON_GetTarget(e);
	var optionList = t.getElementsByTagName('option');
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].selected)
		{
			// Form Add Universe
			//document.getElementById(t.id + "Span").innerHTML = optionList[i].innerHTML;
			if (optionList[i].value == '+')
				COMMON_show_AddFormRequest('./home/layout_addUniverse.php', callbackAddUniverse, callbackOnCloseAddUniverse);
			else if (g_ListArrayRedirect[t.id])
			{
				/*if (g_bIsOwnerUniverse)
				{
					if (window.location.toString().indexOf("home") != -1)
						window.location = "./" + optionList[i].value;
					else
						window.location = "../home/" + optionList[i].value;						
				}
				else
					window.location = "../u/" + optionList[i].value;*/
				var hashValue = location.hash.split('/');
				if (location.hash.length == 0
					|| hashValue[0] == "#home"
					|| hashValue[0] == "#settings"
					|| hashValue[0] == "#com")
				{
					window.location.hash = "#home/" + optionList[i].value;
					//AJAX_SendRequest("./ajax/users.php?u=" + optionList[i].value + "&home");
				}
				else
				{
					window.location.hash = hashValue[0] + "/" + hashValue[1]+ "/" + optionList[i].value;
					//AJAX_SendRequest("./ajax/users.php?u=" + optionList[i].value);
				}
				COMMON_hideAddForm();
			}
			else
			{
				g_changingUniverseId = optionList[i].value;
			}
			return;
		}
	}
}

function callbackAddUniverse()
{
	document.getElementById("addInput").focus();
}

function callbackOnCloseAddUniverse()
{
	var optionList = document.getElementById("universeList");
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].value == g_iCurrentUniverse)
		{
			//document.getElementById("universeListSpan").innerHTML = optionList[i].innerHTML;
			optionList[i].selected = true;
		}
		else
		{
			optionList[i].selected = false;
		}
	}
}

function callbackUniverseBackground()
{
	var bgUniverseButton = document.getElementById("uploadUniverseButton");
	bgUniverseButton.onchange = function (){
	}
	backgroundId = 0;
	{
		var optionList = document.getElementById("universeListChangeBg").getElementsByTagName('option');
		for (var i = 0; i < optionList.length; i++)
		{
			if (optionList[i].selected)
			{
				backgroundId = optionList[i].value;
				break;
			}
		}
		if (backgroundId == 0)
			return false;
	}
	new AjaxUpload(bgUniverseButton,{
		//action: 'upload-test.php', // I disabled uploads in this example for security reasons
		action: './common/utilities/fileuploader.php?iduniverse=' + backgroundId, 
		name: 'file',
		onSubmit : function(file, ext){
			// change button text, when user selects file			
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
		},
		onComplete: function(file, response){					
			// enable upload button
			this.enable();
			readJSonResponseAndExecute(response);
			COMMON_hideAddForm();			
		}
	});
}

function callbackShareLink()
{
	document.getElementById("addInput").value = "http://";
	document.getElementById("addInput").select();
	document.getElementById("addInput").focus();	
}

function createUniverse()
{
	AW_Show(g_lang_universe_creating, false);
	DB_AddUniverse();
}

function modifyUniverse()
{
	AW_Show(g_lang_universe_modifying, false);
	DB_ModifUniverse();
}

function createUniverseAndMoveBox(idBox)
{
	AW_Show(g_lang_universe_creating, false);
	DB_AddUniverseAndMoveBox(idBox);
}

function 	DB_AddUniverseAndMoveBox(idBox)
{
	var sName = document.getElementById('addInput').value;

	var optionList = document.getElementsByName("addTypeRadio");
	var privacyValue = 0;
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].checked)
		{
			privacyValue = optionList[i].value;
			break;
		}
	}
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = './common/universe.php?addUniverseAndMoveBox&name=' + escape(sName) + '&privacy=' + privacyValue + '&idbox=' + idBox;	// Specifying which file to get
	ajaxObjects[ajaxIndex].onCompletion = function(){ DB_responseAddUniverse(ajaxObjects[ajaxIndex].response);};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
}

function DB_ModifUniverse()
{
	var sName = document.getElementById('addInput').value;

	var optionList = document.getElementsByName("addTypeRadio");
	var privacyValue = 0;
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].checked)
		{
			privacyValue = optionList[i].value;
			break;
		}
	}
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = './ajax/universe.php?modifyUniverseForm&id=' + g_iCurrentUniverse + '&name=' + escape(sName) + '&privacy=' + privacyValue;	// Specifying which file to get
	//ajaxObjects[ajaxIndex].requestFile = '../common/links.php?test=info&&url=' + escape(_sUrl);	// Specifying which file to get
	//alert(ajaxObjects[ajaxIndex].requestFile);
	ajaxObjects[ajaxIndex].onCompletion = function(){ DB_responseModifyUniverse(ajaxObjects[ajaxIndex].response);};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
}

function DB_responseModifyUniverse(_sResponse)
{
	if (_sResponse == "-4")
	{
		AW_Show(g_lang_universe_limit_charac, true);
		return;
	}
	else if (_sResponse == "-3")
	{
		AW_Show(g_lang_universe_min_charac, true);
		return;		
	}
	window.location.reload();
}

function DB_AddUniverse()
{
	var sName = document.getElementById('addInput').value;

	var optionList = document.getElementsByName("addTypeRadio");
	var privacyValue = 0;
	for (var i = 0; i < optionList.length; i++)
	{
		if (optionList[i].checked)
		{
			privacyValue = optionList[i].value;
			break;
		}
	}
	
	AJAX_SendRequest('./ajax/universe.php?addUniverse&name=' + escape(sName) + '&privacy=' + privacyValue);
}

function DB_responseAddUniverse(_iUniverseId)
{
	window.location.hash = "#home/" + _iUniverseId;
	AJAX_SendRequest('./ajax/users.php?u=' + _iUniverseId + "&home");
}
