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;font-size:10pt;">元の画面に戻るにはこのボタンをクリック&nbsp;→&nbsp;</span><input type="button" value="閉じる" style="border:0px;cursor:pointer;width:80px;" onclick="closePlayer();" align="absmiddle"><input type="button" value="再読込" style="border:0px;cursor:pointer;margin-left:1px;width:70px;" 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";
	
}


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 getScrollPosition() {
	
	var obj = new Object();
	obj.x = document.documentElement.scrollLeft || document.body.scrollLeft;
	obj.y = document.documentElement.scrollTop || document.body.scrollTop;
	
	return obj;
	
}


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 ];
}

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;

}
