var current_comments = 0;

document.addEvent("domready",function(e){
	$$(".mod-comments").each(function(el){
		el.store("num",current_comments);
		el.getElements(".mod-comments-show").each(function(show){
			show.addEvent("click",function(e)
			{
				$$(".mod-comments").each(function(postsel){
					var posts = postsel.getElements(".mod-comments-posts");
					if(posts[postsel.retrieve("num")].retrieve("open") == 0 || !posts[postsel.retrieve("num")].retrieve("open"))
					{
						posts[postsel.retrieve("num")].store("open",1);
						posts[postsel.retrieve("num")].tween("height",posts[postsel.retrieve("num")].getScrollSize().y);
					}
					else
					{
						posts[postsel.retrieve("num")].store("open",0);
						posts[postsel.retrieve("num")].tween("height",0);
					}
				});
			});
		});
		el.getElements(".mod-comments-add").each(function(add){
			add.addEvent("click",function(e){
				$$(".mod-comments").each(function(postsel){
					var posts = postsel.getElements(".mod-comments-form");
					if(posts[postsel.retrieve("num")].retrieve("open") == 0 || !posts[postsel.retrieve("num")].retrieve("open"))
					{
						posts[postsel.retrieve("num")].store("open",1);
						posts[postsel.retrieve("num")].tween("height",posts[postsel.retrieve("num")].getScrollSize().y);
					}
					else
					{
						posts[postsel.retrieve("num")].store("open",0);
						posts[postsel.retrieve("num")].tween("height",0);
					}
				});
			});
		});
		el.getElements(".mod-comments-form form").each(function(post){
			post.store("num",current_comments);
			post.getChildren().each(function(div){
				div.getElements("input[type=button]").each(function(button){
					button.addEvent("click",function(){
						$$(".mod-comments").each(function(postsel){
							var posts = postsel.getElements(".mod-comments-form");
							var post = posts[postsel.retrieve("num")];
							var form = post.getElement("form");
							if(form.subject.value == "")
							{
								alert("Musite zadat nejaky predmet");
							}
							else if(form.text.value == "")
							{
								alert("Musite zadat nejaky text");
							}
							else
							{
								comments_request(postsel.retrieve("num"),form.toQueryString());
								form.subject.value = "";
								form.text.value = "";
								post.tween("height",0);
								post.store("open",0);
							}
						});
					});
				});
			});
		});
		
		comments_request.periodical(6000,{},[current_comments,""]);
		current_comments++;
	});
});

var comments_request = function(num,string)
{
	var posts = $$(".mod-comments-posts");
	var posts_div = posts[num];
	var url_req = comments_url_prefix;
	request = new Request.HTML({url:url_req,update:posts_div,data:string});
	request.addEvent("success",function(){
			var posts = $$(".mod-comments-posts");
			var posts_div = posts[num];
			var posts_posts = posts_div.getChildren(".mod-comments-post");
			var posts_count = posts_posts.length;
			if(posts_div.retrieve("open"))
			{
				posts_div.tween("height",posts_div.getScrollSize().y);
			}
			var nums = $$(".mod-comments-count");
			var count = nums[num];
			count.set("html",posts_count);
	});
	request.post();
}