function postAjaxComment(formObj, targetURL, theID, isPaged, commentCount, comments_per_page)
{
	new Ajax.Updater(
		{ success: 'commentlist' },	targetURL,
		{
			asynchronous: true,
			evalScripts: true,
			insertion: Insertion.Bottom,
			onComplete: function(request){complete(request,theID,isPaged,commentCount,comments_per_page);},
			onFailure: function(request){failure(request);},
			onLoading: function(request){loading();},
			parameters: Form.serialize(formObj)
		}
	);
}

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

	var theID = targ.href.match(/#more-(.*)/)[1];
	var targetID = "e" + theID;

	if ( window.event ) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else if ( e.stopPropagation ) {
		e.stopPropagation();
		e.preventDefault();
	}
	$(targetID).innerHTML += '<p class="throbber"><img src="' + throbberURL + '" alt="loading" /></p>';
	var AjaxURL = singlePostURL + theID;
	new Ajax.Updater(targetID, AjaxURL, {onLoading:function(request){}, onComplete:function(request){fetchPostComplete();}, asynchronous:true});
	return false;
}

function fetchPostComplete()
{
	if (sweetTitles)
		sweetTitles.tipOut();
	Behaviour.apply();
}

function inlineCommentsComplete(request, targetID) {
	if (!window.opera) {
		new Effect.Phase(targetID);
	} else {
		if ($(targetID).style.display=='')
			$(targetID).style.display='none';
		else
			$(targetID).style.display='';
	}
	//Behaviour.apply();
}

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

	var theID = targ.href.match(/\?p=(.*)/)[1];
	var targetID = "c" + theID;

	if ( window.event ) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else if ( e.stopPropagation ) {
		e.stopPropagation();
		e.preventDefault();
	}
	var targetNode = $(targetID);
	if (targetNode.innerHTML.indexOf('<!-- end of inline comments //-->') != -1)
	{
		if (!window.opera) {
			new Effect.Phase($(targetID));
		} else {
			if (targetNode.style.display=='')
				targetNode.style.display='none';
			else
				targetNode.style.display='';
		}
	} else {
		targetNode.style.display='';
		targetNode.innerHTML = '<p class="throbber"><img src="' + throbberURL + '" alt="loading" /></p>';
		var AjaxURL = this.href;// + theID;
		new Ajax.Updater(targetID, AjaxURL, {onLoading:function(request){}, onComplete:function(request){targetNode.style.display='none';inlineCommentsComplete(request, targetID);}, asynchronous:true});
	}
	return false;
}

function loadPagedComments(theID,thePage)
{
	if ($('comment-pages-top'))
	{
		$('comment-pages-top').innerHTML += '<span class="throbber"><img src="' + throbberURL + '" alt="loading" /></span>';
		$('comment-pages-bottom').innerHTML += '<span class="throbber"><img src="' + throbberURL + '" alt="loading" /></span>';
	}
	var AjaxURL = pagedCommentsURL + theID + "&cp=" + thePage;
	new Ajax.Updater("theComments", AjaxURL, {onLoading:function(request){}, onComplete:function(request){commentsFadeIn(request);}, asynchronous:true});
}

function commentsFadeIn(response)
{
  new Effect.Highlight('comment-pages-top');
  new Effect.Highlight('comment-pages-bottom');
  if (!window.opera) {
    new Effect.PhaseIn('commentlist');
  } else {
  	$('commentlist').style.display = "";
	}
	Behaviour.apply();
}

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function addEvent( obj, type, fn ) {
  if (obj.addEventListener) {
    obj.addEventListener( type, fn, false );
    EventCache.add(obj, type, fn);
  }
  else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
    obj.attachEvent( "on"+type, obj[type+fn] );
    EventCache.add(obj, type, fn);
  }
  else {
    obj["on"+type] = obj["e"+type+fn];
  }
}

function getEventSrc(e) {
  if (!e) e = window.event;

  if (e.originalTarget)
  return e.originalTarget;
  else if (e.srcElement)
  return e.srcElement;
}

var EventCache = function(){
  var listEvents = [];
  return {
    listEvents : listEvents,
    add : function(node, sEventName, fHandler){
      listEvents.push(arguments);
    },
    flush : function(){
      var i, item;
      for(i = listEvents.length - 1; i >= 0; i = i - 1){
        item = listEvents[i];
        if(item[0].removeEventListener){
          item[0].removeEventListener(item[1], item[2], item[3]);
        };
        if(item[1].substring(0, 2) != "on"){
          item[1] = "on" + item[1];
        };
        if(item[0].detachEvent){
          item[0].detachEvent(item[1], item[2]);
        };
        item[0][item[1]] = null;
      };
    }
  };
}();

addEvent(window,'unload',EventCache.flush);

// ===================================================

function ShowUtils() {
  if (!window.opera) {
    new Effect.Phase('authorinfo');
    $("showinfo").style.display = "none";
    $("hideinfo").style.display = "";
  }
}

function HideUtils() {
  if (!window.opera) {
    new Effect.Phase('authorinfo');
    $("showinfo").style.display = "";
    $("hideinfo").style.display = "none";
  }
}

function toggleBox(boxid)
{
  var tgt = $(boxid).getElementsByTagName("ul")[0];
  if (!window.opera) {
    new Effect.Phase(tgt);
  } else {
  	$(tgt).style.display = ($(tgt).style.display != "") ? "" : "none";
	}
}
