// configuration
var divComments;
var divCommentsToggle;
var divAddComment;
var arrComments;
var lnkToggleComments;
var strShowPages;
var strPage;

// initiating the image gallery 
var Comments = {

    init: function() {
    
		divComments = $('comments');
	
		if (divComments) {
		
			// Get comments and add comment
			arrComments = divComments.select('div.comment');
			divAddComment = $('edit-comment');
			
			// Create comments toggle
			divCommentsToggle = document.createElement('div');
			divCommentsToggle.setAttribute('id', 'comments-toggle');
			divComments.appendChild(divCommentsToggle);
			divCommentsToggle = $('comments-toggle');
			
			// Fill comments toggle
			for(i=1;i<arrComments.length;i++) { 
				arrComments[i].remove();
				divCommentsToggle.appendChild(arrComments[i]);
			}
			divAddComment.remove();
			divCommentsToggle.appendChild(divAddComment);
			
			// Add show comments containe
			divToggleCommentsContainer = document.createElement('div');
			divToggleCommentsContainer.setAttribute('id', 'toggle-comments');
			divComments.appendChild(divToggleCommentsContainer);
			
			// Add show comments link
			lnkToggleComments = document.createElement('a');
			lnkToggleComments.setAttribute('href', '#hide-comments');
			lnkToggleComments.className = 'Hide-comments';
			lnkToggleComments.innerHTML = (arrComments.length<=1)?'Hide':'Hide comments';
			divToggleCommentsContainer.appendChild(lnkToggleComments);
			Event.observe(lnkToggleComments, 'click', Comments.toggle);
			
			/* get the pages whos comments should be open */
			strShowPages = readCookie('showPages');
			
			// Get the page name
			strPage = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1, window.location.pathname.lastIndexOf('.'));
			
			// If this page is not set to show
			if(strShowPages.indexOf('.' + strPage + '.')) {Comments.toggle();}

		}
    },
    toggle: function() {
    
		// Text of the link, if there are 0 or 1 comments
		if (arrComments.length<=1) {
			lnkToggleComments.innerHTML = (lnkToggleComments.innerHTML=='Add comment') ? 'Hide':'Add comment';
		} else {
			lnkToggleComments.innerHTML = (lnkToggleComments.innerHTML=='Show comments') ? 'Hide comments':'Show comments';
		}
		
		lnkToggleComments.className = (lnkToggleComments.className=='show-comments') ? 'hide-comments':'show-comments';
		lnkToggleComments.setAttribute('href', (lnkToggleComments.getAttribute('href')=='#show-comments') ? '#hide-comments':'#show-comments');
		
		if(divCommentsToggle.visible()) {
			// Remove the page from the list
			strShowPages=strShowPages.substring(0,strShowPages.indexOf('.'+strPage+'.')+1)+strShowPages.substring(strShowPages.indexOf('.'+strPage+'.')+strPage.length+2,strShowPages.length);
		} else {
			// Add the page to the list
			strShowPages=strShowPages==''?'.'+strPage+'.':strShowPages.indexOf('.'+strPage+'.')>-1?strShowPages:strShowPages+strPage+'.';
		}
		
		divCommentsToggle.toggle();
		createCookie('showPages',strShowPages,'');
		
    }
}

Event.observe(window, 'load', Comments.init);
