/*
* Dojo declarations and functions for newArticle.html
*/

dojo.require("dojo.fx");
dojo.require("dojox.widget.Calendar");

dojo.addOnLoad(loadRecentArticles);

function hideArticle(articleId) {

	var theDiv = "article"+articleId;
	var fadeOut = dojo.fadeOut({node: theDiv, duration: 500});
	var wipeOut = dojo.fx.wipeOut({node: theDiv, duration: 500});
	var animOut = dojo.fx.combine([fadeOut,wipeOut]);
	var fadeIn = dojo.fadeIn({node: theDiv, duration: 500});
	var wipeIn = dojo.fx.wipeIn({node: theDiv, duration: 500});
	var animIn = dojo.fx.combine([fadeIn,wipeIn]);
	
	animOut.onEnd=function(){dojo.byId(theDiv).innerHTML = "<div style='border: 1px solid #3465a4; background-color: #729fcf; color: #CCCCCC; '; color: white;><img src='/static/images/loader.gif' style='vertical-align: middle; padding:15px;'/> <strong>caricamento in corso</strong></div>"; animIn.play()};
	animOut.play();
	
	setTimeout(function () {

		dojo.xhrGet({
			url:"/cms/ajaxCompress"+articleId,
			handleAs:"json",
			load: function(response, ioArgs) {
				animOut.onEnd=function(){dojo.byId(theDiv).innerHTML = response.content; animIn.play()};
				animOut.play();
				return response;
		},
			error: function(response, ioArgs) {
				dojo.byId(theDiv).innerHTML="<strong>Impossibile caricare articolo " + articleId + "</strong>";
				return response;
		}
	})
	}, 1500);
		
	
}

function showArticle(articleId) {

	var theDiv = "article"+articleId;
	var fadeOut = dojo.fadeOut({node: theDiv, duration: 500});
	var wipeOut = dojo.fx.wipeOut({node: theDiv, duration: 500});
	var animOut = dojo.fx.combine([fadeOut,wipeOut]);
	var fadeIn = dojo.fadeIn({node: theDiv, duration: 500});
	var wipeIn = dojo.fx.wipeIn({node: theDiv, duration: 500});
	var animIn = dojo.fx.combine([fadeIn,wipeIn]);
	
	animOut.onEnd=function(){dojo.byId(theDiv).innerHTML = "<div style='border: 1px solid #3465a4; background-color: #729fcf; color: #CCCCCC; '; color: white;><img src='/static/images/loader.gif' style='vertical-align: middle; padding:15px;'/> <strong>caricamento in corso</strong></div>"; animIn.play()};
	animOut.play();
	
	setTimeout(function () {

		dojo.xhrGet({
			url:"/cms/ajax"+articleId,
			handleAs:"json",
			load: function(response, ioArgs) {
				animOut.onEnd=function(){dojo.byId(theDiv).innerHTML = response.content; animIn.play()};
				animOut.play();
				return response;
		},
			error: function(response, ioArgs) {
				dojo.byId(theDiv).innerHTML="<strong>Impossibile caricare articolo " + articleId + "</strong>";
				return response;
		}
	})
	}, 1500);
		
}

function openComments(articleId) {
	elementUl = dojo.byId("comments_" + articleId);
	if (elementUl.getAttribute("class") == "visible")
		elementUl.setAttribute("class", "unvisible");
	else
		elementUl.setAttribute("class", "visible");
}

function loadRecentArticles() {
	targetDiv = dojo.byId("articleListContainer");
	
	recentIdList = "";
	
	dojo.xhrGet({
		url:"/cms/recentArticlesId",
		load: function(response, ioArgs) {
			recentIdList = response.split(",");
			for ( k = 0; k < recentIdList.length; k ++) {
				currentId = recentIdList[k];
				articleDiv = document.createElement("div");
				articleDiv.className="article";
				articleDiv.id="article"+currentId;
				targetDiv.appendChild(articleDiv);
				dojo.xhrGet({
					url:"/cms/ajaxCompress"+currentId,
					handleAs:"json",
					load: function(response, ioArgs) {
						articleId = response.id;
						content = response.content;
						dojo.byId("article"+articleId).innerHTML = content;
						return response;
					},
					error: function(response, ioArgs) {
						dojo.byId("article"+currentId).innerHTML="<strong>Impossibile caricare articolo " + currentId + "</strong>";
						return response;
					}
				});
			}
			return response;
		},
		error: function(response, ioArgs) {
			articleListContainer.innerHTML="<strong>Impossibile caricare lista degli articoli recenti.</strong>";
			return response;
		}
	});
}

function connectWidgets() {
}

