var sort = 'p', order = 'd', page = 1;

$(function(){
	var dic_url = 'http://api.nicodic.jp/e/callback';
	$('#tag li strong').each(function(){
		dic_url += '/' + encodeURIComponent($(this).text());
	});
	$.ajax({
		url: dic_url,
		dataType: 'script',
		cache: true
	});
	if ($('#ichiba').length != 0) ichiba(video_id);
	if ($('#relation').length != 0) relation(video_id, sort, order, page);
});

function callback(b) {
	$('#tag li a.nicopedia').each(function(i){
		if (!b[i]) $(this).css('background', 'gray');
	});
}

function ichiba(video_id) {
	$.ajax({
		url: '/ichiba/' + video_id,
		type: 'GET',
		dataType: 'xml',
		success: function(xml) {
			$('#ichiba').show();
			var items = $(xml).find('Item');
			for (var i=0;i<items.length;i++) {
				$('#ichiba div').append($(document.createElement('p')).attr(
					'class', 'column span-4' + ((i + 1) % 5 == 0 ? ' last' : '')
				));
				$('#ichiba p:last').append($(document.createElement('a')).attr({
					href: items.eq(i).find('DetailPageURL').text(),
					target: '_blank'
				}));
				$('#ichiba p:last a').append($(document.createElement('img')).attr({
					src: items.eq(i).find('LargeImage:first > URL').text() ? items.eq(i).find('LargeImage:first > URL').text().replace('.jpg', '._AA130_.jpg') : 'http://images-jp.amazon.com/images/G/09/nav2/dp/no-image-no-ciu._AA130_.gif',
					alt: items.eq(i).find('Title').text(),
					title: items.eq(i).find('Title').text(),
					width: 130,
					height: 130,
					style: 'display:none;'
				}));
			}
			$('#ichiba img:hidden').tooltip({showURL:false}).fadeIn('slow').click(function(){
				pageTracker._trackPageview('/asin/' + $(this).parent().attr('href').match(/\/dp\/(\w*)/)[1]);
			});
		}
	});
}

function relation(video_id, sort, order, page) {
	$.ajax({
		url: '/relation/' + video_id + '/' + sort + '/' + order + '/' + page,
		type: 'GET',
		dataType: 'xml',
		success: function(xml) {
			if ($(xml).find('related_video').attr('status') == 'ok' && $(xml).find('page_count').text() > 0) {
				$('#sort').val(sort);
				$('#order').val(order);
				$('#relation').show();
				$('#relation-entry').empty();
				var video = $(xml).find('video');
				for (var i=0;i<video.length;i++) {
					$('#relation-entry').append($(document.createElement('p')).attr(
						'class', 'column span-4' + ((i + 1) % 5 == 0 ? ' last' : '')
					));
					$('#relation-entry p:last').append($(document.createElement('a')).attr(
						'href', video.eq(i).find('url').text().replace('http://www.nicovideo.jp', '')
					));
					$('#relation-entry p:last a').append($(document.createElement('img')).attr({
						src: video.eq(i).find('thumbnail').text(),
						alt: video.eq(i).find('title').text(),
						title: video.eq(i).find('title').text(),
						width: 130,
						height: 100,
						style: 'display:none;'
					}));
				}
				$('#relation-entry img:hidden').tooltip({showURL:false}).fadeIn('slow');
				if ($(xml).find('page_count').text() > 1) {
					$('#relation-pager').show();
					page > 1 ? $('#back').show() : $('#back').hide();
					$('#pager-numbers').empty();
					for (var i=1;i<=$(xml).find('page_count').text();i++) {
						i == page ?
							$('#pager-numbers').append(i + ' '):
							$('#pager-numbers').append('<a href="javascript:page=' + i + ';relation(video_id, sort, order, page);">' + i + '</a> ');
					}
					page < $(xml).find('page_count').text() ? $('#next').show() : $('#next').hide();
				}
			}
		}
	});
}

function play(video_id) {
	window.open(
		'http://www.nicozon.net/player.html?video_id=' + video_id,
		'',
		'location=0,directories=0,status=0,toolbar=0,scrollbars=0,menubar=0,resizable=0,width=640,height=480'
	);
}

function full_screen_play(video_id) {
	var player = window.open(
		'http://www.nicozon.net/player.html?video_id=' + video_id,
		'',
		'location=0,directories=0,status=0,toolbar=0,scrollbars=0,menubar=0,resizable=0,left=0,top=0,width=' +
		screen.availWidth +
		',height=' +
		screen.availHeight +
		',fullscreen=1'
	);
	if (!$.browser.msie) {
		player.moveTo(0, 0);
		player.resizeTo(screen.availWidth, screen.availHeight);
	}
}
