var hlVideoCar = {

	path		: '/flv/',
	player		: null,
	curPreview	: null,
	previewTimer: null,
	
	play		: function (file) {
		if (!hlVideoCar.player || !file) {
			return false;
		}
		
		hlVideoCar.player.sendEvent('LOAD', hlVideoCar.path + file);
		hlVideoCar.player.sendEvent('PLAY', 'true');
	},

	preview		: function (file) {
		var $p  = $('#preview');
		var $pT = $('#previewTarget');
		var img = hlVideoCar.path + file;

		window.clearTimeout(hlVideoCar.previewTimer);

		$pT
			.stop()
			.animate({ opacity : 0 }, 50, 'linear', function () {
				$(this)
					.hide()
					.attr('src', $p.attr('src'))
					.show()
					.css({ opacity : 1 });

				$p.attr('src', img);

				$(this)
					.animate({ opacity : 0 }, 500, 'easeInOutExpo', function () {
						$(this)
							.hide()
							.attr('src', img);
					});
			});
	}
}

function playerReady(thePlayer) {
	hlVideoCar.player = document.getElementById(thePlayer.id);
}

$(function () {
	
	hlVideoCar.curPreview = hlCarVideos[hlCarVideos.length-1].preview;
	
	$('#playerHolder')
		.append(
			'<div id="player"></div>' +
			'<div id="videoChooser">' +
				'<div id="previewWrapper">' +
					'<img id="previewTarget" src="' + hlVideoCar.path + hlCarVideos[hlCarVideos.length-1].preview + '" width="500" height="200" />' +
					'<img id="preview" src="' + hlVideoCar.path + hlCarVideos[hlCarVideos.length-1].preview + '" width="500" height="200" />' +
				'</div>' +
				'<img id="carouselPrev" src="img/common/arrow_prev.gif" />' +
				'<div id="carouselWrapper">' +
					'<div class="carousel">' +
						'<ul id="playerCarousel"></ul>' +
					'</div>' +
				'</div>' +
				'<img id="carouselNext" src="img/common/arrow_next.gif" />' +
			'</div>' +
			'<div id="usageHint">マウスをのせるとエピソード内容が表示されます</div>'
		);
	
	$('#playerCarousel li img')
		.live('mouseover', function () { hlVideoCar.preview($(this).attr('preview')); })
		.live('click', function () {
			hlVideoCar.play($(this).attr('video'));
			hlVideoCar.curPreview = $(this).attr('preview');
		})
		.live('mouseout', function () {
			 hlVideoCar.previewTimer = window.setTimeout(function () { hlVideoCar.preview(hlVideoCar.curPreview); }, 150);
		});

	var perPage = 4;
    var items = 0;

	$(hlCarVideos).each(function () {
		var config = this;
		$('#playerCarousel').append(
			"<li><img src='" + hlVideoCar.path + config.image + "' width='100' height='75' " +
			"preview='" + config.preview + "' video='" + config.video + "' /></li>"
		);
		items++;
	});
	for (; items % perPage != 0; items++) {
	    $('#playerCarousel').append('<li></li>');
	}
	
	$(".carousel").jCarouselLite({
		btnNext	: '#carouselNext',
		btnPrev	: '#carouselPrev',
		easing	: 'easeInOutExpo',
		speed	: 800,
		visible	: perPage,
		scroll  : perPage,
		circular: false
	});
	
    swfobject.embedSWF("flv/player.swf", "player", "377", "303", "9.0.115", false, {
		file				: hlVideoCar.path + hlCarVideos[hlCarVideos.length-1].video,
		image				: hlVideoCar.path + hlCarVideos[hlCarVideos.length-1].splash,
		autostart			: "false",
		backcolor			: '000000',
		frontcolor			: 'FFFFFF',
		lightcolor			: '999999'
	}, {
		allowfullscreen		: "true",
		allowscriptaccess	: "always"
	}, {
		id					: "player1",
		name				: "player1"
	});
	
});