var map = null;
var loadingGIF = '/images/ajax-loader.gif';
var loadingGIFLarge = '/images/ajax-loader-large.gif';
var loadingGIFBar = '/images/ajax-loader-bar.gif';

//  システム時刻の設定
var dateObj = new Date();
var year = dateObj.getFullYear();
var month = dateObj.getMonth()+1;
var day = dateObj.getDate();
var hour = dateObj.getHours();

//サイドバーの背景画像、色指定
var areaImgSel = "/images/panel-title-bg.gif";		//県エリアのマウス通過時の背景色
var areaImgUnSel = "/images/panel-title-light-bg.gif";		//県エリアのマウス非通過時の背景色


var areaColSel = "#000099";
var areaColUnSel ="#0000F2";

var prefColSel = "#75DDFF";		//県エリアのマウス通過時の背景色
var prefColUnSel = "#A4E9FF";		//県エリアのマウス非通過時の背景色

function show(area) {

  var areaInf = new AreaInfo(area);

  if(!areaInf.html){
    return;
  }

  loadCourseInfo(areaInf.html);

  showWeather(area);

  loadMapInfo(area);

  showYouTube(area);
}

function loadCourseInfo(filename){

  var infId = "courseinfo";

	cbFunc = {
		success:function(httpObj){
      replaceInnerHTML(infId, httpObj.responseText);
		},
		failure:function(httpObj){
      replaceInnerHTML(infId, 'サーキット情報の読み込みに失敗しました。');
		},
		timeout: 15000
	}

  var loadingImg = '<img src="' + loadingGIFLarge + '" style="margin-left:230px; margin-top:80px;" />';
  replaceInnerHTML(infId, loadingImg);

  connectObj = YAHOO.util.Connect.asyncRequest("get", filename, cbFunc, null);

}

function loadMapInfo(areacode){

  var mapId = "map";

	cbFunc = {
		success:function(httpObj){
      var mapArray = httpObj.responseText.split(':');

      // 地図情報の設定
      lat  = mapArray[0];
      lon  = mapArray[1];
      scale = parseInt(mapArray[2]);

      // マーカーオブジェクトの設定
      var icon = new GIcon();
      icon.image = "/images/gmapico_karting.png";
      icon.iconSize = new GSize(32, 37);
      icon.iconAnchor = new GPoint(16, 37);
      var markeropts = new Object();
      markeropts.icon = icon;

      //地図の表示
      if (!map){
        //初回は地図をロードする
        gmap_load('map', lat, lon, scale, markeropts);

      }else{
        //2回目以降は地図の中心点を移動
        map.clearOverlays();

        var point = new GLatLng(lat, lon);

        map.setCenter(point, scale);
        map.addOverlay(new GMarker(point, markeropts));
      }

		},
		failure:function(httpObj){
      map = null;
      replaceInnerHTML(mapId, '地図情報の読み込みに失敗しました。');
		},
		timeout: 10000
	}

  connectObj = YAHOO.util.Connect.asyncRequest("get", "./getmapinfo.php?ccd=" + areacode, cbFunc, null);

}

function gmap_load(id, lat, lng, scale, markeropts) {
  if (! GBrowserIsCompatible()) return;

  var html="<div style='width:370px; height:20px; border-left: 3px #0000FF solid; border-bottom: 1px #0000FF dashed; background: #ECFBFF; font-size:12px; padding-top:3px;'>";
  html += "<span style='padding-left:20px; vertical-align:middle;'>サーキット周辺の地図</span>";
  html += "</div>";

  replaceInnerHTML('maptitle', html);

  var loadingImg = '<img src="' + loadingGIFLarge + '" style="margin-left:175px; margin-top:175px;" />';
  replaceInnerHTML(id, loadingImg);

  map = new GMap2(document.getElementById(id));

  var point = new GLatLng(lat, lng);

  map.setCenter(point, scale);
  map.addOverlay(new GMarker(point, markeropts));
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  map.setMapType(G_NORMAL_MAP);

  var helpStr = '<span style="padding-top:5px; padding-left:5px; font-size:12px; vertical-align:bottom;">※地図上でマウスドラッグすると地図がスクロールします</span>';
  replaceInnerHTML('maphelp', helpStr);
}

function showWeather(areacode){

  var weatherId = "weather";

	cbFunc = {
		success:function(httpObj){
      replaceInnerHTML(weatherId, httpObj.responseText);
		},
		failure:function(httpObj){
      replaceInnerHTML(weatherId, '天気情報を取得できませんでした。');
		},
		timeout: 20000
	}

  var loadingImg = '<img src="' + loadingGIF + '" style="margin-left:100px; margin-top:50px;" />';
  replaceInnerHTML(weatherId, loadingImg);

  connectObj = YAHOO.util.Connect.asyncRequest("get", "./getweather.php?ccd=" + areacode, cbFunc, null);

}

function showYouTube(areacode){

  var youtubeId = "youtube";

	cbFunc = {
		success:function(httpObj){
      replaceInnerHTML(youtubeId, httpObj.responseText);
		},
		failure:function(httpObj){
      replaceInnerHTML(youtubeId, '<div style="margin:30px auto;">動画を取得できませんでした。</div>');
		},
		timeout: 25000
	}

  var loadingImg = '<img src="' + loadingGIFBar + '" style="margin:40px auto;" />';
  replaceInnerHTML(youtubeId, loadingImg);

  connectObj = YAHOO.util.Connect.asyncRequest("get", "./getyoutube.php?ccd=" + areacode, cbFunc, null);

}

function AreaInfo(circuitName){

  // ******** 北海道
  if( circuitName == 'ark'){
    this.html = './courseinfo/hokkaido/ark.html';
  }
  else if( circuitName == 'chitose'){
    this.html = './courseinfo/hokkaido/chitose.html';
  }
  else if( circuitName == 'akeno'){
    this.html = './courseinfo/hokkaido/akeno.html';
  }
  else if( circuitName == 'kitamifuji'){
    this.html = './courseinfo/hokkaido/kitamifuji.html';
  }
  else if( circuitName == 'uryuu'){
    this.html = './courseinfo/hokkaido/uryuu.html';
  }
  else if( circuitName == 'nanporo'){
    this.html = './courseinfo/hokkaido/nanporo.html';
  }
  else if( circuitName == 'sunagawa'){
    this.html = './courseinfo/hokkaido/sunagawa.html';
  }
  else if( circuitName == 'niyama'){
    this.html = './courseinfo/hokkaido/niyama.html';
  }
  else if( circuitName == 'sapporo'){
    this.html = './courseinfo/hokkaido/sapporo.html';
  }
  // ******** 青森県
  else if( circuitName == 'sp'){
    this.html = './courseinfo/tohoku/aomori/sp.html';
  }
  // ******** 岩手県
  else if( circuitName == 'raspberry'){
    this.html = './courseinfo/tohoku/iwate/raspberry.html';
  }
  else if( circuitName == 'sanmarino'){
    this.html = './courseinfo/tohoku/iwate/sanmarino.html';
  }
  // ******** 宮城県
  else if( circuitName == 'hiland'){
    this.html = './courseinfo/tohoku/miyagi/hiland.html';
  }
  else if( circuitName == 'sugo'){
    this.html = './courseinfo/tohoku/miyagi/sugo.html';
  }
  else if( circuitName == 'utm'){
    this.html = './courseinfo/tohoku/miyagi/utm.html';
  }
  // ******** 秋田県
  else if( circuitName == 'shinkyouwa'){
    this.html = './courseinfo/tohoku/akita/shinkyouwa.html';
  }
  else if( circuitName == 'gosyono'){
    this.html = './courseinfo/tohoku/akita/gosyono.html';
  }
  // ******** 山形県
  else if( circuitName == 'mogamigawa'){
    this.html = './courseinfo/tohoku/yamagata/mogamigawa.html';
  }
  // ******** 福島県
  else if( circuitName == 'bandai'){
    this.html = './courseinfo/tohoku/fukushima/bandai.html';
  }
  else if( circuitName == 'kawamata'){
    this.html = './courseinfo/tohoku/fukushima/kawamata.html';
  }
  // ******** 新潟県
  else if( circuitName == 'mikawa'){
    this.html = './courseinfo/hokuriku/niigata/mikawa.html';
  }
  else if( circuitName == 'nagaoka'){
    this.html = './courseinfo/hokuriku/niigata/nagaoka.html';
  }
  else if( circuitName == 'spn'){
    this.html = './courseinfo/hokuriku/niigata/spn.html';
  }
  // ******** 富山県
  else if( circuitName == 'toprise'){
    this.html = './courseinfo/hokuriku/toyama/toprise.html';
  }
  // ******** 福井県
  else if( circuitName == 'awara'){
    this.html = './courseinfo/hokuriku/fukui/awara.html';
  }
  // ******** 茨城県
  else if( circuitName == 'apple'){
    this.html = './courseinfo/kanto/ibaraki/apple.html';
  } else if( circuitName == 'reon'){
    this.html = './courseinfo/kanto/ibaraki/reon.html';
  } else if( circuitName == 'oharai'){
    this.html = './courseinfo/kanto/ibaraki/oharai.html';
  } else if( circuitName == 'techno'){
    this.html = './courseinfo/kanto/ibaraki/techno.html';
  } else if( circuitName == 'itako'){
    this.html = './courseinfo/kanto/ibaraki/itako.html';
  }
  // ******** 栃木県
  else if( circuitName == 'festika'){
    this.html = './courseinfo/kanto/tochigi/festika.html';
  } else if( circuitName == 'motegi'){
    this.html = './courseinfo/kanto/tochigi/motegi.html';
  } else if( circuitName == 'igashira'){
    this.html = './courseinfo/kanto/tochigi/igashira.html';
  } else if( circuitName == 'modena'){
    this.html = './courseinfo/kanto/tochigi/modena.html';
  } else if( circuitName == 'grande'){
    this.html = './courseinfo/kanto/tochigi/grande.html';
  }
  // ******** 群馬県
  else if( circuitName == 'kanetsu'){
    this.html = './courseinfo/kanto/gunma/kanetsu.html';
  } else if( circuitName == 'haruna'){
    this.html = './courseinfo/kanto/gunma/haruna.html';
  }  else if( circuitName == 'orc'){
    this.html = './courseinfo/kanto/gunma/orc.html';
  }
  // ******** 埼玉県
  else if( circuitName == 'miyazawako'){
    this.html = './courseinfo/kanto/saitama/miyazawako.html';
  }  else if( circuitName == 'chichibu'){
    this.html = './courseinfo/kanto/saitama/chichibu.html';
  }  else if( circuitName == 'akigase'){
    this.html = './courseinfo/kanto/saitama/akigase.html';
  }  else if( circuitName == 'hanyuu'){
    this.html = './courseinfo/kanto/saitama/hanyuu.html';
  }  else if( circuitName == 'rahanno'){
    this.html = './courseinfo/kanto/saitama/rahanno.html';
  }
  // ******** 千葉県
  else if( circuitName == 'mobara'){
    this.html = './courseinfo/kanto/chiba/mobara.html';
  }
  else if( circuitName == 'lakeside'){
    this.html = './courseinfo/kanto/chiba/lakeside.html';
  }
  else if( circuitName == 'newtokyo'){
    this.html = './courseinfo/kanto/chiba/newtokyo.html';
  }
  else if( circuitName == 'minami-chiba'){
    this.html = './courseinfo/kanto/chiba/minami-chiba.html';
  }
  else if( circuitName == 'harbor'){
    this.html = './courseinfo/kanto/chiba/harbor.html';
  }
  else if( circuitName == 'neo'){
    this.html = './courseinfo/kanto/chiba/neo.html';
  }
  // ******** 東京都
  else if( circuitName == 'citykart'){
    this.html = './courseinfo/kanto/tokyo/citykart.html';
  }
  // ******** 神奈川県
  else if( circuitName == 'hiratsuka'){
    this.html = './courseinfo/kanto/kanagawa/hiratsuka.html';
  } else if( circuitName == 'chuofujino'){
    this.html = './courseinfo/kanto/kanagawa/fujino.html';
  } else if( circuitName == 'nakaiinter'){
    this.html = './courseinfo/kanto/kanagawa/nakai.html';
  } else if( circuitName == 'ohoimatsuda'){
    this.html = './courseinfo/kanto/kanagawa/ohoimatsuda.html';
  }
  // ******** 山梨県
  else if( circuitName == 'az'){
    this.html = './courseinfo/koushinetsu/yamanashi/az.html';
  }
  // ******** 長野県
  else if( circuitName == 'azumino'){
    this.html = './courseinfo/koushinetsu/nagano/azumino.html';
  }
  else if( circuitName == 'f1park'){
    this.html = './courseinfo/koushinetsu/nagano/f1park.html';
  }
  else if( circuitName == 'otonari'){
    this.html = './courseinfo/koushinetsu/nagano/otonari.html';
  }
  // ******** 岐阜県
  else if( circuitName == 'mizunami'){
    this.html = './courseinfo/toukai/gihu/mizunami.html';
  }
  // ******** 静岡県
  else if( circuitName == 'shiraito'){
    this.html = './courseinfo/toukai/shizuoka/shiraito.html';
  }
  else if( circuitName == 'hamana'){
    this.html = './courseinfo/toukai/shizuoka/hamana.html';
  }
  else if( circuitName == 'gotenba'){
    this.html = './courseinfo/toukai/shizuoka/gotenba.html';
  }
  else if( circuitName == 'fsw'){
    this.html = './courseinfo/toukai/shizuoka/fsw.html';
  }
  else if( circuitName == 'tsumagoi'){
    this.html = './courseinfo/toukai/shizuoka/tsumagoi.html';
  }
  // ******** 愛知県
  else if( circuitName == 'mihama'){
    this.html = './courseinfo/toukai/aichi/mihama.html';
  }
  else if( circuitName == 'ishino'){
    this.html = './courseinfo/toukai/aichi/ishino.html';
  }
  else if( circuitName == 'toyota'){
    this.html = './courseinfo/toukai/aichi/toyota.html';
  }
  else if( circuitName == 'yrp'){
    this.html = './courseinfo/toukai/aichi/yrp.html';
  }
  else if( circuitName == 'supa'){
    this.html = './courseinfo/toukai/aichi/supa.html';
  }
  // ******** 三重県
  else if( circuitName == 'rainbow'){
    this.html = './courseinfo/toukai/mie/rainbow.html';
  }
  else if( circuitName == 'nemu'){
    this.html = './courseinfo/toukai/mie/nemu.html';
  }
  else if( circuitName == 'suzuka'){
    this.html = './courseinfo/toukai/mie/suzuka.html';
  }
  // ******** 滋賀県
  else if( circuitName == 'biwako'){
    this.html = './courseinfo/kinki/shiga/biwako.html';
  }
  // ******** 京都府
  else if( circuitName == 'yasubaya'){
    this.html = './courseinfo/kinki/kyoto/yasubaya.html';
  }
  // ******** 大阪府
  else if( circuitName == 'maishima'){
    this.html = './courseinfo/kinki/osaka/maishima.html';
  }
  else if( circuitName == 'ikoma'){
    this.html = './courseinfo/kinki/osaka/ikoma.html';
  }
  // ******** 兵庫県
  else if( circuitName == 'miki'){
    this.html = './courseinfo/kinki/hyougo/miki.html';
  }
  else if( circuitName == 'kitakoube'){
    this.html = './courseinfo/kinki/hyougo/kitakoube.html';
  }
  else if( circuitName == 'takarazuka'){
    this.html = './courseinfo/kinki/hyougo/takarazuka.html';
  }
  else if( circuitName == 'dream'){
    this.html = './courseinfo/kinki/hyougo/dream.html';
  }
  else if( circuitName == 'capital'){
    this.html = './courseinfo/kinki/hyougo/capital.html';
  }
  else if( circuitName == 'hachikita'){
    this.html = './courseinfo/kinki/hyougo/hachikita.html';
  }
  else if( circuitName == 'kannabe'){
    this.html = './courseinfo/kinki/hyougo/kannabe.html';
  }
  else if( circuitName == 'inagawa'){
    this.html = './courseinfo/kinki/hyougo/inagawa.html';
  }
  else if( circuitName == 'tsuchiyama'){
    this.html = './courseinfo/kinki/hyougo/tsuchiyama.html';
  }
  // ******** 奈良県
  else if( circuitName == 'hanna'){
    this.html = './courseinfo/kinki/nara/hanna.html';
  }
  // ******** 和歌山県
  else if( circuitName == 'kinokuni'){
    this.html = './courseinfo/kinki/wakayama/kinokuni.html';
  }
  // ******** 鳥取県
  else if( circuitName == 'tottoripl'){
    this.html = './courseinfo/tyuugoku/tottori/tottoripl.html';
  }
  // ******** 岡山県
  else if( circuitName == 'okayama-inter'){
    this.html = './courseinfo/tyuugoku/okayama/okayama-inter.html';
  }
  else if( circuitName == 'nakayama'){
    this.html = './courseinfo/tyuugoku/okayama/nakayama.html';
  }
  // ******** 広島県
  else if( circuitName == 'kartpista'){
    this.html = './courseinfo/tyuugoku/hiroshima/kartpista.html';
  }
  else if( circuitName == 'kouraku'){
    this.html = './courseinfo/tyuugoku/hiroshima/kouraku.html';
  }
  else if( circuitName == 'tamada'){
    this.html = './courseinfo/tyuugoku/hiroshima/tamada.html';
  }
  else if( circuitName == 'ts-takata'){
    this.html = './courseinfo/tyuugoku/hiroshima/ts-takata.html';
  }
  else if( circuitName == 'dqkp'){
    this.html = './courseinfo/tyuugoku/hiroshima/dqkp.html';
  }
  // ******** 山口県
  else if( circuitName == 'yanai'){
    this.html = './courseinfo/tyuugoku/yamaguchi/yanai.html';
  }
  else if( circuitName == 'natura'){
    this.html = './courseinfo/tyuugoku/yamaguchi/natura.html';
  }
  // ******** 香川県
  else if( circuitName == 'shikoku'){
    this.html = './courseinfo/shikoku/kagawa/shikoku.html';
  }
  // ******** 愛媛県
  else if( circuitName == 'tsushima'){
    this.html = './courseinfo/shikoku/ehime/tsushima.html';
  }
  // ******** 高知県
  else if( circuitName == 'moma'){
    this.html = './courseinfo/shikoku/kouchi/moma.html';
  }
  // ******** 福岡県
  else if( circuitName == 'kitakyuusyuu'){
    this.html = './courseinfo/kyuusyuu/fukuoka/kitakyuusyuu.html';
  }
  else if( circuitName == 'fukuoka'){
    this.html = './courseinfo/kyuusyuu/fukuoka/fukuoka.html';
  }
  else if( circuitName == 'koinoura'){
    this.html = './courseinfo/kyuusyuu/fukuoka/koinoura.html';
  }
  // ******** 佐賀県
  else if( circuitName == 'ocean'){
    this.html = './courseinfo/kyuusyuu/saga/ocean.html';
  }
  // ******** 長崎県
  else if( circuitName == 'omurawan'){
    this.html = './courseinfo/kyuusyuu/nagasaki/omurawan.html';
  }
  // ******** 熊本県
  else if( circuitName == 'otachimisaki'){
    this.html = './courseinfo/kyuusyuu/kumamoto/otachimisaki.html';
  }
  // ******** 大分県
  else if( circuitName == 'sonic'){
    this.html = './courseinfo/kyuusyuu/ooita/sonic.html';
  }
  // ******** 鹿児島県
  else if( circuitName == 'maruoka'){
    this.html = './courseinfo/kyuusyuu/kagoshima/maruoka.html';
  }
  else if( circuitName == 'hobby'){
    this.html = './courseinfo/kyuusyuu/kagoshima/hobby.html';
  }
  else if( circuitName == 'osumi'){
    this.html = './courseinfo/kyuusyuu/kagoshima/osumi.html';
  }
  else if( circuitName == 'mahoroba'){
    this.html = './courseinfo/kyuusyuu/kagoshima/mahoroba.html';
  }
  // ******** 沖縄県
  else if( circuitName == 'kukuru'){
    this.html = './courseinfo/okinawa/kukuru.html';
  }
  else if( circuitName == 'ikei'){
    this.html = './courseinfo/okinawa/ikei.html';
  }
  else {
  }

  return this;
}
  //google.setOnLoadCallback(initialize);

function changeLayout(id, layName){

  var infId = "infofigure";

  var layObj = document.getElementById(infId);

  for (i=0;i<layObj.childNodes.length;i++)
  {
    imgObj = layObj.childNodes.item(i);
    imgId = imgObj.getAttribute("id")

    if(imgId != id){
      imgObj.style.display = "none";
    } else {

      replaceInnerHTML('infofigtitle', layName);
      imgObj.style.display = "block";
    }
  }

}


function areaOver(div){
//    setDivBackgroundImage(div, areaImgSel);
//document.getElementById(div).style.color = '#FFFFFF';
//document.getElementById(div).style.cursor = 'pointer';
    setDivBackgroundColor(div, areaColSel);
}

function areaOut(div){
//    setDivBackgroundImage(div, areaImgUnSel);
//document.getElementById(div).style.color = '#000000';
    setDivBackgroundColor(div, areaColUnSel);
}

function prefOver(div){
    setDivBackgroundColor(div, prefColSel);
}

function prefOut(div){
    setDivBackgroundColor(div, prefColUnSel);
}

//
//  In my case I want to load them onload, this is how you do it!
// 

//Event.observe(window, 'load', loadAccordions, false);

//
//	Set up all accordions
//
function loadAccordions() {
	
	var bottomAccordion = new accordion('vertical_container');
	
	var nestedVerticalAccordion01 = new accordion(
    'vertical_nested_container01', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion02 = new accordion(
    'vertical_nested_container02', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion03 = new accordion(
    'vertical_nested_container03', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion04 = new accordion(
    'vertical_nested_container04', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion05 = new accordion(
    'vertical_nested_container05', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion06 = new accordion(
    'vertical_nested_container06', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion07 = new accordion(
    'vertical_nested_container07', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion08 = new accordion(
    'vertical_nested_container08', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

	var nestedVerticalAccordion09 = new accordion(
    'vertical_nested_container09', {
	  classNames : {
			toggle : 'vertical_accordion_toggle',
			toggleActive : 'vertical_accordion_toggle_active',
			content : 'vertical_accordion_content'
		}
	}
  );

}

function initThumbnail() {

  var linkid = 'circuit_web';

  if (document.getElementById(linkid)) {
    var url = document.getElementById(linkid).href;
  }else{
    return;
  }

  var title = '<div style="width:160px; text-align:center;">';
  title += '<img src="http://mozshot.nemui.org/shot?'+url+'" style="width:90px;"/>';
  title += '<br/>' + document.getElementById("infotitlestr").firstChild.nodeValue + '<br/>公式サイト';
  title += '</div>';

  var link_circuit = new YAHOO.widget.Tooltip('lt1',{
      context:linkid,
      text:title,
      autodismissdelay: 8000
  });

}