
var player_w = 0;
var player_h = 0;

// ウィンドウリサイズ処理用
window.onresize = winEvent;

function layer_window( url ) {
	
	url = decodeURL( url );
	
	var display_size = getWindowSize();
	var display_w = display_size[ 0 ];
	var display_h = display_size[ 1 ];
	var scrollPos = getScrollPosition();
	
	player_w = display_w - 100;
	player_h = display_h - 100;
	
	// プレイヤーの表示
	var embed_div = document.getElementById( 'embed' );
	embed_div.style.top = scrollPos.y + 50 + 'px';
	embed_div.style.left = ( ( display_w - player_w ) / 2 ) + 'px';
	embed_div.style.height = player_h + 'px';
	embed_div.style.width = player_w + 'px';
	
	embed_div.innerHTML = '<iframe id="win" name="win" style="border:10px solid #79a4df;padding:1px;background-color:#fff;" frameborder="0" scrolling="auto" marginwidth="1" marginheight="1" width="' + player_w + '" height="' + player_h + '" src="' + url + '"></iframe>';
	embed_div.style.visibility = 'visible';
	
	// ツールバー
	var tbar_div = document.getElementById( 'toolbar' );
	tbar_div.style.top = scrollPos.y + 30 + 'px';
	tbar_div.style.left = ( ( display_w - player_w ) / 2 ) + 'px';
	tbar_div.style.height = '30px';
	tbar_div.style.width = ( player_w + 20 ) + 'px';
	tbar_div.innerHTML = '<div style="background-color:#79a4df;padding-top:5px;padding-right:10px;" align="right" valign="middle"><span style="color:#fff;">元の画面に戻るにはこのボタンをクリック&nbsp;→&nbsp;</span><img src="/sns/img/btn_close.gif" style="border:0px;cursor:pointer;" onclick="closePlayer();" align="absmiddle"><img src="/img/btn_refresh.gif" style="border:0px;cursor:pointer;margin-left:1px;" onclick="parent.win.location.reload();" align="absmiddle"></div>';
	tbar_div.style.visibility = 'visible';
	
	
	var overlay_div = document.getElementById( 'overlay' );
	overlay_div.style.top = scrollPos.y + 'px';
	overlay_div.style.left = '0px';
	overlay_div.style.height = display_h + 'px';
	overlay_div.style.width = display_w + 'px';
	overlay_div.style.filter = 'alpha(opacity=65)';
	overlay_div.style.MozOpacity = '0.65';
	overlay_div.style.opacity = '0.65';
	overlay_div.style.visibility = 'visible';
	
	document.body.style.overflowX = "hidden";
	document.body.style.overflowY = "hidden";
	
}


function add_folder( n ) {
	
	// 検討中フォルダへ追加する
	
	alert( "【情報システム企画／会社名非公開】を検討中フォルダへ登録しました。\nトップページの【検討中フォルダ】タブよりご確認いただけます。" );
	
}




String.prototype.trim = function() {
	
	return this.replace( /^\s+|\s+$/g, "" );
	
}

function closePlayer() {
	
	var embed_div = document.getElementById( 'embed' );
	embed_div.innerHTML = '';
	embed_div.style.visibility = 'hidden';
	embed_div.style.height = '0px';
	embed_div.style.width = '0px';
	var tbar_div = document.getElementById( 'toolbar' );
	tbar_div.innerHTML = '';
	tbar_div.style.visibility = 'hidden';
	tbar_div.style.height = '0px';
	tbar_div.style.width = '0px';
	var overlay_div = document.getElementById( 'overlay' );
	overlay_div.style.visibility = 'hidden';
	overlay_div.style.height = '0px';
	overlay_div.style.width = '0px';
	
	document.body.style.overflowX = "auto";
	document.body.style.overflowY = "auto";
	
}


function winEvent( theEvent ) {
	
	var overlay_div = document.getElementById( 'overlay' );
	try {
		if ( overlay_div.style.visibility == 'visible' ) {
			var display_size = getWindowSize();
			var display_w = display_size[ 0 ];
			var display_h = display_size[ 1 ];
			overlay_div.style.height = display_size[ 1 ] + 'px';
			overlay_div.style.width = display_size[ 0 ] + 'px';
			
			player_w = display_w - 100;
			player_h = display_h - 100;
			
			var tbar_div = document.getElementById( 'toolbar' );
			tbar_div.style.width = ( player_w + 20 ) + 'px';
			
			var embed_div = document.getElementById( 'win' );
//			embed_div.style.left = ( ( display_size[ 0 ] - player_w ) / 2 ) + 'px';
			embed_div.style.height = player_h + 'px';
			embed_div.style.width = player_w + 'px';
		}
	} catch ( e ) {}
}


function getWindowSize() {
	
	var window_width, window_height;
	
	if ( self.innerHeight ) {
		window_width = self.innerWidth;
		window_height = self.innerHeight;
	} else if ( document.documentElement && document.documentElement.clientHeight ) {
		window_width = document.documentElement.clientWidth;
		window_height = document.documentElement.clientHeight;
	} else if ( document.body ) {
		window_width = document.body.clientWidth;
		window_height = document.body.clientHeight;
	}
	return [ window_width, window_height ];
}


// XMLHttpsオブジェクト作成
function createXMLHttp() {
	
	if ( window.ActiveXObject ) {
		// Win e4,e5,e6用
		try {
			return new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch ( e ) {
			try {
				return new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch ( e2 ) {
				return null ;
			}
 		}
	} else if ( window.XMLHttpRequest ) {
		// Win Mac Linux m1,f1,o8 Mac s1 Linux k3用
		return new XMLHttpRequest();
	} else {
		return null;
	}
	
}

function getScrollPosition() {
	
	var obj = new Object();
	obj.x = document.documentElement.scrollLeft || document.body.scrollLeft;
	obj.y = document.documentElement.scrollTop || document.body.scrollTop;
	
	return obj;
	
}


function setGoogleData() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var emb_url = xmlhttp.responseText;
		document.getElementById( "ggv_player" ).innerHTML = emb_url;
		document.getElementById( "videopage" ).innerHTML = '<a href="http://onepage.cc/index.php?url=' + encodeURL( emb_url ) + '" target="_blank" style="text-decoration:none;">ビデオページ編集</a>';
		
	}
}

function setAmebaData() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var emb_url = xmlhttp.responseText;
		var movie_id = emb_url.substring( emb_url.lastIndexOf( 'id=' ) + 3 );
		
		document.getElementById( "ameba_player" ).innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="328" height="298" id="aa" align="middle"><param name="allowScriptAccess" value="always" /><param name="movie" value="http://vm1-2.vision.ameba.jp/mcb.swf?id=' + movie_id + '" /><embed src="http://vm1-2.vision.ameba.jp/mcb.swf?id=' + movie_id + '" quality="high" bgcolor="#ffffff" name="FlashMovie" align="middle" width="328" height="298" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		document.getElementById( "videopage" ).innerHTML = '<a href="http://onepage.cc/index.php?url=' + encodeURL( 'http://visionmovie.ameba.jp/mcb.swf?id=' + movie_id ) + '" target="_blank" style="text-decoration:none;">ビデオページ編集</a>';
		
	}
}

function setDairyMotionData() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var emb_url = xmlhttp.responseText;
		document.getElementById( "dm_player" ).innerHTML = '<object width="425" height="324"><param name="movie" value="' + emb_url + '"></param><param name="allowfullscreen" value="true"></param><embed src="' + emb_url + '" type="application/x-shockwave-flash" width="425" height="324" allowfullscreen="true"></embed></object>';
		document.getElementById( "videopage" ).innerHTML = '<a href="http://onepage.cc/index.php?url=' + encodeURL( emb_url ) + '" target="_blank" style="text-decoration:none;">ビデオページ編集</a>';
		
	}
}

function sendMsg( url ) {
	
	if ( document.getElementById( 'inp_com' ).value == "" ) {
		alert( "メッセージを入力してください。" );
		document.getElementById( 'inp_com' ).focus();
	} else {
		xmlhttp = createXMLHttp();
		link_url = url;
		if ( xmlhttp ) {
			xmlhttp.onreadystatechange = setMsgData;
			xmlhttp.open( 'GET', 'http://spotlight.jp/movie/domiru/msg.php?m=i&u=' + encodeURL( url ) + '&c=' + encodeURL( document.getElementById( 'inp_com' ).value ) );
			xmlhttp.send( null );
		} else {
			alert( "メッセージの投稿に失敗しました。" );
		}
		
	}
}

function setMsgData() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		document.getElementById( "msgifrm" ).src = 'msg.php?u=' + encodeURL( link );
		document.getElementById( "inp_com" ).innerHTML = '';
		document.getElementById( 'inp_com' ).focus();
	}
}

function decodeURL( str ) {
    var s0, i, j, s, ss, u, n, f;
    s0 = "";                // decoded str
    for (i = 0; i < str.length; i++){   // scan the source str
        s = str.charAt(i);
        if (s == "+"){s0 += " ";}       // "+" should be changed to SP
        else {
            if (s != "%"){s0 += s;}     // add an unescaped char
            else{               // escape sequence decoding
                u = 0;          // unicode of the character
                f = 1;          // escape flag, zero means end of this sequence
                while (true) {
                    ss = "";        // local str to parse as int
                        for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse
                            sss = str.charAt(++i);
                            if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {
                                ss += sss;      // if hex, add the hex character
                            } else {--i; break;}    // not a hex char., exit the loop
                        }
                    n = parseInt(ss, 16);           // parse the hex str as byte
                    if (n <= 0x7f){u = n; f = 1;}   // single byte format
                    if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format
                    if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format
                    if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)
                    if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits
                    if (f <= 1){break;}         // end of the utf byte sequence
                    if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte
                    else {break;}                   // abnormal, format error
                }
            s0 += String.fromCharCode(u);           // add the escaped character
            }
        }
    }
    return s0;
}

function encodeURL( str ) {
 	
	var s0, i, s, u;
	s0 = "";
	for (i = 0; i < str.length; i++){
		s = str.charAt(i);
		u = str.charCodeAt(i);
		if (s == " "){s0 += "+";}
		else {
			if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){
				s0 = s0 + s;
			}
			else {
				if ((u >= 0x0) && (u <= 0x7f)){
					s = "0"+u.toString(16);
					s0 += "%"+ s.substr(s.length-2);
				}
				else if (u > 0x1fffff){
					s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
					s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
					s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
					s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
				}
				else if (u > 0x7ff){
					s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
					s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
					s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
				}
				else {
					s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
					s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
				}
			}
		}
	}
	return s0;

}
