if(typeof document.observe == 'function') {
	document.observe('dom:loaded', function () {
		if($('Content').down('h1').innerHTML.match(/Komis/i)) {
			var mArr = $$('#header ul li a');
			for(var i = 0; i < mArr.length; i++)
				if(mArr[i].innerHTML.match(/Komis/i))
					$(mArr[i]).up('li').addClassName('TabOn');
				else
					$(mArr[i]).up('li').removeClassName('TabOn');
		}
	});
}
/*ext:PotfurBBCode*/
function externalLink(id) {
	id.target = "_blank";
}
/*ext:Attachments*/
function DeleteAttachment(AjaxUrl, AttachmentID) {
	var ConfirmText = ("Na pewno chcesz usunąć ten załącznik?");
	if (confirm(ConfirmText)) {
		var AttachmentFileElement = document.getElementById("Attachment_" + AttachmentID);
		if( AttachmentFileElement ) AttachmentFileElement.innerHTML = "Usuwanie...";

		var Parameters = "AttachmentID="+AttachmentID;
		var dm = new DataManager();
		dm.RequestCompleteEvent = RefreshPageWhenAjaxComplete;
		dm.RequestFailedEvent = HandleFailure;
		dm.LoadData(AjaxUrl+"?Action=RemoveAttachment&"+Parameters);
	}
}
/*ext:UserAward*/
function RemoveAwardMessage(AjaxUrl, AwardID, SenderID) {
	var Sender = document.getElementById(SenderID);
	if (Sender) Sender.className = 'Progress';
	var Parameters = "AwardID="+AwardID;
	var dm = new DataManager();
	dm.RequestCompleteEvent = RefreshPageWhenAjaxComplete;
	dm.RequestFailedEvent = HandleFailure;
	dm.LoadData(AjaxUrl+"?"+Parameters);
}
/*ext:CommentRemoval*/
var g_com_id, g_is_discussion, g_baseurl;

function removecomment(baseurl, com_id, is_discussion) {
	var dm = new DataManager();
	g_com_id = com_id;
	g_is_discussion = is_discussion;
	g_baseurl = baseurl;
	document.getElementById('RmComment_' + com_id).className = 'HideProgress';
	document.getElementById('RmComment_' + com_id).innerHTML = '&nbsp;';
	dm.RequestCompleteEvent = _removecomment;
	dm.RequestFailedEvent = _rmc_failure;
	dm.LoadData(baseurl + 'extensions/CommentRemoval/ajax.php?CommentID=' + com_id);
}

function _removecomment(request) {
	//if(request.responseText == 'success')
	if(request.responseText.indexOf('itwasasuccess') != -1) {
		if(g_is_discussion) window.location = g_baseurl; //discussions
		else document.getElementById('Comment_' + g_com_id).style.display = 'none';
	}
	else {
		alert('Error: ' + request.responseText);
		_rmc_cleanup();
	}
}

function _rmc_failure(request) {
	alert('Error ' + request.status + ': ' + request.statusText);
	_rmc_cleanup();
}

function _rmc_cleanup() {
	var rmbutton = document.getElementById('RmComment_' + g_com_id);	
	rmbutton.className = '';
	rmbutton.onclick = 'return false;';
	rmbutton.innerHTML = '(error)';
}
/*ext:Quotations*/
function quote(commentId, authorId, authorName) {
	var tagOpenQuote	= "[quote]";
	var tagOpenCite		= "[cite]";
	var tagCloseQuote	= "[/quote]";
	var tagCloseCite	= "[/cite]";
	
	var selText = getSel();
	var commentField = document.getElementById("CommentBox");
	var commentText = "";

	commentText = dumpCodeTree(document.getElementById("CommentBody_" + commentId));

	if(selText != "") {
		commentText = selText;
	}
	
	if(commentField.value != "") {
		commentField.value += "\r\n" + "\r\n";
	}
	
	commentField.value += tagOpenQuote;
	commentField.value += tagOpenCite;		
	commentField.value += authorName;		
	commentField.value += tagCloseCite;		
	commentField.value += commentText;		
	commentField.value += tagCloseQuote;
	
	commentField.focus();
}

function getSel() {
	if(window.getSelection) {
		var d = document.createElement("p");
		var s = window.getSelection();
	
		for(var i = 0; i < s.rangeCount; ++i) {
			var r = s.getRangeAt(i);
			if (!r.collapsed) {
				d.appendChild(r.cloneContents());
			}
		}
		return dumpCodeTree(d);
	}
	else {
		return "";
	}
}

function dumpCodeTree(root) {
	var children	= root.childNodes;
	var outputText	= "";
	
	//BBCode should use square brackets:
	
	var tagOpenPrefix	= "[";
	var tagOpenSuffix	= "]";
	var tagClosePrefix	= "[/";
	var tagCloseSuffix	= "]";
	
	for (var i = 0; i < children.length; i++) {
		if(children[i].nodeType == 1) {
			var tagName = children[i].tagName.toLowerCase();
			
			//If html tags need to be changed into something else,
			//here is the place to do it:
			
			if(tagName == "br") {
				outputText += "\r\n";
				tagName = "";
			}
			
			if(tagName == "em") {
				tagName = "i";
			}

			if(tagName == "strong") {
				tagName = "b";
			}
			
			if(tagName == "blockquote") {
				tagName = "quote";
			}
			
			if(tagName == "a") {
				outputText += tagOpenPrefix;
				outputText += "url";
				
				for (var j = 0; j < children[i].attributes.length; j++) {
					attributeName	= children[i].attributes[j].name;
					attributeValue	= children[i].attributes[j].value;
					
					if(attributeName == "href" && attributeValue != dumpCodeTree(children[i]))
						outputText += "=" + attributeValue;
				}
				
				outputText += tagOpenSuffix;
				outputText += dumpCodeTree(children[i]);
				outputText += tagClosePrefix;
				outputText += "url";
				outputText += tagCloseSuffix;
				
				tagName = "";
			}
			
			if(tagName == "img") {
				var imageText = ""

				var src = "";
				var alt = "";
				
				imageText += tagOpenPrefix;
				imageText += tagName;
				
				for (var j = 0; j < children[i].attributes.length; j++) {
					attributeName  = children[i].attributes[j].name;
					attributeValue = children[i].attributes[j].value;
					
					if(attributeName == "width") {
						attributeName = "w";
					}
					else if(attributeName == "height") {
						attributeName = "h";
					}
					else if(attributeName == "src") {
						src = attributeValue;
						attributeName = "";
					}
					else if(attributeName == "alt") {
						alt = attributeValue;
						attributeName = "";
					}
					else {
						attributeName = "";
					}
					
					if(attributeName != "" && attributeValue != "null" && attributeValue != "") {
						imageText += " ";
						imageText += attributeName;
						imageText += "=";
						imageText += attributeValue;
					}
				}
				
				imageText += tagOpenSuffix;
				imageText += src;
				imageText += tagClosePrefix;
				imageText += tagName;
				imageText += tagCloseSuffix;
				
				if(alt != "") {
					outputText += alt;
				}
				else {
					outputText += imageText
				}
				
				tagName = "";
			}			
			
			if(tagName != "") {
				outputText += tagOpenPrefix; 
				outputText += tagName;

				for (var j = 0; j < children[i].attributes.length; j++)  {
					attributeName  = children[i].attributes[j].name;
					attributeValue = children[i].attributes[j].value;
					
					//If html attributes need to be changed into something else,
					//here is the place to do it: 
					
					if(attributeName == "target" || attributeName == "hideFocus" || attributeName == "contentEditable" || attributeName == "disabled" || attributeName == "tabIndex") {
						attributeName = "";
					}
					
					if(attributeName != "" && attributeValue != "null" && attributeValue != "") {
						outputText += " ";
						outputText += attributeName;
						outputText += "=";
						outputText += attributeValue;
					}
				}

				outputText += tagOpenSuffix;
				outputText += dumpCodeTree(children[i]);
				outputText += tagClosePrefix;
				outputText += tagName;
				outputText += tagCloseSuffix;
			}
		}
		else if(children[i].nodeType == 3) {
			var nodeValue = children[i].nodeValue;
			
			//strip out extra newlines/returns
			nodeValue = nodeValue.replace(/\r\n/g," ");
			nodeValue = nodeValue.replace(/\n/g," ");
			nodeValue = nodeValue.replace(/\r/g," ");
			
			//strip whitespace
			nodeValue = nodeValue.replace(/^\s*|\s*$/g,'');
			
			outputText += nodeValue;
		}
	}
	
	//return outputText;
	return outputText.replace(/\[(\/?)div([^\]]*)\]/gi,'');
}
