pluginpath = pluginsurl + '/base/';

var player_toggle = false;

function getEmbedPlayerHTMLTag(content){
    content = unescape(content);
    var data = eval('('+content+')');
    var ret = getEmbedPlayerParams(data.purveyor, data.numid, post_id);
    htmlTag = '<object width="'+ret.width+'" height="'+ret.height+'"><param name="movie" value="'+ret.swfUrl+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="flashvars" value="'+ret.flashVarsParam+'"><embed src="'+ret.swfUrl+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+ret.width+'" height="'+ret.height+'" flashvars="'+ret.flashVarsParam+'"></embed></object>';
    return htmlTag.escapeHTML();
}

function getEmbedPlayerParams(purveyor, numid, nicoid){
    var ret = new Object();
    ret.swfUrl = '';
    ret.flashVars = new Object();
    switch(parseInt(purveyor)){
    case 2:
    case 7:
    case 8:
    case 9:
	ret.height = 405;
	ret.swfUrl = pluginpath+'playerSWF.swf?'+playerSWFVersion;
	ret.flashVarsParam = 'numid='+numid+'&purveyor='+purveyor+'&nicoid='+nicoid+'&useragent='+navigator.userAgent;
	ret.flashVars.numid = numid;
	ret.flashVars.purveyor = purveyor;
	ret.flashVars.nicoid = nicoid;
	ret.flashVars.useragent = navigator.userAgent;
	break;
    case 3:
	ret.swfUrl = 'http://www.veoh.com/videodetails2.swf?permalinkId='+numid+'&player=videodetailsembedded&videoAutoPlay=0';
	break;
    case 4:
	ret.swfUrl = 'http://www.dailymotion.com/swf/'+numid+'&related=0';
	break;
    case 5:
	ret.swfUrl = 'http://www.youtube.com/v/'+numid+'&hl=ja&fs=1&fmt=18';
	break;
    case 6:
	break;
    default:
	break;
    }
    return ret;
}

function embedPlayer(content, post_id, player_width, player_height){
		if(swfobject.getFlashPlayerVersion().major == 0){
	$('player').innerHTML = '<a href="http://www.adobe.com/shockwave/download/index.cgi?Lang=Japanese&amp;P1_Prod_Version=ShockwaveFlash" target="_blank">この動画をご覧頂くには、Adobe Flash Playerのバージョン9以上が必要です。<br/><br/>ここをクリックしてAdobe Flash Playerをインストールしてください。</a>';
	return;
    }
    content = unescape(content);
    if(content.isJSON()){
	var data = eval('('+content+')');
	var ret = getEmbedPlayerParams(data.purveyor, data.numid, post_id);
	getEmbedPlayerHTMLTag(data.purveyor, data.numid, post_id);
	var params = {allowscriptaccess:'always', allowfullscreen:'true', wmode:'transparent'};
	//swfobject.addParam("wmode", "transparent");
	swfobject.embedSWF(ret.swfUrl, 'player', player_width, player_height, '9.0.0', pluginpath+'expressinstall.swf', ret.flashVars, params);
    }else{
	$('player').innerHTML = '<br><br>動画情報が古い、もしくは削除されている可能性があります。<br>右側の「削除されている」をクリックしてみてください。';
    }
}

// 報告ボタンを押したら報告する
function inform(post_id, type, func){
    var url = pluginpath + 'inform.php';
    var params = 'post_id=' + post_id + '&type=' + type;
    var obj = new Ajax.Request(url,
			       {
				   method: 'post',
				   parameters: params,
				   onLoading: function(){
				       func['onLoading']();
				   },
				   onComplete: function(){
				       func['onComplete']();
				   }
			       });
}

// プレイヤーからinformの呼び出し
function informFromPlayer(post_id, type){
    var url = pluginpath + 'inform.php';
    var params = 'post_id=' + post_id + '&type=' + type;
    var obj = new Ajax.Request(url,
			       {
				   method: 'post',
				   parameters: params,
				   onLoading: function(){
				   },
				   onComplete: function(){
				   }
			       });
}

// 言語のボタンを押したら報告する
function voteLang(post_id, lang, type, func){
    var url = pluginpath + 'voteLang.php';
    var params = 'post_id=' + post_id + '&type=' + type + '&lang=' + lang;
    var obj = new Ajax.Request(url,
			       {
				   method: 'post',
				   parameters: params,
				   onLoading: function(){
				       func['onLoading']();
				   },
				   onComplete: function(){
				       func['onComplete']();
				   }
			       });
}

function countView(postid){
    if(typeof(postid) != 'number'){
	return;
    }
    var url = pluginpath + 'countView.php';
    var params = 'post_id=' + postid + '&site_id=' + getSiteId();
    var obj = new Ajax.Request(url,
			       {
				   method: 'POST',
				   parameters: params
			       });
    return;
}

function editTitle(title, postid, func){
    if(typeof(postid) != 'number'){
	return;
    }
    var url = pluginpath + 'updateTitle.php';
    var params = 'post_id=' + postid;
    if(title){
	params = params + '&title='+title;
    }
    var obj = new Ajax.Request(url,
			       {
				   method: 'POST',
				   parameters: params,
				   onComplete: func
			       });
    return;
}

function embedLauncher(numid, purveyor, postid){
    var flashvars = {
	numid: numid,
	purveyor: purveyor,
	nicoid: postid
    };
    swfobject.embedSWF(pluginpath+'miniLauncher.swf?'+miniLauncherVersion,
		       'launch_'+postid, 15, 15, '9.0.0',
		       'expressInstall.swf', flashvars);
}

function installPlayer(numid, purveyor, postid, wait){
    var _window = window.open(pluginpath+'installPlayer.php', 'プレイヤーのインストール');
    try{
	_window.addEventListener('load', function(){_window.install(numid, purveyor, postid, wait);},false);
    }catch(e){
	_window.attachEvent('onload', function(){_window.install(numid, purveyor, postid, wait);}, wait);
    }
}

// filenameに含まれる数字をincrementする。
// numを与えるとincrementせずにnumに書き換える。
function increment_filename(filename, num){
    var regexp = new RegExp("([0-9]+)[^0-9]*$", "i");
    filename.match(regexp);
    var digit = String(RegExp.$1).length;
    if(arguments.length != 2){
	num = RegExp.$1;
	num++;
    }
    num = uniform_number(num, digit);
    filename = filename.replace(/[0-9]+([^0-9]*$)/, num+"$1");
    return filename;
}

function uniform_number(number, num){
    var string = String(number);
    if(string.length > num){
	return string;
    }
    for(var i=0; i<num-string.length; i++){
	string = '0' + string;
    }
    return string;
}

function post_comment(postid, comment, func){
    if(typeof(postid) != 'number'){
	return;
    }
    var url = pluginpath + 'editComment.php';
    var params = 'post_id=' + postid + '&mode=add&comment=' + comment;
    var obj = new Ajax.Request(url,
			       {
				   method: 'POST',
				   parameters: params,
				   onComplete: func
			       });
}

function getSiteId(){
    if(site_id){
	return site_id;
    }else{
	return 0;
    }
}

function discern_check_url(url){
    var errmsg = { warn: "対応していないURL形式の動画です。そのうち対応するかもしれません。",
		    err: "対応していないURL形式の動画です。"};
    
    if(url.match(/youku.com/)){
	//if(url.match(/\/(v_show|v_playlist|playlist_show)\/([0-9]|[a-z]|[A-Z]|[=_])+.html$/)){
	if(url.match(/\/(v_show|v_playlist|playlist_show|v_showcommentlist)/)){
	    return true;
	}else{
	    return errmsg['warn'];
	}
    }
    
    if(url.match(/veoh.com/)){
	//if(url.match(/\/videos\/([a-z|A-Z][0-9|a-z|A-Z]+)/)){
	    return true;
	    //}else{
	    //return errmsg['warn'];
	    //}
    }
    
    if(url.match(/dailymotion.com/)){
	return true;
    }
    
    if(url.match(/youtube.com/)){
	return true;
    }

    if(url.match(/guba.com/)){
	return true;
    }

    if(url.match(/tudou.com/)){
	return true;
    }

    if(url.match(/ku6.com/)){
	return true;
    }

    if(url.match(/gametrailers.com/)){
	return true;
    }
    
    var re = new RegExp(siteurl);
    if(url.match(re)){
	return true;
    }else{
	return errmsg['warn'];
    }
    return errmsg['err'];
}

function OpenExtLink(el){
    var url = el.href;
    if(Prototype.Browser.Gecko){
	w = window.open();
	w.document.write('<meta http-equiv="refresh" content="0;url='+url+'">');
	w.document.close();
    }else{
	location.replace(url);
    }
    return false;
};

function editTag(post_id, tagStr, func, mode){
    var url = pluginpath + 'editTag.php';
    var params = 'mode=' + mode + '&post_id=' + post_id + '&tagStr=' + encodeURIComponent(tagStr);
    var obj = new Ajax.Request(url,
			       {
				   method: 'post',
				   parameters: params,
				   onComplete: function(){
				       func['onComplete']();
				   },
				   onLoading: function(){
				       func['onLoading']();
				   }
			       });
    return false;
}

function getOrigTitle(post_id, func){
    var url = pluginpath + 'getOrigTitle.php';
    var params = 'post_id='+post_id;
    var obj = new Ajax.Request(url,
			       {
				   method: 'post',
				   parameters: params,
				   onComplete: func['onComplete'],
				   onLoading: func['onLoading']
			       });
    return false;
}

function getFinishedOrder(order_key, order_name, params, success, status){
    var interval = 5000;
    var url = pluginpath + 'getFinishedOrder.php';
    var exec = false;
    var checkOrderStatus = function(){
	if(exec){
	    return;
	}
	var obj = new Ajax.Request(url, {
		method: 'post',
		parameters: params+'&order_key='+order_key+'&order_name='+order_name,
		onComplete: function(ret){
		    exec = false;
		    switch(ret.responseText){
			case "ERROR":
			case "TIMEOUT":
			break;
			default:
			if(ret.responseText.isJSON()){
			    var data = eval('('+ret.responseText+')');
			    switch(data.stats){
				case 0:
				case 1:
				    break;
				case 2:
				    clearInterval(timerID);
				    $(status).innerHTML = data.result;
				    break;
				case 3:
				    clearInterval(timerID);
				    success(data.result);
				    break;
			    }
			}
		    }
		}
	    });
	exec = true;
    };
    var timerID;
    var timer = function(){
	timerID = setInterval(function(){
		checkOrderStatus();
	    }, interval);
	checkOrderStatus();
    };
    timer();
}

function getOrderKey(){
    return Math.floor(Math.random()*0xFFFFFFFF);
}
