//------------------リダイレクト設定--------------

rd_t_y = new Array(7,7,7,7,7,7,7);
redirect_jump_times = rd_t_y[new Date().getDay()];//回ジャンプ

force_noredirect = 0;

try_list_status = 0;

now_list_get = new Array();
now_jump_list = new Array();
function get_list_ajax(this_eid,this_ch){

	if(redirect_flag[this_eid] == 1 && !now_jump_list[this_eid] && !force_noredirect){now_jump_list[this_eid] = 1;return true;}
	if(now_list_get[this_eid] == 1){return false;}//無反応
	now_list_get[this_eid] = 1;
	eid = this_eid;

	start_get_list_ajax();

	if (redirect_flag[this_eid] == 2 && cookie_read() && !force_noredirect) {
		if (jump_cookie_check(this_ch)){return false;}else{window.open("http://ytu.be/redirecter.php?"+$('jumplink_'+this_eid).href);return false;}//return true;}
	}
	return false;

}

function start_get_list_ajax(){
	httpObj3 = createXMLHttpRequest(res_get_list_ajax);
	if(httpObj3){
		httpObj3.open("GET","http://ytu.be/vlist/"+eid+".txt",true);
		httpObj3.send(null);
	}
}

function res_get_list_ajax(){
	if((httpObj3.readyState == 4) && (httpObj3.status == 200)){
		$('vlist_'+eid).innerHTML = httpObj3.responseText;
		//txtが古い
		try_list_status++;
		if ( (new Date(httpObj3.getResponseHeader("Last-Modified"))).getTime() < (new Date()).getTime()-1000*60*60*24*1 && try_list_status == 1) {
			start_make_list_ajax();
		}
	}else if ((httpObj3.readyState == 4) && (httpObj3.status == 404)){
		try_list_status++;
		if (try_list_status == 1) {
			start_make_list_ajax();
		}
	}
}

function start_make_list_ajax(){
	httpObj7 = createXMLHttpRequest(res_make_list_ajax);
	if(httpObj7){
		httpObj7.open("GET","http://ytu.be/delete.php?mode=get_list&eid="+eid+"&output=html",true);
		httpObj7.send(null);
	}
}

function res_make_list_ajax(){
	if((httpObj7.readyState == 4) && (httpObj7.status == 200)){
		start_get_list_ajax();
	}
}

//------------------関連動画読み込み設定----------
now_related_loading = 0;
function background_related_load(tmp_site,tmp_vid){
	if(!now_related_loading){
		now_related_loading = 1;
		var base_url = "http://ytu.be/delete.php?mode=related&vid="+tmp_vid+"&site="+tmp_site;

		httpObj5 = createXMLHttpRequest(res_related_load);
		if(httpObj5){
			httpObj5.open("GET",base_url,true);
			httpObj5.send(null);
		}
	}
}
function res_related_load(){
	if((httpObj5.readyState == 4) && (httpObj5.status == 200)){
		now_related_loading = 0;
		var res_data = httpObj5.responseText;//No Action
		//alert(res_data);
	}
}

//------------------動画タグ指定------------------
function get_vcode(g_vid,g_site){
	if(!g_site){return "";}
	switch(g_site){
	case 1:
	vhtml = '<object width="【X】" height="【Y】"><param name="movie" value="http://www.youtube.com/v/【V】&autoplay=1&hd=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/【V】&autoplay=1&hd=1" type="application/x-shockwave-flash" wmode="transparent" width="【X】" height="【Y】"></embed></object>';break;
	case 3:
	vhtml = '<object width="【X】" height="【Y】"><param name="movie" value="http://www.dailymotion.com/swf/【V】&autoplay=1"></param><param name="allowfullscreen" value="true"></param><embed src="http://www.dailymotion.com/swf/【V】&autoplay=1" type="application/x-shockwave-flash" width="【X】" height="【Y】" allowfullscreen="true"></embed></object>';break;
	case 5:
	vhtml = '<IFRAME frameBorder="0" allowTransparency="true" width="【X】" height="【Y】" marginHeight="0" scrolling="no" src="http://ytu.be/niconico.html?【V】-【X】-【Y】" MarginWidth="0"></IFRAME>';break;
	case 6:
	vhtml = '<embed src="http://www.veoh.com/veohplayer.swf?permalinkId=【V】&id=anonymous&player=videodetailsembedded&videoAutoPlay=1" allowFullScreen="true" width="【X】" height="【Y】" bgcolor="#FFFFFF" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';break;
	case 7:
	vhtml = '<embed src="http://player.youku.com/player.php/sid/【V】/v.swf" quality="high" width="【X】" height="【Y】" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>';break;
	case 13:
	vhtml = '<object width="【X】" height="【Y】"><param name="movie" value="http://www.megavideo.com/v/【V】"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.megavideo.com/v/【V】" type="application/x-shockwave-flash" allowfullscreen="true" width="【X】" height="【Y】"></embed></object>';break;
	default:
	vhtml = "";
	}
	vhtml = vhtml.replace(new RegExp("【V】","g"),g_vid);
	vhtml = vhtml.replace(new RegExp("【X】","g"),540);
	vhtml = vhtml.replace(new RegExp("【Y】","g"),444);
	if(typeof document.body.style.maxHeight == "undefined" && g_site != 5){vhtml = fixEmbeddedVideo(vhtml);}// IE6以下 bug fix
	if(g_site == 1){qr_tag = "<a href='javascript:qr_code();'>この動画は携帯電話でも閲覧できます</a> 　 ";}else{qr_tag = "";}
	vhtml = "<div style='text-align:right;width:540px;font-size:12px;'>"+qr_tag+"<a href='http://ytu.be/redirect_blog.php?"+g_vid+"."+g_site+"' target='_blank'>最大を画面化</a></div>"+vhtml;
	window.scroll(0,100);
	return vhtml;
}
var fixEmbeddedVideo = function(embedCode) {//Thanks (http://blog.tremend.ro/2008/08/06/dinamically-embed-youtube-videos-on-ie6-using-innerhtml/)
	if(embedCode && embedCode.toLowerCase().indexOf('classid') == -1){
		var objPos = embedCode.toLowerCase().indexOf('object ') + 'object '.length;
			return embedCode.substr(0, objPos) + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' + embedCode.substr(objPos);
	}else{
		return embedCode;
	}
}
function qr_code(){
	try {pageTracker._trackPageview("/counter/qrcode/eid/"+eid);} catch(err) {}
	$('video_html_tag').innerHTML='<div style=\'text-align:center;width:540px;font-size:12px;\'><img src=\'http://chart.apis.google.com/chart?cht=qr&chs=400x400&chl='+document.URL+'\' width=\'400\' height=\'400\' /><br />携帯電話からこのコードを読み込むかこのページと同じアドレスに接続します<br /><br /><br /></div>';
}

//---related video from words and analytics(referer)--------
//setTimeout("relatedfromword_ajax()",3000);
function relatedfromword_ajax(){
	if(!document.referrer || document.referrer.indexOf("http://ytu.be") != -1){return;}
	httpObj6 = createXMLHttpRequest(res_relatedfromword_ajax);
	if(httpObj6){
		httpObj6.open("GET","http://ytu.be/suggest.php?"+decodeURI(document.referrer),true);
		httpObj6.send(null);
	}
}
function res_relatedfromword_ajax(){
	if((httpObj6.readyState == 4) && (httpObj6.status == 200)){
		res_video_data = httpObj6.responseText;
		if(res_video_data.length > 100){eval(res_video_data);}
	}
}

//------------------違反通報設定------------------

function ihan_init(u_vid){

	$("vote").innerHTML = '<select id="ihan_select" style="background-color:black"><option selected value="0">違反理由を選択してください</option><option value="10">無修正サムネイル画像(このページの縮小画像)</option><option value="20">被写体が18歳未満</option><option value="30">タイトルと無関係の動画</option><option value="40">その他権利侵害</option></select><a href="javascript:void(0)" onclick="ihan('+u_vid+')">[決定]</a>';

}

function ihan(u_vid){

	ihan_num = $('ihan_select').value;
	if(ihan_num == "0"){alert("違反理由を選択してください");return false;}
	try {pageTracker._trackPageview("/counter/report/vid/"+u_vid+"/"+ihan_num);} catch(err) {}
	$("vote").innerHTML = '通報にご協力いただきましてありがとうございます。管理者が確認を行います。';
}

//------------------クッキー埋め込み--------------

function cookie_write(){

	readads = eval(cookie_read());
	if(!readads){readads=0;}
	readads++;
	//訪問確認クッキー自動埋込
	exp = new Date();
	exp.setTime(exp.getTime()+1000*60*60*12*1);
	document.cookie = "readads="+readads+"; domain=ytu.be; expires="+exp.toGMTString();

}

function cookie_read(){
	return ((document.cookie+";").match("readads=([^;]*);")!=null)? unescape(RegExp.$1):null;
}

//------------------------------------------------

function jump_cookie_check(sitenum){
	found = 0;
	read_jump_cookie = ((document.cookie+";").match("jump=([^;]*);")!=null)? unescape(RegExp.$1):"";
	jumped_site = read_jump_cookie.split(",");
	for (i=0;i<jumped_site.length;i++) {
		if (jumped_site[i] == sitenum) {found = 1;return found;}
	}

	if (read_jump_cookie){
		read_jump_cookie += ","+sitenum;
	} else {
		read_jump_cookie += sitenum;
	}

	//クッキー埋込
	exp = new Date();
	exp.setTime(exp.getTime()+1000*60*60*12*1);
	document.cookie = "jump="+read_jump_cookie+"; domain=ytu.be; expires="+exp.toGMTString();

	return found;//return 0;
}

//------------------------------------------------
function return_redirect_url(red){
	return "http://ytu.be/redirect_blog.php"
	      +"?"
	      +red;
}


