// JavaScript Document
var comment_form_slider = "";
var comment_form_height_norating = 325;
var comment_form_height_rating = 325;
var comment_form_height = comment_form_height_norating;
var opened = false;
var rating_value = 0;
var IsRating = 0;

if(typeof parkside == "undefined") var parkside = new Object();
if(typeof parkside.rating == "undefined") parkside.rating = new Object();

// Constructor
parkside.rating = function() {
}

/// Class members
parkside.rating.prototype = {
/// Properties
	rating_results: null,
	
/// Methods
	request: function(params) 
	{		
		if( typeof http == "undefined" )
		{
			alert("JSMX not included!");
			return;
		}	
		var url = "custom/modules/rating/rating.cfc?method=rateObjectByAjaxCall";
		var response_function = this.rating_response;
		
		active_request = true;	
		document.getElementById('star-rating-value').innerHTML = "...";
		http("POST", url, response_function, params); //notice the third argument = list_response. This is the callback function below.
	}
	,
	rating_response: function(obj) //callback functions always take one argument. This is the result passed back from the server.
	{ 	
		if(obj !== "undefined") 
		{
			if(as.length > 0)
			{
				document.getElementById('star-rating').innerHTML = as[1] + "<br /><br />";
			}
		}	
	}
}

function initCommentForm()
{
	var comment_actions = document.getElementById('comment_actions');
	var comment_form = document.getElementById('comment_form');

	if(document.getElementById('star-rating'))
		document.getElementById('star-rating').style.display = "block";

	if(comment_actions != null & comment_form != null & document.getElementById('comment_post_error') != null)
	{
		comment_form_height_norating = comment_form.offsetHeight;
				
		comment_actions.style.display = "block";
		comment_form.style.display = "block";
		
		if(document.getElementById('current_rating') != null)
		{
			document.getElementById('current_rating').style.display = "none";
		}
		
		comment_form.style.height = comment_form_height_norating + 1 + "px";
		
		if(document.getElementById('rating_field').value != -1)
		{
			setRating(document.getElementById('rating_field').value);
			document.getElementById('rating_field').value = document.getElementById('rating_field').value;
			comment_form_height_rating = comment_form.offsetHeight + 20;
			comment_form.style.height = comment_form_height_rating + 1 + "px";
		}	
		opened = true;
	}
	else if (comment_actions != null & comment_form != null)
	{
		comment_form_height_norating = comment_form.offsetHeight;
		comment_form_height_rating = comment_form.offsetHeight + 20;

		comment_actions.style.display = "block";
		comment_form.style.display = "none";
		comment_form.style.height = 0;
		comment_form.style.overflow = "hidden";
		if(document.getElementById('current_rating') != null)
		{
			document.getElementById('current_rating').style.display = "none";
		}
	} 
}

function showCommentForm(collapse, rating)
{
	var comment_form = document.getElementById('comment_form');

	if(comment_form_slider != "")
	{
		window.clearInterval(comment_form_slider);
		comment_form_slider = "";
	}
	
	comment_form.style.display = "block";
	var current_form_height = Math.round(comment_form.style.height.substring(0, comment_form.style.height.length-2));	

	if(document.getElementById('star-rating') != null)
	{
		if(rating | 1 == 1)
		{
			document.getElementById('star-rating').style.display = "block";
			document.getElementById('rating_field').value = 0;
			comment_form_height = comment_form_height_rating;
		}
		else
		{
			document.getElementById('star-rating').style.display = "none";
			document.getElementById('rating_field').value = -1;
			comment_form_height = comment_form_height_norating;
		}
	}

	if(comment_form_height > current_form_height)
	{
		opened = false;
		
		comment_form_slider = window.setInterval(slideCommentForm, 1);
	}
	else if(collapse & IsRating == rating | opened == true)
	{
		opened = true;	
		comment_form.style.overflow = "hidden";
		comment_form_slider = window.setInterval(slideCommentForm, 1);
	}
	IsRating = rating;
}

function slideCommentForm()
{
	var comment_form = document.getElementById('comment_form');
	var current_form_height = Math.round(comment_form.style.height.substring(0, comment_form.style.height.length-2));
	if(current_form_height == "")
	{
		current_form_height = 0;
	}

	if(!opened)
	{
		if(comment_form_height > current_form_height)
		{
			comment_form.style.height = current_form_height + 15 + "px";				
		}
		else
		{
			window.clearInterval(comment_form_slider);
			comment_form.style.overflow = "visible";
		}
	}
	else
	{
		if(current_form_height > 0)
		{
			comment_form.style.height = current_form_height - 15 + "px";				
		}
		else
		{
			window.clearInterval(comment_form_slider);
			comment_form.style.overflow = "hidden";
			if(document.getElementById('star-rating') != null)
				document.getElementById('star-rating').style.display = "none";
		}
	}
}

function setRating(value)
{
	if(value < 0 & value > 5)
		value = 0;

	var rating_width = 0;
	if(rating_value != value)
		rating_width = value * 19;
	else value = 0;
	rating_value = value;

	document.getElementById('current_rating').style.width = rating_width + "px";
	document.getElementById('current_rating').style.display = "block";
	document.getElementById('rating_field').value = value;

	if(rating_value != 0)
	{
/*		parkside.rating = new parkside.rating();
		
		var params = new Object();
		params.rating_value = value;
		params.user_id = document.getElementById('star-rating-value-user_id').innerHTML;
		params.id = document.getElementById('star-rating-value-id').innerHTML;
		params.type = document.getElementById('star-rating-value-type').innerHTML;
		parkside.rating.request(params);	*/
		
		var star_rating_value = document.getElementById('star-rating-value-hidden').innerHTML;
		document.getElementById('star-rating-value').innerHTML = value + " " + star_rating_value;
	}
	else
	{
		document.getElementById('star-rating-value').innerHTML = "";
	}
}

function hoverRating(value)
{
	var xvalue = value;
	if(value < 0 & value > 5)
		value = 0;

	var rating_width = 0;
	if(rating_value != value)
		rating_width = value * 19;
	else value = 0;
	rating_value = value;

	document.getElementById('current_rating').style.width = rating_width + "px";
	document.getElementById('current_rating').style.display = "block";
	document.getElementById('rating_field').value = value;


	var star_rating_value = document.getElementById('star-rating-value-hidden').innerHTML;
	document.getElementById('star-rating-value').innerHTML = xvalue + " " + star_rating_value;
}

function resetRating()
{
	document.getElementById('current_rating').style.width = 0 + "px";
	document.getElementById('current_rating').style.display = "block";
	document.getElementById('rating_field').value = "";
	document.getElementById('star-rating-value').innerHTML = "";
}

function replyToComment(id, username)
{
	document.getElementById('parent_id').value = id;
	if(id > 0)
	{
		var text = username + " antworten ";
		text += " <a href='#comment_form' onclick='replyToComment(0);'>[x]</a><br /><br />";
		document.getElementById('reply_to').innerHTML = text;
		comment_form_height_norating += 20;
	}
	else
	{
		document.getElementById('reply_to').innerHTML = "";
	}
}

if (window.addEventListener)
	window.addEventListener("load", initCommentForm, false);
else if (window.attachEvent)
	window.attachEvent("onload", initCommentForm);
