// ***********************************************************************************
// script (c) and written by Radek HULAN
// http://hulan.cz/
// ***********************************************************************************

// use like onclick="return openwin(this.href)"
function openwin(url) {
  if( window.open(url,'','width=800,height=600,left=0,top=0,scrollbars=yes,location=yes,resizable=yes') ) return false; else return true;
}
 
function isValidComment(f) {
	if (f.body.value == "") {
		alert('Prosím zadejte komentář!');
		return false;
	}
	if (f.user.value == "") {
		alert('Prosím zadejte Vaše jméno!');
		return false;
	}
	return true;
}

// helper functions
var isXHTML;
function createNewEle(ele){
	if(isXHTML) { return document.createElementNS('http://www.w3.org/1999/xhtml', ele); }
	else { return document.createElement(ele); };
};	

// ***********************************************************************************
// XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        
// Code licensed under Creative Commons Attribution-ShareAlike License     
// http://creativecommons.org/licenses/by-sa/2.0/                           
// ***********************************************************************************
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new XMLHttpRequest();  }
  catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

// ***********************************************************************************
// BLOG:CMS comments - (c) Radek Hulan, http://hulan.cz                    
// Code licensed under Creative Commons Attribution-ShareAlike License      
// http://creativecommons.org/licenses/by-sa/2.0/                           
// ***********************************************************************************

function waitIconRemove(commentid) {
	var comment = document.getElementById('comment' + commentid);
	if (comment) {
		var img = comment.getElementsByTagName("img");
		for (var i = 0; i < img.length; i++)
			if (img[i].className == 'commentwait')
				img[i].parentNode.removeChild(img[i]);
	}
}

function removeImages(commentid) {
	var ele = document.getElementById('comment'+commentid);
	if (ele) {
		var img = ele.getElementsByTagName('img');
		for (var i = 0; i < img.length; i++)
			if (img[i].className == 'votedown' || img[i].className == 'voteup')
				img[i].style.display = 'none';
	}
}
function voteDone(XML) {
	if (window.ActiveXObject) {
		var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(XML.responseText);
	} else {
		var parser=new DOMParser();
		var doc=parser.parseFromString(XML.responseText,"text/xml");
	}
	var response = doc.getElementsByTagName('response');
	var ok = response[0].getElementsByTagName("result")[0].firstChild.nodeValue;
	if (ok == 1) {
		// response was ok
		var commentid = response[2].getElementsByTagName("result")[0].firstChild.nodeValue;
		var votes = response[3].getElementsByTagName("result")[0].firstChild.nodeValue;
		var ele = document.getElementById('votes'+commentid);
		// change # of votes
		if (ele)
			ele.firstChild.nodeValue = votes;
		// hide vote buttons
		ele = document.getElementById('comment'+commentid);
		if (ele) {
			if (votes >= 10)
				ele.className = 'comment-top';
			else if (votes >= 0)
				ele.className = 'comment-ok';
			else if (votes >= -10)
				ele.className = 'comment-bad';
			else 
				ele.className = 'comment-ban';
		}
		waitIconRemove(commentid);
		removeImages(commentid);
	} else {
		alert(response[1].getElementsByTagName("result")[0].firstChild.nodeValue);
		// hide vote buttons
		var commentid = response[2].getElementsByTagName("result")[0].firstChild.nodeValue;
		waitIconRemove(commentid);
		removeImages(commentid);
	}
}


function waitIcon(commentid) {
	var comment = document.getElementById('comment' + commentid);
	var ele = comment.getElementsByTagName('ins');
	var workEle;
	workEle=ele[0].insertBefore(createNewEle('img'),ele[0].firstChild);
	workEle.src = "/styles/vote/wait.gif";
	workEle.className='commentwait';
	workEle.alt = 'wait';
}

function voteUp(commentid){
	if (commentid > 0 ) {
		waitIcon(commentid);
		var c = new XHConn();
		c.connect("/action.php?action=plugin&name=FancyText&todo=vote", "POST", "cid=" + commentid + "&votetype=up&blogid=" + blogid, voteDone);
	}
}

function voteDown(commentid){
	if (commentid > 0 ) {
		waitIcon(commentid);
		var c = new XHConn();
		c.connect("/action.php?action=plugin&name=FancyText&todo=vote", "POST", "cid=" + commentid + "&votetype=down&blogid=" + blogid, voteDone);
	}
}

// ***********************************************************************************
// script (c) and written by aleto and Radek Hulán
// http://aleto.ch/ | http://hulan.cz/
// ***********************************************************************************

// 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 | rev 20/01/2007

function setTarget(e){
   if(e) return e.target;
   if(event) return event.srcElement;
}
var ns='http://www.w3.org/1999/xhtml';
window.onload=function (e) {
if( document.getElementById && document.getElementsByTagName &&
   ((e && e.target) || (event && typeof event.srcElement=='object')) ){
      var comments;
      if( comments=document.getElementById('comments') ) {
      	  
      	 isXHTML=/html\:/.test(document.getElementsByTagName('body')[0].nodeName);

         comments.onclick=function (e) {
            var ele=setTarget(e), i=0, za=1;			

			// voting
			if (prihlasen) {
         		if(/img/i.test(ele.nodeName)){
         			if (ele.className == 'voteup') {
         				voteUp((ele.parentNode.parentNode.id).substr(7));
         				return;
         			}
         			if (ele.className == 'votedown') {
	         			voteDown((ele.parentNode.parentNode.id).substr(7));
    	     			return;
        	 		}
        		}
        	}
            return true;
         }
      }
   }
}

function insertComment(id) {
	try {
		tinyMCE.execInstanceCommand('nucleus_cf_body', 'mceInsertContent', false, '['+id+'] ', true);
	} catch(err) {
    	if (document.getElementById('nucleus_cf_body')) {
        	document.getElementById('nucleus_cf_body').focus();
            document.getElementById('nucleus_cf_body').value+='['+id+'] ';
		}
	}
}