///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//   プログラム名: Sevennet 共通ライブラリ                                   //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
//   FileName    : sCommonLib.js                                             //
//   Language    : JavaScript                                                //
//   MakeDate    : 2006.06.23(Sevennet.,Inc)                                 //
///////////////////////////////////////////////////////////////////////////////
//   改定履歴                                                                //
///////////////////////////////////////////////////////////////////////////////
// 2006.10.05                                                                //
// WindowsOS判別用ファンクション追加                                         //
//  IsWin2k3()                                                               //
//  IsWinXP()                                                                //
///////////////////////////////////////////////////////////////////////////////
// 2006.12.18                                                                //
// 下記ファンクション追加                                                    //
//  OS判別                                                                   //
//  Browser判別                                                              //
//  Browserバージョン判別                                                    //
//  Strの先頭から連続するの数字をNumで返す                                   //
//  sGetOs()                                                                 //
//  sGetBrow()                                                               //
//  sGetBrowVer()                                                            //
//  sGetPxNumber()                                                           //
//  sGetWinInnerSize                                                         //
//  sGetWinInnerWidth                                                        //
//  sGetWinInnerHeight                                                       //
// 上記ファンクション追加に伴う                                              //
// 下記ファンクションの修正                                                  //
//  funcGetBrowser()                                                         //
//  funcGetOs()                                                              //
///////////////////////////////////////////////////////////////////////////////
// 2007.01.11                                                                //
// ブラウザ起動ファンクション追加                                            //
//  sOpenWEBBrow()                                                           //
///////////////////////////////////////////////////////////////////////////////
// 2007.02.09                                                                //
// Safariバージョン判別不具合対応                                            //
//   Safariのバージョン判別を削除                                            //
//   ＜対象ファンクション＞                                                  //
//     funcGetBrowser()                                                      //
// UTF-8対応                                                                 //
//   UTF-8サイト対策としてプログラム内部で使用している全角を半角文字に       //
//   変更する。                                                              //
//   ＜対象ファンクション＞                                                  //
//     sOpenWEBBrow()                                                        //
///////////////////////////////////////////////////////////////////////////////
// 2007.05.16                                                                //
// 16進数カラーをチェックする                                                //
//   sCheckHexColor()                                                        //
// 頭の'0'を取る                                                             //
//   sStripZero()                                                            //
// getElementByIdのショートカット                                            //
//   $()                                                                     //
// IE5.5以上で拡張子が.pngの画像を表示できるようにする                       //
//   correctPNG()                                                            //
// アルファボタンにイベントを適用する                                        //
//   sSetAlphaBtnEvent()                                                     //
///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////
//                                         //
//   <<< function sfuncCommonLibInit >>>   //
//                                         //
/////////////////////////////////////////////
function sfuncCommonLibInit(){
	this.funcEnvJudge				=	funcEnvJudge;
	this.funcGetBrowser			=	funcGetBrowser;
	this.funcGetOs					=	funcGetOs;
	this.IsWin2k3						=	IsWin2k3;
	this.IsWinXP						=	IsWinXP;
	this.sGetOS							=	sGetOS;
	this.sGetBrow						=	sGetBrow;
	this.sGetBrowVer				=	sGetBrowVer;
	this.sGetCompatMode			=	sGetCompatMode;
	this.sGetPxNumber				=	sGetPxNumber;
	this.sGetWinInnerSize		=	sGetWinInnerSize;
	this.sGetWinInnerWidth	=	sGetWinInnerWidth;
	this.sGetWinInnerHeight	=	sGetWinInnerHeight;
	this.sOpenWEBBrow				=	sOpenWEBBrow;
	this.sCheckHexColor			= sCheckHexColor;
	this.sStripZero					= sStripZero;
	this.correctPNG = correctPNG;
	this.sSetAlphaBtnEvent = sSetAlphaBtnEvent;
}

///////////////////////////////////////
//                                   //
//   <<< function funcEnvJudge >>>   //
//                                   //
///////////////////////////////////////
// Parameter                         //
// 1 : EBook Judge                   //
// 2 : EBook / FlashEBook Judge      //
// Return                            //
// True  : OK                        //
// False : NG                        //
///////////////////////////////////////
function funcEnvJudge(sParam){
	var	fOS					=	this.funcGetOs();
	var	fBrowser			=	this.funcGetBrowser();
	var	bJudge				=	false;

	switch(sParam){
	case	"1"	:	if		(fOS		==	"1"){
						if	(fBrowser	==	"1"){
							bJudge		=	true;
						}else{
							bJudge		=	false;
						}
					}else{
						bJudge	=	false;
					}
					break;
	case	"2"	:	if	(fOS			==	"1"){
						if	((fBrowser	==	"1")
							||
							(fBrowser	==	"2")){
							bJudge		=	true;
						}else{
							bJudge		=	false;
						}
					}else{
						if	(fOS			==	"2"){
							if	((fBrowser	==	"2")
								||
								(fBrowser	==	"3")){
								bJudge		=	true;
							}else{
								bJudge		=	false;
							}
						}else{
							bJudge		=	false;
						}
					}
					break;
	}

	return	bJudge;
}

/////////////////////////////////////////
//                                     //
//   <<< function funcGetBrowser >>>   //
//                                     //
/////////////////////////////////////////
// Return Value                        //
// 0 : EBook / FlashEBook = NG         //
// 1 : EBook = OK (InternetExplorer)   //
// 2 : FlashEBook = OK (FireFox)       //
// 3 : FlashEBook = OK (Other)         //
/////////////////////////////////////////
function funcGetBrowser(){
	var	fBrowser	=	"0";

//---<<< ▽ Opera	==> ua.Opera >>>---//
	if	(this.sGetBrow()=='Opera'){
		return fBrowser;
	}

//---<<< ▽ Internet Explorer	==> ua.InternetExplorer >>>---//
	if	(this.sGetBrow()=='IE'
		&&
		this.sGetBrowVer()	>=	5){
		fBrowser			=	"1";
		return fBrowser;
	}

//---<<< ▽ Netscape	==> ua.Netscape >>>---//
	if	(this.sGetBrow()=='NN'){
		return fBrowser;
	}

//---<<< ▽ Firefox		==> ua.Firefox >>>---//
	if	(this.sGetBrow()=='Firefox'
		&&
		this.sGetBrowVer()	>=	1.0){
		fBrowser			=	"2";
		return fBrowser;
	}

//---<<< ▽ Safari		==> ua.Safari >>>---//
	if	(this.sGetBrow()=='Safari'){
		fBrowser			=	"3";
		return fBrowser;
	}

//---<<< ▽ Mozilla		==> ua.Mozilla >>>---//
	if	(this.sGetBrow()=='Mozilla'
		&&
		this.sGetBrowVer()		>=	7.5){
		fBrowser			=	"3";
		return fBrowser;
	}

	return fBrowser;

}


////////////////////////////////////
//                                //
//   <<< function funcGetOs >>>   //
//                                //
////////////////////////////////////
// Return Value                   //
// 2 : MacOS X                    //
// 1 : Windows OS                 //
// 0 : Other OS                   //
////////////////////////////////////
function funcGetOs(){
	var	fOS			=	"0";

	if		(this.sGetOS()=='Mac'){
			fOS			=	"2";
	}else{
		if	(this.sGetOS()=='Win'){
			fOS			=	"1";
		}
	}
	return fOS;
}


///////////////////////////////////
//                               //
//   <<< function IsWin2k3 >>>   //
//                               //
///////////////////////////////////
function IsWin2k3(){
	if ( navigator.userAgent.indexOf("Windows NT 5.2") > 1 ) {
		return	true;
	}
	return false // WinMe, Win9x, Win2k  
}


//////////////////////////////////
//                              //
//   <<< function IsWinXP >>>   //
//                              //
//////////////////////////////////
function IsWinXP(){
	if( navigator.userAgent.indexOf("Windows NT 5.1")	>	1){
		return	true;
	}
	return	false; // WinMe, Win9x, Win2k, Win2k  
}


/////////////////////////////////
//                             //
//   <<< function sGetOs >>>   //
//                             //
/////////////////////////////////
// Return                      //
// Win     : Windows OS        //
// Mac     : Mac OS            //
// Linux   : Linux OS          //
// Unknown : Other OS          //
/////////////////////////////////
function sGetOS() {
	var ua = navigator.userAgent;
	
	if			(ua.indexOf("Win")	 >= 0)			return "Win";
	else if (ua.indexOf("Mac")	 >= 0)			return "Mac";
	else if (ua.indexOf("Linux") >= 0)			return "Linux";
	else																		return "Unknown";
}


///////////////////////////////////
//                               //
//   <<< function sGetBrow >>>   //
//                               //
///////////////////////////////////
// Return                        //
// IE                            //
// Firefox                       //
// Opera                         //
// NN                            //
// Safari                        //
// Mozilla                       //
// Unknows                       //
///////////////////////////////////
function sGetBrow() {
	var ua = navigator.userAgent;
	
	if			(ua.indexOf("Opera")			>= 0)	return "Opera";
	else if (ua.indexOf("MSIE")				>= 0)	return "IE";
	else if (ua.indexOf("Netscape")		>= 0)	return "NN";
	else if (ua.indexOf("Firefox")		>= 0)	return "Firefox";
	else if (ua.indexOf("Safari")			>= 0)	return "Safari";
	else if (ua.indexOf("Gecko")			>= 0)	return "Mozilla";
	else if (ua.indexOf("Mozilla/4")	>= 0)	return "NN";
	else																		return "Unknown";
}


//////////////////////////////////////
//                                  //
//   <<< function sGetBrowVer >>>   //
//                                  //
//////////////////////////////////////
// Return                           //
// VersionNumber : x.x              //
// Unknown                          //
//////////////////////////////////////
function sGetBrowVer() {
	var ua = navigator.userAgent;
	var nHit;

	if ((nHit = ua.indexOf("Opera")) >= 0) {
																													return ua.substr(nHit+ 6, 3);
	} else if ((nHit = ua.indexOf("MSIE")) >= 0) {
																													return ua.substr(nHit+ 5, 3);
	} else if ((nHit = ua.indexOf("Netscape")) >= 0) {
		if (ua.charAt(nHit+8) == "6" ) {
																													return ua.substr(nHit+10, 3);
		} else {
																													return ua.substr(nHit+ 9, 3);
		}
	} else if ((nHit = ua.indexOf("Safari")) >= 0) {
																													return ua.substr(nHit+ 7);
	} else if ((nHit = ua.indexOf("Firefox")) >= 0) {
																													return ua.substr(nHit+ 8, 3);
	} else if (ua.indexOf("Gecko") >= 0) {
		nHit = ua.indexOf("rv:");
																													return ua.substr(nHit+ 3, 3);
	} else if (ua.indexOf("Mozilla/4") >= 0) {
																													return "4";
	} else {
																													return "Unknown";
	}
}


/////////////////////////////////////////
//                                     //
//   <<< function sGetCompatMode >>>   //
//                                     //
/////////////////////////////////////////
// Return                              //
// true  :標準                         //
// false :互換                         //
/////////////////////////////////////////
function sGetCompatMode() {
	var compat = (document.compatMode=="CSS1Compat")?true:false;
	return compat;
}


///////////////////////////////////////
//                                   //
//   <<< function sGetPxNumber >>>   //
//                                   //
///////////////////////////////////////
function sGetPxNumber(sPx){
	return Number(sPx.match(/^[0-9]*/));
}


/////////////////////////////////////////////
//                                         //
//   <<< function sGetWinInnerSize   >>>   //
//   <<< function sGetWinInnerWidth  >>>   //
//   <<< function sGetWinInnerHeight >>>   //
//                                         //
//   Get window inner size set             //
/////////////////////////////////////////////
// Return                                  //
// w :ウィンドウ表示内の幅                 //
// h :ウィンドウ表示内の高さ               //
/////////////////////////////////////////////
function sGetWinInnerSize() {
	var w, h;
	w = this.sGetWinInnerWidth();
	h = this.sGetWinInnerHeight();
	return {w: w, h: h};
}

function sGetWinInnerWidth() {
	var w = 0;
	if(this.sGetBrow()=='IE'){
		w = document.documentElement.clientWidth;
	} else {
		w = window.innerWidth;
	}
	return w;
}

function sGetWinInnerHeight() {
	var h = 0;
	if(this.sGetBrow()=='IE'){
		if(this.sGetCompatMode()){
			h = document.documentElement.clientHeight;	//標準
		} else {
			h = document.body.clientHeight;							//互換
		}
	} else {
		h = window.innerHeight;
	}
	return h;
}

///////////////////////////////////////////////////////////////////
//                                                               //
//   <<<  function sOpenWEBBrow  >>>                             //
//                                                               //
///////////////////////////////////////////////////////////////////
// Parameter                                                     //
// arg_url     : URL                                             //
// arg_winName : window name                                     //
// arg_pos     : window position & size [width,height,top,left]  //
// arg_param   : 1   メニューなし、スクロールあり                //
//             : 2   メニューなし、スクロールなし                //
//             : etc 任意指定                                    //
// arg_center  : 0 中央表示なし                                  //
//             : 1 中央表示あり                                  //
///////////////////////////////////////////////////////////////////
function sOpenWEBBrow(arg_url, arg_winName, arg_pos, arg_param, arg_center){
	var arg_url, arg_winName, arg_param, arg_center;
	var position, l, t;
	var comma, option;
	var checkFlag = false;
	
	//URL
	if(!arg_url){
//		alert('URLが指定されていません。');
	} else {
		checkFlag = true;
	}
	//window name
	if(!arg_winName){
		arg_winName = '_blank';
	}
	//show center
	if(!arg_center){
		arg_center = '0';
	}
	//window size position
	position = '';
	if(arg_pos){
		arg_pos = arg_pos.split(',');	
		if(arg_pos[0]&&arg_pos[1]){
			position += 'width=' + arg_pos[0] + ', height=' + arg_pos[1];
			if(arg_pos[2]&&arg_pos[3]&&arg_center=='0'){
					position += ', top=' + arg_pos[2] + ', left=' + arg_pos[3];
			} else {
				if(arg_center=='1'){
					l=((window.screen.availWidth/2)-(arg_pos[0]/2));
					t=((window.screen.availHeight/2)-(arg_pos[1]/2));
					position += ', top=' + t + ', left=' + l;
				}
			}
		}
	}
	//other parameter
	if(!arg_param){
		arg_param = 'channelmode=no, directories=no, fullscreen=no, location=yes, status=yes, titlebar=yes, toolbar=yes, resizable=yes, scrollbars=yes, menubar=yes';
	} else {
		switch(arg_param){
			case '1':
				arg_param = 'channelmode=no, directories=no, fullscreen=no, location=no, status=yes, titlebar=yes, toolbar=no, resizable=yes, scrollbars=yes, menubar=no';
				break;
			case '2':
				arg_param = 'channelmode=no, directories=no, fullscreen=no, location=no, status=yes, titlebar=yes, toolbar=no, resizable=no, scrollbars=no, menubar=no';
				break;
			default:
				arg_param = arg_param;
				break;
		}
	}

	if(checkFlag){
		comma = (position=='')?'':', ';
		option = position + comma + arg_param;
		//alert(option);
		var newWin = window.open(arg_url, arg_winName, option);
		newWin.focus();
	}
}


///////////////////////////////////////////////////////////////////
//                                                               //
//   <<<  function sCheckHexColor  >>>                           //
//                                                               //
///////////////////////////////////////////////////////////////////
// 16進数でのカラーチェック                                      //
//                                                               //
// Return:                                                       //
// true / false                                                  //
//                                                               //
// EX:                                                           //
// sCheckHexColor('#FFFFFF');                                    //
///////////////////////////////////////////////////////////////////
function sCheckHexColor(_color){
	var checkFlag	= false;
	var len			= _color.length;
	var str			= "";

	if(len==4 || len==7){

		if(_color.charAt(0)=='#' && len<=7){
			for(var i=1;i<len;i++){
				str = _color.charAt(i);
				if(str.match(/[0-9a-f]/i)){
				} else {
					break;
				}
				if(i==len-1){checkFlag = true;}
			}
		}

	}
	return checkFlag;
}

///////////////////////////////////////////////////////////////////
//                                                               //
//   <<<  function sStripZero  >>>                               //
//                                                               //
///////////////////////////////////////////////////////////////////
// 頭の'0'を削除する                                             //
//                                                               //
// @Sample                                                       //
// sStripZero('00100'); → '100'                                 //
//                                                               //
///////////////////////////////////////////////////////////////////
function sStripZero(_number){
	var temp = _number;
	var len	 = _number.length;
	if(temp!='0'){
		while(temp.charAt(0)=='0' && len>1){
			temp = temp.substring(1,len);
			len = temp.length;
		}
	}
	return temp;
}


///////////////////////////////////////////////////////////////////
//                                                               //
//   <<<  function $  >>>                                        //
//                                                               //
///////////////////////////////////////////////////////////////////
// From prototype-1.4.0.js at 2007/05/17                         //
// Copyright (c) 2005-2007 Sam Stephenson                        //
//                                                               //
///////////////////////////////////////////////////////////////////
   
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

///////////////////////////////////////////////////////////////////
//                                                               //
//   <<<  function correctPNG  >>>                               //
//                                                               //
///////////////////////////////////////////////////////////////////
// From YUI at 2007/05/14                                        //
// Copyright (c) 2007, Yahoo! Inc. All rights reserved.          //
//                                                               //
///////////////////////////////////////////////////////////////////
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
   {
   for(var i=0; i<document.images.length; i++)
      {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	     {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 //alert(strNewHTML);
		 img.outerHTML = strNewHTML
		 i = i-1
	     }
      }
   }

///////////////////////////////////////////////////////////////////
//                                                               //
//   <<<  function sSetAlphaBtnEvent  >>>                        //
//                                                               //
///////////////////////////////////////////////////////////////////
// アルファボタンにイベントを適用する                            //
//                                                               //
//                                                               //
///////////////////////////////////////////////////////////////////
function sSetAlphaBtnEvent(){
	for(var i=0;i<sgaColorBtnsIds.length;i++){
		var d = $(sgaColorBtnsIds[i]);
		if(d){
			d.style.cursor	= 'pointer';
			d.onclick		= new Function(sgaColorBtnsFuncs[i]);
			
			
			if(sgaBtnsColor[sgaColorBtnsIds[i]][0] != "" && soComLib.sCheckHexColor(sgaBtnsColor[sgaColorBtnsIds[i]][0])){
				d.style.backgroundColor	= sgaBtnsColor[sgaColorBtnsIds[i]][0];
				d.onmouseout = function(){
					this.style.backgroundColor = sgaBtnsColor[this.id][0];
				}
			} else {
				d.style.backgroundColor	= sgsBtnDefColor;
				d.onmouseout = function(){
					this.style.backgroundColor = sgsBtnDefColor;
				}
			}

			if(sgaBtnsColor[sgaColorBtnsIds[i]][1] != "" && soComLib.sCheckHexColor(sgaBtnsColor[sgaColorBtnsIds[i]][1])){
				d.onmouseover = function(){
					this.style.backgroundColor = sgaBtnsColor[this.id][1];
				}
			} else {
				d.onmouseover = function(){
					this.style.backgroundColor = sgsBtnOverDefColor;
				}
			}
		}
	}
}

soComLib		=	new sfuncCommonLibInit();
