var closedUrl;
var openUrl;
var thumbs = null;

function initThumbs () {
	thumbs = initThumbArray();
}

/*
loadThumbs
	loads the thumbnail images in the passed list id
	if they haven't already been loaded
*/
function loadThumbs (elementId) {
	var thingy = document.getElementById(elementId);
	
	if (thingy._loaded) { return true; }
	
	if (thingy.className.indexOf('thumbslist') == -1) { 
		thingy._loaded = true;
		return false; 
	}
	
	var imgs = thingy.getElementsByTagName('img');
	
	for (var i = imgs.length - 1; i >= 0; i--) {
		if (imgs[i].src == '') { imgs[i].src = thumbs[imgs[i].id]; }
	}
	
	thingy._loaded = true;
}

/*
initRolls
	prepares the grouped archives page for all its js activity
*/
function initRolls (closed, open) {
	// getting rid of the link to non-js version
	var infobox = document.getElementById('infobox');
	if (infobox) { infobox.parentNode.removeChild(infobox); }
	
	// setting the urls of the closed and open arrow bgr images
	closedUrl = closed;
	openUrl = open;
	
	var word = 'Link';
	var elementId = 'archiveslist';
	
	// initialising the main players
	initThumbs();
	initRoll(elementId + word, elementId, false);
	
	var thingy = document.getElementById(elementId);
	var lists = thingy.getElementsByTagName('ul');
	
	// closing the rest, well almost all the rest
	for (var i = lists.length - 1; i >= 0; i--) {
		var isClosed = i < 2 ? false : true ;		// opening the first month's archives
		initRoll(lists[i].id + word, lists[i].id, isClosed);
		if (!isClosed) { loadThumbs(lists[i].id); }
	}
}

/*
initRoll
	taking care of the comments 'roll/slide' effect
*/
function initRoll (linkId, rollId, close) {
	if (close == null) { close = true; }
	
	var linkThingy = document.getElementById(linkId);
	var rollThingy = document.getElementById(rollId);
	
	linkThingy._rollId = rollId;
	
	if (setFullHeight(rollId) == false) { return false; }
	
	var currentURI = unescape(window.location);

	if (close) { 
		linkThingy.className = "closed";
		rollThingy.style.display = "none"; 
	}
	rollThingy._closed = close;
	
	document.getElementById(linkId).onclick = rollArchives;
}

function rollArchives () {
	var elementId = this._rollId;
	var linkId = this.id;
	var thingy = document.getElementById(elementId);
	var frames = 25/*Math.ceil(thingy._fullHeight / 25)*/;
	var frameRate;
	var currentFrame;

	// making sure animation is not in progress
	if (thingy._currentlyRolling) { return false; }
	
	// making it visible without a roll
	// if browser doesn't support offsetHeight
	if (!thingy._fullHeight) { 
		thingy.style.display = "";	
		return false; 
	}
	
	if (thingy._closed) {
		thingy.style.height = "0px";
		thingy.style.display = "";
		document.getElementById(linkId).style.backgroundImage = "url(" + openUrl + ")";
		currentFrame = 0;
		frameRate = 1;
		loadThumbs(elementId);
	} else {
		setFullHeight(elementId);	// take care of any new additions
		currentFrame = frames;
		frameRate = -1;
	}
	
	thingy._currentlyRolling = window.setInterval(
			/*
			function
				performs a single frame of the rolling as per its current status
				Thanks a lot Souvik (http://souvikdg.blogspot.com) for brainstorming the maths with me 
			*/
			function () {
				//thingy = document.getElementById(elementId);
				
				var angle = (Math.PI / 2) * ((2 * currentFrame - frames) / frames);
				var slope = Math.round( Math.sin(angle) * 100) / 100;
				thingy._currentHeight = (thingy._fullHeight / 2) + Math.round(thingy._fullHeight * slope / 2 );
				currentFrame += frameRate;	// moving the counter
				
				thingy.style.height	= thingy._currentHeight + "px";
				
				if(currentFrame > frames || currentFrame < 0)
				{
					window.clearInterval(thingy._currentlyRolling);
					thingy._currentlyRolling	= null;
					if (currentFrame <= 0)	{
						thingy._closed = true;
						thingy.style.display = "none";
						document.getElementById(linkId).style.backgroundImage = "url(" + closedUrl + ")";
					} else {
						thingy._closed = false;
					}
					thingy.style.height = "";
				}
			},
			15 // the actual frame rate
		);
	return false;
}

/*
setFullHeight
	sets the offsetHeight of the elementId as _fullHeight
	used for the comments roll
*/
function setFullHeight (elementId) {
//	var elementId = 'comments'; // not taking as input to restrict duplication
	var thingy = document.getElementById(elementId);
	
	if (!thingy) { return false; }
	if (thingy.offsetHeight) {
		thingy._fullHeight = thingy.offsetHeight;
		return true;
	}
	return false;
}
