/* ====================================================================
 Name        : 
 Description : jQuery と prototype コンフリクト回避
==================================================================== */
jQuery.noConflict();
var $j = jQuery;
var top_url = 'http://' + location.hostname + '/files/welcome/starter_no/index.php';
//var top_url = 'http://' + location.hostname + '/o-date/jcom/100618_cookie/starter_no/index.php';
var cookie_name = 'j_area';

/* ====================================================================
 Name        : 
 Description : 加入ガイドリダイレクト処理
==================================================================== */
$j(document).ready(function(){
	var v = new Area();
	var dir;
	var cookie_dir;
	var ref = document.referrer;
	var now = location.href;
	var saved_jcom_cookie;
	var jcom_cookie = v.getCookie('jcom_cookie');

	if (jcom_cookie) {
		var tmp = jcom_cookie.split("=");
		saved_jcom_cookie = tmp[1];
	}
				
	dir = v.getDir(now);

	// リファラーが加入ガイドトップでお得プランに飛んできた場合
	if(ref.indexOf('starter_no') != -1) {
		v.setCookie(cookie_name, dir);
	}
/*
	else if ((ref.indexOf('cookie.php') != -1) && (now.indexOf('otoku') != -1)) {
		if (v.links[saved_jcom_cookie]) {
			var next = v.links[saved_jcom_cookie];
			var regexp = /starter_(.*)\//;
			var text = next.match(regexp);
			// 遷移予定のcookieをセット
			var cookie_dir = 'starter_' + text[1];
			
			v.setCookie(cookie_name, cookie_dir);
			
			// debug
			next = next.replace('www.jcom.co.jp/files/welcome/', location.hostname + '/files/welcome/');
			next = next.replace('index', 'otoku/index');

			if (cookie_dir && (cookie_dir != "undefined") && (cookie_dir != dir)) {
				location.href = next;
				return false;
			}
		}
		else {
//			v.setCookie(cookie_name, dir);
			resetArea();
		}
	}		
*/
	else if ((ref.indexOf(location.hostname + '/index') != -1) && (now.indexOf('otoku') != -1)) {
		if (v.links[saved_jcom_cookie]) {
			var next = v.links[saved_jcom_cookie];
			var regexp = /starter_(.*)\//;
			var text = next.match(regexp);
			// 遷移予定のcookieをセット
			var cookie_dir = 'starter_' + text[1];
			
			v.setCookie(cookie_name, cookie_dir);
			
			// debug
			next = next.replace('www.jcom.co.jp/files/welcome/', location.hostname + '/files/welcome/');
			next = next.replace('index', 'otoku/index');

			if (cookie_dir && (cookie_dir != "undefined") && (cookie_dir != dir)) {
				location.href = next;
				return false;
			}
		}
		else {
//			v.setCookie(cookie_name, dir);
			resetArea();
		}
	}		
	else if (ref == 'http://' + location.hostname + '/' && (now.indexOf('otoku') != -1)) {
		if (v.links[saved_jcom_cookie]) {
			var next = v.links[saved_jcom_cookie];
			var regexp = /starter_(.*)\//;
			var text = next.match(regexp);
			// 遷移予定のcookieをセット
			var cookie_dir = 'starter_' + text[1];
			
			v.setCookie(cookie_name, cookie_dir);
			
			// debug
			next = next.replace('www.jcom.co.jp/files/welcome/', location.hostname + '/files/welcome/');
			next = next.replace('index', 'otoku/index');

			if (cookie_dir && (cookie_dir != "undefined") && (cookie_dir != dir)) {
				location.href = next;
				return false;
			}
		}
		else {
//			v.setCookie(cookie_name, dir);
			resetArea();
		}
	}		

	// リファラーがarea_select/welcome_starter.phpの場合
//	else if (ref.indexOf('welcome_starter') != -1) {
	else if (ref.indexOf(location.hostname + '/top/iframe.html') != -1) {
		if (v.links[saved_jcom_cookie]) {
			var next = v.links[saved_jcom_cookie];
	
			var regexp = /starter_(.*)\//;
			var text = next.match(regexp);
			// 遷移予定のcookieをセット
			var cookie_dir = 'starter_' + text[1];
			
			v.setCookie(cookie_name, cookie_dir);
			
			// debug
			next = next.replace('www.jcom.co.jp/files/welcome/', location.hostname + '/files/welcome/');

			if (cookie_dir && (cookie_dir != "undefined") && (cookie_dir != dir)) {
				location.href = next;
				return false;
			}
		}
		else {
//			v.setCookie(cookie_name, dir);
			resetArea();
		}
	}	
	

	
	else if (ref.indexOf('cookie.php') != -1) {
		if (v.links[saved_jcom_cookie]) {
			var next = v.links[saved_jcom_cookie];
			var regexp = /starter_(.*)\//;
			var text = next.match(regexp);
			// 遷移予定のcookieをセット
			var cookie_dir = 'starter_' + text[1];
			
			v.setCookie(cookie_name, cookie_dir);
			
			// debug
			next = next.replace('www.jcom.co.jp/files/welcome/', location.hostname + '/files/welcome/');

			if (cookie_dir && (cookie_dir != "undefined") && (cookie_dir != dir)) {
				location.href = next;
				return false;
			}
		}
		else {
//			v.setCookie(cookie_name, dir);
			resetArea();
		}
	}		


	// リファラーがstarter_no/otokuの場合(otoku/index.phpのみ)
	else if ((ref.indexOf('starter_no/otoku') != -1) && (now.indexOf('otoku') != -1)) {
		v.setCookie(cookie_name, dir);
	}		
	cookie_dir = v.getCookie(cookie_name);
		
	if (cookie_dir && (cookie_dir != "undefined") && (cookie_dir != dir)) {
		url = now.replace(dir, cookie_dir);
		location.href = url;
		return false;
	}
});

// cookieを削除
var resetArea = function() {
	document.cookie = cookie_name + "=; path=/; expires=Thu, 1-Jan-2000 00:00:00 GMT";
	location.href = top_url;
}



// エリア関連クラス
function Area() {

	// thisのエイリアス
 	outer = this;
}
	
Area.prototype = {

	links : {'sumida': 'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'tokorozawa': 'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'kanagawa-central': 'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'urayasu': 'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'ibaraki':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'kisarazu':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'sendai':	'http://www.jcom.co.jp/files/welcome/starter_sk/index.html',
			'chofu-setagaya':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'itabashi':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'taito':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'tokyo':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'edogawa':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'mediasaitama':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'shonan':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'nishitokyo':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'higashikanto':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'yyyachiyo':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'setamachi':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'yokohama':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'sagamihara-yamato':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'gunma':	'http://www.jcom.co.jp/files/welcome/starter_sk/index.html',
			'tojo':	'http://www.jcom.co.jp/files/welcome/starter_a/index.html',
			'noselected':	'http://www.jcom.co.jp/files/welcome/starter_no/index.html',
			'fukuoka':	'http://www.jcom.co.jp/files/welcome/starter_sk/index.html',
			'kitakyushu':	'http://www.jcom.co.jp/files/welcome/starter_sk/index.html',
			'sapporo':	'http://www.jcom.co.jp/files/welcome/starter_sk/index.html',
			'cablevision':	'http://www.jcom.co.jp/files/welcome/starter_sk/index.html',
			'shimonoseki':	'http://www.jcom.co.jp/files/welcome/starter_sk/index.html',
			'hokusetsu':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'toyonaka-ikeda':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'kansai':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'kobe-miki':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'takatsuki':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'kobe-ashiya':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'higashiosaka':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'osaka-central':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'kitakawachi':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'suita':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php',
			'miyavision':	'http://www.jcom.co.jp/files/welcome/starter_w/index.php'
			},
				
	//ディレクトリ名を取得
	getDir: function(now) {
		if(now.indexOf('starter_no') != -1) {
			dir = 'starter_no';
		}
		else if (now.indexOf('starter_a') != -1) {
			dir = 'starter_a';	
		}
		else if (now.indexOf('starter_w') != -1) {
			dir = 'starter_w';	
		}
		else if (now.indexOf('starter_sk') != -1) {
			dir = 'starter_sk';	
		}
		else {
			dir = 'starter_no';	
		}
				
		return dir;
	},

	// cookieを保存	
	setCookie: function(cookie_name, value) {

		if (value == 'starter_no') {
			return;
		}
		document.cookie = cookie_name + "=" + value + "; path=/; expires=Thu, 1-Jan-2030 00:00:00 GMT";
			
		return dir;
	},
	
	// cookieを取得
	getCookie: function(cookie_name) {
        var st="";
        var ed="";
        if (document.cookie.length>0){
            st=document.cookie.indexOf(cookie_name + "=");
            if (st!=-1){ 
                st=st+cookie_name.length+1;
                ed=document.cookie.indexOf(";",st);
                if (ed==-1) ed=document.cookie.length;
                return unescape(document.cookie.substring(st,ed));
            } 
        }
        return "";
	}
}


/* ====================================================================
 Name        : 
 Description : 開閉式メニュー
==================================================================== */
$j(document).ready(function(){
	$j(".toggle_container").hide();
	$j("div.trigger").toggle(function(){
		$j(this).addClass("active"); 
		}, function () {
		$j(this).removeClass("active");
	});
	
	$j("div.trigger").click(function(){
		$j(this).next(".toggle_container").slideToggle("slow,");
	});

});

/* ====================================================================
 Name        : rollOver
 Description : 画像ロールーバー
==================================================================== */
/**
 * rollOver on jQuery
 * rollOver tag:img,input
 * rollOver class:Over
 * rollOver FileName:*_o.*
 * Last modify:20081210
 * Licensed:MIT License
 * @author AkiraNISHIJIMA(http://nishiaki.probo.jp/)
 */
function rollOver(){
    var preLoad = new Object();
    $j('img.over,input.over').not("[src*='_o.']").each(function(){
        var imgSrc = this.src;
        var fType = imgSrc.substring(imgSrc.lastIndexOf('.'));
        var imgName = imgSrc.substr(0, imgSrc.lastIndexOf('.'));
        var imgOver = imgName + '_o' + fType;
        preLoad[this.src] = new Image();
        preLoad[this.src].src = imgOver;
        $j(this).hover(
            function (){
                this.src = imgOver;
            },
            function (){
                this.src = imgSrc;
            }
        );
    });
}
$j(document).ready(rollOver);

/* ====================================================================
 Name        : 
 Description : スムーズスクロール
==================================================================== */
$j(function () {
	if (! $j.browser.safari) {
		$j('#pagetop').click(function () {
			$j(this).blur();
			$j('html,body').animate({ scrollTop: 0 }, 'slow');
			return false;
		});
	}
});


/* ====================================================================
 Name        : 
 Description : 加入ガイドタブ切り替え
==================================================================== */

var navi01On = '<img src="/files/welcome/common/images/top/tab_kodate.jpg" alt="" width="377" height="50" />';
var navi02On = '<img src="/files/welcome/common/images/top/tab_syugou.jpg" alt="" width="378" height="50" />';

var navi01Off = '<a href="javascript:void(0);" onclick="navi01();" title=""><img src="/files/welcome/common/images/top/tab_kodate.jpg" alt="" width="377" height="50" /></a>';
var navi02Off = '<a href="javascript:void(0);" onclick="navi02();" title=""><img src="/files/welcome/common/images/top/tab_syugou.jpg" alt="" width="378" height="50" /></a>';

var navi01Btn;
var navi02Btn;

var content01;
var content02;

function getNavi() {
	navi01Btn = document.getElementById('navi01');
	navi02Btn = document.getElementById('navi02');
}

function getContent() {
	content01 = document.getElementById('content01');
	content02 = document.getElementById('content02');
}

function navi01() {
	getNavi();
	navi01Btn.innerHTML = navi01On;
	navi02Btn.innerHTML = navi02Off;

	getContent();
	content01.className = 'main';
	content02.className = 'hide';

}

function navi02() {
	getNavi();
	navi01Btn.innerHTML = navi01Off;
	navi02Btn.innerHTML = navi02On;

	getContent();
	content01.className = 'hide';
	content02.className = 'main';
	
}
