$(function(){
	var sendLogFlag = true;
	var rpcFlag = true;
	var flg = { 'play':false, '10s':false, '20s':false, '30s':false, '40s':false, '50s':false, 'ended':false };
	var sq = 45017;
	var dispArea = "#" + "area1";

	function clip_log (type, sq){
		if (sendLogFlag){
			//if( !rpcFlag ) return;
			rpcFlag = false;

			var url = 'https://en.cchan.tv/clip/rpc/log/';
            var opt = { "type":type, "sq": sq , "pid": "0073" };
            $.post(url, opt, function(json){
            	rpcFlag = true;
            }, 'json');
            return false;
		}
	}

	if ("#" === dispArea){
		dispArea = "#cchan-part";
	}

	$(dispArea).html ('<div class="cchan_clip"><video poster="https://ccs3.akamaized.net/cchanclips/57d339ab9d804ba0bc5e61090600821d/th_w320_0032_m.jpg" preload="auto" class="video" controls><source src="https://ccs3.akamaized.net/cchanclips/57d339ab9d804ba0bc5e61090600821d/clip.mp4" type="video/mp4"><source src="https://ccs3.akamaized.net/cchanclips/57d339ab9d804ba0bc5e61090600821d/clip.webm" type="video/webm"></video></div>');

	$(dispArea + ' video').on('play', function(){
		if( !flg['play'] ){
			flg['play'] = true;
			clip_log('play', sq);
		}
	}).
	on('ended', function(){
		if( !flg['ended'] ){
			flg['ended'] = true;
			clip_log('ended', sq);
			flg = { 'play':false, '10s':false, '20s':false, '30s':false, '40s':false, '50s':false, 'ended':false };
		}
	}).
	on('timeupdate', function(){
		var s = $(dispArea + ' video').get(0).currentTime;
		if( s > 50 ){
			if( !flg['50s'] ){
				flg['50s'] = true;
				clip_log('50s', sq);
			}
		}else if( s > 40 ){
			if( !flg['40s'] ){
				flg['40s'] = true;
				clip_log('40s', sq);
			}
		}else if( s > 30 ){
			if( !flg['30s'] ){
				flg['30s'] = true;
				clip_log('30s', sq);
			}
		}else if( s > 20 ){
			if( !flg['20s'] ){
				flg['20s'] = true;
				clip_log('20s', sq);
			}
		}else if( s > 10 ){
			if( !flg['10s'] ){
				flg['10s'] = true;
				clip_log('10s', sq);
			}
		}
	}).
	click (function(){
		if (this.paused){
			this.play();
		} else{
			this.pause();
		}
		return false;
	});

	clip_log('show', sq);
});
