var globChildrenElementId;
var globElementId;
var globLoadedChildrens = Array();
var globLoadedAddForm;
var globParent;

function GetChildrenComments(id, type, parent)
{
        var url = "parent," + parent + ",type," + type + ",id," + id + ",AjaxViewChildren,Comment.html";
        var pars = "";

        //pars="parent=" + parent + "&type=" + type + "&id=" + id;
        globChildrenElementId = parent;
        var myAjax = new Ajax.Request(
                url,
                {
                        method: 'post',
                        parameters: pars,
                        onComplete: showCommentResponseView
                });

		globLoadedChildrens[parent] = true;
}

function ShowChildrenComments(id, type, parent)
{
	var moreButton, hideButton;

	if(typeof globLoadedChildrens[parent] == 'undefined') {
		GetChildrenComments(id, type, parent);
	}

	document.getElementById("children" + parent).style.display = 'block';

	moreButton = document.getElementById("childrenMoreButton" + parent);
	hideButton = document.getElementById("childrenHideButton" + parent);

	if (moreButton && hideButton) {
		moreButton.style.display = 'none';
		hideButton.style.display = 'block';
	}
}

function HideChildrenComments(id, type, parent)
{
	var moreButton, hideButton;

	moreButton = document.getElementById("childrenMoreButton" + parent);
	hideButton = document.getElementById("childrenHideButton" + parent);

	if (moreButton && hideButton) {
		moreButton.style.display = 'block';
		hideButton.style.display = 'none';
	}

	document.getElementById("children" + parent).style.display = 'none';
}

function ShowAddForm(id, type, parent)
{
	var url, pars;

	url = "parent," + parent + ",type," + type + ",id," + id + ",AjaxAddForm,Comment.html";
	pars = '';
	
	if (typeof globLoadedAddForm != 'undefined') {
		document.getElementById(globLoadedAddForm).innerHTML = '';
	}

	globElementId = parent;

	if (document.getElementById('noComments') != null) {
		document.getElementById('noComments').style.display = 'none';
	}

	var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: pars,
			onComplete: showCommentResponseAdd
		});
}

function ShowHideReplyButton(element, show)
{
	var hideButton, showButton;

	showButton = document.getElementById('childrenReply' + element);
	hideButton = document.getElementById('childrenReplyClose' + element);
	
	if (show == undefined || !show) {
		show = false;
	}

	if (showButton) {
		showButton.style.display = (show ? 'inline' : 'none');
	}

	if (hideButton) {
		hideButton.style.display = (show ? 'none' : 'inline');
	}
}

function ShowHideFrameButton(show)
{
	var frameCancelButton, frameAddButton;

	if (show == undefined || !show) {
		show = false;
	}
	
	frameAddButton = document.getElementById('buttonFrameAddComment');
	frameCancelButton = document.getElementById('buttonFrameCancelComment');

	if (frameAddButton != null && frameAddButton != undefined && frameCancelButton != null && frameCancelButton != undefined) {

		frameAddButton.style.display = (show ? 'block' : 'none');
		frameCancelButton.style.display = (show ? 'none'  : 'block');
	}
}

function HideAddForm()
{
	ShowHideReplyButton(globElementId, true);
	ShowHideFrameButton(true);

	document.getElementById(globLoadedAddForm).innerHTML = '';

	if (document.getElementById('noComments') != null) {
		document.getElementById('noComments').style.display = 'block';
	}
}

function submitCommentForm(form, func, cont, id, parent, type, deep)
{
	var params, error;

	params = new Object();

	if (form.elements['title'].value == '') {
		document.getElementById('titleerror').innerHTML = 'Musisz podać treść';
		error = true;
	}

	if (form.elements['content'].value == '') {
		document.getElementById('contenterror').innerHTML = 'Musisz podać tytuł';
		error = true;
	}

	if (error) {
		return;
	}

	if (deep == undefined) {
		deep = false;
	}

	if (parent != 0 && !deep) {
		ShowChildrenComments(id,type,parent);
	}

	params['url'] = "func," + func + ",id," + id + ",parent," + parent + ",type," + type + ",AjaxSendForm," + cont + ".html";
	params['onComplete'] = showCommentResponseAdded;

	globParent = parent;

	sendForm(form,func,cont,params);
}

function showCommentResponseView(originalRequest)
{
    //put returned XML in the textarea
    document.getElementById("children" + globChildrenElementId).innerHTML = originalRequest.responseText;
	document.getElementById("childrenHideButton" + globChildrenElementId).innerHTML = "ukryj";
}

function showCommentResponseAdd(orginalRequest)
{
	var openReply, loadedAddForm, hideReplyFormButton, showReplyFormButton;

	ShowHideReplyButton(globElementId);
	ShowHideFrameButton();

	globLoadedAddForm = 'childrenAdd' + globElementId;

	loadedAddForm = document.getElementById(globLoadedAddForm);
	loadedAddForm.innerHTML = orginalRequest.responseText;
	loadedAddForm.style.display = 'block';

	openReply = document.getElementById('commentOpenReply');

	if (openReply != null && openReply != undefined) {
		if (openReply.innerHTML && openReply.innerHTML != globElementId) {
			
			hideReplyFormButton = document.getElementById('childrenReplyClose' + openReply.innerHTML);
			showReplyFormButton = document.getElementById('childrenReply' + openReply.innerHTML);

			if (hideReplyFormButton != null && hideReplyFormButton != undefined) {
				hideReplyFormButton.style.display = 'none';
			}

			if (showReplyFormButton != null && showReplyFormButton != undefined) {
				showReplyFormButton.style.display = 'inline';
			}
		}

		openReply.innerHTML = globElementId;
	}
}

function showCommentResponseAdded(orginalRequest)
{
	var child, childAdd, childMore, childHide, parent, parentObj, more;
	
	ShowHideFrameButton(true);

	child = document.getElementById('children' + globParent);
	childAdd = document.getElementById('childrenAdd' + globParent);
	childMore = document.getElementById('childrenMore' + globParent);
	childHide = document.getElementById('childrenHide' + globParent);

	if (childMore != undefined && childMore != null) {
		childMore.style.display = 'block';
	}

	if (childHide != undefined && childHide != null) {
		childHide.style.display = 'none';
	}

	if (childAdd != undefined && childAdd != null) {
		childAdd.innerHTML = '';
		childAdd.style.display = 'none';
	}

	if (child.innerHTML == undefined || child.innerHTML == null) {
		child.innerHTML = '';
	}

	if (globParent != 0) {
		parentObj = document.getElementById('parentOfChildren' + globParent);
		parent = globParent;

		while(true) {

			if (parentObj == null || parentObj == undefined || parentObj.innerHTML == 0) {
				break;
			}

			parent = parseInt(parentObj.innerHTML);
			parentObj = document.getElementById('parentOfChildren' + parent);
		}

		more = document.getElementById('childrenMoreNumber' + parent);

		//Żeby się nie wywalało jak się coś niezaładuje.
		if (more != null && more != undefined) {
			more.innerHTML = (parseInt(more.innerHTML) + 1).toString();

			parent = (parseInt(parent.innerHMTL) == 0 ? globParent : parent.innerHTML);

			if (parent != null && parent != undefined) {
				parent.innerHTML = (parseInt(parent) + 1).toString();
			}
		}

	}

	child.innerHTML = orginalRequest.responseText + child.innerHTML;
	child.style.display = 'block';

	ShowHideReplyButton(document.getElementById('commentOpenReply').innerHTML, true);
}
