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;

var selectedParagraphCookie;
var commentParentCookie;
/*
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;';
	}
}
*/

/* this hack was done without research of libraries or time.. sigh. :)
	 it could have been done so much better with a few tools i found
	 after writing this. like:
	 
	 http://jquery.com/
	 
	 now, that would have been nice.
*/

/* thank you http://www.quirksmode.org */
/*
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

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){
	//alert(document.getElementById(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) {

}


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 debugAlert(){
	//alert(id('comment_contentIndex').value);
}

function id(varname){
	return document.getElementById(varname);
}
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');
	//window.pageYOffset += 200;	
	//helpTab();


	//if(isset('activeTab')){
	var hasCommentBox = new String(document.getElementById('commentTextContainer').value);
	if(hasCommentBox){
		blockCommentTab();
		postCommentTabContent('loginLayer');
		allCommentsTab();
	}
	//alert("Back off ben! you're causing problems!");
	//}
	//commentIndex
	if(window.location.hash){
		var hash = new String(window.location.hash);
		//postCommentTab();
		//showCommentBlurb(id);
		//highlightParagraphWithTitle(hash.substring(1));
		//alert(hash.substring(22));
		paragraphCommentBubble('show-comment-jump',hash.substring(22));
	}
	YAHOO.util.Event.onAvailable("reportNav", YAHOO.example.onMenuBarAvailable);
	
	//var myLogReader = new YAHOO.widget.LogReader("myLogger");
	//var dd1 = new YAHOO.util.DD("myLogger"); 	
}

function isset(varname){
	if(typeof( window[ varname ] ) != "undefined") {
		return true;
	}
	else{ 
		return false;
	}
}

function switchActiveTab(id) {
	var active;
	var str = new String(document.getElementById('activeTab').value);
	//alert(str);
	if(str){
		active = document.getElementById('activeTab').value;
	}
	else{
		active = 'tab3';	
		/*
		for(var i = 0; i < 5; i++){
			var str = new String(document.getElementById('tab' + i).value);
			if(str){
				active = 'tab' + i;
			}
		}*/		
	}
		
	//alert(active);
	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 postCommentTabContent(id){
	if(!document.getElementById('userLoggedIn').value){
		document.getElementById('postCommentContents').innerHTML = document.getElementById(id).innerHTML;
	}
}

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; //> -1) ? document.getElementById('comment_contentIndex').value : -1;
	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 showPageComments(){
	var contentIndex = 0; //document.getElementById('comment_contentIndex').value; //> -1) ? document.getElementById('comment_contentIndex').value : -1;
	var commentCount = document.getElementById('commentCount').value;
	var showErroMessage = true;
	
	document.getElementById('comment_page_index_div_null').style.display = 'none';
	
	for(var i = 0; i < commentCount; i++){
		//alert(i);
		if(document.getElementById('comment_page_index_' + i).value != 0){
			document.getElementById('comment_page_index_div_' + i).style.display = 'none';
		}
		else{
			showErroMessage = false;
			document.getElementById('comment_page_index_div_' + i).style.display = 'block';		
		}
	}
	if(showErroMessage){
		document.getElementById('comment_page_index_div_null').style.display = 'block';
	}	
}





/*
function expandNavigationItem(id){
	var sections = new Array("menu_assessment", "menu_wayforward", "menu_appendices", "menu_about");
	
	for(var i =0; i < sections.length; i++){
		document.getElementById(sections[i]).style.display = 'none';
	}
	document.getElementById(id).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';
	//setStyleById('comment_contentIndex_'+id, 'color', 'red');
	//alert(get);
	var tag = id;
	if(tag < 3){
		tag = 'jumptoptag';
	}
	else{
		tag = tag - 1;
	}
	window.location.hash = "#comment_contentIndex_" + tag;
	document.getElementById('comment_contentIndex').value = id; // this is for form

}



function highlightAllParagraphs(){
	
	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_'+i).className = 'blockHighlight';
	}	
	document.getElementById('comment_contentIndex').value = 0; // 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';
	}	
	document.getElementById('comment_contentIndex').value = -1; // this is for form
}
*/

function toggleHighlightAllParagraphs(){
	if(document.getElementById('comment_contentIndex').value == 0){
		highlightAllParagraphs();
	}
	else{
		unHighlightAllParagraphs();	
	}	
}


function highlight(id){
		document.getElementById('comment_contentIndex_'+id).className = 'blockHighlight';
}

function highlightParagraphNoJump(id){

	
	var size = document.getElementById('paragraph_count').value;
	var highlighted = (document.getElementById('comment_contentIndex_'+id).className == 'blockHighlight') ? true : false;		
	
	for(var i = 0; i < size; i++){
		document.getElementById('comment_contentIndex_'+ i ).className = 'noHighlight';
	}	
	
	if(!highlighted){
		document.getElementById('comment_contentIndex_'+id).className = 'blockHighlight';
	}
	else{
		allCommentsTab();
	}
	
	document.getElementById('comment_contentIndex').value = id; // this is for form
}


function highlightParagraphWithTitle(title){
	
	var size;
	
	if(isset(document.getElementById('paragraph_count'))){
		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';

	//this is a bug. we need to get this info so that we can go to post automatically
	//document.getElementById('comment_contentIndex').value = id; // this is for form
	//window.location.hash = title;
	//window.scrollByLines(0,30);

	
	var id = title.substring(21);
	//alert(id);
	//paragraphCommentBubble('show-comment',id);
	
	var tag = id;
	if(tag < 3){
		tag = 'jumptoptag';
	}
	else{
		tag = tag - 1;
	}
	window.location.hash = "#comment_contentIndex_" + tag;

}



function unHighlightAllParagraphs(){
	
	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').value = "0"; // this is for form	
}


function detectAndShowOnlyOneLayer(){
/*
	if(document.getElementById('comment_contentIndex').value == -1){
		showOnlyOneLayer('comment-no-form-div');	
	}
	else{*/
		showOnlyOneLayer('comment-form-div');
//	}

}

function setContentIndex(id){
	document.getElementById('comment_contentIndex').value = id; // this is for form	
}

function getContentIndex(){
	document.getElementById('comment_contentIndex').value; // this is for form	
}

/*
function setContentIndex(id){
	document.getElementById('comment_contentIndex').value = id;
}
*/

function clearText(theField){
	if(theField.defaultValue == theField.value){
		theField.value = ""
	}
}

function setReplyId(id){
	document.getElementById('comment_parent').value = id; // this is for form	
	//createCookie('selectedParagraphCookie',document.getElementById('comment_contentIndex').value,1);
	//createCookie('commentParentCookie',id,1);
	//alert(document.getElementById('comment_parent').value);
}

function setCommentParent(id){
	document.getElementById('comment_parent').value = id;
}

function showCommentBlurb(id){
	if(id){
		var blurb = new String(document.getElementById('allTheCommentText-'+id).innerHTML);
		document.getElementById('topCommentBlurb').innerHTML = '<em>reply to:</em><blockquote class=\'commentquote\'>' + blurb.substr(0, 1000) + "</blockquote>";
	}
	else{
		var blurb = new String(document.getElementById('allTheCommentText-'+id).innerHTML);
		//document.getElementById('topCommentBlurb').innerHTML = '<em><small>Click on one of the <img src="wp-content/themes/commentpress/images/comment.gif"> icons for the paragraph you want to talk about.</small></em> </div>';	
	}
}

function changeBlockCommentText(id){
	if(id == 0){
		document.getElementById('commentState').innerHTML = "for this Page";
		document.getElementById('topCommentBlurb').innerHTML = "the entire page";
	}
	else if(id == 'all') {
		document.getElementById('commentState').innerHTML = "All (Paragraph &amp; Page)";	
	}
	else if(id == 'help') {
		document.getElementById('commentState').innerHTML = "Help";	
	}
	else if(id){
		document.getElementById('commentState').innerHTML = "Paragraph "+id;
		document.getElementById('topCommentBlurb').innerHTML = "Paragraph "+id;
	}
}


function isurrentParagraphHighlighted(id){
	if(document.getElementById('comment_contentIndex_'+id).className == 'blockHighlight'){
		return true;
	}
	else{
		return false;
	}
}
	
//high level functions			
function postCommentTab(){
	switchActiveTab('tab1');
	detectAndShowOnlyOneLayer('comment-form-div');
	changeBlockCommentText(document.getElementById('comment_contentIndex').value);
	setCommentParent(0);
	//setContentIndex(0);
	//highlightParagraphNoJump(getContentIndex());
	setReplyId(0);	
	//changeBlockCommentText();
	//alert(document.getElementById('comment_contentIndex').value);
}


function allCommentsTab(){
	switchActiveTab('tab3');
	showOnlyOneLayer('comments-div');
	changeBlockCommentText('all');
	unHighlightAllParagraphs(); 
}


function helpTab(){
	switchActiveTab('tab4');
	showOnlyOneLayer('comments-help');
	changeBlockCommentText('help');
}


function blockCommentTab(){
	switchActiveTab('tab2');
	showBlockComments();
	showOnlyOneLayer('comments-block-div'); 
}

function pageCommentsTab(){
	unHighlightAllParagraphs(); 
	showOnlyOneLayer('comments-page-div'); 
	showPageComments();
	switchActiveTab('tab5');
	highlight(0);
	
/*
	setContentIndex(0);	
	switchActiveTab('tab5');
	showPageComments();
	showOnlyOneLayer('comments-page-div'); 
*/
/*
	setContentIndex(0);	
	switchActiveTab('tab5');
	showPageComments();
	changeBlockCommentText(0);
	showOnlyOneLayer('comments-page-div');
	unHighlightAllParagraphs(); 
	highlightParagraphNoJump(0);			
*/
	/*
	setContentIndex(cindex);
	showBlockComments();
	showOnlyOneLayer('comments-block-div');
	switchActiveTab('tab2');
	changeBlockCommentText(cindex);
	highlightParagraphNoJump(cindex);
	*/
}


function replyLink(id, index){
	setContentIndex(index);
	highlightParagraph(index);
	postCommentTab();
	showCommentBlurb(id);
	setReplyId(id);
}

function paragraphCommentBubble(type, cindex){

	//if(document.getElementById('comment_contentIndex').value ){
	if(cindex == document.getElementById('comment_contentIndex').value){
		//cindex = -1;	
	}
	switch(type){
		case 'post-comment':
				setContentIndex(cindex);
				showOnlyOneLayer('comment-form-div');
				switchActiveTab('tab1');
				changeBlockCommentText(cindex);
				highlightParagraphNoJump(cindex);
				setCommentParent(0);			
		break;

		case 'show-comment-jump':
			if(cindex == 0){
				//alert(cindex);
				showOnlyOneLayer('comments-div');
				setContentIndex(cindex);
				showPageComments();
				showOnlyOneLayer('comments-page-div');
				switchActiveTab('tab5');
				changeBlockCommentText(cindex);
				highlightParagraph(cindex);			
			}
			else{			
				//alert(cindex);
				showOnlyOneLayer('comments-div');
				setContentIndex(cindex);
				showBlockComments();
				showOnlyOneLayer('comments-block-div');
				switchActiveTab('tab2');
				changeBlockCommentText(cindex);
				highlightParagraph(cindex);			
			}
		break;
		
		case 'show-comment':
			if(cindex == 0){
				showOnlyOneLayer('comments-div');
				setContentIndex(cindex);
				showPageComments();
				showOnlyOneLayer('comments-page-div');
				switchActiveTab('tab5');
				changeBlockCommentText(cindex);
				highlightParagraphNoJump(cindex);			
			}
			else{			
				showOnlyOneLayer('comments-div');
				setContentIndex(cindex);
				showBlockComments();
				showOnlyOneLayer('comments-block-div');
				switchActiveTab('tab2');
				changeBlockCommentText(cindex);
				highlightParagraphNoJump(cindex);			
			}
			
		break;		
	}
	/*
	else{
	
	}
	*/
}

/*

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
*/
//addEvent(window, 'load', windowLoaded); /* sorry Apple IE */
//addEvent(window, 'unload', windowLoaded);


function popupVideo(url){
	myVid = window.open(url,'video','height=300,width=370,scrollbars=0,status=1,location=0');
	myVid.focus();
}

