﻿var quotePre = '<div class="commentTextQuoteName">Zitat von $1</div><div class="commentTextQuote">';
var quotePost = '</div>';

var cID;
var contentID;
var contentType;
var maxPerPage;
var actPage;
var actPageOrder;
var pid;
var pk;
var rateList = new Array();
var rateElement = Object();

$(document).ready(function()
{

	$('#btnReset').bind("click", resetForm);
	$('#btnShowPreview').bind("click", showPreview);

});

function resetForm(e)
{

	$('#commentText').val('');

	updatePreview($("#commentText"));

}

function showPreview(e)
{

	$('#btnShowPreview').unbind("click", showPreview);
	$('#btnShowPreview').bind("click", hidePreview);

	$('#commentPreview').show();

	updatePreview($("#commentText"));

}

function hidePreview(e)
{

	$('#btnShowPreview').bind("click", showPreview);
	$('#btnShowPreview').unbind("click", hidePreview);

	$('#commentPreview').hide();

}

function updatePreview(e)
{

	var t  = $(e).val();
			
	// xml predefined entities
	t = t.replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\\/g, '&apos;').replace(/\"/g, '&quot;');

	// quote
	if(t.match(/\[QUOTE=([[äüöÄÖÜ\w+_\-\.\;\(\)!@#|$'" ]*)\]\n?/g) && t.match(/\[\/QUOTE\]\n?/)){

		t = t.replace(/\[QUOTE=([[äüöÄÖÜ\w+_\-\.\;\(\)!@#|$'" ]*)\]\n?/g, quotePre);
		t = t.replace(/\[\/QUOTE\]\n?/g, quotePost);

	}
	
	// line break
	t = t.replace(/\n/g, '<br />');
	
	$('#commentPreview > .text').html(t);

}

function edit(commentID, commentText)
{

	$('#btnEditSend').show();
	$('#btnSend').hide();

	$('#commentFormEdit').show();
	
	cID = commentID;

	$('#commentText').val(commentText);

	updatePreview($("#commentText"));

	window.location.href = '#comments';

}

function activateForm()
{
	
	$('#commentFormEdit').hide();
	
	$('#btnEditSend').hide();
	$('#btnSend').show();
	
	cID = undefined;
	resetForm();

	$('#commentForm').show();
	$('#commentFormConfirm').hide();
	
}

function sendComment(e)
{
	var get_rand = (Math.random()*100000);
	
	if($('#commentText').val().length == 0){
	
		return;
	
	}
	
	$('#commentForm').hide();
	$('#commentFormLoader').show();
	$('#commentFormConfirm').hide();
	
	var commText = $('#commentText').val();
	commText = commText.replace(/</g, '&lt;');
	
	if(cID != undefined){
	
		$.get('/_misc/comments/commentAction.cfm', { pid: pid, pk: pk, commentID: cID, maxPerPage: maxPerPage, action: 'edit', page: actPage, pageOrder: actPageOrder, commentText: commText, r: get_rand}, sendCommentComplete, 'xml');
	
	} else {

		$.get('/_misc/comments/commentAction.cfm', { pid: pid, pk: pk, contentID: contentID, contentType: contentType, maxPerPage: maxPerPage, action: 'new', page: actPage, pageOrder: actPageOrder, commentText: commText, r: get_rand}, sendCommentComplete, 'xml');
	
	}
	
}

function sendCommentComplete(responseData)
{
	
	var status = $("success", responseData).text();
	var data = $("message", responseData).text(); 
	
	if(status == 'false'){
	
		$('#commentFormError').show();
		
	} else {
		
		$('#commentFormConfirm').show();
		
		setTimeout(activateForm, 10000);
		
	}
	
	$('#commentForm').hide();
	$('#commentFormLoader').hide();
	
	$('#commentText').val('');

	updatePreview($("#commentText"));

	$('#commentPage').html(data);

}

function showReportForm(commentID)
{
		
	$('#blockReportConfirm').remove();
	$('#blockAction' + commentID).empty();

	var e = $('#blockReportConfirmP').clone().attr('id', 'blockReportConfirm');
		
	$('#blockAction' + commentID).append(e);

	$('#blockReportConfirm').show();
	$('#blockReportConfirm').data('commentID', commentID);
	
}

function hideReportForm()
{

	$('#blockAction' + $('#blockReportConfirm').data('commentID')).empty();

}


function report()
{		

	var commentID = $('#blockReportConfirm').data('commentID');
	var get_rand = (Math.random()*100000);
	
	$('#blockAction' + commentID).empty();
	
	var e = $('#blockReportSuccessP').clone().attr('id', 'blockReportSuccess');
	$('#blockAction' + commentID).append(e);
	
	$('#blockReportSuccess').show();

	$.get('/_misc/comments/commentAction.cfm', { pid: pid, pk: pk, commentID: commentID, maxPerPage: maxPerPage, action: 'report', r: get_rand});
	
}	

function showLoginMessage(commentID)
{
	
	var e = $('#blockLoginMessageP').clone().attr('id', 'blockLoginMessage');

	$('#blockAction' + commentID).append(e);
	
	$('#blockLoginMessage').show();
	
}


function rate(commentID, rate, element)
{		
	var get_rand = (Math.random()*100000);
	
	if(rateList[commentID]){
	
		return;
	
	}

	rateList[commentID] = true;

	rateElement = element;

	$.get('/_misc/comments/commentAction.cfm', { pid: pid, pk: pk, commentID: commentID, maxPerPage: maxPerPage, action: 'rate', rate: rate, r: get_rand }, rateComplete, 'xml');
	
}	

function rateComplete(responseData)
{
	
	var status = $("success", responseData).text();
	
	if(status == 'true'){
	
		var voteNumber = parseInt($(rateElement).find("> span").html())
		
		$(rateElement).find("> span").html(voteNumber + 1);
		
	} 

}

function quote(userName, text)
{

	$('#commentText').val('[QUOTE=' +  userName + ']' + text + '[/QUOTE]');

	updatePreview($("#commentText"));

	window.location.href = '#comments';

}		

function loadPage(e, contentID, contentType, page, pageOrder)
{
	var get_rand = (Math.random()*100000);

	if(e.preventDefault) {
		
		e.preventDefault();
		
	} else {
		
		e.returnValue = false;

	}

	$('#commentLoader').show();
	$('#commentPage').hide();

	$.get('/_misc/comments/comment.cfm', { pid: pid, pk: pk, contentID: contentID, contentType: contentType, maxPerPage: maxPerPage, page: page, pageOrder: pageOrder, r: get_rand }, loadPageComplete, 'html');

	reloadStatisticx('RC_COMMENTS', contentID + '_' + contentType + '_' + page);

}	

function loadPageComplete(responseText)
{

	$('#commentPage').html(responseText);

	$('#commentLoader').hide();
	$('#commentPage').show();		

	window.location.href = '#comments';

}

function reloadStatisticx(code,id) 
{

	var id = (!id) ? id = "" : "" + id;
	var get_rand = "d=" + (Math.random()*100000);
	var get_referer = "r=" + escape(document.referrer);
	var countIVW = new Image();
	var lowercode = code.toLowerCase();
	countIVW.src = "http://gastar.ivwbox.de/cgi-bin/ivw/CP/" + code + "?" + get_referer + "&" + get_rand;
	try {
		if(pageTracker) {
			pageTracker._trackEvent('ajax', code, id);
		}
	} catch(err) {}

}
