document.observe("dom:loaded", function() {
	var i = 1;
	while($('article'+i) != null){
		$('article'+i).observe('click', populateArticle.curry(i));
		i++;
	}
	if (popupArticleAtStart == true && popupArticleContent != null && popupArticleContent.length > 1){
		populateArticle(popupArticleContent.length-1);
	}
//	alert(i + "articles");
//	$('article2').observe('click', populateArticle.curry('popupArticle2'));
//	$('article3').observe('click', populateArticle.curry('popupArticle3'));
});

function populateArticle(article)
{
	// Window dimensions
	if (popupArticleContent != null && popupArticleContent.length > article && popupArticleContent[article] != ""){
		scroll(0,0);
		var leftOffset = document.body.clientWidth / 2 - 300
		$('printArticleInner').innerHTML = popupArticleContent[article];
		$('popupArticleMain').setStyle({ 
			position: 'absolute', top: '25px', left: leftOffset+'px', width: 640+'px', opacity: '1.0'
		});
	
		var myObj = document.createElement("div");
		myObj.id = "myOverlay";
		var height = (document.height !== undefined) ? document.height : document.body.offsetHeight;
		height += 25;
		if(height < $('popupArticleMain').getHeight() + 25) {
			height = $('popupArticleMain').getHeight() + 25;
		}
		new Insertion.Bottom(document.body, $(myObj));
		myObj.setStyle({ height: height + "px", opacity:'0.0' });
		
		new Effect.Appear('myOverlay', { duration: 0.4, from: 0, to: 0.6 });
		new Effect.Appear('popupArticleMain', { duration: 0.4, afterFinish: addListener.curry('popupArticleMain') });

		window.onscroll = function(){
			var height = (document.height !== undefined) ? document.height : document.body.offsetHeight;
			if ($('myOverlay').style.height != height){
				height += 25;
				if(height < $('popupArticleMain').getHeight() + 100) {
					height = $('popupArticleMain').getHeight() + 100;
				}
				if (parseInt($('myOverlay').style.height) < (height-26)){
					$('myOverlay').style.height = height+"px";
				}
			}
		};
	}
}

function addListener(article)
{
	var close = article + "Close";
	$(close).observe('click', closePopup.curry(article));
	close = article + "CloseBottom";
	$(close).observe('click', closePopup.curry(article));
}

function closePopup(article)
{
	new Effect.Fade(article, { duration: 0.4 });
	new Effect.Fade('myOverlay', { duration: 0.4, afterFinish: function(e) { if ($('myOverlay') != null) $('myOverlay').remove(); } });
	window.onscroll = null;
}

