function loadComments(id, dzial, start, limit, expandAll, order, baseURL) {
	var dataString = '', altParm = '';
	dataString = addNewParameter(dataString, "function", "getComments");
	dataString = addNewParameter(dataString, "format", "json");
	dataString = addNewParameter(dataString, "id", id);
	dataString = addNewParameter(dataString, "dzial", dzial);
	dataString = addNewParameter(dataString, "start", start);
	dataString = addNewParameter(dataString, "order", order);
	dataString = addNewParameter(dataString, "limit", limit);
	dataString = addNewParameter(dataString, "dFormat", "d-m-Y, H:i");
	altParm = 'order:'+order+'||baseURL:'+ baseURL;
	if(expandAll == true) {
		altParm += '||expandAll';
	}
	jQuery("#mainCommentsBox").attr("alt", altParm);
	jQuery.ajax({
		type: "GET",
		url: "http://p1.idg.pl/xml/contentXML/getCommentsLibrary.php?"+dataString,
		dataType: "jsonp",
		callback: getCommentsJSON
	});
}

function getCommentsJSON(response) {
	var i, tmpEl, tmpHTML = '', arrCount = 0, treeArr = new Array, baseURL;
	baseURL = getAltValue('mainCommentsBox', 'baseURL');
	for(i=0; i<response.root.element.length; i++) {
		tmpEl = response.root.element[i];
		tmpHTML += '<div class="komfull komfull_border"><div class="autodane">';
		if(tmpEl.subcount > 0) {
			tmpHTML += '<a onclick="loadSimpleTree('+tmpEl.lp+');" style="cursor:pointer;text-decoration:none" class="dat_d" id="subIndicator'+tmpEl.lp+'" title="rozwiń" alt="rozwiń">[+]</a> ';
			treeArr[arrCount] = tmpEl.lp;
			arrCount++;
		}
		highlight_user = '';
		if(tmpEl.highlight_user == 1) {
			highlight_user = ' class="comment_highlight_user"';
		}
		if(tmpEl.email.length > 0) {
			tmpHTML += '<a href="mailto:'+tmpEl.email+'"'+highlight_user+'><strong>'+tmpEl.nick+'</strong></a>';
		}
		else {
			tmpHTML += '<strong'+highlight_user+'>'+tmpEl.nick+'</strong>';
		}
		tmpHTML +=' <span class="dat_d">'+tmpEl.data+'</span>';
		if(tmpEl.subcount > 0) {
			tmpHTML += ' <span class="dat_d">['+tmpEl.subcount+']</span>';
		}
		tmpHTML += '</div><a href="'+baseURL+'/komentarze/'+tmpEl.dzial+'_'+tmpEl.tabela_id+'/'+tmpEl.lp+'.html"><strong>'+tmpEl.tytul+'</strong></a><div>'+tmpEl.opis+'</div><span id="commentTree'+tmpEl.lp+'" style="display:none"></span></div>';
	}
	jQuery("#mainCommentsBox").html(tmpHTML);
	for(i=0; i<arrCount; i++) {
		loadSimpleTree(treeArr[i]);
	}
}

function loadSimpleTree(parentID) {
	if(jQuery("#commentTree"+parentID).html().length > 0) {
		if(jQuery("#commentTree"+parentID).css("display") == 'none') {
			jQuery("#subIndicator"+parentID).html('[-]');
			jQuery("#subIndicator"+parentID).attr('title', 'zwiń');
			jQuery("#subIndicator"+parentID).attr('alt', 'zwiń');
			jQuery("#commentTree"+parentID).css("display", "inline")
		}
		else {
			jQuery("#subIndicator"+parentID).html('[+]');
			jQuery("#subIndicator"+parentID).attr('title', 'rozwiń');
			jQuery("#subIndicator"+parentID).attr('alt', 'rozwiń');
			jQuery("#commentTree"+parentID).css("display", "none")
		}
	}
	else {
		var dataString = '';
		dataString = addNewParameter(dataString, "function", "getSimpleTree");
		dataString = addNewParameter(dataString, "format", "json");
		dataString = addNewParameter(dataString, "parent_id", parentID);
		dataString = addNewParameter(dataString, "dFormat", "d-m-Y, H:i");
		orderTest = jQuery("#mainCommentsBox").attr("alt");
		if(orderTest.indexOf('order:1') != -1) {
			dataString = addNewParameter(dataString, "order", 1);
		}
		else {
			dataString = addNewParameter(dataString, "order", 0);
		}
		jQuery.ajax({
			type: "GET",
			url: "http://p1.idg.pl/xml/contentXML/getCommentsLibrary.php?"+dataString,
			dataType: "jsonp",
			callback: getSimpleTreeJSON
		});
	}
}

function getSimpleTreeJSON(response) {
	var tmpHTML = '<ul class="komlist" style="padding-left: 8px">', baseURL;
	baseURL = getAltValue('mainCommentsBox', 'baseURL');
	for(i=0; i<response.root.element.length; i++) {
		tmpEl = response.root.element[i];
		tmpHTML += '<li><a href="'+baseURL+'/komentarze/'+tmpEl.dzial+'_'+tmpEl.tabela_id+'/'+tmpEl.lp+'.html"><strong>'+tmpEl.tytul+'</strong></a> - <span class="autodane">';
		highlight_user = '';
		if(tmpEl.highlight_user == 1) {
			highlight_user = ' class="comment_highlight_user"';
		}
		if(tmpEl.email.length > 0) {
			tmpHTML += '<a href="mailto:'+tmpEl.email+'"'+highlight_user+'><strong>'+tmpEl.nick+'</strong></a>';
		}
		else {
			tmpHTML += '<strong'+highlight_user+'>'+tmpEl.nick+'</strong>';
		}
		tmpHTML +=' <span class="dat_d">'+tmpEl.data+'</span>';
		if(tmpEl.subcount > 0) {
			tmpHTML += ' <span class="dat_d">['+tmpEl.subcount+']</span>';
		}
		tmpHTML += '</span></li>';
	}
	tmpHTML += '</ul>';
	jQuery("#commentTree"+response.root.element[0].parent_id).html(tmpHTML);
	treeTest = jQuery("#mainCommentsBox").attr("alt");
	if(treeTest.indexOf('expandAll') != -1) {
		jQuery("#commentTree"+response.root.element[0].parent_id).css("display", "inline")
		jQuery("#subIndicator"+response.root.element[0].parent_id).html('[-]');
		jQuery("#subIndicator"+response.root.element[0].parent_id).attr('title', 'zwiń');
		jQuery("#subIndicator"+response.root.element[0].parent_id).attr('alt', 'zwiń');
	}
}

function loadPagesList(id, dzial, ile, strona, baseURL) {
	var dataString = '';
	dataString = addNewParameter(dataString, "function", "getCommentsCount");
	dataString = addNewParameter(dataString, "format", "json");
	dataString = addNewParameter(dataString, "id", id);
	dataString = addNewParameter(dataString, "dzial", dzial);
	dataString = addNewParameter(dataString, "strona", strona);
	dataString = addNewParameter(dataString, "ile", ile);
	altParm = 'baseURL:'+ baseURL;
	jQuery("#pagesBox").attr("alt", altParm);
	jQuery.ajax({
		type: "GET",
		url: "http://p1.idg.pl/xml/contentXML/getCommentsLibrary.php?"+dataString,
		dataType: "jsonp",
		callback: getPagesListJSON
	});
}

function getPagesListJSON(response) {
	var tmpEl, tmpHTML = '', baseURL, count, dzial, id, strona, ile;
	count = response.root.element[0].count;
	dzial = response.root.element[0].dzial;
	id = response.root.element[0].id;
	strona = parseInt(response.root.element[0].strona);
	ile = response.root.element[0].ile;
	liczbaStron = Math.ceil(count/ile);
	baseURL = getAltValue('pagesBox', 'baseURL');

	if(liczbaStron > 1) {
		if(strona > 1) {
			tmpHTML = '&nbsp;<a class="prevPage" href="'+baseURL+'/komentarze/'+dzial+'_'+id+'_'+(strona-1)+'.html"><span>wstecz</span></a>&nbsp;';
		}
		for(i=0; i<liczbaStron; i++) {
			tmpStrona = i+1;
			tmpHTML += '&nbsp;';
			if(tmpStrona == strona) {
				tmpHTML += '<span class="thisPage">'+tmpStrona+'</span>';
			}
			else {
				tmpHTML += '<a class="otherPage" href="'+baseURL+'/komentarze/'+dzial+'_'+id+'_'+tmpStrona+'.html">'+tmpStrona+'</a>';
			}
		}
		if(strona < liczbaStron) {
			tmpHTML += '&nbsp;<a class="nextPage" href="'+baseURL+'/komentarze/'+dzial+'_'+id+'_'+(strona+1)+'.html"><span>dalej</span></a>&nbsp;';
		}
	}

	jQuery("#pagesBox").html(tmpHTML);
}

function loadCommentsCount(id, dzial) {
	var dataString = '';
	dataString = addNewParameter(dataString, "function", "getCommentsCount");
	dataString = addNewParameter(dataString, "format", "json");
	dataString = addNewParameter(dataString, "id", id);
	dataString = addNewParameter(dataString, "dzial", dzial);
	dataString = addNewParameter(dataString, "getAllComments", "true");
	jQuery.ajax({
		type: "GET",
		url: "http://p1.idg.pl/xml/contentXML/getCommentsLibrary.php?"+dataString,
		dataType: "jsonp",
		callback: getCommentsCountJSON
	});
}

function getCommentsCountJSON(response) {
	var tmpEl, tmpHTML = '';
	count = response.root.element[0].count;
	jQuery("#commentsCountBox").html(count);
}

function loadMainComment(commentID, userID, order, baseURL) {
	var dataString = '';
	dataString = addNewParameter(dataString, "function", "getCommentInfo");
	dataString = addNewParameter(dataString, "format", "json");
	dataString = addNewParameter(dataString, "id", commentID);
	dataString = addNewParameter(dataString, "userID", userID);
	dataString = addNewParameter(dataString, "dFormat", "d-m-Y, H:i");
	altParm = 'order:'+order+'||baseURL:'+ baseURL;
	jQuery("#mainCommentBox").attr("alt", altParm);
	jQuery.ajax({
		type: "GET",
		url: "http://p1.idg.pl/xml/contentXML/getCommentsLibrary.php?"+dataString,
		dataType: "jsonp",
		callback: getCommentInfoJSON
	});
}

function getCommentInfoJSON(response) {
	var tmpEl, tmpHTML, baseURL;
	baseURL = getAltValue('mainCommentBox', 'baseURL');
	tmpEl = response.root.element[0];
	tmpHTML = '	<div class="komfull"><div class="autodane">';
	highlight_user = '';
	if(tmpEl.highlight_user == 1) {
		highlight_user = ' class="comment_highlight_user"';
	}
	if(tmpEl.email.length > 0) {
		tmpHTML += '<a href="mailto:'+tmpEl.email+'"'+highlight_user+'><strong>'+tmpEl.nick+'</strong></a>';
	}
	else {
		tmpHTML += '<strong'+highlight_user+'>'+tmpEl.nick+'</strong>';
	}
	tmpHTML += ' <span class="dat_d">'+tmpEl.data+'</span> | IP: '+tmpEl.ip+' | <span class="nserwisu">'+tmpEl.serwis+'</span></div>';
	tmpHTML += '<div class="comment_title">'+tmpEl.tytul+'</div>';
	tmpHTML += '<div class="comment_text">'+tmpEl.opis+'</div>';
	tmpHTML += '<a href="#edytor" class="skom">Skomentuj tą wypowiedź</a><ul class="opcjekom"><li>';
	if(tmpEl.id_subskrypcji > 0) {
		tmpHTML += '<a href="/cru/subskrypcje.asp?remove='+tmpEl.id_subskrypcji+'">zaprzestań śledzenia wątku</a>';
	}
	else {
		tmpHTML += '<a href="'+baseURL+'/komentarze/subskrypcja/'+tmpEl.dzial+'_'+tmpEl.tabela_id+'/'+tmpEl.lp+'.html">śledź dyskusję</a>';
	}
	tmpHTML += '</li><li><a href="'+baseURL+'/komentarze/moderacja/'+tmpEl.modKey+'.html"><img src="http://g1.idg.pl/idg/gfx/komentarze/stop.gif" alt="zgłoś do moderatora"/></a>  <a href="'+baseURL+'/komentarze/moderacja/'+tmpEl.modKey+'.html">zgłoś do moderatora</a></li></ul><ul class="opcjekom"><li>';
	orderTest = jQuery("#mainCommentBox").attr("alt");
	if(orderTest.indexOf('order:1') != -1) {
		tmpHTML += '<a href="'+baseURL+'/komentarze/'+tmpEl.dzial+'_'+tmpEl.tabela_id+'_0/'+tmpEl.lp+'.html">pokaż komentarze od najstarszego</a>';
	}
	else {
		tmpHTML += '<a href="'+baseURL+'/komentarze/'+tmpEl.dzial+'_'+tmpEl.tabela_id+'_1/'+tmpEl.lp+'.html">pokaż komentarze od najnowszego</a>';
	}
	tmpHTML += '</li><li><a onclick="changeAllTree();" name="true" class="rozwin" style="cursor:pointer" id="changeAllLink">rozwiń wszystkie</a></li></ul>';
	tmpHTML += '</div></li>';
	jQuery("#mainCommentBox").html(tmpHTML);
}

function loadFullTree(commentID, dzial, id, order, baseURL) {
	var dataString = '';
	dataString = addNewParameter(dataString, "function", "getFullTree");
	dataString = addNewParameter(dataString, "format", "json");
	dataString = addNewParameter(dataString, "lp", commentID);
	dataString = addNewParameter(dataString, "dzial", dzial);
	dataString = addNewParameter(dataString, "id", id);
	dataString = addNewParameter(dataString, "dFormat", "d-m-Y, H:i");
	dataString = addNewParameter(dataString, "order", order);
	altParm = 'baseURL:'+ baseURL;
	jQuery("#mainTreeBox").attr("alt", altParm);
	jQuery.ajax({
		type: "GET",
		url: "http://p1.idg.pl/xml/contentXML/getCommentsLibrary.php?"+dataString,
		dataType: "jsonp",
		callback: getFullTreeJSON
	});
}

function getFullTreeJSON(response) {
	var tmpEl, tmpHTML='', level = 1, expand = 0, baseURL;
	baseURL = getAltValue('mainTreeBox', 'baseURL');
	for(i=0; i<response.root.element.length; i++) {
		tmpEl = response.root.element[i];
		if(tmpEl.level > level) {
			tmpHTML += '<span id="commentTree'+tmpEl.parent_id+'" style="display: ';
			if(expand == 1) {
				tmpHTML += 'block';
			}
			else {
				if(getTreeType(tmpEl.parent_id) == '+') {
					tmpHTML += 'block';
				}
				else {
					tmpHTML += 'none';
				}
			}
			tmpHTML += '"><ul class="komlist">';
		}
		if(tmpEl.level < level) {
			diff = level-tmpEl.level;
			for(j=0; j<diff; j++) {
				tmpHTML += '</ul></span>';
			}
		}
		level = tmpEl.level;
		expand = tmpEl.expand;
		tmpHTML += '<li>';
		if(tmpEl.subcount > 0) {
			tmpHTML += '<a onclick="changeTree('+tmpEl.lp+');" style="cursor:pointer;text-decoration:none" class="dat_d" id="subIndicator'+tmpEl.lp+'"';
			if(tmpEl.expand == 1) {
				tmpHTML += ' title="zwiń" alt="zwiń">[-]';
				setCommentCookie(tmpEl.lp, '+');
			}
			else {
				if(getTreeType(tmpEl.lp) == '+') {
					tmpHTML += ' title="zwiń" alt="zwiń">[-]';
				}
				else {
					tmpHTML += ' title="rozwiń" alt="rozwiń">[+]';
				}
			}
			tmpHTML += '</a> ';
		}
		if(tmpEl.highlight == 1) {
			tmpHTML += '<strong class="comment_highlight">'+tmpEl.tytul+'</strong>';
		}
		else {
			tmpHTML += '<a href="'+baseURL+'/komentarze/'+tmpEl.dzial+'_'+tmpEl.tabela_id+'/'+tmpEl.lp+'.html"><strong>'+tmpEl.tytul+'</strong></a>';
		}
		tmpHTML += ' - <span class="autodane">';
		highlight_user = '';
		if(tmpEl.highlight_user == 1) {
			highlight_user = ' class="comment_highlight_user"';
		}
		if(tmpEl.email.length > 0) {
			tmpHTML += '<a href="mailto:'+tmpEl.email+'"'+highlight_user+'><strong>'+tmpEl.nick+'</strong></a>';
		}
		else {
			tmpHTML += '<strong'+highlight_user+'>'+tmpEl.nick+'</strong>';
		}
		tmpHTML +=' <span class="dat_d">'+tmpEl.data+'</span>';
		if(tmpEl.subcount > 0) {
			tmpHTML += ' <span class="dat_d">['+tmpEl.subcount+']</span>';
		}
		tmpHTML += '</span><br />'+tmpEl.zajawka+'</li>';
	}
	tmp = document.createElement("div");
	mainTreeBoxDiv = document.getElementById('mainTreeBox');
	if(mainTreeBoxDiv != null) {
		tmp.innerHTML = tmpHTML;
		mainTreeBoxDiv.appendChild(tmp);
	}
}

function changeTree(commentID) {
	if(jQuery("#commentTree"+commentID).css("display") == 'block') {
		jQuery("#commentTree"+commentID).css("display", "none");
		jQuery("#subIndicator"+commentID).html('[+]');
		jQuery("#subIndicator"+commentID).attr('title', 'rozwiń');
		jQuery("#subIndicator"+commentID).attr('alt', 'rozwiń');
		setCommentCookie(commentID, '-');
	}
	else {
		jQuery("#commentTree"+commentID).css("display", "block");
		jQuery("#subIndicator"+commentID).html('[-]');
		jQuery("#subIndicator"+commentID).attr('title', 'zwiń');
		jQuery("#subIndicator"+commentID).attr('alt', 'zwiń');
		setCommentCookie(commentID, '+');
	}
}

function changeAllTree() {
	jQuery("span").each(function() {
		elID = jQuery(this).attr("id");
		if(elID != undefined && elID.indexOf('commentTree') == 0) {
			cID = elID.replace('commentTree', '');
			if(jQuery("#changeAllLink").attr("name") == 'true') {
				jQuery(this).css("display", "block");
				jQuery("#subIndicator"+cID).html('[-]');
				jQuery("#subIndicator"+cID).attr('title', 'zwiń');
				jQuery("#subIndicator"+cID).attr('alt', 'zwiń');
				setCommentCookie(cID, '+');
			}
			else {
				jQuery(this).css("display", "none");
				jQuery("#subIndicator"+cID).html('[+]');
				jQuery("#subIndicator"+cID).attr('title', 'rozwiń');
				jQuery("#subIndicator"+cID).attr('alt', 'rozwiń');
				setCommentCookie(cID, '-');
			}
		}
	});
	if(jQuery("#changeAllLink").attr("name") == 'true') {
		jQuery("#changeAllLink").html('zwiń wszystkie');
		jQuery("#changeAllLink").attr("name", "false");
	}
	else {
		jQuery("#changeAllLink").html('rozwiń wszystkie');
		jQuery("#changeAllLink").attr("name", "true");
	}
}

function setCommentCookie(tmpCommentID, type) {
	var cookieValue, commentID, i;
	commentID = tmpCommentID;
	cookieValue = '';
	if(type == '+') {
		type = '1';
	}
	else {
		type = '0';
	}
	if(haveCookie('commentsSettings')) {
		cookie = getMultipleCookieValue('commentsSettings', 'commentsTreeConf');
		if(cookie) {
			commentIDStart = cookie.indexOf(commentID);
			if(commentIDStart != -1) {
				commentIDEnd = cookie.indexOf('#', commentIDStart);
				if(commentIDEnd == -1) {
					commentIDEnd = cookie.length;
				}
				cookieCommentID = cookie.substr(commentIDStart, commentIDEnd-commentIDStart);
				cookie = cookie.replace(cookieCommentID, commentID.toString()+type);
				cookieValue = cookie;
			}
			else {
				cookieValue = cookie+'#'+commentID+type;
			}			
		}
		else {
			cookieValue = cookie+'#'+commentID+type;
		}
	}
	else {
		cookieValue = commentID+type;
	}
	saveMultipleCookie('commentsSettings', 'commentsTreeConf', cookieValue);
}

function getTreeType(commentID) {
	if(haveCookie('commentsSettings')) {
		cookie = getMultipleCookieValue('commentsSettings', 'commentsTreeConf');
		if(cookie) {
			commentIDStart = cookie.indexOf(commentID);
			if(commentIDStart != -1) {
				commentIDStart += commentID.toString().length;
				commentIDEnd = cookie.indexOf('#', commentIDStart);
				if(commentIDEnd == -1) {
					commentIDEnd = cookie.length;
				}
				treeType = cookie.substr(commentIDStart, commentIDEnd-commentIDStart);
				if(treeType == 1) {
					treeType = '+';
				}
				else {
					treeType = '-';
				}
				return treeType;
			}
		}
		return false;
	}
	return false;
}

function getAltValue(divBox, valueName) {
	var value, tmp, tmp2;
	value = jQuery("#"+divBox).attr("alt");
	tmp = value.indexOf(valueName+':');
	if(tmp != -1) {
		tmp=tmp+valueName.length+1;
		tmp2 = value.indexOf('||', tmp);
		if(tmp2 == -1) {
			tmp2 = value.length;
		}
		value = value.substr(tmp, tmp2-tmp);
	}
	else {
		value = '';
	}
	return value;
}

