var OnlineDiscussion = {
	
	nextMessageCountdown : 0,
	
	sendNewMessage : function(compId) {
		var controls = $("#formId_"+compId+" .discussion-form").find("input, textarea");
		var newMessage = new OnlineDiscussionMessage();
		for(var i = 0; i < controls.length; i++) {
			switch($(controls[i]).attr("id")) {
				case OnlineDiscussion.Message.Fields.title:
					newMessage.msg_title = $(controls[i]).val().trim();
					break;
				case OnlineDiscussion.Message.Fields.name:
					newMessage.msg_name = $(controls[i]).val().trim();
					break;
				case OnlineDiscussion.Message.Fields.email:
					newMessage.msg_email = $(controls[i]).val().trim();
					break;
				case OnlineDiscussion.Message.Fields.body:
					newMessage.msg_body = $(controls[i]).val().trim();
					break;
			}
		}
		if(this.nextMessageCountdown <= 0) {
			newMessage.save();
		}
	},
	
	showAnswerForm : function(row, uid) {
		$("#b"+row).hide();
		$("#c"+row).show();
		$(".online-discussion > .discussion-form-answer").clone().appendTo("#q"+row);
		$("#q"+row+" .discussion-form-answer input[name='parent']").val(uid);
		
	},

	hideAnswerForm : function(row) {
		$("#b"+row).show();
		$("#c"+row).hide();
		$("#q"+row+" .discussion-form-answer").remove();
		
	},
	
	ignorePost : function(row, uid) {
		if(confirm("Opravdu chcete tento příspěvek odstranit?")) {
			$.post("/ajax-online-discussion/", { cmd : 'ignore', uid : uid }, function(data){
				if(data == "ok") {
					$("#q"+row).fadeOut(400, function() {
						$(this).remove();
						$("#a"+row).remove();
					});
				}
				else {
					alert("Příspěvek nemohl být odstraněn: "+data);
				}
			});
		}
	},
	
	refreshPosts : function() {
		window.location.href = window.location.href;
	},
	
	close : function() {
		if(confirm("Ukončením rozhovoru nebudou moci lidé posílat dotazy, ale vy budeme moci ještě odpovídat. Přejete si online hovor ukončit?")) {
			sendCommand("CmdCloseDiscussion");
		}
	},
	
	nextMessageTimeout : function(timestamp, repeat) {
		if(typeof timestamp == "number") {
			this.nextMessageCountdown = timestamp;
			if(timestamp > 0) {
				--timestamp;
				$("#send-new-message").val("Další dotaz můžete odeslat za "+timestamp+"s");
				setTimeout("OnlineDiscussion.nextMessageTimeout("+timestamp+")", 1000);
			}
			else {
				$("#send-new-message").val("Odeslat dotaz");
				clearTimeout(this.nextMessageInterval);
				this.nextMessageInterval = null;
			}
		}
	},
	
	
	
	Message :  {
		
		Fields : {
			title : "msg_title",
			name : "msg_name",
			email : "msg_email",
			body : "msg_body"
		}
		
	}
	
}


function OnlineDiscussionMessage() {
	
	this.msg_title = null;
	this.msg_name = null;
	this.msg_email = null;
	this.msg_body = null;
	this.errorsCount = 0;
	
	this.save = function() {
		
		if(this.msg_name != null && this.msg_name == '') {
			//$("#msg_name").after('<div class="required-field-abs">Jméno je povinné</div>');
			InlineFieldError.show($("#msg_name"),'Jméno je povinné');
			this.errorsCount++;
		}
		else {
			InlineFieldError.hide($("#msg_name"));
		}
		if(this.msg_body == '') {
			InlineFieldError.show($("#msg_body"),'Obsah vzkazu je prázdný');
			this.errorsCount++;
		}
		else {
			InlineFieldError.hide($("#msg_body"));
		}
		if(this.errorsCount == 0) {
			sendCommand('CmdAddPost');
		}
	}
	
}

var InlineFieldError = {

	msgPositions : ['right'],
	
	show : function(el, message, position) {
		
		if(typeof position == "undefined") var position = 'right';
		
		position = this._getPosition(position);
		
		var uid = this._getUid(el);
		
		var existsElement = $('#'+uid);
		
		if(existsElement.length == 1) {
			existsElement.html('<span>'+message+'</span>');
		}
		else {
			switch(position) {
				case "right":
					var width = $(el).width() + 20;
					var height = $(el).height() + 6;
					if($.browser.msie) {
						width = 5;
						height = 0;
					}
					$(el).addClass("need-value");
					$(el).after('<div class="required-field-abs" id="'+uid+'" style="margin-left: '+width+'px; margin-top: -'+height+'px"><span>'+message+'</span></div>');
					break;
			}
		}
	},
	
	hide : function(el) {
		var uid = this._getUid(el);
		$("#"+uid).remove();
		$(el).removeClass("need-value");
	},
	
	_exists : function(uid) {
		return ($('#'+uid).length == 1) ? true : false;
	},
	
	_getUid : function(el) {

		var name = $(el).attr("name");
		if(typeof name == "undefined") name = "nx";
		
		var id = $(el).attr("id");
		if(typeof id == "undefined") id = "ix";
		
		var uid = 'required-field-'+id+'-'+name;
		
		return uid;
	},
	
	_getPosition : function(pos) {
		var found = false;
		for(var i = 0; i < this.msgPositions.length; i++) {
			if(this.msgPositions[i] == pos) {
				found = true;
			}
		}
		if(!found) return "right";
		return pos;
	}
}
