function getComments(targetDiv,opinionId,placeId,userId)
{
	var callback =
	{
		success: function(o)
		{
			//target div
			var target = $(o.argument[0]);
			var preview = YAHOO.util.Dom.getElementsByClassName("opinions_comments_preview","div",target)[0];
			var full = YAHOO.util.Dom.getElementsByClassName("opinions_comments_full","div",target)[0];
			preview.style.display = "none";
			full.innerHTML = o.responseText;
			full.style.display = "";
			
		},
		failure: function(o)
		{
			alert("Error in getComments()");
		},
		argument: [targetDiv]
	}
	
	var post = "opinionId=" + opinionId;
	post += "&targetDiv=" + targetDiv;
	post += "&placeId=" + placeId;
	
	YAHOO.util.Connect.asyncRequest('POST', "mod/comment/xhrWrapper.php", callback, post); 
}

function closeComments(targetDiv)
{
	var target = $(targetDiv);
	var preview = YAHOO.util.Dom.getElementsByClassName("opinions_comments_preview","div",target)[0];
	var full = YAHOO.util.Dom.getElementsByClassName("opinions_comments_full","div",target)[0];
	preview.style.display = "";
	full.style.display = "none";
}

function addComment(rating, opinionId)
{
	var post = "rating=" + rating;
	post += "&opinionId=" + opinionId;
	
	var callback =
	{
		success: function(o)
		{
			if(o.responseText == "__loginFirst__")
			{
				openLoginDialog();
			}
			else
			{
				var panelDiv;
				if(addCommentPanel)
				{
					panelDiv = $("addComment_c");
					document.body.removeChild($("addComment_mask"));
					panelDiv.removeChild($("addComment"));
					document.body.removeChild(panelDiv);
					overlayManager.remove(addCommentPanel);
				}
				panelDiv = document.createElement("div");
				panelDiv.id = "addComment";
				document.body.appendChild(panelDiv);
				
				panelDiv.innerHTML = o.responseText;				
				
				var panel = new YAHOO.widget.Panel(panelDiv,
					{ fixedcenter: true, 
					constraintoviewport: true, 
					visible:false,
					modal: true, 
					width:"485px", 
					//height:"560px",
					draggable:true } );
					
				overlayManager.register(panel);
				
				addCommentPanel = panel;
				
				var editorConfig = {
			        height: '100px',
			        width: '407px',
			        dompath: false,
			        focusAtStart: false,
				    toolbar: {
				        titlebar: 'Mein Kommentar dazu:',
				        buttons: [
				            { group: 'textstyle',
				                buttons: [
				                    { type: 'push', label: 'Fett', value: 'bold' },
				                    { type: 'push', label: 'Kursiv', value: 'italic' }
				                ]
				            }
				            /*,
							{ type: 'separator' },
						    { group: 'insertitem',
						        buttons: [
						            { type: 'push', label: 'HTML Link', value: 'createlink' }
						        ]
						    }*/			        
						]
				    }
				};
				
				var addCommentText = new YAHOO.widget.SimpleEditor('addCommentText', editorConfig);
				addCommentText.render();

				panel.hideEvent.subscribe(function(){
					this.editor.hide();
				}) 

				panel.render();
				panel.show();
				
				panel.editor = addCommentText
			}
		},
		failure: function(o)
		{
			alert("Error in addComment()");
		}
	}
	YAHOO.util.Connect.asyncRequest('POST', "mod/comment/xhrAddComment.php", callback, post); 		
}

function saveComment()
{
	addCommentPanel.editor.saveHTML();
	
	var form = document.getElementById("commentAddForm");
	var error = new Array();
	
	if(isEmpty(trim(form.comment.value)))
	{
		error.push("Bitte gib dein Kommentar ein!");
	}
	if(isEmpty(error))
	{
		YAHOO.util.Connect.setForm(form, true);
		placeId = form.placeId.value;
		opinionId = form.opinionId.value;
		var callback =
		{
			success: addCommentSuccess,
			upload: addCommentSuccess,
			failure: function(o)
			{
				alert("Error in saveComment()");
			},
			argument: [placeId, opinionId]
		};
		
		YAHOO.util.Connect.asyncRequest('POST', "mod/comment/xhrSaveComment.php", callback); 
	}
	else
	{
		$("commentAddError").innerHTML = error.join("<br>");
		$("commentAddError").style.display = "";
	}
}

function addCommentSuccess(o)
{
	//addCommentPanel.editor.hide();
	addCommentPanel.hide();
	placeId = o.argument[0];
	opinionId = o.argument[1];
	openDashboard(placeId, opinionId);
	getRecentOpinions(1,null,true);
	mapRefreshPlaces();
}