// JavaScript Document

var map;
var map2;
var baseValue = 6;
var defaultAlpha = 0;

function GetWindowWidthHeight(){
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number'){
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {'width' : myWidth, 'height' : myHeight};
}

function resizeCheck(myEvents){
		sBHeight = GetWindowWidthHeight().height;
		document.getElementById('gmap').style.height = sBHeight+'px';
		sBWidth = GetWindowWidthHeight().width;
		document.getElementById('gmap').style.width = sBWidth+'px';

		sPHeight = sBHeight*0.5;
		document.getElementById('gmap2').style.height = sPHeight+'px';
		sPWidth = sBWidth*0.8;
		document.getElementById('gmap2').style.width = sPWidth+'px';

		sTop = (sBHeight-sPHeight)*0.6;
		document.getElementById('gmap2').style.top = sTop+'px';

		sLeft = (sBWidth-sPWidth)/2;
		document.getElementById('gmap2').style.left = sLeft+'px';

		/*表示領域中心からmap2の中心の差*/
		var dif = (sTop+(sPHeight/2))-(sBHeight/2);
		
		/*ヘッダーボックスセット*/
		var headerHeight = sTop*0.8;
		var headerWidth = sPWidth;
		var scheduleBoxHeight = headerHeight*0.25;
		var miniSquareHeight = headerHeight-scheduleBoxHeight;
		var miniSquare = (sPWidth-headerHeight-10-30)/4;
		
		document.getElementById('logobox').style.height = headerHeight+'px';
		document.getElementById('logobox').style.width = headerHeight+'px';
		document.getElementById('logobox').style.left = sLeft+'px';
		document.getElementById('logo').style.width = headerHeight+'px';

		document.getElementById('scheduletimelinebox').style.width = (sPWidth-headerHeight-10)+'px';
		document.getElementById('scheduletimelinebox').style.height = scheduleBoxHeight-10+'px';
		document.getElementById('scheduletimelinebox').style.left = sLeft+headerHeight+10+'px';
		document.getElementById('scheduletimelinebox').style.top = 40+10+miniSquareHeight+'px';
		
		document.getElementById('categorybox').style.height = miniSquareHeight-10+'px';
		document.getElementById('categorybox').style.width = miniSquare-10+'px';
		document.getElementById('categorybox').style.left = sLeft+headerHeight+10+miniSquare+10+'px';
		document.getElementById('categoryul').style.marginLeft = (miniSquare-10)*0.2/2;

		document.getElementById('areabox').style.height = miniSquareHeight-10+'px';
		document.getElementById('areabox').style.width = miniSquare-10+'px';
		document.getElementById('areabox').style.left = sLeft+headerHeight+10+miniSquare+10+miniSquare+10+'px';
		document.getElementById('areaul').style.marginLeft = (miniSquare-10)*0.2/2;

		document.getElementById('loginbox').style.height = miniSquareHeight-10+'px';
		document.getElementById('loginbox').style.width = miniSquare-10+'px';
		document.getElementById('loginbox').style.left = sLeft+headerHeight+10+miniSquare+10+miniSquare+10+miniSquare+10+'px';
		document.getElementById('login').style.marginLeft = (miniSquare-10)*0.2/2;
		document.getElementById('loginid').style.width = (miniSquare-10)*0.7+'px';
		document.getElementById('loginpass').style.width = (miniSquare-10)*0.7+'px';
		document.getElementById('loginsubmit').style.width = (miniSquare-10)*0.7+'px';

		document.getElementById('schedulebox').style.width = miniSquare-10+'px';
		document.getElementById('schedulebox').style.height = miniSquareHeight-10+'px';
		document.getElementById('schedulebox').style.left = sLeft+headerHeight+10+'px';
		document.getElementById('scheduleul').style.marginLeft = (miniSquare-10)*0.2/2;

		//document.getElementById('ad').style.left = (sBWidth-728)/2+'px';

		document.getElementById('mapcontroll').style.top = sTop+3+'px';
		document.getElementById('mapcontroll').style.left = sLeft+sPWidth-300+'px';

		document.getElementById('searchbox').style.top = sTop+25+'px';
		document.getElementById('searchbox').style.left = sLeft+sPWidth-300+'px';

		document.getElementById('blogbox').style.width = sBWidth+'px';
		document.getElementById('blogbox').style.left = -sBWidth+30+'px';
		document.getElementById('blogbox').style.top = sBHeight-340+'px';
}

window.onresize = resizeCheck;

window.onload = function(){
	sBHeight = GetWindowWidthHeight().height;
	document.getElementById('gmap').style.height = sBHeight+'px';
	sBWidth = GetWindowWidthHeight().width;
	document.getElementById('gmap').style.width = sBWidth+'px';

	sPHeight = sBHeight*0.5;
	document.getElementById('gmap2').style.height = sPHeight+'px';
	sPWidth = sBWidth*0.8;
	document.getElementById('gmap2').style.width = sPWidth+'px';

	sTop = (sBHeight-sPHeight)*0.6;
	document.getElementById('gmap2').style.top = sTop+'px';

	sLeft = (sBWidth-sPWidth)/2;
	document.getElementById('gmap2').style.left = sLeft+'px';

	document.getElementById('satellite').disabled = true;

	/*ヘッダーボックスセット*/
	var headerHeight = sTop*0.8;
	var headerWidth = sPWidth;
	var scheduleBoxHeight = headerHeight*0.25;
	var miniSquareHeight = headerHeight-scheduleBoxHeight;
	var miniSquare = (sPWidth-headerHeight-10-30)/4;
	
	document.getElementById('logobox').style.height = headerHeight+'px';
	document.getElementById('logobox').style.width = headerHeight+'px';
	document.getElementById('logobox').style.left = sLeft+'px';
	document.getElementById('logo').style.width = headerHeight+'px';

	document.getElementById('scheduletimelinebox').style.width = (sPWidth-headerHeight-10)+'px';
	document.getElementById('scheduletimelinebox').style.height = scheduleBoxHeight-10+'px';
	document.getElementById('scheduletimelinebox').style.left = sLeft+headerHeight+10+'px';
	document.getElementById('scheduletimelinebox').style.top = 40+10+miniSquareHeight+'px';
	
	document.getElementById('categorybox').style.height = miniSquareHeight-10+'px';
	document.getElementById('categorybox').style.width = miniSquare-10+'px';
	document.getElementById('categorybox').style.left = sLeft+headerHeight+10+miniSquare+10+'px';
	document.getElementById('categoryul').style.marginLeft = (miniSquare-10)*0.2/2;

	document.getElementById('areabox').style.height = miniSquareHeight-10+'px';
	document.getElementById('areabox').style.width = miniSquare-10+'px';
	document.getElementById('areabox').style.left = sLeft+headerHeight+10+miniSquare+10+miniSquare+10+'px';
	document.getElementById('areaul').style.marginLeft = (miniSquare-10)*0.2/2;

	document.getElementById('loginbox').style.height = miniSquareHeight-10+'px';
	document.getElementById('loginbox').style.width = miniSquare-10+'px';
	document.getElementById('loginbox').style.left = sLeft+headerHeight+10+miniSquare+10+miniSquare+10+miniSquare+10+'px';
	document.getElementById('login').style.marginLeft = (miniSquare-10)*0.2/2;
	document.getElementById('loginid').style.width = (miniSquare-10)*0.7+'px';
	document.getElementById('loginpass').style.width = (miniSquare-10)*0.7+'px';
	document.getElementById('loginsubmit').style.width = (miniSquare-10)*0.7+'px';

	document.getElementById('schedulebox').style.width = miniSquare-10+'px';
	document.getElementById('schedulebox').style.height = miniSquareHeight-10+'px';
	document.getElementById('schedulebox').style.left = sLeft+headerHeight+10+'px';

	//document.getElementById('ad').style.left = (sBWidth-728)/2+'px';

	document.getElementById('mapcontroll').style.top = sTop+3+'px';
	document.getElementById('mapcontroll').style.left = sLeft+sPWidth-300+'px';

	document.getElementById('searchbox').style.top = sTop+25+'px';
	document.getElementById('searchbox').style.left = sLeft+sPWidth-300+'px';

	document.getElementById('blogbox').style.width = sBWidth+'px';
	document.getElementById('blogbox').style.left = -sBWidth+30+'px';
	document.getElementById('blogbox').style.top = sBHeight-340+'px';

	loadgmap();
}

function loadgmap(){
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmap"));
		map.setCenter(new GLatLng(35.659094,139.700759), 18);
		map.setMapType(G_SATELLITE_MAP);
		map.disableDragging();
		map2 = new GMap2(document.getElementById("gmap2"));
	
		/*表示領域中心からmap2の中心の差*/
		var dif = (sTop+(sPHeight/2))-(sBHeight/2);
	
		var rectObj = map.getBounds();
		var mapWidth = rectObj.toSpan().lng();
		var mapWidthRate = mapWidth/sBWidth;
		var mapHeight = rectObj.toSpan().lat();
		var mapHeightRate = mapHeight/sBHeight;
		var map2CenterLat = 35.659094-mapHeightRate*dif;

		map2.setCenter(new GLatLng(map2CenterLat,139.700759), 18);
		map2.setMapType(G_SATELLITE_MAP);
		map2.addControl(new GLargeMapControl());
		gGeo = new GClientGeocoder();

		var msec = (new Date()).getTime();
		httpObj = GXmlHttp.create();
		httpObj.open("get","http://realtimestreet.net/schedule.xml?cash="+msec);
		httpObj.onreadystatechange = function(){
		if(httpObj.readyState == 4){
				document.getElementById('logobox').style.display = 'block';
				document.getElementById('schedulebox').style.display = 'block';
				document.getElementById('categorybox').style.display = 'block';
				document.getElementById('areabox').style.display = 'block';
				document.getElementById('loginbox').style.display = 'block';
				document.getElementById('scheduletimelinebox').style.display = 'block';
				document.getElementById('mapcontroll').style.display = 'block';
				document.getElementById('searchbox').style.display = 'block';
				document.getElementById('info').style.display = 'block';
				document.getElementById('footer').style.display = 'block';
				//document.getElementById('ad').style.display = 'block';
				document.getElementById('blogbox').style.display = 'block';
				document.getElementById('loading').style.display = 'none';
				addMarker();
			}
		}
		httpObj.send(null);

		adObj = GXmlHttp.create();
		adObj.open("get","http://realtimestreet.net/ad.xml?cash="+msec);
		adObj.onreadystatechange = function(){
		if(adObj.readyState == 4){
				addAdMarker();
			}
		}
		adObj.send(null);
	}
}

function addMarker(){
	var xmlData = httpObj.responseXML;
	var pointData = xmlData.getElementsByTagName("point");
	for(var i=0;i<pointData.length;i++){
		var mx = pointData[i].getElementsByTagName("x")[0].firstChild.nodeValue;
		var my = pointData[i].getElementsByTagName("y")[0].firstChild.nodeValue;
		var comment = pointData[i].getElementsByTagName("comment")[0].firstChild.nodeValue;
		var marker = new GMarker(new GLatLng(my,mx));
		var marker2 = new GMarker(new GLatLng(my,mx));
		map.addOverlay(marker2);
		marker.mx = mx;
		marker.my = my;
		marker.comment = comment;
		map2.addOverlay(marker);
		GEvent.addListener(marker,"click",function(marker){
			map2.openInfoWindowHtml(new GLatLng(this.my,this.mx),this.comment);
		});
	}
}

function addAdMarker(){
	var xmlData = adObj.responseXML;
	var pointData = xmlData.getElementsByTagName("client");
	for(var i=0;i<pointData.length;i++){
		var adx = pointData[i].getElementsByTagName("x")[0].firstChild.nodeValue;
		var ady = pointData[i].getElementsByTagName("y")[0].firstChild.nodeValue;
		var adimage = pointData[i].getElementsByTagName("image")[0].firstChild.nodeValue;
		var adurl = pointData[i].getElementsByTagName("url")[0].firstChild.nodeValue;
		customMarkerObj = new GIcon();
		customMarkerObj.image = adimage;
		customMarkerObj.iconSize = new GSize(88,50);
		customMarkerObj.iconAnchor = new GPoint(44,50);
		adMarker = new GMarker(new GLatLng(ady,adx),customMarkerObj);
		adMarker2 = new GMarker(new GLatLng(ady,adx),customMarkerObj);
		adMarker.url = adurl;
		map2.addOverlay(adMarker);
		map.addOverlay(adMarker2);
		GEvent.addListener(adMarker,'click',function(){adMarkerClick(this.url);});
	}
}
function adMarkerClick(url){
	window.open(url,"_blank");
}
function searchPoint(){
	document.getElementById('result').innerHTML = "検索中・・・";
	var adress = document.getElementById("geoName").value;
	gGeo.getLatLng(adress,checkGeo);
}

function checkGeo(point){
	if(point){
		map.setCenter(point,18);
		var dif = (sTop+(sPHeight/2))-(sBHeight/2);
	
		var rectObj = map.getBounds();
		var mapWidth = rectObj.toSpan().lng();
		var mapWidthRate = mapWidth/sBWidth;
		var mapHeight = rectObj.toSpan().lat();
		var mapHeightRate = mapHeight/sBHeight;
		var map2CenterLat = point.lat()-mapHeightRate*dif;

		map2.setCenter(new GLatLng(map2CenterLat,point.lng()), 18);
		document.getElementById('result').innerHTML = "";
	}else{
		document.getElementById('result').innerHTML = "見つかりませんでした。";
	}
}

var NormalFlg = 0;
var SatelliteFlg = 1;
var HybridFlg = 0;

function toNormal(){
	if(NormalFlg == 0){
		map.setMapType(G_NORMAL_MAP);
		map2.setMapType(G_NORMAL_MAP);
		SatelliteFlg = 0;
		NormalFlg = 1;
		HybridFlg = 0;
		document.getElementById('normal').disabled = true;
		document.getElementById('satellite').disabled = false;
		document.getElementById('hybrid').disabled = false;
	}
}

function toSatellite(){
	if(SatelliteFlg == 0){
		map.setMapType(G_SATELLITE_MAP);
		map2.setMapType(G_SATELLITE_MAP);
		SatelliteFlg = 1;
		NormalFlg = 0;
		HybridFlg = 0;
		document.getElementById('normal').disabled = false;
		document.getElementById('satellite').disabled = true;
		document.getElementById('hybrid').disabled = false;
	}
}

function toHybrid(){
	if(HybridFlg == 0){
		map.setMapType(G_HYBRID_MAP);
		map2.setMapType(G_HYBRID_MAP);
		SatelliteFlg = 0;
		NormalFlg = 0;
		HybridFlg = 1;
		document.getElementById('normal').disabled = false;
		document.getElementById('satellite').disabled = false;
		document.getElementById('hybrid').disabled = true;
	}
}

function idFocus(){
	if(document.getElementById('loginid').value == 'メールアドレス'){
		document.getElementById('loginid').value = '';
		document.getElementById('loginid').style.color = '#000000';
	}
}

function idBlur(){
	if(document.getElementById('loginid').value == ''){
		document.getElementById('loginid').value = 'メールアドレス';
		document.getElementById('loginid').style.color = '#D9D9D9';
	}
}

function passFocus(){
	if(document.getElementById('loginpass').value == 'パスワード'){
		document.getElementById('loginpass').value = '';
		document.getElementById('loginpass').style.color = '#000000';
	}
}

function passBlur(){
	if(document.getElementById('loginpass').value == ''){
		document.getElementById('loginpass').value = 'パスワード';
		document.getElementById('loginpass').style.color = '#D9D9D9';
	}
}

function closeDiv(id){
	document.getElementById(id).style.display = 'none';
}

function openWindow(lat,lng,comment){
	map2.openInfoWindowHtml(new GLatLng(lat,lng),comment);
}

function openblogbox(width,height){
	nowValue = 	document.getElementById('blogbox').style.left;
	nowValue = Number(nowValue.replace('px',''));
	setValue = nowValue+(0-nowValue)/baseValue;
	document.getElementById('blogbox').style.left = setValue+'px';
	if((0-nowValue)/baseValue < 1){
		document.getElementById('blogboxbutton').innerHTML = '<a href="javascript:closeblogbox_set();"><img src="http://realtimestreet.net/images/close.gif" alt="路上ライブストリートライブクローズ" /></a>';
		clearInterval(blogboxOpenId);
	}
}

function closeblogbox(width,height){
	nowValue = 	document.getElementById('blogbox').style.left;
	nowValue = Number(nowValue.replace('px',''));
	setValue = nowValue+(-width+30-nowValue)/baseValue;
	document.getElementById('blogbox').style.left = setValue+'px';
	if((-width+30-nowValue)/baseValue > -1){
		document.getElementById('blogbox').style.left = -width+30+'px';
		document.getElementById('blogboxbutton').innerHTML = '<a href="javascript:openblogbox_set();"><img src="http://realtimestreet.net/images/open.png" alt="路上ライブストリートライブオープン" /></a>';
		clearInterval(blogboxCloseId);
	}
}

function openblogbox_set(){
	document.getElementById('blogboxbutton').innerHTML = '<img src="http://realtimestreet.net/images/close.gif" alt="路上ライブストリートライブクローズ" />';
	var setWidth = GetWindowWidthHeight().width;
	var setHeight = GetWindowWidthHeight().height;
	blogboxOpenId = setInterval('openblogbox('+setWidth+','+setHeight+')',1);
}

function closeblogbox_set(){
	document.getElementById('blogboxbutton').innerHTML = '<img src="http://realtimestreet.net/images/open.png" alt="路上ライブストリートライブオープン" />';
	var setWidth = GetWindowWidthHeight().width;
	var setHeight = GetWindowWidthHeight().height;
	blogboxCloseId = setInterval('closeblogbox('+setWidth+','+setHeight+')',1);
}

function closeup(lat,lng,level){
	map2.setCenter(new GLatLng(lat,lng),level);
}

var alphaValue = 10;
var alphaAdd = 2;
var alphaGoal = 6;

function scheduletimelinedisp(time){
	document.getElementById('scheduletimelinecontents').style.width = GetWindowWidthHeight().width+'px';
	document.getElementById('scheduletimelinecontents').style.height = GetWindowWidthHeight().height+'px';
	document.getElementById('scheduletimelinecontents').style.display = 'block';

	defaultAlpha += alphaAdd;
	document.getElementById('scheduletimelinecontents').style.filter = 'alpha(opacity='+defaultAlpha*alphaValue+')';
	document.getElementById('scheduletimelinecontents').style.MozOpacity = defaultAlpha/alphaValue;
	document.getElementById('scheduletimelinecontents').style.opacity = defaultAlpha/alphaValue;

	alphaIntervalId = setInterval('alphaset()',1);
	document.getElementById('scheduletimelinecontents_inner').innerHTML = '<a href="javascript:closeshceduletimelinecontents();" style="position:absolute; top:0; right:0; color:#FFFFFF;">CLOSE</a><iframe src="./time.php?num='+time+'" frameBorder="0" allowtransparency="true"></iframe>';
	document.getElementById('scheduletimelinecontents_inner').style.width = GetWindowWidthHeight().width*0.8+'px';
	document.getElementById('scheduletimelinecontents_inner').style.height = GetWindowWidthHeight().height*0.8+'px';
	document.getElementById('scheduletimelinecontents_inner').style.left = GetWindowWidthHeight().width*0.1+'px';
	document.getElementById('scheduletimelinecontents_inner').style.top = GetWindowWidthHeight().height*0.1+'px';
	document.getElementById('scheduletimelinecontents_inner').style.display = 'block';
}

function alphaset(){
	defaultAlpha += alphaAdd;
	document.getElementById('scheduletimelinecontents').style.filter = 'alpha(opacity='+defaultAlpha*alphaValue+')';
	document.getElementById('scheduletimelinecontents').style.MozOpacity = defaultAlpha/alphaValue;
	document.getElementById('scheduletimelinecontents').style.opacity = defaultAlpha/alphaValue;
	if(defaultAlpha >= alphaGoal){
		document.getElementById('scheduletimelinecontents').onclick = closeshceduletimelinecontents;
		clearInterval(alphaIntervalId);
	}
}
function closeshceduletimelinecontents(){
	defaultAlpha = 0;
	document.getElementById('scheduletimelinecontents').onclick = '';
	document.getElementById('scheduletimelinecontents').style.filter = 'alpha(opacity=0)';
	document.getElementById('scheduletimelinecontents').style.MozOpacity = '0';
	document.getElementById('scheduletimelinecontents').style.opacity = '0';
	document.getElementById('scheduletimelinecontents').style.display = 'none';
	document.getElementById('scheduletimelinecontents_inner').style.display = 'none';
}

