﻿// JScript File
function PageHandler()
{

	this.getTitle = function() {
		return document.title.substring(0, document.title.lastIndexOf('-'));
	}
	
    this.getPageHash = function() 
    {
        var url = document.location.pathname;
        var pos = url.lastIndexOf(".");
        if(pos > 0)
        {
            url = url.substring(0,pos);
        }
        
        var hash = hex_md5(url);
        //return hash;
        return url;
    }
    
    this.populateComments = function(commentsArray)
    {
		var hash = this.getPageHash();
		
		for(var i = 0; i < commentsArray.length; i++)
        {
            //hör kommentaren till denna sida?
            if(commentsArray[i].pl == hash)
            {
                //Lägg till kommentaren
                var notFound = false;
                targets = commentsArray[i].path.split(':');
                target = document.getElementById('main');
                for (var j = 0; j < targets.length - 1; j++) {
                    try
                    {
					    target = target.childNodes[targets[j]];
					}
					catch(exc)
					{
					    //return;
					    notFound = true;
					}
                }
                
                if(!notFound)
                {
                    start = parseInt(commentsArray[i].start);
                    wordlength = parseInt(commentsArray[i].wordlength);
                    
                    html = target.innerHTML.substring(0, commentsArray[i].start);
                    html += '<span class="comment" onmouseout="commentmouseout(this);" onmouseover="commenthover(this);" onclick="commentclick(this);" id="comment' + commentsArray[i].CommentId + '">' + target.innerHTML.substring(start, start + wordlength) + '</span>';
                    html += target.innerHTML.substring(start + wordlength);
                    
                    //this.addCommentIcon(commentsArray[i].path, commentsArray[i].start, commentsArray[i].wordlength);
                    target.innerHTML = html;
                }
            }
        }
    }
    
    this.addCommentIcon = function(path, startAt, commentLength, commentId)
    {

        
    }
    
    this.getPagePath = function(node)
    {
        // find the containing element of the new dfn element
		var commentPath = ""; // commentPath = 1;2; means "the second element inside the first element"
		var obj = node;
		var objParent = obj.parentNode;
		var objBlock = obj;
		
		//go up in tree to look for named item (id=)
		while (objBlock.id != 'main') 
		{
			for(var i = 0; i < objParent.childNodes.length; i++)
		    {
				// TODO: remove textnode checks for Firefox
		        if(objParent.childNodes[i] == objBlock)
		        {
		            commentPath = i + ":" + commentPath;
		            break;
		        }
		    }
		    
			objBlock = objParent;
			objParent = objParent.parentNode;
		}
		
		return commentPath;
		
		// this function ends here ------------------------------------------------------------ 
		
		if (false && columns) { //????
			// count elements in each column until we reach the current column
			for (var i = 0; i < columns.length; i++) {
				if (columns[i] == objParent) {
					break;
				}
				else {
					for (var k = 0; k < columns[i].childNodes.length; k++) {
						if (columns[i].childNodes[k].tagName && columns[i].childNodes[k].className != 'additional') {
							commentPath++;
						}
					}
					// if the column is adjusted upwards, there is duplicated elements. 
					if (parseInt(columns[i].style.top) != 0)
						commentPath--;
				}
			}
			// if the column is adjusted upwards, there is duplicated elements. 
			if (parseInt(objParent.style.top) != 0)
				commentPath--;
		
		} // end if columns	
		// the elements of all preceding columns are counted, count the current column's elements
		// Skip all additionals. 
		for (var i = 0; i < objParent.childNodes.length; i++) {
			if (objParent.childNodes[i].tagName && objParent.childNodes[i].className != 'additional') {
				if (objParent.childNodes[i] == objBlock) {
					break;
				}
				else {
					commentPath++;
				}
			}
		}
		
		commentPath += '';
		objParent = obj.parentNode;
		while (objParent.className != 'column') {
			i = 0;
			skip = 0;
			while (objParent.childNodes[i] != obj) {
				if (objParent.childNodes[i].tagName == null || objParent.childNodes[i].className == 'additional') 
					skip++;
				i++;
			}
			i -= skip;
			obj = objParent;
			objParent = obj.parentNode;
			commentPath = commentPath + ';' + i;
		}
		return commentPath;
    }
    
    this.getStartAtPosForComment = function(el)
    {
        var startAt;
				
		var str = el.innerHTML;
		
	    startAt = str.indexOf('<SPAN class=comment id=newcomment');
	    if (startAt < 0) {
		    startAt = str.indexOf('<span id="newcomment"');
	    }
	    
	    re = /<span .*?>/ig;
	    str = str.substring(0, startAt);
	    while (arr = re.exec(str)) {
		    startAt -= (arr[0].length + 7);
	    }
	    re = /target=_blank/ig; // ie does not include quotation marks for <a target="_blank">
	    str = str.substring(0, startAt);
	    while (arr = re.exec(str)) {
		    startAt += 2;
	    }
	    re = /<br>/ig; // ie does not include end tag for <br> tags: <br />
	    str = str.substring(0, startAt);
	    while (arr = re.exec(str)) {
		    startAt += 2;
	    }
	    
	    return startAt;
    }
}
