// jquery.blackout.js (c)2002-2009 econosys system
// http://www.econosys.jp/system/
//
// 要：jquery.js
// 要：jquery.wresize.js

// Version
//	1.0  とりあえず作成
//	1.1  cssをjsファイル内に吸収


jQuery.noConflict();
var j$ = jQuery;

var blackout_css_obj = {
  'position'        : 'fixed!important' ,
  'position'        : 'absolute' ,
  'top'             : 0 ,
  'left'            : 0 ,
  'width'           : '100%' ,
  'height'          : '100%' ,
  'z-index'         : 1000 ,
  'background-color': '#000000' ,
  '-moz-opacity'    : '0.8' ,
  'opacity'         : '.80' ,
  'filter'          : 'alpha(opacity=80)'
}


//----------------------------------------------- blackout
function blackout( option_obj ){

	if (! option_obj  ){ option_obj = new Object; }

	// get version
	var ua = ECOGetVersion();

	// set ID name
	if (! option_obj.id_name ){ option_obj.id_name = 'blackout'; }

	// OFF
	/*
	if ( ua.indexOf('IE',0)!=-1 ){
		option_obj.id_name = 'blackout_ie';
	}
	*/
	// OFF
	//alert(option_obj.id_name);


	// dom_obj
	var dom_obj = document.createElement('div');
	dom_obj.setAttribute('id', option_obj.id_name);
	document.body.appendChild(dom_obj);

	// css
	j$('#'+option_obj.id_name).css(blackout_css_obj);


	// hide IE6 select
	_hide_ie6_select();

	// （IE only!）
	if ( ua.indexOf('IE',0)!=-1 ){
		_set_windowsize_ie(option_obj.id_name);
	}
	

//	dom_obj.innerHTML+='<div onclick="delete_blackout('+"'"+option_obj.id_name+"'"+');"></div>';

	j$("#"+option_obj.id_name).hide();

/*
	 // IE の時再度 透明度をセット OFF
 	if ( ua.indexOf('IE',0)!=-1 ){
		j$("#"+option_obj.id_name).css({
			filter : 'alpha(opacity=80)'
		});
	}
*/

	j$("#"+option_obj.id_name).fadeIn(400, function(){
		// callback function
		option_obj.open_postrun_func();
	});


	// window resize event（ IE only ）
	if ( ua.indexOf('IE',0)!=-1 ){
		j$(window).wresize(function(){
			// alert('wresize');
			_set_windowsize_ie(option_obj.id_name);
		});
	}

	// onclick event 0.5秒後に登録する
	setTimeout( function() {
		j$("#"+option_obj.id_name).click(function () {
			close_blackout( option_obj );
		});
	}, 500 );
	
}




//----------------------------------------------- close_blackout
function close_blackout( option_obj ){

	if (! option_obj  ){ option_obj = new Object; }
	if (! option_obj.id_name ){ option_obj.id_name = 'blackout'; }

	j$("#"+option_obj.id_name).unbind('click');
	
	// prerun function が存在する場合はそのまま実行
	if (option_obj.close_prerun_func){
		option_obj.close_prerun_func();
	}
	// prerun function_name が存在する場合は function_name を eval
	else if (option_obj.close_prerun_func_name){
		eval(option_obj.close_prerun_func_name+'()');
	}
	
	
	setTimeout( function() {
		j$("#"+option_obj.id_name).fadeOut(400, function(){
			j$("#"+option_obj.id_name).remove();
			// show IE6 select
			_show_ie6_select();
		});
	}, 600 );
}


/*
//----------------------------------------------- delete_blackout
function delete_blackout( id_name ){
	var dom_obj        = document.getElementById(id_name);
	var dom_obj_parent = dom_obj.parentNode;
	dom_obj_parent.removeChild(dom_obj);
}
*/


/*
//----------------------------------------------- _import_css
function _import_css() {
	var nbr_att = arguments.length;
	var css_style=document.createElement("link");
	css_style.setAttribute("rel", "stylesheet");
	css_style.setAttribute("type", "text/css");
	css_style.setAttribute("href", arguments[0]);
	css_style.setAttribute("media", "screen");
	if(nbr_att>1){
		for (var i = 1; i<nbr_att; i++){
			css_style.setAttribute(arguments[i][0], arguments[i][1]);
		}
	}
	document.getElementsByTagName("head")[0].appendChild(css_style);
}
*/


//----------------------------------------------- ECOGetVersion : Version 2.1
// Safari3 対応
function ECOGetVersion(){
	if (navigator.userAgent.indexOf('Mac',0) != -1){ useos='Mac';}
	else if (navigator.userAgent.indexOf('Win',0) != -1){ useos='Win';}
	else {useos='other'; }
	if (navigator.userAgent.indexOf('Opera',0) != -1){usebrowser='Opera';}
	else if (navigator.userAgent.indexOf('MSIE 3.',0) != -1){usebrowser='IE3';}
	else if (navigator.userAgent.indexOf('MSIE 4.',0) != -1){usebrowser='IE4';}
	else if (navigator.userAgent.indexOf('MSIE 5.',0) != -1){usebrowser='IE5';}
	else if (navigator.userAgent.indexOf('MSIE 6.',0) != -1){usebrowser='IE6';}
	else if (navigator.userAgent.indexOf('MSIE 7.',0) != -1){usebrowser='IE7';}
	else if (navigator.userAgent.indexOf('MSIE 7.',0) != -1){usebrowser='IE8';}
	else if (navigator.userAgent.indexOf('Netscape/7.',0) != -1){usebrowser='NN7';}
	else if (navigator.userAgent.indexOf('Netscape/6.',0) != -1){usebrowser='NN6';}
	else if (navigator.userAgent.indexOf('Mozilla/4.',0) != -1){usebrowser='NN4';}
	else if (navigator.userAgent.indexOf('Safari',0) != -1 && navigator.userAgent.indexOf('Version/3',0) != -1){usebrowser='Safari3';}
	else if (navigator.userAgent.indexOf('Safari',0) != -1){usebrowser='Safari';}
	else if (navigator.userAgent.indexOf('Firefox',0) != -1){usebrowser='Firefox_NN';}
	else if (navigator.userAgent.indexOf('Gecko',0) != -1){usebrowser='other_Gecko_NN';}
	else {usebrowser='other_IE';}
	return (usebrowser);
}



//----------------------------------------------- get_browser_width_height
function get_browser_width_height(){
	
	var nowX=nowY=windowX=windowY=bodyX=bodyY=screenX=screenY=0;
	var ua=ECOGetVersion();

	// nowX nowY の取得
	if ( ua.indexOf('IE',0)!=-1 || ua.indexOf('Opera',0)!=-1 || ua.indexOf('Safari',0)!=-1 ){
		if ( document.compatMode == 'CSS1Compat'){
			nowX=document.documentElement.scrollLeft;
			nowY=document.documentElement.scrollTop;
		}
		else{
			nowX=document.body.scrollLeft;
			nowY=document.body.scrollTop;
		}
	}
	else if ( ua.indexOf('NN',0)!= -1){
		nowY=window.pageYOffset;
	}

	// windowX windowY の取得
	// Mac版Firefox1.5ではスクロールバーを含んだ値を返す。
	if ( window.innerWidth ) { windowX=window.innerWidth; }  
	else if ( document.documentElement && document.documentElement.clientWidth != 0 ) { windowX=document.documentElement.clientWidth; }
	else if ( document.body ) { windowX=document.body.clientWidth; }

	if ( window.innerHeight ) { windowY=window.innerHeight; }
	else if ( document.documentElement && document.documentElement.clientHeight != 0 ) { windowY=document.documentElement.clientHeight; }  
	else if ( document.body ) { windowY=document.body.clientHeight; }

	// Safari
	if ( ua.indexOf('Safari',0)!=-1 ){
		bodyX = document.width;
		bodyY = document.height;
	}
	// Opera , IE
	else if ( ua.indexOf('Opera',0)!=-1 || ua.indexOf('IE',0)!=-1 ){
		if (document.documentElement.scrollWidth){
			bodyX=document.documentElement.scrollWidth;
		}
		else{
			bodyX=document.body.scrollWidth;
		}
		if (document.documentElement.scrollHeight){
			if ( document.documentElement.scrollHeight < document.body.scrollHeight ){
				bodyY=document.body.scrollHeight;
			}
			else{
				bodyY=document.documentElement.scrollHeight;
			}
		}
		else{
			bodyY=document.body.scrollHeight;
		}
	}
	// Firefox
	else{
		bodyX=document.documentElement.scrollWidth;
		bodyY=document.documentElement.scrollHeight;
	}

	// スクリーンの幅と高さを取得
	screenX=screen.width;
	screenY=screen.height;

	var obj = {
		nowX:    nowX ,
		nowY:    nowY ,
		windowX: windowX ,
		windowY: windowY ,
		bodyX:   windowY,
		bodyY:   bodyY ,
		screenX: screenX,
		screenY: screenY
	}
	return obj;
}



	//----------------------------------------------- _set_windowsize_ie : Version 1.00
	function _set_windowsize_ie(id_name){

		if (navigator.userAgent.indexOf('MSIE',0) == -1){ return; }

		obj = get_browser_width_height();
		// 横スクロールバーが表示されている場合はスクリーン幅に合わせる
		if ( obj.windowX < obj.bodyX ){
			obj.screenX -=21;
			j$("#"+id_name).css('width', obj.screenX+'px');
		}
		else{
			j$("#"+id_name).css('width', obj.windowX+'px');
		}
		// 縦スクロールバーが表示されている場合はページ縦幅に合わせる
		if ( windowY < bodyY ){
			j$("#"+id_name).css('height', obj.bodyY+'px');
		}
		else{
			j$("#"+id_name).css('height', obj.windowY+'px');
		}
	}



	//----------------------------------------------- _hidden_ie6_select : Version 1.00
	function _hide_ie6_select(){
	
		if (navigator.userAgent.indexOf('MSIE 6.',0) == -1){ return; }
		else{
			var elements = document.getElementsByTagName('select');
			for (var j = 0, len2 = elements.length; j < len2; j++) {
				elements[j].style.visibility = 'hidden';
			}
		}
	}

	//----------------------------------------------- _visible_ie6_select : Version 1.00
	function _show_ie6_select(){

		if (navigator.userAgent.indexOf('MSIE 6.',0) == -1){ return; }
		else{
			var elements = document.getElementsByTagName('select');
			for (var j = 0, len2 = elements.length; j < len2; j++) {
				elements[j].style.visibility = 'visible';
			}
		}
	}
