// JavaScript Document

////////////////////////////////////////////////////////////////////////////////
/// userAgent設定
////////////////////////////////////////////////////////////////////////////////

var nUa=navigator.userAgent;
var sW=screen.width;

var nUaIPh=nUa.indexOf('iPhone;')!=-1;//iPhone
var nUaIPa=nUa.indexOf('iPad;')!=-1;//iPad
var nUaIPo=nUa.indexOf('iPod;')!=-1;//iPod
var nUaAnd=nUa.indexOf('Linux; U; Android ')!=-1;//Android
//var nUaBlb=nUa.indexOf('BlackBerry')!=-1;//BlackBerry
//var nUaWmb=nUa.indexOf('Windows Phone')!=-1;//Windows Mobile

var nUaDocomo=nUa.toLowerCase().indexOf('docomo')!=-1;//docomo
var nUaAu=nUa.toLowerCase().indexOf('kddi')!=-1;//au
var nUaSb=nUa.toLowerCase().indexOf('softbank')!=-1;//softbank

var nUaOp=nUa.toLowerCase().indexOf('opera')!=-1;//Opera

var nUaSmp;
if(nUaIPh||nUaIPa||nUaIPo||nUaAnd){
	nUaSmp=1;//smartPhone
}
var nUaPC;
if((sW>=800)&&!nUaAnd&&!nUaDocomo&&!nUaAu&&!nUaSb){
	nUaPC=1;//PC
}
var nUaMB;
if(nUaDocomo||nUaAu||nUaSb){
	nUaMB=1;//MOBILE
}
/*
if(nUaPC){
	//PC
}else if(nUaSmp){
	//スマホ
}else if(nUaMB){
	//モバイル
}else{
	//モバイル
}
*/


////////////////////////////////////////////////////////////////////////////////
/// meta,linkタグ追記（iPhone,iPad,iPod）
////////////////////////////////////////////////////////////////////////////////

function wMeta(){
	if(!nUaOp){
		document.write('<meta name="apple-mobile-web-app-capable" content="yes" />');
		document.write('<meta name="format-detection" content="telephone=no" />');
		if( nUaIPa ){
			document.write('<meta name="viewport" content="width=1024" />');
		}else{
			document.write('<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0, maximum-scale=10" />');
		}
		document.write('<link rel="apple-touch-icon" href="/apple-touch-icon.png" />');
	}
}


////////////////////////////////////////////////////////////////////////////////
/// Tel Link解除
////////////////////////////////////////////////////////////////////////////////

$(document).ready( function(){
	if(nUaPC){
		$('#content a.tel').each(function(){
			$(this).attr('href','javascript:void(0);');
			$(this).css('color','inherit').css('text-decoration','none').css('cursor','text');
		});
	}
});







////////////////////////////////////////////////////////////////////////////////
/// 以下、201105以前からの記述
////////////////////////////////////////////////////////////////////////////////

/* className IE対策
------------------------------------------------------------*/
var attName;
if(document.documentElement.getAttribute("style") == document.documentElement.style){
	attName='className';
}else{
	attName='class';
}

/* サブウィンドウオープン
------------------------------------------------------------*/
function openSWin(uri,name,w,h) {
	sub=window.open(uri,name,"location=no,directories=no,scrollbars=1,resizable=0,width="+w+",height="+h+",left=5,top=5");
	sub.focus();
}

/* 親ウィンドウオープン
-----------------------------------------------------------------------------------------------------------------*/
function openWin(strUrl,winName) {
  winName = window.open(strUrl,winName);
  winName.focus();
}

/* ずかんページのポップアップ
-----------------------------------------------------------------------------------------------------------------*/
function zukanWin( url )
{
	point = window.open("","reference","width=833,height=678,toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=yes");
	point.location.href = url;
	point.focus( );
}


/* リンク先と今のドメインが違ってたら別ウィンドウで開く
-----------------------------------------------------------------------------------------------------------------*/
function tgtChg(aTag){
	var linkUrl=aTag.href;
	var locHost=location.host;
	var locDomain="http://"+locHost;
	if(linkUrl.indexOf(locDomain,0)==-1){
		aTag.target='_blank';
	}
}

/* 画像マウスオーバー
------------------------------------------------------------*/
function imgRoll(obj,flag) {
	var pathsrc = obj.getAttribute("src");
	var path = pathsrc.slice(0,pathsrc.lastIndexOf("/")+1);
	var imgname = pathsrc.slice(pathsrc.lastIndexOf("/")+1,pathsrc.length);
	if(flag) {
		var imgname = imgname.replace(/_def/i,"_ovr");
	} else {
		var imgname = imgname.replace(/_ovr/i,"_def");
	}
	obj.setAttribute("src",path + imgname);
}

/* 右ナビ現在地表示
------------------------------------------------------------*/

/*
操作開始クローズ
--------------------------------------------------*/
var closeDigit=6;
var closeRightNav=function(){
	if(document.getElementById("rnav")){
		var idLn=document.getElementById("rnav");
		tNUl=idLn.getElementsByTagName("ul");
		for(var i=0;i<tNUl.length;i++){
			var idUl=tNUl[i].getAttribute("id");
			if(idUl&&idUl.length>=closeDigit){
				document.getElementById(idUl).style.display="none";
			}
		}
	}
};

/*
ライトナビ操作
--------------------------------------------------*/
function loadRightNavi(){
	closeRightNav();
	var locPath=location.pathname;
	var locHost=location.host;
	var locDomain="http://"+locHost;
	if(document.getElementById("rnav")){
		var idLn=document.getElementById("rnav");
		tNA=idLn.getElementsByTagName("a");
	}
	for(var i=0;i<tNA.length;i++){
		var atHref=tNA[i].getAttribute("href");
		var qStr=atHref.indexOf('?');
		if(qStr>-1){
			atHref=atHref.substring(0,qStr);
		}
		if(atHref.indexOf(locHost)==-1){
			atHref=locHost+atHref;
		}
		if((locDomain+locPath).indexOf(atHref)!=-1){
			var idLi=tNA[i].parentNode.getAttribute("id");
		}
	}
	if(idLi) var lenIdLi=idLi.length;
	else return;
	if(document.getElementById(idLi)) document.getElementById(idLi).className="current";
	if(idLi){
		var idGGParentUl=idLi.slice(0,lenIdLi-6)+"00";
		var idGParentUl=idLi.slice(0,lenIdLi-4)+"00";
		var idParentUl=idLi.slice(0,lenIdLi-2)+"00";
		var idChildtUl=idLi+"00";
	}
	if(idLi.length==6){
		var idParentLi=idLi.slice(0,4);
		if(document.getElementById(idParentLi)) document.getElementById(idParentLi).className="current";
	}
//alert(idLi+" "+idGGParentUl+" "+idGParentUl+" "+idParentUl+" "+idChildtUl);
	if(document.getElementById(idGGParentUl)) document.getElementById(idGGParentUl).style.display="block";
	if(document.getElementById(idGParentUl)) document.getElementById(idGParentUl).style.display="block";
	if(document.getElementById(idParentUl)) document.getElementById(idParentUl).style.display="block";
	if(document.getElementById(idChildtUl)) document.getElementById(idChildtUl).style.display="block";
}


/* 右クリック禁止 IE対策
------------------------------------------------------------*/
function notes(eve){
	if(document.all){
		if(event.button == 2){
			alert("右クリック禁止になっています。画像のコピーはしないでね！");
			return false;
		}
	}else if(document.layers){
		if(eve.which == 3){
			alert("右クリック禁止になっています。画像のコピーはしないでね！");
			return false;
		}
	}
}

/* 巨大コンテンツへのリンクの確認ダイアログ
------------------------------------------------------------*/
function confirmLargeContents(){
	var useragent = navigator.userAgent;
	var useragent_low = useragent.toLowerCase();
	if(
		useragent_low.indexOf('opera mini',0)>=0
		|| useragent_low.indexOf('iphone',0)>=0
		|| useragent_low.indexOf('nintendo wii',0)>=0
		|| useragent_low.indexOf('nintendo ds',0)>=0
	){
		if( !confirm( 'これより先のページにつきましては、パソコンのウェブブラウザでの閲覧を推奨いたします。'+"\n"+'パソコン以外の閲覧環境下での動作は保証いたしかねます。ご了承くださいませ。' ) ){
			return false;
		}
	}
	return true;
}

/* オンロード
------------------------------------------------------------*/
$(function(){
	//--------------------------------------
	//  過去のニュースを隠す
	(function(){
		//--------------------------------------
		//	文字列を指定文字で埋める
		var str_pad = function( $string , $bytes , $padStr , $option ){
			if( $string === undefined ){ $string = ''; }
			if( $string === null ){ $string = ''; }
			$string += '';
			if( !$option ){ $option = 'STR_PAD_LEFT'; }
			if( !$padStr ){ $padStr = '_'; }
			if( !$bytes ){ return $string; }
			while( $string.length < parseInt( $bytes ) ){
				if( $option == 'STR_PAD_RIGHT' ){
					$string = $string + $padStr;
				}else{
					$string = $padStr + $string;
				}
			}
			return $string;
		}
		var $trList = $('.newsList tr');
		var $today = new Date();
		$today = Number( str_pad($today.getFullYear(),4,'0','STR_PAD_LEFT') + '' + str_pad($today.getMonth()+1,2,'0','STR_PAD_LEFT') + '' + str_pad($today.getDate(),2,'0','STR_PAD_LEFT') );
		for( var $i = 0; $i<$trList.length; $i ++ ){
			var $tmpelm = $('th span.expire',$trList[$i])[0];
			if(!$tmpelm){continue;}
			var $expdate = $tmpelm.innerText;
			$expdate = Number( $expdate );
			if( $today >= $expdate ){
				$($trList[$i]).hide();
			}
		}
	})();
	//  / 過去のニュースを隠す
	//--------------------------------------
});
function docWriteSearchScript(){
	var RTN = '';
	if( nUaPC ){
//		RTN += '<scr'+'ipt type="text/javascr'+'ipt" src="http://pokemon.dga.jp/common/js/jquery.js"></scr'+'ipt>' + "\n";
//		RTN += '<scr'+'ipt type="text/javascr'+'ipt">' + "\n";
//		RTN += '<!--' + "\n";
//		RTN += 'jQuery.noConflict();' + "\n";
//		RTN += '//-->' + "\n";
//		RTN += '</scr'+'ipt>' + "\n";
		RTN += '<scr'+'ipt type="text/javascr'+'ipt" src="http://pokemon.dga.jp/common/js/sa/i_search_assist.js"></scr'+'ipt>' + "\n";
		RTN += '<scr'+'ipt type="text/javascr'+'ipt" src="http://pokemon.dga.jp/common/js/search_tool.js"></scr'+'ipt>' + "\n";
	}else if( nUaSmp ){
		RTN += '<scr'+'ipt type="text/javascr'+'ipt" src="http://pokemon.dga.jp/common/js/search_tool.js"></scr'+'ipt>' + "\n";
	}
	return RTN;
}//docWriteSearchScript();


if(document.layers)document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=notes;


//onload自走処理
$(document).ready(function(){
	if(!nUaMB){
		//img.img を置き換え
		var imgList = $('img');
		for( var i = 0; imgList[i]; i ++ ){
			var img = $(imgList[i]);
			var src = img.attr('src');
			if(!src){continue;}
			if( src.match( new RegExp('^/_sys/ws/(.*)/([a-zA-Z0-9\-\_]+).*?(\.[a-zA-Z0-9]+)$') ) ){
				img[0].src = ( '/'+(RegExp.$1)+'/'+(RegExp.$2)+(RegExp.$3) );
			}
		}

		//img.imgLink を置き換え
		var imgList = $('span.sys_imgLink');
		for( var i = 0; imgList[i]; i ++ ){
			var a = $('a',imgList[i]);
			var src = a.attr('href');
			if(!src){continue;}
			if( src.match( new RegExp('^/_sys/ws/(.*)/([a-zA-Z0-9\-\_]+).*?(\.[a-zA-Z0-9]+)$') ) ){
				var img = new Image();
				img.src = ( '/'+(RegExp.$1)+'/'+(RegExp.$2)+(RegExp.$3) );
				$(img).width('100%');
				$(a).replaceWith( img );
			}
		}
	}
});



/* クッキー処理
------------------------------------------------------------*/
//	Cookieの値を取得
function getCookie( name ){
	var RTN = ( function(){
		var m = document.cookie.match( new RegExp("(^|;\\s*)"+name+"=([^;\\s]+)") );
		return m && m[2];
	} )();
	if( RTN === null || RTN === undefined ){ return ''; }
	return RTN;
}
//	Cookieの値を設定
function setCookie ( name , value , options ){
	if( !options ){ options = {}; }
	if( !options.path ){ options.path = '/'; }
	var cookieVal = name+'='+value;
	for (var key in options) {
		cookieVal+= '; '+key+'='+options[key];
	}
	document.cookie = cookieVal;
}
//	Cookieの値を削除
function removeCookie( name , options ){
	options = options || {};
	options.expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
	this.setCookie( name , '' , options );
}


/* 独自アラート 白地
------------------------------------------------------------*/
function newAlert(args){
	/*
		・必ず、alertの見出しと説明文を１つずつ用意してください。
		・予めbodyがposition:relative;出ない場合はflagは１に → bodyを一時的にposition:relative; → 閉じる時に元のpositionに戻す
		・実行方法：newAlert({title:'見出しのテキスト',expla:'注意文言の説明文',type:'色の名前など'});
		・newAlert()の引数のtypeには色の名前が入ります。typeは省略、未定義可能でその場合、whiteがdefaultで設定されます。
	*/
	//arartのtype
	var alertType = '';
	if( args.type == '' || args.type == undefined){
		alertType = 'white';
	}else{
		alertType = args.type;
	}

	//bodyのpositionをcheck
	var adjustPositionFlg = '0';
	var defaultBodyPosition = $('body').css('position');
	if( defaultBodyPosition !== 'relative' ){
		adjustPositionFlg = '1';
	}

	//newAlertのテンプレート
	var tempHtml = '';
	tempHtml += '<div class="newAlert ' + alertType + '"><div class="newAlert_base">';
	tempHtml += '<div class="bg_gray_trans"></div>';
	tempHtml += '<div class="alert_mainbody1"><div class="alert_mainbody2">';
	tempHtml += '<div class="bg_box_r_557_t"><div class="bg_box_r_557_b"><div class="bg_box_r_557_m">';
	tempHtml += '<p class="alert_title">'+args.title+'</p>';
	tempHtml += '<p class="alert_expla">'+args.expla+'</p>';
	tempHtml += '<p class="btn_close"><img src="/common/images/btn/btn_close_162x56.png" alt="閉じる" /></p>';
	tempHtml += '</div></div></div>';
	tempHtml += '</div></div>';
	tempHtml += '</div></div>';
	if( adjustPositionFlg == '1' ){
		$('body').css({position:'relative'}).prepend(tempHtml);
	}else{
		$('body').prepend(tempHtml);
	}

	//アラート本体縦位置の調整
	var alert_mainbodyHeight = $('div.newAlert div.alert_mainbody1').height()/2;
	$('div.newAlert div.alert_mainbody2').css({top:'-'+ alert_mainbodyHeight +'px'});

	//閉じるボタンをクリックした時の挙動
	$('div.newAlert p.btn_close img').bind('click',function(event){
		switch ( event.type ){
		case 'click':
			//newAlertを削除
			$('div.newAlert').remove();
			if( adjustPositionFlg == '1' ){
				$('body').css({position:defaultBodyPosition});
			}
			break;
		default:
			return
			break;
		}
	});

}




