menuOpen = false;
var p = new Image();
p.src = "/up.assets/images/loading-applicationicon.png";

var applications = new Object();

function showApplication(application, forceReload) {
	application = application.replace("#", "");
	toggleMenu(true);
	//alert("application: " + application);
	hideAllContainers();

	//if ($.inArray(application, new Array("welcome", "facegent")) == -1) return;
	
	if (!applications[application]) {
		applications[application] = new Object();
		applications[application]["tab"] = "";
		applications[application]["title"] = (applicationsTitles ? applicationsTitles[application] : "Home page");
	}
	
	var app = applications[application];
	
	if (!app["container"] || forceReload) {
		$("#applicationTitle").css("background-image", "url(/up.assets/images/loading-applicationicon.png)");
		$("#applicationTitle").text("Caricamento.");
		$("#applicationPrompt").text("Soltanto un momento...");
		
		$.post("/up.core/up." + application + ".php", function(data) { 
			app["container"] = data;
			$("#" + application + "Container").html(data);
			$.getScript("/up.core/up.authentication.script.php");
			$.getScript("/up.core/up." + application + ".script.php");
			displayApplication(application);
		} );
	} else {
		displayApplication(application);
	}

	var allTabs = "";
	for (var myApp in applications) {
		var appObject = applications[myApp];
		appObject["tab"] = "</div><div align=\"left\" id=\"" + myApp + "Tab\" class=\"tab tab" + (application == myApp ? "Selected" : "Unselected") + "\" onclick=\"document.location.href = '/#" + myApp + " '\"><div style=\"background-image: url(/up.assets/images/" + myApp + "-blockicon.png);\" class=\"tabImage\">" + appObject["title"] + "</div></div>";
		allTabs = allTabs + appObject["tab"];
	}
	
	$("#tabbar").html("<div id=\"menuButton\" onclick=\"toggleMenu();\"></div>" +allTabs);
}

function emulateClick(who) {
	$("#" + who + "Select").show();
	$("#" + who + "Select").trigger("click");
}

function changeProperty(fieldName, fieldValue, tableName, keyName, keyValue) {
	$.post("/up.core/up.property.php", {"fieldname": fieldName, "fieldvalue": fieldValue, "keyname": keyName, "keyvalue": keyValue, "tablename": tableName }, function(data) { } );
}

function displayApplication(application) {
	$("#applicationTitle").css("background-image", "url(/up.assets/images/" + application + "-applicationicon.png)");
	$("#applicationTitle").html(applicationsTitles[application] + ".");
	$("#applicationPrompt").html(applicationsDescriptions[application] + ".");
	$("#" + application + "Container").css("display", "block");
}

function showMenu() {
	$("#menuGroups").load("/up.core/up.menu.php");
}

function closeAllApplications() {
	applications = new Object();
}

function showLocation(who) {
	if (!isFieldEmpty(who.id.replace("Link", "Text"))) {
		var myval = $("#" + who.id.replace("Link", "Text")).val();
		window.open("http://maps.google.it?q=" + myval);
	}
}

function checkLocation(who) {
	var field = who.id.replace("Link", "Text");
	if (!isFieldEmpty(field)) {
		var myval = $("#" + field).val();
		$.post("/up.components/up.checklocation.php", {"location": myval}, function (data) { if (data == "") {alert("Nessuna corrispondenza trovata sulle mappe.");} else { $("#" + field).attr("value", data); autosizeMe(field); }  } );
	}
}

$(document).ready(function() {
	showMenu();
	$("#currentUser").load("/up.core/up.connecteduser.php");
});

$(window).resize(function() {
	$("#containers").css("height");
});

function focusText(field) {
	if (field.value == field.title) {
		field.value = "";
		placeholdText(field);
	}
}

function isFieldEmpty(field) {
	var fieldValue = $("#" + field).val();
	if (fieldValue.replace(/ /gi, "") == "") return true;
	if (fieldValue == $("#" + field).attr("title")) return true;
	return false;
}

function placeholdText(field) {
	field.style.color = (field.value != field.title) ? 'black' : 'gray';
}

function blurText(field) {
	if (field.value == "") {
		field.value = field.title;
		placeholdText(field);
	}
}

function setFieldValue(field, value, cont) {
	clearField(field);
	if(value != "") {
		$("#" + field).attr("value", value);
		$("#" + field).css("color", (isFieldEmpty(field) ? "gray" : "black"));
	}
	
	if ($("#" + field).attr("role") == "autosized") {
		autosizeMe(field);
	}
}

function showMessage(field, message, type) {
	$("#" + field + "-message").css('visibility', 'visible');
	$("#" + field + "-messageleft").css("background-image", "url(/up.assets/images/message" + type + "-left.png)");
	$("#" + field + "-messagecenter").css("background-image", "url(/up.assets/images/message" + type + "-center.png)");
	$("#" + field + "-messageright").css("background-image", "url(/up.assets/images/message" + type + "-right.png)");
	$("#" + field + "-messagetext").text(message);
}

function showControls(field) {
	
	if (!isFieldEmpty(field + "Text")) $("#" + field + "Controls").css("visibility", "visible");
}

function mailto(who) {
	if (!isFieldEmpty(who.id.replace("Link", "Text"))) {
		var myval = $("#" + who.id.replace("Link", "Text")).val();
		window.location = "mailto:" + myval;
	}
}

function followLink(who) {
	if (!isFieldEmpty(who.id.replace("Link", "Text"))) {
		var myval = $("#" + who.id.replace("Link", "Text")).val();
		window.open("http://" + myval);
	}
}

function phoneFormat(who) {
	if (!isFieldEmpty(who.id.replace("Link", "Text"))) {

		var myval = $("#" + who.id.replace("Link", "Text")).val();
		myval = myval.replace("+39" , "");
		myval = myval.replace(".", "");
		myval = myval.replace(/[^\d.]/g, "");
		
		var mylen = 0;
		
		var arr2 = new Array("02", "06");
		var arr3 = new Array(
			"390", "391", "392", "393", "330", "333", "334", "335", "336", "373", "338", "340",
			"343", "346", "347", "348", "349", "320", "323", "327", "328", "329", "380", "383",
			"388", "389", "334", "335", "336", "373", "338", "339", "360", "363", "366", "368",
			"010", "011", "019", "030", "031", "015", "035", "039", "040", "041", "045", "049",
			"050", "051", "055", "059", "070", "071", "075", "079", "080", "081", "085", "089",
			"090", "091", "095", "099", "345");
		var arr4 = new Array(
			"0121", "0122", "0123", "0124", "0125", "0131", "0141", "0142", "0143", "0144", "0161", 
			"0163", "0165", "0166", "0171", "0172", "0173", "0174", "0175", "0182", "0183", "0184",
			"0185", "0187", "0321", "0322", "0323", "0324", "0331", "0332", "0341", "0342", "0343",
			"0344", "0345", "0346", "0362", "0363", "0364", "0365", "0371", "0372", "0373", "0374",
			"0375", "0376", "0377", "0381", "0382", "0383", "0384", "0385", "0386", "0421", "0422",
			"0423", "0424", "0425", "0426", "0427", "0428", "0429", "0431", "0432", "0433", "0434",
			"0435", "0436", "0437", "0438", "0439", "0442", "0444", "0445", "0461", "0462", "0463",
			"0464", "0465", "0471", "0472", "0473", "0474", "0481", "0521", "0522", "0523", "0524",
			"0525", "0532", "0533", "0534", "0535", "0536", "0541", "0542", "0543", "0544", "0545",
			"0546", "0547", "0564", "0565", "0566", "0571", "0572", "0573", "0574", "0575", "0577",
			"0578", "0583", "0584", "0585", "0586", "0587", "0588", "0721", "0722", "0731", "0732",
			"0733", "0734", "0735", "0736", "0737", "0742", "0743", "0744", "0746", "0761", "0763",
			"0765", "0766", "0771", "0773", "0774", "0775", "0776", "0781", "0782", "0783", "0784",
			"0785", "0789", "0823", "0824", "0825", "0827", "0828", "0831", "0832", "0833", "0835",
			"0836", "0861", "0862", "0863", "0864", "0865", "0871", "0872", "0873", "0874", "0875",
			"0881", "0882", "0883", "0884", "0885", "0921", "0922", "0923", "0924", "0925", "0931",
			"0932", "0933", "0934", "0935", "0941", "0942", "0961", "0962", "0963", "0964", "0965",
			"0966", "0967", "0968", "0971", "0972", "0973", "0974", "0975", "0976", "0981", "0982",
			"0983", "0984", "0985");
		var arr6 = new Array("004191");
		
		if ($.inArray(myval.substring(0, 2), arr2) != -1) mylen = 2;	
		if ($.inArray(myval.substring(0, 3), arr3) != -1) mylen = 3;	
		if ($.inArray(myval.substring(0, 4), arr4) != -1) mylen = 4;	
		if ($.inArray(myval.substring(0, 6), arr6) != -1) mylen = 6;
		
		myval = myval.substring(0, mylen) + "." + myval.substring(mylen);
		$("#" + who.id.replace("Link", "Text")).attr("value", myval);
		autosizeMe(who.id.replace("Link", "Text"));
		$("#" + who.id.replace("Link", "Text")).trigger("blur");
	}	
}

function hideControls(field) {
	$("#" + field + "Controls").css("visibility", "hidden");
}

function clearField(field) {
	$("#" + field).attr("value", $("#" + field).attr("title"));
	$("#" + field).css("color", "gray");
	if ($("#" + field).attr("role") == "autosized") autosizeMe(field);
}

function showDefaultMessage(field, message) {
	showMessage(field.replace("Text", ""), message, 'default');
}

function showErrorMessage(field, message) {
	showMessage(field.replace("Text", ""), message, 'error');
}

function hideMessage(field) {
	$("#" + field.replace("Text", "") + "-message").css('visibility', 'hidden');
}

function toggleMenu(forceClose) {
	menuOpen = !menuOpen;
	if (forceClose) menuOpen = false;
	$("#menu").animate({height: (menuOpen ? "600px" : "0px")}, 200);
}

function autosizeMeObject(who) {
	autosizeMe(who.id);
}

function autosizeMe(who) {
	var whoo = $("#" + who);
	$("#sizeTester").css("font-weight", whoo.css("font-weight"));
	$("#sizeTester").css("font-size", whoo.css("font-size"));
	$("#sizeTester").text(whoo.val());
	whoo.css("width", ($("#sizeTester").width() + 15) + "px");
}
