//URL for AJAX-Call
var sUrl = "http://www.rc-magazin.de/ajax.php";

function rateComment(cid, rating)
{
	//thumbs ausblenden
	//fading old out
	var fade = new YAHOO.util.Anim('c'+cid+'_thumbs', {
	    opacity: {from: 1, to: 0}
	}, 1, YAHOO.util.Easing.easeOut);
	fade.duration = 0.8;
	fade.animate();

	//make AJAX-Call
	var callback =
	{
		success: function(o)
		{

			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }

			//Sysmessage
			if(json_msg.responsecode == 200)
			{
				//neue Wertung anzeigen
				newRating = json_msg.newrating;
				rateEl = document.getElementById('c'+cid+'_rating');

				if(newRating > 0)
				{
					rateEl.innerHTML = '+'+newRating;
					rateEl.className = 'positive';
				}
				else if(newRating == 0)
				{
					rateEl.innerHTML = '0';
					rateEl.className = 'neutral';
				}
				else if(newRating < 0)
				{
					rateEl.innerHTML = newRating;
					rateEl.className = 'negative';
				}
			}
			else
			{
				alert(json_msg.error+" [Err #"+json_msg.responsecode+"]");
			}
		},
		failure: function(o)
		{
			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }
		}
	}

	var postData = 'action=rateReviewComment&cid='+cid+'&rate='+rating;

	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}


function openCommentTextfield()
{
	document.getElementById('newComment').style.visibility = 'visible';
	//fade it in...
	var fade = new YAHOO.util.Anim('newComment', {
	    opacity: {from: 0, to: 1}
	}, 1, YAHOO.util.Easing.easeOut);
	fade.duration = 0.8;
	fade.animate();

	//let the field grow...
	var growth = new YAHOO.util.Anim('newComment', {
	    height: {from: 0, to: 165}
	}, 1, YAHOO.util.Easing.easeOut);
	growth.duration = 0.5;
	growth.animate();

	document.getElementById('createCommentLink').onclick = new Function("closeCommentTextfield()");
}

function closeCommentTextfield()
{
	//fade it in...
	var fade = new YAHOO.util.Anim('newComment', {
	    opacity: {from: 1, to: 0}
	}, 1, YAHOO.util.Easing.easeOut);
	fade.duration = 0.2;
	fade.animate();

	//let the field grow...
	var growth = new YAHOO.util.Anim('newComment', {
	    height: {from: 165, to: 0}
	}, 1, YAHOO.util.Easing.easeOut);
	growth.duration = 0.8;
	growth.animate();

	document.getElementById('createCommentLink').onclick = new Function("openCommentTextfield()");
}

function submitComment(rid)
{
	comment = document.getElementById('newCommentText').value;
	if(comment.length == 0)
	{
		alert("Sie haben keinen Text eingegeben!");
		return;
	}

	//buttons verstecken und Warten anzeigen
	document.getElementById('saveCommentButton').style.visibility = 'hidden';
	document.getElementById('saveCommentWaiting').style.visibility = 'visible';

	//make AJAX-Call
	var callback =
	{
		success: function(o)
		{

			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {

	        	//buttons anzeigen und Warten verstecken
				document.getElementById('saveCommentButton').style.visibility = 'hidden';
				document.getElementById('saveCommentWaiting').style.visibility = 'visible';
	            return;
	        }

			//Sysmessage
			if(json_msg.responsecode == 200)
			{
				//Textfeld wieder leeren und verstecken
				closeCommentTextfield();
				document.getElementById('newCommentText').value="";

				//buttons anzeigen und Warten verstecken
				document.getElementById('saveCommentButton').style.visibility = 'visible';
				document.getElementById('saveCommentWaiting').style.visibility = 'hidden';

				//Das Kommentar als Element erstellen und anzeigen
				newComment = document.createElement("div");
				newComment.id = 'comment_'+json_msg.comment.id;
				newComment.className = 'comment_box';
				newComment.style.height = '200';
				newComment.style.opacity = '1';

				newText = document.createElement("div");
				newText.className = 'text';
				newText.innerHTML = json_msg.comment.comment_nl2br+'<div class="voting">(<span class="neutral">0</span>)</div>';
				newComment.appendChild(newText);

				//autor-text
				author = "<i>";
				if(json_msg.comment.author_user.image.length > 0)
					author = author+'<img src="http://www.rc-magazin.de/'+json_msg.comment.author_user.image+'" alt="" style="height:25px; margin-bottom: -7px;" />'
				else
					author = author+'<img src="http://www.rc-magazin.de/images/unknown-person.jpg" alt="" style="height:25px; margin-bottom: -7px;" />'


				author = author+' <b>'+json_msg.comment.author_user.username+'</b> am '+json_msg.comment.date+' um '+json_msg.comment.time+' Uhr</i>'


				newAutor = document.createElement("div");
				newAutor.className = 'autor';
				newAutor.innerHTML = author;
				newComment.appendChild(newAutor);

				document.getElementById('newComments').appendChild(newComment);

				//fade it in...
				var fade = new YAHOO.util.Anim('comment_'+json_msg.comment.id, {
				    opacity: {from: 0, to: 1}
				}, 1, YAHOO.util.Easing.easeOut);
				fade.duration = 0.8;
				fade.animate();

				//let the field grow...
				var growth = new YAHOO.util.Anim('comment_'+json_msg.comment.id, {
				    height: {from: 0, to: 100, unit: '%'}
				}, 1, YAHOO.util.Easing.easeOut);
				growth.duration = 0.5;
				growth.animate();

			}
			else
			{
				alert(json_msg.error+" [Err #"+json_msg.responsecode+"]");
			}
		},
		failure: function(o)
		{
			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }

	        //buttons anzeigen und Warten verstecken
			document.getElementById('saveCommentButton').style.visibility = 'hidden';
			document.getElementById('saveCommentWaiting').style.visibility = 'visible';
		}
	}

	var postData = 'action=saveComment&rid='+rid+'&c='+comment;

	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

function deleteComment(cid, hard)
{
	//make AJAX-Call
	var callback =
	{
		success: function(o)
		{

			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }

			//Sysmessage
			if(json_msg.responsecode == 200)
			{
				if(hard == 1)
				{
					var container = document.getElementById('comments');
					var comment = document.getElementById('comment_box_'+cid);

					container.removeChild(comment);
				}
				else
				{
					//let the field change the Color...
					var color = new YAHOO.util.ColorAnim('comment_box_text_'+cid, {
					    backgroundColor: {to: '#FFD2CE'}
					});
					color.duration = 0.5;
					color.animate();
				}

			}
			else
			{
				alert(json_msg.error+" [Err #"+json_msg.responsecode+"]");
			}
		},
		failure: function(o)
		{
			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }
		}
	}

	var postData = 'action=deleteReviewComment&cid='+cid+'&hard='+hard;

	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}


function restoreComment(cid)
{
	//make AJAX-Call
	var callback =
	{
		success: function(o)
		{

			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }

			//Sysmessage
			if(json_msg.responsecode == 200)
			{
				//let the field change the Color...
					var color = new YAHOO.util.ColorAnim('comment_box_text_'+cid, {
					    backgroundColor: {to: '#F0F0F0'}
					});
					color.duration = 0.5;
					color.animate();
			}
			else
			{
				alert(json_msg.error+" [Err #"+json_msg.responsecode+"]");
			}
		},
		failure: function(o)
		{
			//Try-Catch and parse the response-Text with JSON-Lib
	        try {
	            json_msg = YAHOO.lang.JSON.parse(o.responseText);
	        }
	        catch (x) {
	            return;
	        }
		}
	}

	var postData = 'action=restoreReviewComment&cid='+cid;

	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}

