function postAjaxComment(formObj, targetURL, theID, isPaged, comments_per_page)
{
	new Ajax.Updater(
		{ success: 'commentlist' },	targetURL + '?comment_count=' + $('cCount').innerHTML,
		{
			asynchronous: true,
			evalScripts: true,
			insertion: Insertion.Bottom,
			onComplete: function(request){complete(request,theID,isPaged,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 = "inline-more-" + theID;

	if ( window.event ) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else if ( e.stopPropagation ) {
		e.stopPropagation();
		e.preventDefault();
	}

	if ($(targetID).innerHTML.length == 0)
	{
		$(targetID).innerHTML += '<p class="throbber"><img src="' + throbberURL + '" alt="loading" /></p>';
		$(targetID).style.display = "";
		var AjaxURL = singlePostURL + theID;
		new Ajax.Updater(targetID, AjaxURL, {onLoading:function(request){}, onComplete:function(request){fetchPostComplete(theID);}, asynchronous:true});
	} else {
		$("more-" + theID).style.display = "none";
		$("hide-1-more-" + theID).style.display = "";
		$("hide-2-more-" + theID).style.display = "";
		new Effect.Highlight('hide-1-more-' + theID);
		new Effect.Highlight('hide-2-more-' + theID);
		if (!window.opera) {
			new Effect.PhaseIn('inline-more-' + theID);
		} else {
			$('inline-more-' + theID).style.display = "";
		}
	}
	return false;
}

function hidePost(theID)
{
	$("more-" + theID).style.display = "";
	$("hide-1-more-" + theID).style.display = "none";
	$("hide-2-more-" + theID).style.display = "none";
	if (!window.opera) {
		new Effect.PhaseOut('inline-more-' + theID);
	} else {
		$('inline-more-' + theID).style.display = "none";
	}
}

function fetchPostComplete(theID)
{
	if (sweetTitles)
		sweetTitles.tipOut();
	if (document.getElementsByTagName && myLightbox)
	{
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];

			var relAttribute = String(anchor.getAttribute('rel'));

			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
				anchor.onclick = function () {myLightbox.start(this); return false;}
			}
		}
	}
	$("more-" + theID).style.display = "none";
	$("hide-1-more-" + theID).style.display = "";
	$("hide-2-more-" + theID).style.display = "";
	new Effect.Highlight('hide-1-more-' + theID);
	new Effect.Highlight('hide-2-more-' + theID);
	if (!window.opera) {
		new Effect.PhaseIn('inline-more-' + theID);
	} else {
		$('inline-more-' + theID).style.display = "";
	}
	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";
	}
}