function ArticleContainer( id ) {
	this.divID = id;
	this.articleurl = "/article-proxy/";
	this.clearReadArticlesText = "Olvasottak törlése";
	this.clearArticlesText = "Cikkek törlése";
	this.emptyArticlesText = "Tegyen cikket a cikkgyűjtőbe!";

	this.articles = new Array();
	this.articleFrames = new Array();

	this.cookieName = "ac-articles";

	this.splitChars = new String("-#-");
	this.splitLines = new String("-@-");
	this.loaded = false;
}
ArticleContainer.prototype.loadArticles = function() {
	var astr = this.getCookie(this.cookieName);

	this.loaded = true;

	if (!astr) return false;

	var tmp = new Array();
	tmp = astr.split(this.splitLines);
	for(var i = 0; i < tmp.length; i++) {
		if (!tmp[i]) continue;
		var tmp2 = tmp[i].split(this.splitChars);

		var a = new Array(
			[tmp2[0]],
			decodeURIComponent(tmp2[1]),
			[tmp2[2]]
		);
		this.articles.push(a);
	}
}
ArticleContainer.prototype.saveArticles = function() {
	if (!this.isLoaded()) return false;

	var str = "";
	for(var i = 0; i < this.articles.length; i++) {
		var item = this.articles[i];

		var aid = item[0];
		var title = item[1];
		var read = item[2];

		str += aid + this.splitChars + encodeURIComponent(title) + this.splitChars + read + this.splitLines;
	}
	var path = "/";
	var exp = new Date();
	exp.setTime(exp.getTime() + (365 * 24 * 60 * 60 * 1000));
	return this.setCookie(this.cookieName, str, exp, path);
}
ArticleContainer.prototype.showArticles = function() {
	this.updateFrames();

	var d = document.getElementById(this.divID);
	if (!d) return false;

	/*
	if (jQuery) {
		window.status = "s";
	}
	*/

	d.innerHTML = "";

	if (this.articles.length) {
		var ul = document.createElement("UL");
		for(var i = 0; i < this.articles.length; i++) {
			if (!this.articles[i]) continue;

			var item = this.articles[i];

			var id = item[0];
			var title = item[1];
			var read = item[2];

			var li = document.createElement("LI");
			li.className = (read > 0) ? "on" : "off";

			var a = document.createElement("A");
			a.href = this.articleurl + id + '/';
			a.innerHTML = title;

			li.appendChild(a);

			ul.appendChild(li);
		}

		d.appendChild(ul);

		// remove read
		if (this.clearReadArticles.length) {
			a = document.createElement("A");
			a.href = "javascript:clearReadArticles(); void(0);";
			//a.href = "#";
			a.setAttribute("onclick", "clearReadArticles(); return false;");
			a.onclick = "clearArticles(); return false;"
			a.className = "clearread";
			a.innerHTML = this.clearReadArticlesText;

			d.appendChild(a);
		}

		// remove all
		if (this.clearArticles.length) {
			var a = document.createElement("A");
			a.href = "javascript:clearArticles(); void(0);";
			//a.href = "#";
			a.setAttribute("onclick", "clearArticles(); return false;");
			a.onclick = "clearArticles(); return false;"
			a.className = "clearall";
			a.innerHTML = this.clearArticlesText;

			d.appendChild(a);
		}
	} else {
		d.innerHTML = this.emptyArticlesText;
	}
}
ArticleContainer.prototype.updateFrames = function() {
	for(var i = 0; i < this.articleFrames.length; i++) {
		if (!this.articleFrames[i]) continue;

		var data = this.articleFrames[i];
		var pos = this.inContainer(data[0]);
		if (pos == null) {
			this.showControl(data[0], data[1]);
		} else {
			this.hideControl(data[0], data[1]);
		}
	}
}
ArticleContainer.prototype.addArticle = function( aid, title ) {
	if (!this.inContainer(aid)) {
		var a = new Array(aid, title, 0);
		this.articles.push(a);
		if (this.saveArticles()) this.showArticles();
	}

	return false;
}
ArticleContainer.prototype.readArticle = function( aid ) {
	var pos = this.inContainer(aid);
	if (pos === null) return false;

	this.articles[pos][2]++;
	if (this.saveArticles()) {
		this.showArticles();
		return true;
	}
	return null;
}
ArticleContainer.prototype.clearArticles = function() {
	this.articles = new Array();
	if (this.saveArticles()) {
		this.showArticles();
		return true;
	}
}
ArticleContainer.prototype.clearReadArticles = function() {
	var newArticles = new Array();
	for(var i = 0; i < this.articles.length; i++) {
		if (!this.articles[i]) continue;

		var item = this.articles[i];
		if (item[2] > 0) continue;

		newArticles.push(item);
	}
	this.articles = newArticles;
	if (this.saveArticles()) {
		this.showArticles();
		return true;
	}
}
ArticleContainer.prototype.showControl = function( aid, win ) {
	return this.setControlVisibility(aid, win, 'visible');
}
ArticleContainer.prototype.hideControl = function( aid, win ) {
	return this.setControlVisibility(aid, win, 'hidden');
}
ArticleContainer.prototype.setControlVisibility = function( aid, win, visible ) {
	if (!win) window.alert(aid);
	var i = win.document.getElementById('ac_' + aid);
	if (!i) return;
	i.style.visibility = visible;

	return true;
}
ArticleContainer.prototype.inContainer = function( id ) {
	for(var i = 0; i < this.articles.length; i++) {
		if (!this.articles[i]) continue;
		if (this.articles[i][0] == id) return i;
	}
	return null;
}
ArticleContainer.prototype.inFrames = function( id ) {
	for(var i = 0; i < this.articleFrames.length; i++) {
		if (!this.articleFrames[i]) continue;
		if (this.articleFrames[i][0] == id) return i;
	}
	return null;
}
ArticleContainer.prototype.isLoaded = function() {
	return this.loaded;
}
ArticleContainer.prototype.registerArticle = function( aid, win ) {
	var a = new Array(aid, win);
	this.articleFrames.push(a);

	if (this.inContainer(aid) !== null) {
		this.hideControl(aid, win);
		return false;
	}

	this.showControl(aid, win);
}
ArticleContainer.prototype.getCookie = function( name ) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}
ArticleContainer.prototype.setCookie = function( name, value, expires, path, domain, secure ) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	return true;
}

/* GLOBAL FUNCTIONS */
function getArticleContainer() {
	if (window.parent && window.parent.articleContainer) return window.parent.articleContainer;
	else if (window.articleContainer) return window.articleContainer;

	return null;
}

function addArticle( href, aid, title ) {
	var ac = getArticleContainer();
	if (!ac) return false;

	if (ac.addArticle(aid, title)) {
		href.style.visibility = 'hidden';
	}

	return false;
}
function readArticle( aid ) {
	var ac = getArticleContainer();
	if (!ac) return false;

	return ac.readArticle(aid);
}
function registerArticle( aid, window ) {
	var ac = getArticleContainer();
	if (!ac) return false;

	return ac.registerArticle(aid, window);
}
function clearArticles() {
	var ac = getArticleContainer();
	if (!ac) return false;

	return ac.clearArticles();
}
function clearReadArticles() {
	var ac = getArticleContainer();
	if (!ac) return false;

	return ac.clearReadArticles();
}
