function initContentFeedbackPanel(servletPath, panelId, panelElems, contentId, contentType) {
	Event.observe(window, 'load', function() {
		showAjaxWaitingScreen(panelId+'waiting');
		initContentFeedbackEvents(servletPath, panelId, panelElems, contentId, contentType);
		catchContentFeedbackPanelInfosByAjax(servletPath, panelId, panelElems, contentId, contentType);
	});
}

function initContentFeedbackEvents(servletPath, panelId, panelElems, contentId, contentType) {
	// rating popup
	$$('#'+panelId+' .editableRating .editableRatingPopup').each(function(item){
		Event.observe(item, 'mouseover', function(event) {
			item.show();
		});
		Event.observe(item, 'mouseout', function(event) {
			item.hide();
		});
	});
	// rating popup img-bars
	$$('#'+panelId+' .editableRating .editableRatingPopup .rtngBar').each(function(item){
		Event.observe(item, 'mouseover', function(event) {
			item.down('span').addClassName('active');
		});
		Event.observe(item, 'mouseout', function(event) {
			item.down('span').removeClassName('active');
		});
		Event.observe(item, 'click', function(event) {
			//TODO....
			var rating = '';
			item.classNames().each(function(cn){
				if (cn.indexOf('rtngBar_')==0) {
					rating = cn.substring('rtngBar_'.length);
				}
			});
			contentFeedbackRating(item, rating, contentId, contentType, servletPath, panelId, panelElems);
		});
	});
	// rating link
	$$('#'+panelId+' .editableRating .actionLink a').each(function(lnk){
		Event.observe(lnk, 'click', function(event) {
			event.preventDefault();
		});
		Event.observe(lnk, 'mouseover', function(event) {
			var popup = lnk.up('.editableRating').down('.editableRatingPopup');
			if (popup) {
				popup.show();
				popup.setStyle({
					top: lnk.positionedOffset()['top']+lnk.getHeight()+'px',
					left: lnk.positionedOffset()['left']+'px'
				                                       });
			}
		});
	});
	// comment link
	$$('#'+panelId+' .commentCount a').each(function(item){
		Event.observe(item, 'click', function(event) {
			event.stop();
			if ($('displayedEntries')) {
				$('displayedEntries').scrollTo();
			}
		});
	});
}

function contentFeedbackRating(item, rating, contentId, contentType, servletPath, panelId, panelElems) {
	item.up('.editableRatingPopup').hide();
	showAjaxWaitingScreen(panelId+'waiting');
	
	var url = servletPath+'/servlet/rating?contentRating='+rating+'&contentId='+contentId+'&contentType='+contentType
				+'&siteid='+window.siteid+'&locale='+window.locale;
	new Ajax.Request(url, {
		method: 'get',
		onFailure: function() {
			hideAjaxWaitingScreen(panelId+'waiting');
		},
		onSuccess: function(response) {
			catchContentFeedbackPanelInfosByAjax(servletPath, panelId, panelElems, contentId, contentType);
		}
	});

//	$('editableRating').innerHTML = '<xsl:value-of select="/Response/AppendedContent/kraftRecipeSetup/Response/Content/ContentTopic[@SSID='4' and @Pos='9']/HTTitle"/><span class="rating'+selIndex+'">&#160;</span>';
}


function catchContentFeedbackPanelInfosByAjax(servletPath, panelId, panelElems, contentId, contentType) {
	var url = servletPath+'/servlet/AjaxActionServlet';
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'action=contentFeedbackPanel&elems='+encodeURIComponent(panelElems.toJSON())+'&cid='+contentId+'&contenttype='+contentType
					+'&siteid='+window.siteid+'&locale='+window.locale,
		onComplete: function() {
			hideAjaxWaitingScreen(panelId+'waiting');
		},
		onSuccess: function(response) {
			if (response.responseXML) {
				var rootElem = response.responseXML.firstChild;
				if(rootElem) {
					var code = rootElem.getAttribute("code");
					if (code == '0') {
						var elems = rootElem.getElementsByTagName('elem');
						if (elems) {
							$A(elems).each(function(elem){
								var type = elem.getAttribute('type');
								if (type == '_commentCount') {
									updateContentFeedbackPanelCommentCount(panelId, elem.firstChild.data);
								} else if (type == '_editableRating') {
									updateContentFeedbackPanelRating(panelId, 
																	elem.firstChild.getAttribute('count'), 
																	elem.firstChild.getAttribute('avg'),
																	elem.firstChild.getAttribute('ar'));
								} else if (type == '_recipeBoxCount') {
									updateContentFeedbackPanelRecipeBoxCount(panelId, elem.firstChild.data);
								} else if (type == '_tellAFriendCount') {
									updateContentFeedbackPanelTellAFriendCount(panelId, elem.firstChild.data);
								}
							});
						}
					}
				}
			}
		}
	});

}

function updateContentFeedbackPanelCommentCount(panelId, count) {
	var countSpan = $(panelId).down('.commentCount a span.num');
	if (countSpan) {
		countSpan.update(count);
	}
}

function updateContentFeedbackPanelRecipeBoxCount(panelId, count) {
	var countSpan = $(panelId).down('.recipeBoxCount span.num');
	if (countSpan) {
		countSpan.update(count);
	}
}

function updateContentFeedbackPanelTellAFriendCount(panelId, count) {
	var countSpan = $(panelId).down('.tellAFriendCount span.num');
	if (countSpan) {
		countSpan.update(count);
	}
}

function updateContentFeedbackPanelRating(panelId, count, avg, ar) {
	// already rated?
	if (ar=='true') {
		$(panelId).down('.editableRating .actionLink').hide();
		$(panelId).down('.editableRating .alreadyRated').show();
	} else {
		$(panelId).down('.editableRating .actionLink').show();
		$(panelId).down('.editableRating .alreadyRated').hide();
	}
	// not rated yet?
	if (parseInt(count)==0) {
		$(panelId).down('.editableRating .totalCount').hide();
		$(panelId).down('.editableRating .imgBar').hide();
		$(panelId).down('.editableRating .noRatedYet').show();
	} else {
		$(panelId).down('.editableRating .totalCount').show();
		$(panelId).down('.editableRating .imgBar').show();
		$(panelId).down('.editableRating .noRatedYet').hide();
	}
	$$('#'+panelId+' .editableRating .totalCount span.num').each(function(item){
		item.update(count);
	});
	$$('#'+panelId+' .editableRating .totalCount span.votes').each(function(item){
		if(!Prototype.Browser.IE) {
			item.update('<span class="value-title" title="'+count+'"></span>');
		}
	});
	var className = 'rating'+Math.round(avg);
	$(panelId).down('.editableRating .imgBar span.rating').addClassName(className);
}
