/**
 * @requires Nico.messages
 */
var FavMylistMessage = {
	adding: Nico.messages.adding,
	add_confirm: Nico.messages.add_confirm,
	add_success: Nico.messages.add_success,
	add_fail: Nico.messages.add_fail
}

var FavMylist = {
	item_id: null,
	csrf_token: '',
	url: '/api/watchitem/add',

	add: function(elm) {
		if (jQuery(elm).hasClass('disable')) {
			return false;
		}
		if (!confirm(FavMylistMessage.add_confirm)) {
			return false;
		}

		var item_id = jQuery(elm).attr('data-item-id');
		if (typeof item_id === 'undefined') {
			return false;
		}
		jQuery(elm).addClass('active');
		jQuery(elm).addClass('disable');
		jQuery(elm).text(FavMylistMessage.adding);

		data = {item_type: 7, item_id: item_id, token: this.csrf_token};

		jQuery.ajax({
			type: 'post', url: this.url, dataType: 'json', data: data,
			success: function(json) {
				var fail = json.status == 'fail';
				var btnMessage;
				if (fail) {
					btnMessage = FavMylistMessage.add_fail;
				} else {
					btnMessage = FavMylistMessage.add_success;
					jQuery('.watchedCount').text(parseInt(jQuery('.watchedCount').text(), 10) + 1);
				}
				jQuery('.fav_mylist_btn').text(btnMessage);

			},
			complete: function(req, status) {
			},
			error: function(xhr) {
				raiseError(xhr.responseText);
			}
		});
	}
}
//Nico.onReady(function() {
//	alert('hoge');
//	var test = jQuery('.fav_mylist_btn');
//	jQuery('.fav_mylist_btn').click(function() {FavMylist.add(this);});
//	//jQuery('span').click(function() {FavMylist.add(this);});
//});

