

function setRating(rating, media_key){

	var myAjax = new Ajax.Request(
		"ajax_com.php?action=mediarating",
			{
				method:'post',
				onComplete:showRatingResult,
				onFailure:showError,
				postBody: 'rating=' + rating + '&mediakey=' + media_key
			}
	)

}


function setFavorite(media_key){

	var myAjax = new Ajax.Request(
		"ajax_com.php?action=favourite",
		{
				method:'post',
				onComplete:showFavouriteResult,
				onFailure:showError,
				postBody: '&mediakey=' + media_key
			}
	)
}


function showFavouriteResult(r){

	if(CheckIfError(r)) return ;

	var result = r.responseXML.documentElement;
	var image = result.getElementsByTagName('image')[0].firstChild.data;
	var image_alt = result.getElementsByTagName('image_alt')[0].firstChild.data;
	var message = result.getElementsByTagName('message')[0].firstChild.data;

	$('favorite_button').src = image;
	$('favorite_button').alt = $('favorite_button').title= image_alt.unescapeHTML();

}

function showRatingResult(r){

	if(CheckIfError(r)) return ;

	var result = r.responseXML.documentElement;
	var stars = result.getElementsByTagName('stars')[0].firstChild.data;
	var message = result.getElementsByTagName('message')[0].firstChild.data;

	$('media_rating').innerHTML = stars + '<br /><br />' + message + '<br /><br />';
}

function checkMediaForm(){


	if(!Form.Element.present('comment_text_id')){
		Form.Element.focus('comment_text_id');

		new Effect.Highlight('comment_text_id', { startcolor: '#FF0000' } );

		return false;
	}


	var form_value = Form.Element.getValue('comment_text_id');
	form_value.stripTags();
	form_value.escapeHTML();

	var myAjax = new Ajax.Request(
		"ajax_com.php?action=comments",
			{
				method:'post',
				onComplete:showCommentResult,
				onFailure:showError,
				postBody: 'comment=' + form_value + '&mediakey=' + Form.Element.getValue('comment_mediakey_id')
			}
	)


	// Xhttp-Request

return false;
}

function showError(r){
	alert("Error: " + r.status + "/n" + r.statusText);
}

function CheckIfError(r){

	var result = r.responseXML.documentElement;
	var error = result.getElementsByTagName('error')[0].firstChild.data;
	var error_string = result.getElementsByTagName('error_string')[0].firstChild.data;

	if(error == 1){
		if(error_string != ""){
			alert("Error: " + error_string);
		}

	return true;
	}

return false;
}



function showCommentResult(r){
	if(CheckIfError(r)) return ;

	var result = r.responseXML.documentElement;
	var comments_count = result.getElementsByTagName('comments_count')[0].firstChild.data;
	var comments_result = result.getElementsByTagName('posted')[0].firstChild.data;

	if(comments_result == 0) return ;

	new Insertion.After('comment', '<p class="success_message"><b>' + comments_result + '</b></p>');
	Element.toggle('comment');

	$('comment_show').style.visibility = 'hidden';
	$('comment_count').innerHTML = comments_count;
	$('comment_show').innerHTML = '';

	for(var i=0; i< result.getElementsByTagName("comment").length; i++){
		var base = result.getElementsByTagName("comment")[i];

		var id = base.getElementsByTagName("CID")[0].firstChild.nodeValue;
		var name = base.getElementsByTagName("CUSER")[0].firstChild.nodeValue;
		var userid = base.getElementsByTagName("CUSERID")[0].firstChild.nodeValue;

		var text = base.getElementsByTagName("CTEXT")[0].firstChild.nodeValue;
		var date = base.getElementsByTagName("CDATE_DIFF")[0].firstChild.nodeValue;

		document.getElementById('comment_show').innerHTML += '<div class="boxdottedborder">'+ text +'</div>';
		document.getElementById('comment_show').innerHTML += '<a class="userlink" href="userprofile.php?uid='+ userid +'">'+ name +'</a> ' + date;
	}

	Element.remove('comment');

	window.setTimeout("$('comment_show').style.visibility='visible'; Effect.Appear('comment_show')", 1000);
}



