var DOMAIN = "www.virkato.net";

// Utility関数
function getobject(str){
	var obj = document.getElementById(str);

	return obj;
}

function is_iphone(){
	var iphone = false;
	if( navigator.userAgent.match(/iPod;/) ){
		iphone = true;
	}
	if( navigator.userAgent.match(/iPhone/) ){
		iphone = true;
	}

	return iphone;
}

function padding(text,padding,length){
	var str=''+text;
	var len= str.length;

	for(var i=len;i<length;i++){
		str = padding+str;
	}

	return str;
}

function setCookie(atr,val,day){
	var line="";

	if( day != null && day != undefined){
		var date = new Date();
		date.setTime(date.getTime()+(day*1000*60*60*24));

		line = atr+"="+escape(val)+"; path=/; domain="+DOMAIN+"; expires="+date.toGMTString();
	}else{
		line = atr+"="+escape(val)+"; path=/; domain="+DOMAIN+"; ";
	}
	document.cookie = line;
}

function getCookie(atr){
	var cookiename = atr;
	var cookies = document.cookie.split("; ");
	var result = "";
	for (var i = 0; i < cookies.length; i++) {
		var str = cookies[i].split("=");
		if( atr == str[0] && str[1] != ""){
			result = unescape(str[1]);
		}
	}

	if( result == "undefined"){
		result = "";
	}

	return result;
}

function clearCookie(atr){
  document.cookie = atr + "= ; path=/; domain="+DOMAIN+";";
}


//=========================================
// ストリーミング機能
//=========================================
//
function STREAMING_start(num,url,url2,bgimage,width,height,title,filesize,streaming_width,auto,downloadflag,authmessage){
	autoNum = 1;
	if( auto != "true"){
		autoNum = 0;
	}

	dispDownload = 1;
	try{
		if( downloadflag == "false"){
			dispDownload = 0;
		}
	}catch(e){
		;
	}

	var html="";
	if( autoNum == 1){
		html = STREAMING_make_streaming(num,url,url2,bgimage,width,height,title,filesize,streaming_width,auto,downloadflag,bgimage,"true");
	}else{
		html = STREAMING_make_image(num,url,url2,bgimage,width,height,title,filesize,streaming_width,auto,downloadflag,authmessage);
	}

	if( url.match(/\.wmv$/) ){
		height=height+24+45;
	}else{
		height=height+20;
	}

	document.write('<div id="STREAMING_area_'+num+'">');
	document.write(html);
	document.write('</div>');

	document.getElementById('STREAMING_width_'+num).style.width  = streaming_width;
}

function STREAMING_change(evt,num,url,url2,bgimage,width,height,title,filesize,streaming_width,auto,downloadflag,authmessage){

	if( authmessage != 'undefined' && authmessage != ""){
		if( !confirm(authmessage) ){
			try{
				if( window.event != undefined){
					window.event.cancelBubble = true;
				}
				if( evt != undefined && evt != null){
					evt.stopPropagation();
				}
			}catch(e){
				;
			}
			return false;
		}
	}

	try{
		if( is_iphone() == true){
			if( url.match(/\.mp[34]$/) ){
				location.href = url;
			}else{
				alert("iPhoneではサポートしてないムービーです。");
			}
			return false;
		}
		var html = STREAMING_make_streaming(num,url,url2,"http://www.virkato.net/vss/img/bg_streaming_4gamer.gif",width,height,title,filesize,streaming_width,auto,downloadflag,bgimage,"true");

		var area = document.getElementById('STREAMING_area_'+num);
		area.innerHTML = html;
		document.getElementById('STREAMING_width_'+num).style.width  = streaming_width;
	}catch(e){
		;
	}

	try{
		if( window.event != undefined){
			window.event.cancelBubble = true;
		}
		if( evt != undefined && evt != null){
			evt.stopPropagation();
		}
	}catch(e){
		;
	}

	return false;
}

function STREAMING_make_image(num,url,url2,bgimage,width,height,title,filesize,streaming_width,auto,downloadflag,authmessage){
	var html="";

	autoNum = 1;
	if( auto != "true"){
		autoNum = 0;
	}

	dispDownload = 1;
	try{
		if( downloadflag == "false"){
			dispDownload = 0;
		}
	}catch(e){
		;
	}

	if( !url.match(/\.wmv$/)){
		if( url2 == ""){
			dispDownload = 0;
		}else{
			dispDownload = 2;
		}
	}

	var mask = "filter:alpha(opacity=50) ;  -moz-opacity:0.5;  opacity:0.5;";
	if( bgimage == "http://www.virkato.net/vss/img/bg_streaming_4gamer.gif"){
		mask = "";
	}

	html += '<p>'+ title +'</p>';

	title = title.replace("&#039;",'&amp;#039;');

	if( is_iphone()){
		if( !url.match(/\.mp[34]$/)){
			html += '<p>iPhoneでは未サポートなムービーです。</p>';
		}
	}

	html += '<div style="position:relative;background-color: #000000;width: '+width+'px; height: '+height+'px;text-align:center;margin:0 auto;overflow:hidden;">';
	html += '<div id="MediaPlayer_'+num+'"';
	html += ' style="cursor:pointer; margin:0 auto; text-align:center;width:'+width+'px;height:'+height+'px;background:url('+bgimage+') no-repeat center center #000000; '+mask+'"';
	html += ' onClick="return STREAMING_change(event,\''+num+'\',\''+url+'\',\''+url2+'\',\''+bgimage+'\','+width+','+height+',\''+title+'\',\''+filesize+'\',\''+streaming_width+'\',\'true\',\''+downloadflag+'\',\''+authmessage+'\');"';
	html += '></div><div style="position:absolute;top:52%;left:0;z-index:10;width:100%;cursor: pointer;" onClick="return STREAMING_change(event,\''+num+'\',\''+url+'\',\''+url2+'\',\''+bgimage+'\','+width+','+height+',\''+title+'\',\''+filesize+'\',\''+streaming_width+'\',\'true\',\''+downloadflag+'\',\''+authmessage+'\');"><img src="http://www.virkato.net/vss/img/bg_streaming_4gamer_play.gif" width="164" height="105" alt="Clik to Play" style="cursor: pointer;"';

	if( is_iphone() == false){
		html += ' onClick="return STREAMING_change(event,\''+num+'\',\''+url+'\',\''+url2+'\',\''+bgimage+'\','+width+','+height+',\''+title+'\',\''+filesize+'\',\''+streaming_width+'\',\'true\',\''+downloadflag+'\',\''+authmessage+'\');"';
	}

	html += '></div></div>';

	var controlImage = "http://www.virkato.net/vss/img/bg_streaming_4gamer_control.gif";
	if( width <= 320){
		controlImage = "http://www.virkato.net/vss/img/bg_streaming_4gamer_control_320.gif";
	}
	if( width >= 512){
		controlImage = "http://www.virkato.net/vss/img/bg_streaming_4gamer_control_512.gif";
	}
	var controlHeight=69;
	if( !url.match(/\.wmv$/) ){
		var controlImage = "http://www.virkato.net/vss/img/bg_streaming_flash_control_512.gif";
		if( width <= 320){
			controlImage = "http://www.virkato.net/vss/img/bg_streaming_flash_control_320.gif";
		}
		controlHeight=20;
	}

	html += '<img src="'+controlImage+'" onClick="return STREAMING_change(event,\''+num+'\',\''+url+'\',\''+url2+'\',\''+bgimage+'\','+width+','+height+',\''+title+'\',\''+filesize+'\',\''+streaming_width+'\',\'true\',\''+downloadflag+'\',\''+authmessage+'\');" width="'+width+'" height="'+controlHeight+'" style="cursor:pointer;">';

	html += '<div id="STREAMING_width_'+num+'">';

	if( dispDownload == 1){
		html += '<p>動画を保存する場合は，ダウンロードボタンの上で<br /><strong>右クリック&nbsp;＞&nbsp;対象をファイルに保存</strong>を行ってください</p>';
	}else{
		html += '';
	}

	html += '	<div>';
	html += '		<div>';

	if( dispDownload == 1){
		html += '		<span><a href="'+ url +'"><img src="http://www.virkato.net/vss/img/button_streaming_dl_480.gif" width="140" height="25" alt="ダウンロード" /></a></span>';

		if( url2 != ''){
			html += '		<span><a href="'+ url2 +'"><img src="http://www.virkato.net/vss/img/button_streaming_hqdl_480.gif" width="140" height="25" alt="高画質版ダウンロード" /></a></span>';
		}

		if(document.all){
			// 再生中のみ有効なのでクリックしてWMPに切り替えてから
			// 表示。
			/*
			html += '		<span><img src="http://www.virkato.net/vss/img/button_streaming_full_480.gif" width="140" height="25" alt="再生中のみ有効" OnClick="STREAMING_fullscreen('+num+');" /></span>';
			*/
		}
	}else if( dispDownload == 2){
			html += '		<span><a href="'+ url2 +'"><img src="http://www.virkato.net/vss/img/button_streaming_hqdl_480.gif" width="140" height="25" alt="高画質版ダウンロード" /></a></span>';
	}

	html += '		</div>';
	if( dispDownload == 1 || dispDownload == 2){
		html += '	<p>'+ filesize +'</p>';
	}
	html += '	</div>';
	html += '</div>';

	return html;
}

function STREAMING_make_streaming(num,url,url2,bgimage,width,height,title,filesize,streaming_width,auto,downloadflag,flashbgimage,flashautostart){
	var html="";

	autoNum = 1;
	if( auto != "true"){
		autoNum = 0;
	}

	dispDownload = 1;
	try{
		if( downloadflag == "false"){
			dispDownload = 0;
		}
	}catch(e){
		;
	}

	if( !url.match(/\.wmv$/)){
		if( url2 == ""){
			dispDownload = 0;
		}else{
			dispDownload = 2;
		}
	}

	html += '<p>'+ title +'</p>';
	title = title.replace("'","&#039;");

	var objectMode = "WMP";
	if( !url.match(/\.wmv$/) ){
		objectMode = "FLASH"
		height=height+20;
	}else{
		height=height+24+45;
	}

	if( objectMode == "FLASH"){
		html += '<embed src="http://www.virkato.net/vss/swf/player.swf" width="'+width+'" height="'+height+'" allowscriptaccess="always" allowfullscreen="true" flashvars="height='+height+'&width='+width+'&autostart='+flashautostart+'&file='+url+'&image='+flashbgimage+'&fullscreen=true"/>';
	}else{
	html += '<object id="MediaPlayer_'+num+'" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"';
	html += ' width=' + width;
	html += ' height=' + height;
	html += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"';
	html += ' style="background:url('+bgimage+') no-repeat center center #000000;"';
	html += ' standby="Loading Microsoft Windows Media Player components...">';
	html += '<param name="Filename" value="'+ url +'">';
	html += '<param name="AutoStart" value="'+auto+'">';
	html += '<param name="ShowControls" value="true">';
	html += '<param name="ShowDisplay" value="false">';
	html += '<param name="ShowStatusBar" value="true">';
	html += '<param name="TransparentAtStart" value="true">';
	html += '<param name="Volume" value="-1500">';

	html += '     <embed id="MediaPlayer'+num+'"';
	html += '            name="MediaPlayer'+num+'"';
	html += '            type="application/x-mplayer2"';
	html += '            src="'+ url +'"';
	html += '            pluginspage="http://www.microsoft.com/windows/windowsmedia/download/"';
	html += '            AutoStart="'+autoNum+'"';
	html += '            ShowControls="1"';
	html += '            ShowDisplay="0"'
	html += '            ShowStatusBar="1"';
	html += '            Volume="-1500"';
	html += '            width=' + width;
	html += '            height=' + height;
	html += '            pluginspage="http://www.microsoft.com/windows/windowsmedia/download/" />';
	html += '</object>';
	}

	html += '<div id="STREAMING_width_'+num+'">';

	if( dispDownload == 1){
		html += '<p>動画を保存する場合は，ダウンロードボタンの上で<br /><strong>右クリック&nbsp;＞&nbsp;対象をファイルに保存</strong>を行ってください</p>';
	}else{
		html += '';
	}

	html += '	<div>';
	html += '		<div>';

	if( dispDownload == 1){
		html += '		<span><a href="'+ url +'"><img src="http://www.virkato.net/vss/img/button_streaming_dl_480.gif" width="140" height="25" alt="ダウンロード" /></a></span>';

		if( url2 != ''){
			html += '		<span><a href="'+ url2 +'"><img src="http://www.virkato.net/vss/img/button_streaming_hqdl_480.gif" width="140" height="25" alt="高画質版ダウンロード" /></a></span>';
		}

		if(document.all){
			if( url.match(/\.wmv$/) ){
				html += '		<span><img src="http://www.virkato.net/vss/img/button_streaming_full_480.gif" width="140" height="25" alt="再生中のみ有効" OnClick="STREAMING_fullscreen('+num+');" /></span>';
			}
		}
	}else if( dispDownload == 2){
			html += '		<span><a href="'+ url2 +'"><img src="http://www.virkato.net/vss/img/button_streaming_hqdl_480.gif" width="140" height="25" alt="高画質版ダウンロード" /></a></span>';
	}

	html += '		</div>';
	if( dispDownload == 1 || dispDownload == 2){
		html += '	<p>'+ filesize +'</p>';
	}
	html += '	</div>';
	html += '</div>';

	return html;
}

function STREAMING_fullscreen(num){
	try{
		player = document.getElementById('MediaPlayer_'+num);
		if (player.PlayState == 2) {
			player.DisplaySize=3;
		}
	}catch(e){
		alert(e);
	}
}
