var selectedText;
var canvas, context;
var x1, y1, x2, y2, dx, dy;
var remoteCommentCount = 0;
var localCommentCount = 0;
var selectedTab = 'note-link';
var lastTab;
var start, end;

/*
function toggle_content_display(name, lines){		
	if(document.getElementById(name + "-content").style.overflow=='visible'){
		document.getElementById(name + '-content').style.overflow='hidden';
		document.getElementById(name + '-content').style.height= lines+'em';
		document.getElementById(name + '-content-button').innerHTML='read more &raquo;';		
	}
	else{
		document.getElementById(name + '-content').style.overflow='visible';
		document.getElementById(name + '-content').style.height='100%';
		document.getElementById(name + '-content-button').innerHTML='show less &laquo;';
	}
}
*/

function getElementsByClass(node,searchClass,tag) {
  var classElements = new Array();
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("\b"+searchClass+"\b");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
	return classElements;
}

function showOnlyOneLayer(id){
	if(document.getElementById('comment-block-empty') && document.getElementById(id)){
		document.getElementById('comment-block-empty').innerHTML = document.getElementById(id).innerHTML;
	}
}


function showOnlyOneVideoLayer(id){
	//alert(id);
	if(document.getElementById('comment-video-block-empty') && document.getElementById(id)){
		document.getElementById('comment-video-block-empty').innerHTML =  document.getElementById(id).innerHTML ;
	}
}

function showNotesLayer(){
	showOnlyOneLayer('comment-block-note');
}

function showCommentsLayer(){
	showOnlyOneLayer('comment-block-comments');
}

function showFeaturedLayer(){
	showOnlyOneVideoLayer('item-list-type-video-0');
}



function makeBold(id){
	document.getElementById('note-link').style.color = "#D4600D";
	document.getElementById('comments-link').style.color = "#D4600D";
	document.getElementById('post-link').style.color = "#D4600D";

	document.getElementById(id).style.color = "black";
}


function setSelectedTab(tab_id){
	document.getElementById(tab_id).className='selected-tab';
	document.getElementById(selectedTab).className='inactive-tab';
	selectedTab = tab_id;
}




function getSelectedText(){

	var selectedText = (textSelection()) ? stripHTML(textSelection()) : false;
	var fullHtml = new String(document.getElementById('main_column').innerHTML);
	var fullText = new String(stripHTML(fullHtml));
	
	if(selectedText){		
		start = fullText.indexOf(selectedText);
		end = start + selectedText.length;
		//alert(start + " " + end);
		highlightWordsInText(start, end);
	}
	else{
		return false;
	}
}




function displayLayerId(id){
	document.getElementById(id).style.display = (document.getElementById(id).style.display=='block') ? 'none' :'block';
}

function hideLayerId(id){
	document.getElementById(id).style.display = (document.getElementById(id).style.display=='block') ? 'none' :'block';
}

function toggleLayerId(id){
	document.getElementById(id).style.display = (document.getElementById(id).style.display=='block') ? 'none' :'block';
}

function selectedCommentPostId(id){
	document.getElementById('comment_post_ID').value= id;
}

function stripHTML(oldString) {
   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              inTag = false;
              i++;
        }
        if(!inTag) newString += oldString.charAt(i);
   }
   return newString;
}

function stripHighlightHTML(oldString) {
   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              inTag = false;
              i++;
        }
        if(!inTag) newString += oldString.charAt(i);
   }
   return newString;
}


function highlightWordsInText(start , end){

	var fullText = new String(document.getElementById('main_column').innerHTML);
	
	//fullText = stripHTML(fullText);
	highlightedText = fullText;
	
	//var highlightedText = new String(fullText.substr(0, start-1) + "<span name='highlighted' id='highlighted'>" + fullText.substring((start-1), end) + "</span>" + fullText.substr((end), fullText.length));
	document.getElementById('main_column').innerHTML = highlightedText;

}


function highlightManyTextBlocks(start , end){

	var fullText = new String(document.getElementById('main_column').innerHTML);
	
	highlightedText = stripHTML(fullText);
	var highlightedText;
	
	for(var i=0; i < start.length; i++){
		highlightedText = new String(highlightedText.substr(0, start-1) + "<span name='highlighted' style='background-color: yellow'>" + highlightedText.substring((start-1), end) + "</span>" + highlightedText.substr((end), highlightedText.length));
		document.getElementById('main_column').innerHTML = highlightedText;
	}
}


function textSelection(){
	if (window.getSelection){
		return new String(window.getSelection());
	}
	else if (document.getSelection){
		return new String(document.getSelection());
	}
	else if (document.selection){
		return new String(document.selection.createRange().text);
	}
	else{
		return false;
	}
}


function viewCommentsInRange(){
	toggleLayerId('view-comments');  
}

function ifTextSelectedShowLeaveCommentForm(){
	if(textSelection() != false){
		toggleLayerId('leave-comment-form'); 
		getSelectedText();
		document.getElementById('start').value = start;
		document.getElementById('end').value = end;		
	}
}


function tabs(current){

}

function showComment(id){
	if(document.getElementById('comment-block-empty') && document.getElementById(id)){
		document.getElementById('comment-block-empty').innerHTML = document.getElementById(id).innerHTML;
	}
}


function windowLoaded() {
	var canvas = document.getElementById('contentContainer');	
	//canvas.addEventListener("onload"  ,mouseReleased,false);
	//canvas.addEventListener("mousedown"  ,switchActiveTab4();,false);	
	
	//switchActiveTab('tab1');
	//switchActiveTab('tab2');
	//document.getElementById('activeTab').value = 'tab3';

	document.getElementById('comment_contentIndex').value = -1;	
	showBlockComments();
	//switchActiveTab('tab4');
	switchActiveTab('tab3');
	showOnlyOneLayer('comments-div');

	if(window.location.hash){
		var hash = new String(window.location.hash);
		highlightParagraphWithTitle(hash.substring(1));
	}

}

function switchActiveTab(id) {
	var active;
	
	if(document.getElementById('activeTab').value){
		active = document.getElementById('activeTab').value;
	}
	else{
		active = 'tab3';
	}
		
	document.getElementById('activeTab').value = id;
	document.getElementById(active).id = id;
}



function mousePressed(evt) {
	//if(parentNode(evt).id == "contentContainer"){
	//	alert("mouseReleased");
	//}
}

function mouseDragged(evt) {

}

function mouseReleased(evt) {
	//alert(parentNode(evt).id);
	//if(parentNode(evt).id == "contentContainer"){
	// alert("mouseReleased");
	//}
	
}


function setTime(){

}

function getCommentNotification(){
	if(remoteCommentCount > localCommentCount ){
		alert("new comment");
	}
}

function addComment(){
	document.getElementById("commentCount").innerHTML = localCommentCount++;
}

function updateCycle() {
	setTime();
	getCommentNotification();
	setTimeout("updateCycle()",10000);
}

function showReplyBox(id){
	document.getElementById("reply-area-"+id).innerHTML = "<input type='text' name='name' />";
}

function parentNode(evt) {
	var targ;
	if (!evt) var evt = window.event;
	if (evt.target) targ = evt.target;
	else if (evt.srcElement) targ = evt.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}


function showBlockComments(){
	var contentIndex = document.getElementById('comment_contentIndex').value;
	var commentCount = document.getElementById('commentCount').value;
	var showErroMessage = true;
	
	document.getElementById('comment_index_div_null').style.display = 'none';
	
	for(var i = 0; i < commentCount; i++){
		if(document.getElementById('comment_index_' + i).value != contentIndex){
			document.getElementById('comment_index_div_' + i).style.display = 'none';
		}
		else{
			showErroMessage = false;
			document.getElementById('comment_index_div_' + i).style.display = 'block';		
		}
	}
	if(showErroMessage){
		document.getElementById('comment_index_div_null').style.display = 'block';
	}
}


function highlightParagraph(id){
	
	var size = document.getElementById('paragraph_count').value;
	for(var i = 0; i < size; i++){
		document.getElementById('comment_contentIndex_'+ i ).className = 'noHighlight';
	}	
	document.getElementById('comment_contentIndex_'+id).className = 'blockHighlight';

	window.location.hash = "#comment_contentIndex_" + id;

	document.getElementById('comment_contentIndex').value = id; // this is for form
}



function highlightParagraphNoJump(id){
	
	var size = document.getElementById('paragraph_count').value;
	for(var i = 0; i < size; i++){
		document.getElementById('comment_contentIndex_'+ i ).className = 'noHighlight';
	}	
	document.getElementById('comment_contentIndex_'+id).className = 'blockHighlight';

	document.getElementById('comment_contentIndex').value = id; // this is for form
}


function highlightParagraphWithTitle(title){
	
	var size = document.getElementById('paragraph_count').value;
	for(var i = 0; i < size; i++){
		document.getElementById('comment_contentIndex_'+ i ).className = 'noHighlight';
	}	
	document.getElementById(title).className = 'blockHighlight';


	document.getElementById('comment_contentIndex').value = id; // this is for form
}



function unHighlightAllParagraphs(){
	
	var size = document.getElementById('paragraph_count').value;
	for(var i = 0; i < size; i++){
		document.getElementById('comment_contentIndex_'+ i ).className = 'noHighlight';
	}	
}


function detectAndShowOnlyOneLayer(){

	if(document.getElementById('comment_contentIndex').value == -1){
		showOnlyOneLayer('comment-no-form-div');	
	}
	else{
		showOnlyOneLayer('comment-form-div');
	}

}
