
var voteTickArray = new Array();



function replaceText(el, text)
{
	if (el != null)
	{
	clearText(el);
	var newNode = document.createTextNode(text);
	el.appendChild(newNode); 
	}
}




function getText(el)
{
	var text = "";
	if (el!= null)
	{
		if (el.childNodes)
		{
			for (var i=0;i<el.childNodes.length; i++)
			{
				var childNode = el.childNodes[i];
				if (childNode.nodeValue != null)
				{
					text = text + childNode.nodeValue;
				}
			}
		}
	}
	return text;
}

function getTextFromComment(el)
{
	var text = "";
	if (el!= null)
	{
		if (el.childNodes)
		{
			for (var i=0;i<el.childNodes.length; i++)
			{
				var childNode = el.childNodes[i];
				if (childNode.nodeValue != null)
				{
					text = text + childNode.nodeValue;
				} else
				{
				    
				    if ((childNode.tagName == "A")  && 
				        (childNode.href.match(/#\d+$/)) && 
				        (i+2 < el.childNodes.length) &&
				        (el.childNodes[i+1].nodeValue == ": ") &&
				        (el.childNodes[i+2].tagName == "I")
				        )
				    {
				    	text = text + "";
				    	i=i+2;
				    } else
				    {
					if (childNode.className != "reply")
					{
						
						if (childNode.tagName == "B") { text = text + "<B>"; }
						if (childNode.tagName == "I") { text = text + "<I>"; }
					    if (childNode.tagName == "STRIKE") { text = text + "<STRIKE>"; }
						text = text + getTextFromComment(childNode)
						if (childNode.tagName == "B") { text = text + "</B>"; }
						if (childNode.tagName == "I") { text = text + "</I>"; }
					    if (childNode.tagName == "STRIKE") { text = text + "</STRIKE>"; }
					}
					}
				}
			}
		}
	}
	return text;
}



function clearText(el)
{

	if (el!= null)
	{
		if (el.childNodes)
		{
			for (var i=0;i<el.childNodes.length; i++)
			{
				var childNode = el.childNodes[i];
				el.removeChild(childNode);
			}
		}
	}
}


function commentvote()
{
	var parent = this.parentNode;
	
	
	if (parent.className == "vote dn")
	{
		if (!confirm("Are you sure that this comment is Illegal or in violation of our Terms of Service?\\nInappropriate should not be used to suppress dissenting views."))
		{
			return false;
		}
		
	}
	parent.removeChild(this);
	
	
	
	
	var cid = parent.parentNode.id.replace("h","");
	var dir = "up";
	if (parent.className == "vote dn")
	{
		dir = "dn";
		replaceText(parent,"flag inappropriate");
	} else
	{
		replaceText(parent,"vote cool");
	}
	
	var headlines = document.getElementsByTagName("h1");	
	
	sendVoteRequest("/post?action=vote&resp=xml&dir=" + dir + "&commentid=" + cid + "&storyid=" + headlines[0].id.replace("s","")); 
	
	return false;
}


function queuevoteup()
{
	var parent = this.parentNode;
	var sqid = parent.parentNode.id.replace("sq","");	
	sendVoteRequest("/post?action=votequeue&resp=xml&score=1&channel_id=1&vote_on_story=1&story_queue_id=" + sqid);	
	return false;

}

function queuevotedn()
{
	var parent = this.parentNode;
	var sqid = parent.parentNode.id.replace("sq","");
	sendVoteRequest("/post?action=votequeue&resp=xml&score=-0.5&channel_id=1&vote_on_story=1&reason=9&story_queue_id=" + sqid);	
	return false;
}


function queuevotehlup()
{
	var parent = this.parentNode;
	
	var sqid = parent.parentNode.id.replace("hl","");	
	sendVoteRequest("/post?action=votequeue&resp=xml&score=1&channel_id=1&vote_on_story=0&story_queue_id=" + sqid);	
	return false;

}

function queuevotehldn()
{
	var parent = this.parentNode;
	var sqid = parent.parentNode.id.replace("hl","");
	sendVoteRequest("/post?action=votequeue&resp=xml&score=-1&channel_id=1&vote_on_story=0&reason=9&story_queue_id=" + sqid);	
	return false;
}



 function reply(cid, username)
	{
	
	
	
	var currentText = document.getElementById("comment").value;

	if (currentText != "")
	{
	 currentText = currentText + "\n";
	}
	var comment = getTextFromComment(document.getElementById("c" + cid));
	
	
	// var heading = document.getElementById("h" + cid);
	// var username = getText(heading.getElementsByClassName("ui").item(0).getElementsByTagName("a").item(0));
	
	//var username = getText($$("#h"+cid + " .ui a").toArray()[0]);
	
	
	comment = comment.replace(/<a href=\"#\d+\">/,"quoted");
	comment = comment.replace(/^[\n\r\t ]+/,"");
	comment = comment.replace(/[\n\r\t ]+$/,"");
	
	
	comment = "<quote user=\"" + username + "\" cid=\"" + cid + "\">" + comment + "</quote>";
	
	    var commentBox = document.getElementById("comment");
	    
	commentBox.value = currentText + comment + "\n\n";
	commentBox.focus();
	
        if(commentBox.createTextRange) {
            var range = commentBox.createTextRange();
            range.move('character', commentBox.value.length);
            range.select();
        }
        else {
            if(commentBox.selectionStart) {
                commentBox.focus();
                commentBox.setSelectionRange(commentBox.value.length, commentBox.value.length);
            }
            else
                commentBox.focus();
        }
    
    

	}

function createRequest()
{
	var request = null;
	try
	{
		request = new XMLHttpRequest();
	} catch (trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (dumbmicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failureisanoption)
			{
				request = null;
			}
		}
	} 
	
	if (request == null)
	{
		alert("Error creating request object!");
	} else {
		return request;
	}	
	
}

var request = createRequest(); 

function tickVoteScore(commentid, startscore, endscore)
{


	clearTimeout(voteTickArray[commentid]);
 
	var heading = document.getElementById("h" + commentid);
	
	
	

	var score = $$("#h" + commentid + " .score .small").toArray()[0];



	var newscore = parseFloat(startscore);
	var newendscore = parseFloat(endscore);
	var step = 0.003;
	if (newscore>newendscore)
	{
		step = -0.003;
	}
	
	newscore += step;
	


	var diff = newscore-newendscore;
	if (diff<0) { diff = 0-diff; }
	
	if (diff>0.005)
	{
		replaceText(score,  newscore.toFixed(3));		
		voteTickArray[commentid]=setTimeout("tickVoteScore(" + commentid + "," + newscore.toFixed(3) +","+ newendscore.toFixed(3) + ")",180);
	} else
	{
		replaceText(score,  newscore.toFixed(0));	
	}

	
}

function updateVoteScore()
{

	
	
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			var xmlDoc = request.responseXML;

			
			if (document.URL.match("/comments"))
			{
				if (request.responseXML.getElementsByTagName("error").length>0)
				{
					var message = xmlDoc.getElementsByTagName("error")[0].firstChild.nodeValue;
					alert(message);
					return;
				}
			
				var commentId = xmlDoc.getElementsByTagName("comment-id")[0].firstChild.nodeValue;
				
				var displayScore = xmlDoc.getElementsByTagName("display-score")[0].firstChild.nodeValue;
				var finalScore = xmlDoc.getElementsByTagName("final-score")[0].firstChild.nodeValue;
				
			
				tickVoteScore(commentId,displayScore,finalScore);
			}
			
			if (document.URL.match("/queue"))
			{
			
			var scores = xmlDoc.getElementsByTagName("storyscore");
			
			for (var i=0;i<scores.length;i++)
			{
			    replaceText($("score" + scores[i].attributes["id"].nodeValue),  scores[i].attributes["score"].nodeValue)			
			}
			
			scores = xmlDoc.getElementsByTagName("hlscore");
			
			for (var i=0;i<scores.length;i++)
			{
			    replaceText($("scorehl" + scores[i].attributes["id"].nodeValue),  scores[i].attributes["score"].nodeValue)			
			}
			}
		}
		
	}

}
function sendVoteRequest(url)
{
	
	request.onreadystatechange = updateVoteScore;

	request.open("GET",url,true);
	
	request.send(null);
	
	
	
}



function addVoteHandlers()
{


	if (document.URL.match("/comments/"))
	{
	correctHistoryLinks();

	var voteLinks = document.getElementsByClassName("vote");
	
	for (var i=0;i<voteLinks.length; i++)
	{
		var actualLink = voteLinks[i].getElementsByTagName("a");		 	
		actualLink[0].onclick = commentvote;
//		var commentId = voteLinks[i].parentNode.id.replace("h","");		
//		actualLink[0].href = "#" + commentId;		
 
	}	
	}	
	
	if (document.URL.match("/queue"))
	{
	
	var voteLinks = document.getElementsByClassName("voteup");	
	for (var i=0;i<voteLinks.length; i++)
	{
		var actualLink = voteLinks[i];			
		actualLink.onclick = queuevoteup;					
	}	
	
	var voteLinks = document.getElementsByClassName("votedn");	
	for (var i=0;i<voteLinks.length; i++)
	{
		var actualLink = voteLinks[i];			
		actualLink.onclick = queuevotedn;					
	}	
	
	var voteLinks = document.getElementsByClassName("votehlup");	
	for (var i=0;i<voteLinks.length; i++)
	{
		var actualLink = voteLinks[i];			
		actualLink.onclick = queuevotehlup;					
	}	
	
	var voteLinks = document.getElementsByClassName("votehldn");	
	for (var i=0;i<voteLinks.length; i++)
	{
		var actualLink = voteLinks[i];			
		actualLink.onclick = queuevotehldn;					
	}	
	
	
	
	
	}	
}

function clearAllReplies()
{
alert('ok');
}

function correctHistoryLinks()
{
	var commentHeaders = document.getElementsByClassName("ch");
	var commentsOnThisPage = new Array();
	
	for (var i=0;i<commentHeaders.length; i++)
	{
		var cid = commentHeaders[i].id.replace("h","");
		commentsOnThisPage[cid]=1;
	}
	
	var allLinks = document.getElementsByTagName("a");
	
	var storyId = $$("h1").toArray()[0].id.replace("s","");
	
	var thisPageWithoutAnchor = document.URL + "#";
	
	thisPageWithoutAnchor=thisPageWithoutAnchor.substring(0,thisPageWithoutAnchor.indexOf("#"));

	for (var i=0;i<allLinks.length;i++)
	{
	
		if (allLinks[i].href.indexOf(thisPageWithoutAnchor + "#")==0)
		{
			
			var url = allLinks[i].href.substring(thisPageWithoutAnchor.length+1);
			if (commentsOnThisPage[url] != 1)
			{
				allLinks[i].href = "/comments/" + storyId + "/c" + url + "#" + url;
			}	
		} 
	}
	

	
	
	
}

