/**
 * @requires Nico.messages
 */
var mylistcomment_api = 'api/mylistcomment/list';
var load_flags = {mylistcomment: false}

function getinfo(contents, video_id) {
	if (!load_flags[contents]) {
		if (contents == 'mylistcomment') {
			new Ajax.Request(
				mylistcomment_api,
				{
					mechod: 'get',
					parameters: 'item_type=0&item_id='+video_id,
					onComplete: function (req) {
								displayMylistComment(req, video_id);
							}
				}
			);
		}
		load_flags[contents] = true;
	}
}

function displayMylistComment(request, video_id) {
	var json = eval('('+request.responseText+')');
	var mylistcomment = json.mylistcomment;
	var more = false;
	if (mylistcomment.length > 3) {
		mylistcomment = mylistcomment.slice(0, 3);
		more = true
	} else if (mylistcomment.length == 0) {
		mylistcomment = null;
	}
	if (mylistcomment) {
		for (var i=0; i < mylistcomment.length; i++) {
			mylistcomment[i].create_time = mylistcomment[i].create_time.replace(/(.*)-(.*)-(.*) (.*):(.*):(.*)/, "$1年$2月$3日 $4:$5");
		}
	}
	var source = $('info_mylistcomment').innerHTML;
	var dictionary = { player_video_id: video_id, mylistcomments: mylistcomment, more: more };
	var result = Jarty.eval(source, dictionary);
	$('mylist_comments').innerHTML = result;
}

function checkVideoArticle(video_id) {
	if(Nicopedia.inMaintenance) return;
	var article = Nicopedia.Article.get(video_id, 'v');
	$('video_article').href = article.getURL();
	article.loadWritten(function (article) {
		if (article.written) {
			$$('#video_article img')[0].src = '/img/common/icon/dic_on.png';
		}
	});
}

function addWatchlist(id, token){
	if(confirm(Nico.messages.favorite_add)){
		var pars = 'item_type=1&item_id='+id+'&token='+token;
		new Ajax.Request(
			'/api/watchitem/add',
			{
				method: 'post',
				parameters: pars,
				onComplete: function(request) {
					var res = eval('('+request.responseText+')');
					if (res.status == 'ok') {
						$('addWatchlist').style.display = 'none';
						$('addedWatchlist').style.display = 'block';
					} else {
						alert(Nico.messages.default_add_error);
					}
				}
			}
		);
	}
	return false;
}

function addChannelBookmark(channel_id, key, time, url) {
	var pars = 'channel_id='+channel_id+'&key='+key+'&time='+time;
	new JSONP( url, {
		parameters: pars
	});
}

var ChannelBookmark = {};

ChannelBookmark.addOnChangeButtonHide = function(json) {
	$('addChannelBookmark').style.display = 'none';
	$('addedChannelBookmark').style.display = 'block';
}

ChannelBookmark.addOnError = function(json) {
	alert(json.message);
}

var NicommonsTree =  {
	load: function (url, id, limit, callback) {
		var params = 'id='+encodeURIComponent(id)+'&limit='+encodeURIComponent(limit)+'&callback='+encodeURIComponent(callback);
		new JSONP( url, {
			parameters: params
		});
	},

	callback: function (jsonData) {
		if (jsonData && jsonData.countparent > 0) {
			Element.update($("mov_parent_num"), jsonData.countparent);
			$$("#mov_parent .parentImg img").each(function(elm){
				elm.src = jsonData.parent[0].thumbnail;
			});
			$$("#mov_parent .contentLink").each(function(elm){
				elm.href = jsonData.parent[0].url;
			});
			$$("#mov_parent .parentTitle").each(function(elm){
				elm.innerHTML = jsonData.parent[0].title;
			});
			var registered_date = jsonData.parent[0].registered_date.replace(/(.*)-(.*)-(.*) (.*):(.*):(.*)/, "$1年$2月$3日 $4:$5");
			$$("#mov_parent .parentRegisterDate").each(function(elm){
				elm.innerHTML = registered_date;
			});
			$("mov_parent").style.display = "block";
		}
	}
}
