
var GroupContent = Class.create(Basic, {
	
	getAnswers : function(id, from, type) {
	new Ajax.Request(FULL_HNAME + '/groupcontent/getanswers/' + id + ',' + from, {
		method : 'post',
		parameters : {type: type},
		onSuccess : function(result) {
			$('content-answers').update(result.responseText);
			$('content-comments').update('');
			$('content-answer').update('');
		}
	});
	},
	
	voteAll : function(gid, id, mark) {
		
		var id = id;
		new Ajax.Request(FULL_HNAME + '/groupcontent/voteall/' + gid + ',' + id, {
			method: "post",
			parameters: {
				mark: mark
			},
			onSuccess: function(result){
					$('vote_' + id).update('oddano głos');
					$('mark_' + id).update(result.responseText);			
			}
		});
	
	},
	
	getPopAnswers : function(gid, type, site) {
		new Ajax.Request(FULL_HNAME + '/groupcontent/getpopanswers/' + gid + ',' + type + ',' + site, {
			method : 'post',
			onSuccess : function(result) {
				$('content_box').update(result.responseText);
			}
		});
	},
	
	getEmptyAnswers : function(gid, type, site) {
		new Ajax.Request(FULL_HNAME + '/groupcontent/getemptyanswers/' + gid + ',' + type + ',' + site, {
			method : 'post',
			onSuccess : function(result) {
				$('content_box').update(result.responseText);
			}
		});
	}

});



var groupcontent = new GroupContent

var People = Class.create(Basic, {
	
	getViewTree: function(tree_id) {
				$('tree-loading').show();
				new Ajax.Request(FULL_HNAME + '/Content/viewtree/' + tree_id,  { 
							method : 'post',
								onSuccess : function(t) {
								$('tree-loading').hide();
								$('viewtree').update(t.responseText);
								
								}});
				
			}

});

var Startpage = Class.create(Basic, {	
	getNewAnswers: function(site) {
				new Ajax.Request(FULL_HNAME + '/Start/getnewanswers/' + site,  { 
							method : 'post',
								onSuccess : function(t) {
								$('answers_content').update(t.responseText);
								
								}});			
			},
	GetMainTopics: function(cat_id, type) {
	      $('loading_'+cat_id).show();
				new Ajax.Request(FULL_HNAME + '/Start/getmaintopics/'  + cat_id + ',' + type, { 
							method : 'get',
								onSuccess : function(t) {
								$('box_'+cat_id).update(t.responseText);
								$('loading_'+cat_id).hide();
								
								}});			
			}
});

var startpage = new Startpage();

var Content = Class.create(Basic, {

			ACTION_ANSWER	:	1,
			ACTION_COM		:	2,
			
			answerTab: function(action) {
				
				var id = $('answer_id').innerHTML;
				if(action == this.ACTION_ANSWER) {
					if($('answer_all_content').visible())
						return;
					$('disc_non').show();
					$('disc_act').hide();
					$('ans_non').hide();
					$('ans_act').show();
					$('comments_all_content').toggle();
					$('answer_all_content').toggle();
				} 
				else if(action == this.ACTION_COM) {
					if($('comments_all_content').visible())
						return;
					new Ajax.Request(FULL_HNAME + '/Content/getnewcomments/' + id + ',' + 1, {
							method : 'post',
							onSuccess : function(result) {
						$('comments_box').update(result.responseText);
						$('disc_non').hide();
						$('disc_act').show();
						$('ans_non').show();
						$('ans_act').hide();
						$('answer_all_content').toggle();
						$('comments_all_content').toggle();
					}
					});	
					
				}
			},
			openCommentArea : function(id) {
				
				if(arguments.length == 1) {
					var temp_feedback = $('com_' + id).innerHTML.replace(/<br>/g, '').unescapeHTML().split("\n");
					for(var i = 0; i < temp_feedback.size(); i++)
						temp_feedback[i] = '>>' + temp_feedback[i]; 
					$('feedback').value = "\n\n" + temp_feedback.join("\n");
						
				}
				location.href = location.toString().replace(/#form_kom/g, "").replace(/#last_comment/g, "") + '#form_kom';
				
			},
			
			spyQuestion	: function(question_id) {
				
				new Ajax.Request(FULL_HNAME + '/Content/spyquestion/' + question_id,  { 
							method : 'post',
								onSuccess : function() {
									$('spy_question').hide();
									$('stopspy_question').show();
									
								}});
				
			},
			
			stopSpyQuestion	: function(question_id) {
				
				new Ajax.Request(FULL_HNAME + '/Content/stopspyquestion/' + question_id,  { 
							method : 'post',
								onSuccess : function() {
								$('stopspy_question').hide();
									$('spy_question').show();
									
									
								}});
				
			},
			
			getViewTree: function(tree_id) {
				$('tree-loading').show();
				new Ajax.Request(FULL_HNAME + '/Content/viewtree/' + tree_id,  { 
							method : 'post',
								onSuccess : function(t) {
								$('tree-loading').hide();
								$('viewtree').update(t.responseText);
								
								}});
				
			},
			
			getPopAnswers : function(site) {
				new Ajax.Request(FULL_HNAME + '/Content/getpopanswers/' + site, {
					method : 'post',
					onSuccess : function(result) {
						$('popular-box').update(result.responseText);
					}
				});
			},
			getNewestAnswers : function(site) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewanswers/' + site, {
					method : 'post',
					onSuccess : function(result) {
						$('newest-box').update(result.responseText);
					}
				});
			},
			getNewPopAnswers : function(type, site) {
				new Ajax.Request(FULL_HNAME + '/Content/getpopanswers/' + type + ',' + site, {
					method : 'post',
					onSuccess : function(result) {
						$('content_box').update(result.responseText);
					}
				});
			},
			getNewRecAnswers : function(type, site) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewrecanswers/' + type + ',' + site, {
					method : 'post',
					onSuccess : function(result) {
						$('content_box').update(result.responseText);
					}
				});
			},
			
			getEmptyAnswers : function(type, site) {
				new Ajax.Request(FULL_HNAME + '/Content/getemptyanswers/' + type + ',' + site, {
					method : 'post',
					onSuccess : function(result) {
						$('content_box').update(result.responseText);
					}
				});
			},
			
			deleteAttachment : function(aid, id) {
				new Ajax.Request(FULL_HNAME + '/Content/deletefile/' + aid + ',' + id, {
					method: 'post',
					onSuccess : function(result) {
						$('attachments').update(result.responseText);
					}
				});
			
			},
			getNewNewestAnswers : function(type, site) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewanswers/' + type + ',' + site, {
					method : 'post',
					onSuccess : function(result) {
						$('content_box').update(result.responseText);
					}
				});
			},
			getGroupComments : function(type, site) {
				new Ajax.Request(FULL_HNAME + '/Content/getgroupcomments/' + type + ',' + site, {
					method : 'post',
					onSuccess : function(result) {
						$('content_box_discussion').update(result.responseText);
					}
				});
			},
			getAnswers : function(id, from, type) {
				new Ajax.Request(FULL_HNAME + '/Content/getanswers/' + id + ',' + from, {
					method : 'post',
					parameters : {type: type},
					onSuccess : function(result) {
						$('content-answers').update(result.responseText);
						$('content-comments').update('');
						$('content-answer').update('');
					}
				});
			},
			
			getAnswersByMark : function(id, from, type) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewanswersbymark/' + id + ',' + from, {
					method : 'post',
					parameters : {type: type},
					onSuccess : function(result) {
						$('content-answers').update(result.responseText);
						$('content-comments').update('');
						$('content-answer').update('');
					}
				});
			},
			
			getNewAnswers : function(id, from) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewanswersbydate/' + id + ',' + from, {
					method : 'post',
					parameters : {},
					onSuccess : function(result) {
						$('answers_list').update(result.responseText);
						
					}
				});
			},
			
			getNewAnswersByMark : function(id, from) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewanswersbymark/' + id + ',' + from, {
					method : 'post',
					parameters : {},
					onSuccess : function(result) {
						$('answers_list').update(result.responseText);
					}
				});
			},
			
			getComments : function(id, from) {
				
				if(arguments.length == 3) {
					
					new Ajax.Request(FULL_HNAME + '/Content/getexpcomments/' + id + ',' + from + ',' + arguments[2], {
					method : 'post',
					onSuccess : function(result) {
						$('content-comments').update(result.responseText);
					}
					});	
				}
				else {
					new Ajax.Request(FULL_HNAME + '/Content/getcomments/' + id + ',' + from, {
					method : 'post',
					onSuccess : function(result) {
						$('content-comments').update(result.responseText);
					}
					});	
				}
				
			},
			
			getNewComments : function(id, from) {
			
				new Ajax.Request(FULL_HNAME + '/Content/getnewcomments/' + id + ',' + from, {
				method : 'post',
				onSuccess : function(result) {
//					console.log(result.responseText);
					$('comments_box').update(result.responseText);
				}
				});		
			},
			
			getAnswer : function(id) {
				new Ajax.Request(FULL_HNAME + '/Content/getanswer/' + id, {
				method: "post",
				onSuccess :  function(result) {
					var ans = $$('.answerek');
					for(var i=0; i < ans.length; i++) {
						if(ans[i].id == ('cona_' + id))
							ans[i].style.fontWeight = 'bold';
						else
							ans[i].style.fontWeight ='normal';
					}
					$('content-answer').update(result.responseText);
					$('content-comments').update('');
				}
				});
			},
			
			getNewContentAnswer : function(id) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewanswer/' + id, {
				method: "post",
				onSuccess :  function(result) {
					var ans = $$('.answerek');
					for(var i=0; i < ans.length; i++) {
						if(ans[i].id == ('cona_' + id))
							ans[i].style.fontWeight = 'bold';
						else
							ans[i].style.fontWeight ='normal';
					}
					$('answer_box_area').update(result.responseText);
					$('answer_id').innerHTML = id;
				}
				});
			},
			
			getNewAnswer : function(id) {
				new Ajax.Request(FULL_HNAME + '/Content/getnewanswer/' + id, {
				method: "post",
				onSuccess :  function(result) {
					$('content-answer').update(result.responseText);
					$('content-comments').update('');
				}
				});
			},
			
			vote : function(id, mark) {
				
				if (arguments.length == 3) {
					new Ajax.Request(FULL_HNAME + '/Content/voteexp/' + id + ',' + arguments[2], {
						method: "post",
						parameters: {
							mark: mark
						},
						onSuccess: function(result){
							$('vote').update(result.responseText);
						}
					});
				}
				else {				
					new Ajax.Request(FULL_HNAME + '/Content/vote/' + id, {
						method: "post",
						parameters: {
							mark: mark
						},
						onSuccess: function(result){
							$('vote').update(result.responseText);
						}
					});
				}
			},
			
			voteAll : function(id, mark) {
				
					var id = id;
					new Ajax.Request(FULL_HNAME + '/Content/voteall/' + id, {
						method: "post",
						parameters: {
							mark: mark
						},
						onSuccess: function(t){
							//if(result.responseText == '') {
							//	$('vote_' + id).update('nie masz praw do zasobu');
							//} else {
							  
								$('mark_' + id).update(t.responseText);
								
							//}
						}
					});
				
			},
			
				showCommentField : function(id) {
				
					var id = id;
					new Ajax.Request(FULL_HNAME + '/Content/addcomment/' + id + ',1', {
						method: "get",
						onSuccess: function(t){
							//if(result.responseText == '') {
							//	$('vote_' + id).update('nie masz praw do zasobu');
							//} else {
							  
								$('comment_' + id).update(t.responseText);
								
							//}
						}
					});
				
			},
			
				showMessageField : function(id) {
				
					var id = id;
					new Ajax.Request(FULL_HNAME + '/Post/sendmsg/' + id, {
						method: "get",
						onSuccess: function(t){
							//if(result.responseText == '') {
							//	$('vote_' + id).update('nie masz praw do zasobu');
							//} else {
							  
								$('comment_' + id).update(t.responseText);
								
							//}
						}
					});
				
			},			
			
			addFeedback : function(id) {
				if($('kom').visible())
					$('kom').hide();
				else
					$('kom').show();
			},
			
			sendNewFeedback : function(id) {
				
				var full_name = FULL_HNAME;
				if(this.feed != null && this.feed == true)
					return;
					
				this.feed = true;
				new Ajax.Request(FULL_HNAME + '/Content/addnewcomment/' + id, {
					method: "post",
					parameters: $('form_kom').serialize(true),
					onSuccess: function(result){
						$('feedback').value = '';
						this.feed = false;
						if (result.responseText.length > 10) {
							$('comments_box').update(result.responseText);
							location.href = location.toString().replace(/#form_kom/g, "").replace(/#last_comment/g, "") + '#last_comment';
						}
						/*
						console.log(full_name);
						new Ajax.Request(full_name + '/Content/getnewcomments/' + id + ',1', {
							method: 'post',
							onSuccess: function(result){
								$('comments_box').update(result.responseText);
//								location.href = '#top_comments';
							}
						});*/
					}.bindAsEventListener(this)
				});
				
			},
			
			sendFeedback : function(id) {
				
				var full_name = FULL_HNAME;
				
				if (arguments.length == 2) {
					var sendfeedback_id = arguments[1];	
					new Ajax.Request(FULL_HNAME + '/Content/addexpcomment/' + arguments[1], {
						method: "post",
						parameters: $('form_kom').serialize(true),
						onSuccess: function(result){
							$('kom').toggle();
							$('feedback').value = '';
							$('comments_count').innerHTML = parseInt($('comments_count').innerHTML) + 1;
							$('kominfo').update(result.responseText);
							if ($('kominfo').visible() == false) 
								$('kominfo').show();
							new Ajax.Request(full_name + '/Content/getexpcomments/' + id + ',1,' + sendfeedback_id, {
								method: 'post',
								onSuccess: function(result){
									$('content-comments').update(result.responseText);
								}
							});
						}
					});
				
				}
				else {
				
				
					new Ajax.Request(FULL_HNAME + '/Content/addcomment', {
						method: "post",
						parameters: $('form_kom').serialize(true),
						onSuccess: function(result){
							$('kom').toggle();
							$('feedback').value = '';
							$('comments_count').innerHTML = parseInt($('comments_count').innerHTML) + 1;
							$('kominfo').update(result.responseText);
							if ($('kominfo').visible() == false) 
								$('kominfo').show();
							new Ajax.Request(full_name + '/Content/getcomments/' + id + ',1', {
								method: 'post',
								onSuccess: function(result){
									$('content-comments').update(result.responseText);
								}
							});
						}
					});
				}
				
				return false;
		},
		getAnswersAnswer : function(id, from, type, element) {
				if (arguments.length == 5) {
					new Ajax.Request(FULL_HNAME + '/Content/getexpanswers/' + id + ',' + from + ',' + arguments[4], {
						method: 'post',
						parameters: {
							'type': type
						},
						onSuccess: function(result){
							$('' + element + '').update(result.responseText);
						}
					});
				}
				else {			
					new Ajax.Request(FULL_HNAME + '/Content/getanswers/' + id + ',' + from, {
						method: 'post',
						parameters: {
							'type': type
						},
						onSuccess: function(result){
							$('' + element + '').update(result.responseText);
						}
					});
				}
			},
			
		getAnswersByMarkAnswer : function(id, from, type, element) {
				
				if (arguments.length == 5) {
					
					new Ajax.Request(FULL_HNAME + '/Content/getexpanswersbymark/' + id + ',' + from + ',' + arguments[4], {
						method: 'post',
						parameters: {
							type: type
						},
						onSuccess: function(result){
							$('' + element + '').update(result.responseText);
						}
					});
				}
				else {
				
					new Ajax.Request(FULL_HNAME + '/Content/getanswersbymark/' + id + ',' + from, {
						method: 'post',
						parameters: {
							type: type
						},
						onSuccess: function(result){
							$('' + element + '').update(result.responseText);
						}
					});
				}
			},
		getAnswerAnswer : function(id) {
				
				if(arguments.length == 2) {
					
					new Ajax.Request(FULL_HNAME + '/Content/getexpanswer/' + id + ',' + arguments[1], {
					method: "post",
					onSuccess :  function(result) {
						$('answer-box').update(result.responseText);
						$('content-comments').update('');					
						}
					});
				}
				else {
					
					new Ajax.Request(FULL_HNAME + '/Content/getanswer/' + id, {
					method: "post",
					onSuccess :  function(result) {
						$('answer-box').update(result.responseText);
						$('content-comments').update('');					
						}
					});
				}
			}
});

var content = new Content();

var Premium = Class.create(Basic, {

		getPosts : function(id, from, action) {
			new Ajax.Request(FULL_HNAME + '/Content/getposts/' + id + ',' + from + ',' + action, {
				method : 'post',
				onSuccess : function(result) {
					$('posts-box').update(result.responseText);
				}
			});
		},
		addToggle : function() {
			$('addpost-box').toggle();
		},
		sendPost : function(id) {
			new Ajax.Request(FULL_HNAME + '/Content/addpost/' + id, {
				method: "post",
				parameters : $('post-form').serialize(true),
				onSuccess : function(result) {
					$('addpost-box').toggle();
					$('posts-box').update(result.responseText);
					
			}});
		}

});

var premium = new Premium();
