QANow = {
    changeProfileImgUrl: null,
    favUrl: null,
    niceUrl: null,
    followUrl: null,
    revokeUrl: null,
    mobileRegisterUrl: null,
    qrcodeUrl: null,
    magic: null
};

QANow.revoke = function() {
    if (confirm('本当に退会しますか？') == true) {
	var form = document.createElement('form');
	form.method = 'post';
	form.action = QANow.revokeUrl;
	form.style.display = 'none';
	var hidden = document.createElement('input');
	hidden.type = 'hidden';
	hidden.name = 'magic';
	hidden.value = QANow.magic;
	form.appendChild(hidden);
	if (document.body) {
	    $(document.body).append(form);
	} else {
	    $(document.documentElement).append(form);
	}
	form.submit();
    }
    return false;
};

(function($) {
    $.fn.extend({
	fav: function(id) {
	    var dst = this;
	    $.ajax({
		type: 'post',
		url: QANow.favUrl,
		data: 'id=' + encodeURIComponent(id) +
		      '&magic=' + encodeURIComponent(QANow.magic),
		success: function(ret) {
		    dst.replaceWith(ret);
		}
	    });
	},

	nice: function(id,user_id,nice_cnt) {
	    var dst = this;
	    nice_cnt = nice_cnt + 1;
	    $.ajax({
		type: 'post',
		url: QANow.niceUrl,
		data: 'id=' + encodeURIComponent(id) +
		      '&user_id=' + encodeURIComponent(user_id) +
		      '&nice_cnt=' + encodeURIComponent(nice_cnt),
		success: function(ret) {
		    dst.replaceWith(ret);
		}
	    });
	},


	follow: function(id) {
	    var dst = this;
	    $.ajax({
		type: 'post',
		url: QANow.followUrl,
		data: 'id=' + encodeURIComponent(id) +
		      '&magic=' + encodeURIComponent(QANow.magic),
		success: function(ret) {
		    dst.replaceWith(ret);
		}
	    });
	},

	changeProfileImg: function(id) {
	    var dst = this;
	    $.ajax({
		type: 'post',
		url: QANow.changeProfileImgUrl,
		data: 'magic=' + encodeURIComponent(QANow.magic),
		success: function(ret) {
		    dst.replaceWith(ret);
		}
	    });
	},

	showStrlen: function(s) {
	    var n = 140 - s.length;
	    if (n > 0) {
		this.addClass('plus');
		this.removeClass('minus');
	    } else {
		this.addClass('minus');
		this.removeClass('plus');
	    }
	    this.html('<span class="' + (n > 0 ? 'plus' : 'minus') + '">' + n +
		      '</span>');
	}
    });
})(jQuery);

QANow.mobileRegister = $(function(){
	$("#mobile_register").submit(function(){
		var mail = $("#mail").val();
		$("#result").load(QANow.mobileRegisterUrl + "?mail=" + encodeURIComponent(mail) );
		return false;
	});
});

QANow.qrcode = function() {
	var qrcode_id = $("#qrcode").val();
	$("#qrcode_result").load(QANow.qrcodeUrl + "?id=" + encodeURIComponent(qrcode_id) );
	return false;
};
