// ***********************************************************************************
// Copyright (C) Aleto 
// http://aleto.ch
// ***********************************************************************************

// This work is licensed under the Creative Commons Attribution License. To  view a
// copy of this license, visit http://creativecommons.org/licenses/by/1.0/  or send
// a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,  California 94305,
// USA.

// rev 13/09/2004

function setTarget(e){
   if(e) return e.target;
   if(event) return event.srcElement;
}
var ns='http://www.w3.org/1999/xhtml';
var parentEle='';
window.onload=function (e) {
if( document.getElementById && document.getElementsByTagName &&
   ((e && e.target) || (event && typeof event.srcElement=='object')) ){
      var comments;
      if( comments=document.getElementById('comments') ) {
         var isXHTML=/html\:/.test(document.getElementsByTagName('body')[0].nodeName);
         function createNewEle(ele){
            if(isXHTML) { return document.createElementNS('http://www.w3.org/1999/xhtml', ele); }
            else { return document.createElement(ele); };
         };			 
         var liArr=comments.getElementsByTagName('li'), liEle, i=0, workEle;
		     parentEle=comments.parentNode.nodeName;
         for(i=0; liEle=liArr[i]; i++ ) if( liEle.parentNode==comments ) {
         (workEle=liEle.insertBefore(createNewEle('dfn'),liEle.firstChild)).appendChild(document.createTextNode('reset'));
         workEle.className='reset';
         (workEle=liEle.insertBefore(createNewEle('dfn'),liEle.firstChild)).appendChild(document.createTextNode('show comment in his context'));
         workEle.className='show';
         };         
         comments.onclick=function (e) {
            function markComments (fromEle, childType, withName, withGoTo) { 
               var childArr, k=0, childEle;
               if( childArr=fromEle.getElementsByTagName(childType) ) {
                  for(k=0; childEle=childArr[k]; k++) {
                     if(childEle.getElementsByTagName('a')[0] && /cmmnt/.test(childEle.getElementsByTagName('a')[0].href) ) {
                        var listArrNo=childEle.getElementsByTagName('a')[0].href.replace(/^.*cmmnt(.*)$/,'$1');
                        listArr[listArrNo-1].className=withName;
                        if(withGoTo) goTo='#cmmnt'+listArrNo;
                     }
                  }
               }
            };
            var ele=setTarget(e), i=0, za=1;			
            var liArr=this.getElementsByTagName('li'), listArr=[], liEle, i=0, num, childArr, childLi, classN=false, goTo=false, indent=false;
            for(i=0; liEle=liArr[i]; i++ ) if( liEle.parentNode==this ) listArr.push(liEle);
            if(/a/i.test(ele.nodeName) && /commentform/.test(ele.href) ) {
               ele=ele.parentNode;
               i=0;
               while(listArr[i]!=ele) i++;            
               document.getElementById('nucleus_cf_body').focus();
               document.getElementById('nucleus_cf_body').value+='['+(i+1)+'] ';             
            } else {
				if(!/a/i.test(ele.nodeName)){
				   if(/dfn/i.test(ele.nodeName)) { 
					  indent=true;
					  classN=ele.className;
				   }
				   while( ele.parentNode.parentNode && ele.parentNode.parentNode.nodeName !=parentEle ) ele=ele.parentNode; // here is the adaption
				   if(!indent) classN=ele.className;
				   if(/li/i.test(ele.nodeName) ) {
					  for(i=0; liEle=listArr[i]; i++) liEle.className='';
					  if(indent) {
						 if(classN=='show'){ele.parentNode.className='indent'} else {ele.parentNode.className='';}
					  };
					  if(classN=='show' || !classN ) {                        
						 markComments(ele, 'span', 'parent',true);
						 ele.className='current';
						 if(!goTo) {
							i=0;
							while(listArr[i]!=ele) i++;
							goTo='#cmmnt'+(i+1);
						 };
						 if(ele.getElementsByTagName('ol')[0]) markComments(ele.getElementsByTagName('ol')[0], 'li', 'child');
						 for(i=0; liEle=listArr[i]; i++) if(!/current|parent|child/.test(liEle.className)) liEle.className='notPart';
						 if(goTo && indent ) location.href+=goTo;
					  };
					  if(classN=='hide') {
						 i=0;
						 while(listArr[i]!=ele) i++;
						 goTo='#cmmnt'+(i+1);
						 location.href+=goTo;
					  }
				   }
				}
			}
            return true;
         }
      }
   }
}

// ***********************************************************************************
// Copyright (C) Radek HULAN
// http://hulan.cz/contact/
// ***********************************************************************************

// This work is licensed under the Creative Commons Attribution License. To  view a
// copy of this license, visit http://creativecommons.org/licenses/by/1.0/  or send
// a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,  California 94305,
// USA.

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function ct(textpre,textpost){ 
 var ele;
 if (ele=document.getElementById('nucleus_cf_body')) {
 	if (ele.setSelectionRange) {
 		var start = ele.selectionStart;
 		var end = ele.selectionEnd; 
 		ele.value = ele.value.substring(0, start) + textpre + ele.value.substring(start, end) + textpost + ele.value.substring(end);
 		var pos = start + textpre.length + textpost.length + (end-start)+1;
 		setSelectionRange(ele, pos, pos);
 	} else if (ele.selection) {
 		ele.focus();
 		document.selection.createRange().text = textpre + document.selection.createRange().text + textpost;
	}
 }
}

function commentFormExtras(){
	isXHTML=/html\:/.test(document.getElementsByTagName('body')[0].nodeName);
	var ele=document.getElementById('nucleus_cf_body');
	if (!ele) return;
	var base=ele.parentNode.insertBefore(createNewEle('div'),ele);
	base.id='toolbar';
	var toolbar=['strong','cite','em','q','abbr','code','list','link'];
	var href1=['[strong]','[cite]','[em]','[q]','[abbr title=YOURTEXT]','[code]','[li]','[a href=LINKURL]'];
	var href2=['[/strong]','[/cite]','[/em]','[/q]','[/abbr]','[/code]','[/li]','[/a]'];
	var title=['bold text','citation','emphasized text','short quotation','abbreviation','source code','unordered list','link/anchor'];
	for(var i=0; i<toolbar.length; i++) {
		var aEle;
		(aEle=base.appendChild(createNewEle('a'))).appendChild(document.createTextNode(toolbar[i]));
		aEle.href='javascript:ct(\''+href1[i]+'\',\''+href2[i]+'\')';
		aEle.title=title[i];
	};
}

function isValidComment(f) {
	if (f.body.value == "") {
		alert('Please enter your comment!');
		return false;
	}
	if (f.user.value == "") {
		alert('Please enter your name and/or nickname!');
		return false;
	}
	return true;
}
