﻿// m-questions
function Questions(mag) {
	this._module = $('#m-questions');
	this._id = null;
	this._binded = false;
	this._mag = mag;
}

Questions.prototype = {
	show: function(id) {
		var that = this;
		if (!this._binded) {
			this._bind();
			this._binded = true;
		}
		
		$.ajax({
			url: "/ajax/object_questions", 
			data: { id: id, mag: that._mag },
			type: 'get',
			success: function(html) {				
				that._module.html(html);
				that._module.show();
				
			
				// триггер открытия Опросов
				that._module.trigger('onshow');
				
				/*that._module.find('.QapTcha').QapTcha({
					disabledSubmit:true,
					autoRevert:true,
					txtUnlock: "Разблокировано.",
					txtLock: "Оживите форму, передвинув слайдер к замку"
				});*/
			}
		});
		this._id = id;
	},
	
	hide: function() {
		this._module.hide().empty();
	},
	
	_bind: function() {
		var that = this;
		$('#voting_object').live('click', function() {
			var  user_id = $('#user_id').val();
			if (account.isLogin) {
				$.ajax({
					type: 'post',
					url: 'ajax/object_voit', 
					data: {
						user_id: account.id,
						object_id: that._id,
						rate: $('.voting_object:checked').val()
					}, 
					success: function(data) {
						$('#m-question-vote').html(data);
					}
				});
			} else {
				alert('Необходим выполнить вход или зарегистрироваться');
			}
		});
		
		$('#m-question-close').live('click', function() {
			that.hide();
			that._module.trigger('onclose');
		});
		
		/*$('#send_message_question').live('click', function() {
			var valid = true;
			var message = $('#comment_text_question').val();
			if (message.length > 0) {
				if (message.length > 1000) {
					alert('to long message');
				} else {
					if (!account.isLogin) {				
						alert('Необходим выполнить вход или зарегистрироваться');
					} else {
						$.ajax({
							type: 'post',
							url: 'ajax/object_public_comments', 
							data: {
								comment_text: message,
								user_id: account.id,
								object_id: that._id
							}, 
							success: function(data) {
								if (data == 0) {
									alert('Необходим выполнить вход или зарегистрироваться');
								} else {								
									that.show(that._id);
								}
							}
						});
					}
				}
			
			}

		});*/
		
		/*$('#m-question-form').live('submit', function() {
			return false;
		});*/
	},

	bind: function(eventType, handler) {
		$.fn.bind.apply(this._module, arguments);
	}
};
