/*
	--------------------------------
	更新日: 2009-03-26
	--------------------------------
*/



/* --------------------------------
	Configuration
-------------------------------- */

// サブウィンドウ操作用オブジェクト
var _window = {
	minLeft: 0,
	minTop: 0,
	maxLeft: screen.availWidth - 10,
	maxTop: screen.availHeight - 10,
	object: []
};

// OSの種類を取得
var _OS = getOS();

// ブラウザの種類とバージョンを取得
var _browser = getBrowser();



/* --------------------------------
	Functions
-------------------------------- */

/* サブウィンドウのポップアップ ---------------- */
function popup( loc, name, left, top, width, height, modes ) {
	// modes : [ scrollbars, resizable, status, menubar, toolbar, addressbar, directories ]

	var size = adjustPopupSize( width, height, modes, 0 );

	openWindow( loc, name, left, top, size.width, size.height, modes, 0 );
}

/* サブウィンドウをスクリーンの中央にポップアップ ---------------- */
function popupCenter( loc, name, width, height, modes ) {
	// modes : [ scrollbars, resizable, status, menubar, toolbar, addressbar, directories ]

	var minL = _window.minLeft;
	var minT = _window.minTop;
	var maxL = _window.maxLeft;
	var maxT = _window.maxTop;

	var size = adjustPopupSize( width, height, modes, 0 );

	var left = Math.floor( ( screen.availWidth - size.width ) / 2 );
	var top = Math.floor( ( screen.availHeight - size.height ) / 2 );
	if ( left < minL ) left = minL;
	if ( top < minT ) top = minT;
	if ( left > maxL ) left = maxL;
	if ( top > maxT ) top = maxT;

	var win = openWindow( loc, name, left, top, size.width, size.height, modes, 0 );

	try {
		win.moveTo( left, top );
	} catch(e) {}
}

/* サブウィンドウをフルスクリーンサイズでポップアップ ---------------- */
function popupFullscreen() {
	// arguments : ( loc, name, modes [, term ] )
	// modes : [ scrollbars, resizable, status, menubar, toolbar, addressbar, directories ]

	var bn = _browser.name;
	var bv = _browser.version;

	var args = arguments;
	if ( !args.length ) return false; // in place of undefined for IE

	var loc = args[ 0 ];
	var name = args[ 1 ];
	var modes = args[ 2 ];
	var term = args[ 3 ];

	var size = adjustPopupSize( screen.availWidth, screen.availHeight, modes, 1 );

	var win = openWindow( loc, name, 0, 0, size.width, size.height, modes, term );

	if ( _OS == 'Mac' && bn == 'Firefox' ) {
		try {
			win.moveTo( 0, 0 );
		} catch(e) {}
	}
	if ( !term && _OS == 'Win' && bn == 'IE' && bv < 6 || _OS == 'Mac' && bn == 'IE' || bn == 'Netscape' || bn == 'Opera' && bv < 8 ) win.resizeTo( size.width, size.height );
}

/* サブウィンドウに設定するサイズを諸条件をもとに調整 ---------------- */
function adjustPopupSize( width, height, modes, fullscreen ) {
	// modes : [ scrollbars, resizable, status, menubar, toolbar, addressbar, directories ]

	var bn = _browser.name;
	var bv = _browser.version;

	var scrollbars = modes[ 0 ];
	var resizable = modes[ 1 ];
	var status = modes[ 2 ];
	var menubar = modes[ 3 ];
	var toolbar = modes[ 4 ];
	var addressbar = modes[ 5 ];
	var directories = modes[ 6 ];

	if ( fullscreen ) {
		if ( _OS == 'Mac' && bn == 'Netscape' ) if ( resizable || status ) height -= 18; // Mac Netscape
		if ( bn == 'Safari' ) { // Safari
			var flg = false;
			for ( var i = 0; i < modes.length; i++ ) {
				if ( modes[ i ] ) {
					flg = true;
					break;
				}
			}
			if ( !flg ) height -= 22;
		}
	} else {
		if ( scrollbars ) {
			if ( _OS == 'Win' && bn == 'IE' && bv <= 5 ) width += 18; // Win IE 5.0-
			else if ( _OS == 'Win' && bn == 'IE' && bv == 5.5 ) width += 16; // Win IE 5.5
			else if ( _OS == 'Win' && bn == 'IE' && bv >= 6 ) width += 17; // Win IE 6.0+
			else if ( _OS == 'Win' && bn == 'Firefox' ) width += 19; // Win Firefox 1.0+
			else if ( _OS == 'Win' && bn == 'Opera' ) width += 17; // Win Opera 6.x+
			else if ( _OS == 'Win' && bn == 'Mozilla' ) width += 19; // Win Mozilla 1.7.5+
			else if ( _OS == 'Win' && document.layers ) width += 16; // Win Netscape 4.x-
			else if ( _OS == 'Mac' && bn == 'IE' ) { width -= 1; height -= 16; } // Mac IE
			else if ( _OS == 'Mac' && bn == 'Firefox' ) width += 15; // Mac Firefox 1.0+
			else if ( _OS == 'Mac' && bn == 'Opera' ) width += 15; // Mac Opera 6.x+
			else if ( _OS == 'Mac' && bn == 'Mozilla' ) width +=16; // Mozilla
			else if ( _OS == 'Mac' && document.layers ) if ( !status ) width += 15; else height -= 15; // Mac Netscape 4.x-
			else if ( bn == 'Netscape' && bv < 7 ) width += 14; // Netscape 6.x
			else if ( bn == 'Netscape' && bv >= 7 ) width += 15; // Netscape 7.x
			else if ( bn == 'Safari' ) width +=15; // Safari
			else if ( bn == 'Camino' ) width +=15; // Camino
		}
		if ( menubar ) {
			if ( _OS == 'Win' && bn == 'IE' ) height -= 19; // Win IE
			else if ( _OS == 'Win' && document.layers && width < 376 ) height += 18; // Win Netscape 4.x-
		}
		if ( bn == 'Safari' ) { // Safari
			height += 1;
			if ( status ) height += 16;
			if ( addressbar ) height += 51;
		}
	}

	var val = [ width, height ];
	val[ 'width' ] = val[ 'w' ] = width;
	val[ 'height' ] = val[ 'h' ] = height;

	return val;
}

/* 与えられた条件をもとにウィンドウを開く処理 ---------------- */
function openWindow( loc, name, left, top, width, height, modes, fullscreen ) {
	// modes : [ scrollbars, resizable, status, menubar, toolbar, addressbar, directories ]

	var scrollbars = ( modes[ 0 ] ) ? 'yes' : 'no';
	var resizable = ( modes[ 1 ] ) ? 'yes' : 'no';
	var status = ( modes[ 2 ] ) ? 'yes' : 'no';
	var menubar = ( modes[ 3 ] ) ? 'yes' : 'no';
	var toolbar = ( modes[ 4 ] ) ? 'yes' : 'no';
	var addressbar = ( modes[ 5 ] ) ? 'yes' : 'no';
	var directories = ( modes[ 6 ] ) ? 'yes' : 'no';

	var props = 'menubar=' + menubar + ',toolbar=' + toolbar + ',location=' + addressbar + ',directories=' + directories +
		',status=' + status + ',scrollbars=' + scrollbars + ',resizable=' + resizable;

	if ( fullscreen && _OS == 'Win' && _browser.name == 'IE' ) props += ',fullscreen=yes';
	else props += ',left=' + left + ',top=' + top + ',width=' + width + ',height=' + height;

	_window.object[ name ] = window.open( loc, name, props );
	_window.object[ name ].focus();

	return _window.object[ name ];
}

/* 関数を呼び出したウィンドウ自身をスクリーンの中央に移動 ---------------- */
function moveToCenter() {
	// arguments : ( [ width, height ] )

	var minL = _window.minLeft;
	var minT = _window.minTop;
	var maxL = _window.maxLeft;
	var maxT = _window.maxTop;

	var args = arguments;

	var win = getWindowSize();
	var w = ( args[ 0 ] ) ? args[ 0 ] : win.width;
	var h = ( args[ 1 ] ) ? args[ 1 ] : win.height;

	var left = Math.floor( ( screen.availWidth - w ) / 2 );
	var top = Math.floor( ( screen.availHeight - h ) / 2 );
	if ( left < minL ) left = minL;
	if ( top < minT ) top = minT;
	if ( left > maxL ) left = maxL;
	if ( top > maxT ) top = maxT;

	try {
		window.moveTo( left, top );
	} catch(e) {}
}

 /* 関数を呼び出したウィンドウ自身を指定されたサイズにリサイズしスクリーンの中央に移動 ---------------- */
function resizeAndMoveToCenter( width, height ) {
	window.resizeTo( width, height );
	moveToCenter( width, height );
}

 /* 関数を呼び出したウィンドウ自身をフルスクリーンサイズにリサイズ ---------------- */
function resizeToFullscreen() {
	try {
		window.moveTo( 0, 0 );
	} catch(e) {}
	window.resizeTo( screen.availWidth, screen.availHeight );
}

/* OSの種類を取得 ---------------- */
function getOS() {
	var av = navigator.appVersion;

	return ( av.match( 'Win' ) ) ? 'Win' : ( av.match( 'Mac' ) ) ? 'Mac' : 'unknown';
}

/* ブラウザの種類とバージョンを取得 ---------------- */
function getBrowser() {
	// arguments : ( [ mode ] )

	var args = arguments;

	var name = 'unknown';
	var ver = 'unknown';
	var ua = navigator.userAgent;
	var ap = 1;
	if ( ua.match( 'Opera\/' ) ) { name = 'Opera'; ua = ua.split( '/' ); } // Opera 6.x+
	else if ( ua.match( /Opera / ) ) { name = 'Opera'; ua = ua.split( 'Opera ' ); } // Opera 6.x+
	else if ( ua.match( /MSIE/ ) ) { name = 'IE'; ua = ua.split( 'MSIE ' ); } // IE
	else if ( ua.match( /Firebird|Firefox/ ) ) { name = 'Firefox'; ua = ua.split( '/' ); ap = 3; } // Firefox
	else if ( ua.match( /Safari/ ) ) { name = 'Safari'; ua = ua.split( '/' ); ap = 3; } // Safari
	else if ( ua.match( /Netscape/ ) ) { name = 'Netscape'; ua = ua.split( '/' ); ap = 3; } // Netscape 6.x+
	else if ( ua.match( '^Mozilla\/[0-9\.]+ \[' ) ) { name = 'Netscape'; ua = ua.split( '/' ); } // Netscape 4.x-
	else if ( ua.match( 'Gecko\/[0-9]{8}$' ) ) { name = 'Mozilla'; ua = ua.split( 'rv:' ); } // Mozilla
	else if ( ua.match( /Camino/ ) ) { name = 'Camino'; ua = ua.split( '/' ); ap = 3; } // Camino
	
	if ( name != 'unknown' ) {
		if ( !ua[ ap ].match( '\.' ) ) {
			ver = ua[ ap ];
		} else {
			var temp = ua[ ap ].split( '.' );
			ver = temp[ 0 ] + '.' + temp[ 1 ].substr( 0, 1 );
		}
	}

	var val = [ name, ver ];
	val[ 'name' ] = name;
	val[ 'version' ] = ver;

	return ( args[ 0 ] == 'name' ) ? name : ( args[ 0 ] == 'ver' ) ? ver : val;
}

/* ウィンドウのサイズを取得 ---------------- */
function getWindowSize() {
	// arguments : ( [ mode ] )

	var args = arguments;

	var w = ( window.innerWidth ) ? window.innerWidth :
		( document.documentElement.clientWidth ) ? document.documentElement.clientWidth :
		( document.body.clientWidth ) ? document.body.clientWidth :
		0;
	var h = ( window.innerHeight ) ? window.innerHeight :
		( document.documentElement.clientHeight ) ? document.documentElement.clientHeight :
		( document.body.clientHeight ) ? document.body.clientHeight :
		0;

	var val = [ w, h ];
	val[ 'width' ] = val[ 'w' ] = w;
	val[ 'height' ] = val[ 'h' ] = h;

	return ( args[ 0 ] == 'w' ) ? w : ( args[ 0 ] == 'h' ) ? h : val;
}

/* 親ウィンドウのロケーションを変更 ---------------- */
function changeOpenerLocation() {
	// arguments : ( loc [, name ] )

	var args = arguments;

	var loc = args[ 0 ];
	var name = ( args[ 1 ] ) ? args[ 1 ] : 'opener';

	if ( window.opener && !window.opener.closed ) {
		window.opener.focus();
		window.opener.location.href = loc;
	} else {
		var win = window.open( loc, name );
		win.focus();
	}
}








