/*
  Seven-Eleven Japan 
  URL: http://www.sej.co.jp/
	summary: common script
  LastUpdate: 2008-02-06
--------------------------------------*/

/* ブラウザ環境変数取得
--------------------------------------*/
var ua = navigator.userAgent.toLowerCase();


/* favicon.ico
--------------------------------------*/
document.write('<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="/favicon.ico">');


/* ショートカット関数
--------------------------------------*/
function getId(ID) {
	if (!document.getElementById) return false;
	return document.getElementById(ID);
}

/* Cookie制御
--------------------------------------*/
function createCookie(name,value) {
	var expires = 'expires=Tue, 1-Jan-2030 00:00:00 GMT';
		//2030年1月1日（火）を有効期限に指定（無期限）
	document.cookie = name + '=' + value + '; ' + expires + '; path=/';
}

function readCookie(name) {
  var nameEQ = name + '=';
  var cItem = document.cookie.split(';');
  for(var i=0, l=cItem.length; i<l; i++) {
    var c = cItem[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


/* Cookieによる地域別表示制御
--------------------------------------*/
function setDistCookie(value) {
	document.cookie = "dist_info="+value+"; expires=Tue, 1-Jan-2030 00:00:00 GMT; path=/;";
}

function getCookieDistInfo() {
	theCookie=document.cookie+";";
	start=theCookie.indexOf("dist_info=");
	val="";
	if(start!=-1) {
		end=theCookie.indexOf(";",start);
		val=theCookie.substring(start+10,end);
	}
	return val;
}

function isNullDistInfo() {
	val = getCookieDistInfo();
	if(val == "") {return true;}
	return false;
}

function getProductURL(){
	val=getCookieDistInfo();
	switch(val) {
		case "kanto"     : location.replace("/products/kanto/"); break;
		case "hokkaido"  : location.replace("/products/hokkaido/"); break;
		case "tohoku"    : location.replace("/products/tohoku/"); break;
		case "koshinetsu": location.replace("/products/koshinetsu/"); break;
		case "tokai"     : location.replace("/products/tokai/"); break;
		case "kinki"     : location.replace("/products/kinki/"); break;
		case "chugoku"   : location.replace("/products/chugoku/"); break;
		case "kyushu"    : location.replace("/products/kyushu/"); break;
		default: location.replace("/products/kanto/");
	}
}

function getProductWeeklyURL(){
	val=getCookieDistInfo();
	switch(val) {
		case "kanto"     : location.replace("/products/new/index_kanto.html"); break;
		case "hokkaido"  : location.replace("/products/new/index_hokkaido.html"); break;
		case "tohoku"    : location.replace("/products/new/index_tohoku.html"); break;
		case "koshinetsu": location.replace("/products/new/index_koshinetsu.html"); break;
		case "tokai"     : location.replace("/products/new/index_tokai.html"); break;
		case "kinki"     : location.replace("/products/new/index_kinki.html"); break;
		case "chugoku"   : location.replace("/products/new/index_chugoku.html"); break;
		case "kyushu"    : location.replace("/products/new/index_kyushu.html"); break;
		default: location.replace("/products/new/");
	}
}


/* 文字の大きさ制御
   (with jQuery)
--------------------------------------*/
var fontSize = {
	value: (readCookie('font_size') == 'large') ? 'large' : 'small',
	flag: false,
	btnImgS_oj: new Image(),
	btnImgL_oj: new Image(),
	btnImgS_on_oj: new Image(),
	btnImgL_on_oj: new Image(),
	btnImgS: '/shared/image/btn_fontsize_s.gif',
	btnImgL: '/shared/image/btn_fontsize_l.gif',
	btnImgS_on: '/shared/image/btn_fontsize_s_on.gif',
	btnImgL_on: '/shared/image/btn_fontsize_l_on.gif',
	code: function() {
		var btnS = this.btnImgS;
		var btnL = this.btnImgL;
		if (this.value == 'large') btnL = this.btnImgL_on;
		else btnS = this.btnImgS_on;
		
		var s = '';
		s += '<div class="section" id="font-size-change">';
		s += '<h2><img src="/shared/image/title_fontsize.gif" alt="文字の大きさを変更する" width="76" height="18"></h2>';
		s += '<ul>';
		s += '<li class="small"><a href="javascript:;"><img '+'s'+'r'+'c'+'="' + btnS + '" alt="文字の大きさ「小」" id="btnSmall"></a></li>';
		s += '<li class="large"><a href="javascript:;"><img '+'s'+'r'+'c'+'="' + btnL + '" alt="文字の大きさ「大」" id="btnLarge"></a></li>';
		s += '</ul>';
		s += '<p><a href="/support/faq/font_size.html" title="文字の大きさについての詳細">[詳細]</a></p>';
		s += '</div>';
		s += '<hr>';
		document.write(s);
		
		this.set();
		this.flag = true;
	},
	set: function() {
		var element = getId('font-size-change');
		var btnS = getId('btnSmall');
		var btnL = getId('btnLarge');
		var self = this;
		if(!element) return false;// code()が実行されていない場合は実行しない
	
		var btnS_on = function() {
			btnS.src = self.btnImgS_on;
			btnS.onmouseover = function() {return false;}
			btnS.onmouseout  = function() {return false;}
			btnL.src = self.btnImgL;
			btnL.onmouseover = function() {this.src = self.btnImgL_on;};
			btnL.onmouseout  = function() {this.src = self.btnImgL;};
		}
		var btnL_on = function() {
			btnS.src = self.btnImgS;
			btnS.onmouseover = function() {this.src = self.btnImgS_on;};
			btnS.onmouseout  = function() {this.src = self.btnImgS;};
			btnL.src = self.btnImgL_on;
			btnL.onmouseover = function() {return false;}
			btnL.onmouseout  = function() {return false;}
		}
		
		var a = element.getElementsByTagName('a');
		a[0].onclick = function() {
			self.value = "small";
			createCookie('font_size', self.value);
			$('body').removeClass('large').addClass('small');
			btnS_on();
		}
		a[1].onclick = function() {
			self.value = 'large';
			createCookie('font_size', self.value);
			$('body').removeClass('small').addClass('large');
			btnL_on();
		}
		if (self.value == 'large') {
			$('body').removeClass('small').addClass('large');
			btnL_on();
		}
		else {
			$('body').addClass('small');
			btnS_on();
		}
	}
}
// ボタンのプリロード
fontSize.btnImgS_oj.src = fontSize.btnImgS;
fontSize.btnImgL_oj.src = fontSize.btnImgL;
fontSize.btnImgS_on_oj.src = fontSize.btnImgS_on;
fontSize.btnImgL_on_oj.src = fontSize.btnImgL_on;


/* グローバルナビゲーションのカレント表示
   (with jQuery)
--------------------------------------*/
function gNavCurrent() {
	if (!getId('global-nav')) return;
	
	var path = document.URL.split('/');
	var dir = path[3];
	var dirCorp = path[4];
	if (dir == 'corp') {
		var nav = {
			company: $('div#global-nav li.company img'),
			ir:      $('div#global-nav li.ir img'),
			social:  $('div#global-nav li.social img'),
			owner:   $('div#global-nav li.owner img'),
			cases:   $('div#global-nav li.cases img'),
			recruit: $('div#global-nav li.recruit img'),
			news:    $('div#global-nav li.news img')
		};
		switch(dirCorp) {
			case 'company': var btn = nav.company; break;
			case 'ir'     : var btn = nav.ir; break;
			case 'social' : var btn = nav.social; break;
			case 'owner'  : var btn = nav.owner; break;
			case 'cases'  : var btn = nav.cases; break;
			case 'recruit': var btn = nav.recruit; break;
			case 'news'   : var btn = nav.news; break;
			default: return;
		};
	}
	else {
		var nav = {
			products: $('div#global-nav li.products img'),
			services: $('div#global-nav li.services img'),
			order:    $('div#global-nav li.order img'),
			cmp:      $('div#global-nav li.cmp img'),
			enjoy:    $('div#global-nav li.enjoy img'),
			nanaco:   $('div#global-nav li.nanaco img')
		};
		switch(dir) {
			case 'products': var btn = nav.products; break;
			case 'services': var btn = nav.services; break;
			case 'order'   : var btn = nav.order; break;
			case 'cmp'     : var btn = nav.cmp; break;
			case 'enjoy'   : var btn = nav.enjoy; break;
			case 'nanaco'  : var btn = nav.nanaco; break;
			default: return;
		};
	};
	
	var src = $(btn).attr('src');
	var ftype = src.substring(src.lastIndexOf('.'), src.length);
	var srcOn = src.replace(ftype, '_on'+ftype);
	$(btn).attr('src',srcOn);
	$(btn).removeClass('btn');
}


/* MM_swapImg / MM_preloadImages
   (Macromedia Dreamweaver Script)
--------------------------------------*/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


/*　MM_openBrWindow
   (Macromedia Dreamweaver Script)
--------------------------------------*/
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


/*　Flashプラグイン（バージョン8以上）を検知
   (Macromedia Dreamweaver Script)
--------------------------------------*/
var MM_contentVersion = 8;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
	var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	for (var i = 0; i < words.length; ++i) {
		if (isNaN(parseInt(words[i]))) continue;
		var MM_PluginVersion = words[i]; 
	}
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
} else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<scr' + 'ipt language=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</scr' + 'ipt\> \n');
}


/* ポップアップ
--------------------------------------*/
// 本部地図
function winopenCorpMap() {
	var subopen = null;
	subopen=window.open("","sw02","width=625,height=600,left=0,top=0,toolbar=1,location=0,directories=0,scrollbars=1,resizable=1,menubar=1");
	if (window.focus) subopen.focus();
}

// 社会・環境　キッズコンテンツ
function kidsPopup(element) {
	var uri = element.href;
	var subopen = null;
	subopen=window.open(uri,"kids","width=731,height=586,left=0,top=0,toolbar=0,location=0,directories=0,menubar=0,status=0,scrollbars=0,resizable=1");
	if (window.focus) subopen.focus();
}


/* ロールオーバースクリプト
   Standards Compliant Rollover Script
   Author : Daniel Nolan
   http://www.bleedingego.co.uk/webdev.php
--------------------------------------*/
function initRollovers() {
	if (!document.getElementById) return
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');
	for (var i=0, l=aImages.length; i<l; i++) {		
		if (aImages[i].className == 'btn') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var srcOn = src.substring(src.length-(ftype.length+3), src.length);
			if (srcOn == '_on'+ftype) return;
			var hsrc = src.replace(ftype, '_on'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}


/* ウィンドウ読み込み時の処理
--------------------------------------*/
$(function() {
	gNavCurrent();
	initRollovers();
});
window.onunload = function() {
 createCookie("font_size", fontSize.value);
}
