window.onload = function() {
	init();
}

function adjust_height() {
	var minimumHeight = 320;
	var height = getWinHeight() - ($('header_container').clientHeight) - 20; //20 means height of footer
	if (height < minimumHeight) {
		height = minimumHeight;
	}
	
	$('content').style.height = $('main').style.height = height + 'px';

	$('back_to_kingdom').style.top = height - 140 + 'px';
	
	if ( String(window.location).match('LatestNews') ) {
		height += 120;
		$('news_display').style.top = height * -1 + 'px'; 
		$('news_display').style.height = height - 30 + 'px';
		$('news').style.height = height + 'px';
		$('news_close_l').style.top = height - 30 + 'px';
	}
}

function getWinTop () {
	var winTop;
	if ( BrowserDetect.browser == 'Explorer' ) {
		winTop = document.body.scrollTop;
	} else {
		winTop = self.pageYOffset;
	}
	//alert('winTop:'+winTop);
	return winTop;
}
function getWinHeight () {
	var winHeight;
	if ( BrowserDetect.browser == 'Explorer' ) {
		//http://d.hatena.ne.jp/onozaty/20060802/p1
		if ( BrowserDetect.version >= 7 ) {
			winHeight = document.documentElement.clientHeight;
		} else {
			winHeight = document.body.clientHeight;
		}
	} else {
		winHeight = window.innerHeight;
	}
	//alert('winHeight:'+winHeight);
	return winHeight;
}

var wait_timer;
var fade_timer;
var t;
function show_submenu(menu) {
	hide_all_menu();
	$('submenu_' + menu).style.visibility = 'visible';
	clearTimeout(wait_timer);
}
function hide_all_menu() {
	var m = new Array('Information', 'ShopDetails', 'CastDetails'); // , 'GrandMenu' , 'ShopSystem'
	for (var i=0; i < m.length; i++) {
		$('submenu_' + m[i]).style.visibility = 'hidden';
	}
}
function wait_submenu(menu) {
	t = 80;
	wait_timer = setTimeout("fade_submenu(\""+menu+"\")", 500);
}

function fade_submenu(menu) {
	clearTimeout(wait_timer);
	t -= 20;
	$('submenu_' + menu).style.opacity = t / 100;
	$('submenu_' + menu).style.mozOpacity = t / 100;
	$('submenu_' + menu).style.filter = "alpha(opacity=" + t + ")";
	
	if (t <= 0) {
		clearTimeout(fade_timer);
		$('submenu_' + menu).style.visibility = 'hidden';
		$('submenu_' + menu).style.opacity = 0.9;
		$('submenu_' + menu).style.mozOpacity = 0.9;
		$('submenu_' + menu).style.filter = "alpha(opacity=90)";
	} else {
		setTimeout("fade_submenu(\""+menu+"\")", 50);
	}
}
function killTimer() {
	clearTimeout(wait_timer);
}

function init() {
/*
	if ( String(window.location).match('ContactUs.html') ) {
		saveFormContent_init();
	}
*/
	
	adjust_height();
	window.onresize = adjust_height;
	
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
	header_flash();
	
	$('menu_Information').onmouseover = function() {
		show_submenu('Information');
	}
	$('menu_Information').onmouseout = function() {
		wait_submenu('Information');
	}
	$('menu_ShopDetails').onmouseover = function() {
		show_submenu('ShopDetails');
	}
	$('menu_ShopDetails').onmouseout = function() {
		wait_submenu('ShopDetails');
	}
	$('menu_CastDetails').onmouseover = function() {
		show_submenu('CastDetails');
	}
	$('menu_CastDetails').onmouseout = function() {
		wait_submenu('CastDetails');
	}
	/*
	$('menu_GrandMenu').onmouseover = function() {
		show_submenu('GrandMenu');
	}
	$('menu_GrandMenu').onmouseout = function() {
		wait_submenu('GrandMenu');
	}
	*/
	
	/*
	$('menu_ShopSystem').onmouseover = function() {
		show_submenu('ShopSystem');
	}
	$('menu_ShopSystem').onmouseout = function() {
		wait_submenu('ShopSystem');
	}
	*/
	
	$('submenu_Information').onmouseover =
	$('submenu_ShopDetails').onmouseover =
	$('submenu_CastDetails').onmouseover =
	//$('submenu_GrandMenu').onmouseover = 
	//$('submenu_ShopSystem').onmouseover = 
	function() {
		killTimer();
	}
	$('submenu_Information').onmouseout = function() {
		wait_submenu('Information');
	}
	$('submenu_ShopDetails').onmouseout = function() {
		wait_submenu('ShopDetails');
	}
	$('submenu_CastDetails').onmouseout = function() {
		wait_submenu('CastDetails');
	}
	/*
	$('submenu_GrandMenu').onmouseout = function() {
		wait_submenu('GrandMenu');
	}
	*/
	/*
	$('submenu_ShopSystem').onmouseout = function() {
		wait_submenu('ShopSystem');
	}
	*/
	
	//for LatestNews
	if ( String(window.location).match('LatestNews') ) {
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			var relAttribute = String( anchor.getAttribute('rel') );
			if ( (relAttribute.toLowerCase().match('news')) && anchor.getAttribute('href').match(/news\/(\d{6}-\d+)\.html$/) ) {
				anchor.setAttribute('href', '#'+RegExp.$1);
				anchor.onclick = function() {
					var id = ( String(this).match(/#(\d{6}-\d+)$/) )[1];
					news_load(id);
					//return false;
				}
			}
		}
		//if ( location.search && location.search.match(/^\?(\d{6})$/) ) {
		if ( String(window.location).match(/#(\d{6}-\d+)$/) ) {
			var id = RegExp.$1;
			news_load(id);
		}
		//$('news_container').onclick = function() {
		//	news_close();
		//}
		//$('news_container').setAttribute('title','click to close');
	}
	
	//if ( !String(window.location).match('.html') || String(window.location).match('index.html') ) {
	if ( String(window.location).match('AccessMap.html') ) {
		//<![CDATA[
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(34.65960092805431,135.50598800182343),16);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
		map.setMapType(G_NORMAL_MAP);
		var markeropts = new Object();
		markeropts.title = "天文観測Diner アフィリア・スターズ";
		var marker = new GMarker(new GPoint(139.732912,35.663311), markeropts);
		var html = '<div style="text-align:center; font-weight:bold; padding-top:5px; vartical-align:middle; color:#000; white-space:nowrap;">天体観測Diner アフィリア・スターズ<br />東京都港区六本木3-11-10 ココ六本木ビル6F<br /><span style="color:#f00;">地下鉄5番出口すぐ</span></div>';
		GEvent.addListener(marker, "click", function () {
			marker.openInfoWindowHtml(html);
		});
		map.addOverlay(marker);
		marker.openInfoWindowHtml(html);
		//]]>
	}
	
	
}

function header_flash() {
	if ( String(window.location).match('.html') && !String(window.location).match('index.html') ) {
		$('header_flash').innerHTML = '<img src="img/header.jpg" alt="" />';
		//$('header_flash').style.backgroundImage = 'url(img/header_flash_h100.jpg)';
	} else {
	//	var so = new SWFObject("cafe_index.swf", "cafe_index", "760", "200", "8", "#000000");
	//	so.useExpressInstall('../lib/expressinstall.swf');
	//	so.write("header_flash");
		$('header_flash').innerHTML = '<img src="img/header.jpg" alt="" />';
	}
}

function header_flash_hide() {
	if ( !String(window.location).match('.html') || String(window.location).match('index.html') ) {
		$('header_flash').innerHTML = '';
	}
}

function news_load(id) {
	new Ajax.Request ('news/' + id + '.html', {
		method: 'get',
		onComplete: news_display 
		});
}

function news_display(httpObj) {
	$('news_display').innerHTML = httpObj.responseText;
	$('news_container').style.visibility = 'visible';
	new Rico.Effect.Position("news", null, '0', 100, 10);
}

function news_close() {
	var h = Number( ( $('news_display').style.height ).replace('px','') ) * -1;
	new Rico.Effect.Position("news", null, h, 100, 10, { complete:news_hide });
	return false;
}

function news_hide() {
	$('news_container').style.visibility = 'hidden';
}

function scrollToTop() {
	$('main').scrollTop = 0;
}

