/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
	examples:
	To get all a elements in the document with a “info-links” class.
  getElementsByClassName(document, "a", "info-links");
  To get all div elements within the element named “container”, with a “col” class.
  getElementsByClassName(document.getElementById("container"), "div", "col"); 
  To get all elements within in the document with a “click-me” class.
  getElementsByClassName(document, "*", "click-me");
*/
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

/**
 * returns the value of the radio button from the group of radio groups
 * that is checked,  return an empty string if none are checked, or there are
 * no radio buttons in the group
 * @param object|array radioObj group of radio buttons can be selected from the
 * document form as document.forms['formName'].elements['elementsName'];
 */
function getCheckedValue(radioObj)
{
 if(!radioObj)
		return "";

 var radioLength = radioObj.length;
 //single one object
 if(radioLength == undefined)
  if(radioObj.checked)
	 return radioObj.value;
	else
 	 return "";

  //group of radio buttons
  for(var i = 0; i < radioLength; i++) {
	 if(radioObj[i].checked) {
	  return radioObj[i].value;
	 }
	}
	return "";
}

/* set the radio button with the given value as being checked do nothing if there
 * are no radio buttons if the given value does not exist, all the radio buttons
 * are reset to unchecked
 * @param object|array radioObj group of radio buttons can be selected from the
 * document form as document.forms['formName'].elements['elementsName'];
 * @param string|int newValue
 */
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

/**
 * remove all child element from the specific node
 * (c) Mike Chambers - http://www.mikechambers.com/blog/
 */
function removeChildrenFromNode(node)
{
 var len = node.childNodes.length;
 
 while (node.hasChildNodes()) {
  node.removeChild(node.firstChild);
 }
}

/**
 * Used for simple preview. Content of source element is copied into
 * the destination.
 * @string sourceElemId 
 * @string destElemId
 */
function preview(sourceElemId,destElemId)
{
 var newText = document.getElementById(sourceElemId).value;
 //var splitText = newText.split(/\n/).join("<br />");
 var destElem = document.getElementById(destElemId);
 //opera.postError(destElem.id);
 destElem.innerHTML = newText;
}

/**
 * prints ajax "loading" animated gif into the element.
 * @string elementId element id
 */
function ajaxWhenLoading(elementId)
{
	var e = document.getElementById(elementId);
	e.innerHTML = String.fromCharCode(60)+'img src="'+baseurl+'/images/ajax-loader.gif" alt="ajaxLoader" />';
}

/**
 * makes ajax request for article rating. After successful ajax call afterRating
 * function is called, which updates some part of the page. 
 * @int articleId id of the article
 * @int rating rating for the article
 */
function rateArticle(articleId,rating)
{
 var ajax = new sack();
  ajax.setVar("rating",rating);
  ajax.setVar("articleId",articleId);
  ajax.requestFile = baseurl+"/blog/action/article-rating";
  ajax.method = "POST";
  ajax.element = 'ratingMessage';
  ajax.onLoading = function(){ ajaxWhenLoading('ratingMessage') };
  ajax.onLoaded = ajax.onLoading;
  ajax.onInteractive = ajax.onLoading;
  ajax.onCompletion = function(){ afterRating(ajax.response) };
  ajax.runAJAX();
}

/**
 * Makes ajax request for view of archive. Archive view is composed from calendar
 * view and list of month statistics.
 * @int month month number (1-12)
 * @int year full year number (e.g. 2002)
 */
function viewArchive(month,year)
{
 var monthNum=new Number(month);
 if (isNaN(monthNum)) {
  alert("month "+month+" isn't number");
  return false;
 }

 var yearNum=new Number(year);
 if (isNaN(yearNum)) {
  alert("year "+year+" isn't number");
  return false;
 }

 if (monthNum < 1 || month > 12 ) {
  alert("month must be in interval 1..12");
  return false;
 }
 
 var ajax = new sack();
  ajax.setVar("month",month);
  ajax.setVar("year",year);
  ajax.requestFile = baseurl+"/blog/action/view-archive-widgets";
  ajax.method = "POST";
  ajax.element = 'archiveWidget';
  ajax.onLoading = function(){ ajaxWhenLoading('ajaxArchiveProgress') };
  ajax.onLoaded = ajax.onLoading;
  ajax.onInteractive = ajax.onLoading;
  ajax.runAJAX();
}

/**
 * Is called after successful ajax call. Updates some elemets of the
 * page with rating infomations.
 * @string resp JSON like string which is evaluted into the javascript object 
 */
function afterRating(resp)
{
 //response is evaluated JSON -> javascript object
 var response=eval("("+resp+")");
 //update contents of page elements
 //rating message
 var e=document.getElementById("ratingMessage");
 e.innerHTML=response.message;
 
 //upadate current rating
 if (response.average && response.votingCount) {
  e=document.getElementById("averageRating");
  e.innerHTML=response.average;
  e=document.getElementById("votingCount");
  e.innerHTML=response.votingCount;
  
  //remove all children from star rating list except the current-rating UL 
  var articleRatingNode=document.getElementById("articleRatigList"); 
  var listItems = articleRatingNode.getElementsByTagName("LI");
 
  //opera.postError("list items - "+listItems.length);
	
	//array for the elements, which will be removed.
  var needRemove = new Array();
 
  for(var i = 0; i < listItems.length; i++) {
   //opera.postError("i "+i);
   e=listItems[i];
   if (e.id !== undefined) {
    if (e.id=="current-rating") {
     //opera.postError("skip node with id "+e.id);
     continue;
    }
   }
   needRemove.push(e);
  }
  
  //opera.postError("for removing "+needRemove.length);
  
  //array for the elements, which will be removed.
  for(i = 0; i < needRemove.length; i++) {
   e=needRemove[i];
   e.parentNode.removeChild(e);
  }
  
  //change width for the current-rating
  e=document.getElementById("current-rating");
  e.style.width=Math.round(response.average*30)+"px";
 }


}
