function _ArturoForum() {
	//
	// Support utilities
	//
	this.setCookie = function(cName, cValue, days) {
		if (!days) { days = 1; }
		var startDate = new Date(); var utime = startDate.getTime();
		startDate.setHours(0); startDate.setMinutes(0); startDate.setSeconds(0);
		var expDate = new Date(startDate.getTime()+(86400 * 1000 * days));
		var max_age = parseInt((expDate.getTime()- utime) / 1000);

		// build the cookie string
		var c = cName+"="+escape(cValue)+"; max-age="+max_age+"; expires="+expDate.toGMTString()+"; path=/;";
		document.cookie = c;
		return true
	}

	this.getCookie = function(name) {
		var cookieData = document.cookie;
		var start = cookieData.indexOf(name + "=");
		var len = start + name.length + 1;
		if ((!start) && (name != cookieData.substring(0, name.length))) {
			return '';
		}
		if (start == -1) { return ''; }
		var end = cookieData.indexOf(';', len);
		if (end == -1) { end = cookieData.length; }
		return unescape(cookieData.substring( len, end ));
	}

	this.detectFlash = function() {
		var movieName = 'ArturoFOM';
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			if(document[movieName].length != undefined) {
				return document[movieName][1];
			}
			return document[movieName];
		}
	}

	this.jsonSerialize = function(obj) {
		var a = new Array();
		for (key in obj) {
			a.push(key+": "+obj[key]);
		}
		return "{ "+a.join(", ")+" }";
	}

	this.cookieSerialize = function(obj) {
		var a = new Array();
		for (key in obj) {
			a.push(key+": "+obj[key]);
			if (a.join(",").length > 3800) { break; }
		}
		return "{"+a.join(",")+"}";
	}

	this.loadStoredInfo = function() {
		if (this.storage) {
			try	{
				this.readAll = parseInt(this.storage.readAll) || this.defaultReadall;
				this.readThread = eval("("+this.storage.readThread+")") || { };
			} catch (e) { }
		} else {
			try	{
				this.readAll = parseInt(this.getCookie("AFReadAll")) || this.defaultReadall;
				this.readThread = eval("("+this.getCookie("AFReadThread")+")") || { };
			} catch (e) { }

		}
	}

	this.saveStoredInfo = function() {
		if (this.storage) {
			this.storage.readAll = new String(this.readAll);
			this.storage.readThread = this.jsonSerialize(this.readThread);
		} else {
			this.setCookie("AFReadAll", this.readAll, 10);
			this.setCookie("AFReadThread", this.cookieSerialize(this.readThread), 2);
		}

	}

	this.parseId = function(str) {
		var res = new Object();
		var a1 = str.split("_");
		for (i = 0; i < a1.length; i++) {
			var a2 = a1[i].split(":");
			res[a2[0]] = parseInt(a2[1]);
		}
		return res;
	}

	this.threadIsUnread = function(info) {
		if (info.id) {
			var info = this.parseId(info.id);
		}
		if (!info) return false;
		var r = this.readThread[info["t"]] || 0;
		if (info["p"] <= this.readAll) {
			return false;
		}
		if (!r || r < info["p"]) { return true; }
		return false;
	}

	this.boardIsUnread = function(info) {
		if (info.id) {
			var info = info.id.split("-");
		}
		if (!info.length) { return false; }
		for (var i = 0; i < info.length; i++) {
			if (this.threadIsUnread(this.parseId(info[i]))) {
				return true;
			}
		}
		return false;
	}

	this.setThreadRead = function(thread, id) {
		if (!thread || !id) return;
		this.readThread[thread] = id;
		this.saveStoredInfo()
		if (this.flashStore) {
			this.flashStore.setThreadRead(thread, id);
		}
	}

	this.setAllRead = function(id) {
		if (!id) return;
		this.readAll = id;
		this.readThread = { };
		this.saveStoredInfo();
	}

	this.markUnread = function() {
		var el = null;
		var board_list = window.jQuery(".ArturoBoard");
		for (var i = 0; i < board_list.length; i++) {
			el = board_list.eq(i);
			if (this.boardIsUnread(el.get(0))) { el.addClass("ArturoUnread"); }
		}

		var thread_list = window.jQuery(".ArturoThread");
		for (var i = 0; i < thread_list.length; i++) {
			el = thread_list.eq(i);
			if (this.threadIsUnread(el.get(0))) { el.addClass("ArturoUnread"); }
		}

		var post_list = window.jQuery(".ArturoPost");
		for (var i = 0; i < post_list.length; i++) {
			el = post_list.eq(i);
			if (this.threadIsUnread(el.get(0))) {
			    el.addClass("ArturoUnread");
				// Per l'ultimo messaggio, marca il thread letto se necessario
				if ((i+1) >= post_list.length) {
					var info = this.parseId(el.attr("id"));
					try {
						if (this.readThread[thread] < info["lastpostid"]) { continue; }
					} catch (e) { }
					this.setThreadRead(parseInt(info["t"]), parseInt(info["p"]))
				}
			}
		}
	}

	this.markThreadRead = function(info) {
		if (this.threadIsUnread(info)) {
			if (info.id) { info = this.parseId(info.id); }
			if (info) {
				r =  this.readThread[info["t"]] || 0;
				if (info["p"] > this.readAll && r < info["p"]) {
					this.readThread[info["t"]] = info["p"];
				}
			}
		}
	}

	this.markBoardRead = function() {
		var el = null;
		var info = null;
		var r = null;

		var board_list = window.jQuery(".ArturoBoard");
		for (var i = 0; i < board_list.length; i++) {
			el = board_list.eq(i);
			info = el.get(0);
			if (this.boardIsUnread(info)) {
				if (info.id) { info = info.id.split("-"); }
				if (info.length) {
					for (var i = 0; i < info.length; i++) {
						this.markThreadRead(this.parseId(info[i]));
					}
				}
			}
		}

		var thread_list = window.jQuery(".ArturoThread");
		for (var i = 0; i < thread_list.length; i++) {
			el = thread_list.eq(i);
			this.markThreadRead(el.get(0));
		}

		this.saveStoredInfo();
		window.jQuery(".ArturoUnread").removeClass("ArturoUnread");
		window.jQuery(".img_thread_unread").remove();
		this.markUnread();
		alert('Tutti i messaggi di questo forum sono stati marcati come già letti');

	}

	this.markAllRead = function() {
		var el = null;
		var info = null;
		var maxId = this.defaultReadall;
		var board_list = window.jQuery(".ArturoBoard");
		for (var i = 0; i < board_list.length; i++) {
			info = board_list.eq(i).attr("id").split("-");
			for (var ii = 0; ii < info.length; ii++) {
				var pid = this.parseId(info[ii]);
				if (pid["p"] > maxId) { maxId = pid["p"]; }
			}
		}

		var thread_list = window.jQuery(".ArturoThread");
		for (var i = 0; i < thread_list.length; i++) {
			info = this.parseId(thread_list.eq(i).attr("id"));
			if (info["p"] > maxId) { maxId = info["p"]; }
		}

		this.setAllRead(maxId);
		window.jQuery(".ArturoUnread").removeClass("ArturoUnread");
		window.jQuery(".img_thread_unread").remove();
		this.markUnread();
		alert('Tutti i messaggi sono stati marcati come già letti');

	}


	this.markBookmarked = function() {
		if (!this.ArturoUserId) return;
		var ArturoUserId = this.ArturoUserId;
		window.jQuery(".ArturoThread").each(function() {
			var userList= window.jQuery(this).attr("rel").split("-");
			for (var i = 0; i < userList.length; i++) {
				if (ArturoUserId == parseInt(userList[i])) {
					window.jQuery(this).addClass("ArturoBookmarked");
				}
			}
		});


	}


	try	{ this.hostname = window.location.hostname; }
	catch (e) { }
	if (!this.hostname) { this.hostname = "community.videogame.it"; }
	this.storage = null;
	if (window.globalStorage) { this.storage = window.globalStorage[this.hostname]; }
	else if (window.localStorage) { this.storage = window.localStorage[this.hostname]; }
	else if (window["localStorage"]) { this.storage = window["localStorage"][this.hostname]; }

	this.defaultReadall = 0;
	this.flashStore = false;
	this.readAll = this.defaultReadall;
	this.readThread = {};
	this.loadStoredInfo();
	if (window.Arturo) {
		this.ArturoUserId = window.Arturo.UserID;
	} else {
		try {
			this.ArturoUserId = parseInt(this.getCookie("ArturoUserID").split("-")[0]);
		} catch (e) { }
	}
}

window.ArturoForum = new _ArturoForum();
window.jQuery().ready(function() {
	window.ArturoForum.markUnread();
	window.ArturoForum.markBookmarked();
	$(".ArturoMarkAllRead").click(function() { window.ArturoForum.markAllRead(); return false; });
	$(".ArturoMarkBoardRead").click(function() { window.ArturoForum.markBoardRead(); return false; });

});