addLoadListener(PreInit);

function PreInit() {
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
	//document.getElementsByTagName('html')[0].onmousemove = function(e) { CurrentStatus.ShowRootMouse(e); }
	document.getElementsByTagName('body')[0].oncontextmenu = function() { return false; }
	var GetHtml = document.getElementsByTagName('html')[0];
	GetHtml.onmousemove = CurrentStatus.TotalMoveEffect;
	GetHtml.onmouseup = CurrentStatus.TotalMoveEnd;
	GetHtml.style.overflow = 'hidden';
	
	//get cookie
	var MapEnviSet = CurrentStatus.GetCookie('MapEnviSet');
	if (MapEnviSet) CurrentStatus.LoadEnviSet(MapEnviSet);
	
	//url var
	CurrentStatus.UrlVars = MkUrlVars();
	CurrentStatus.EnviSetUpdate();
	
	var hash = $H();
	hash['action'] = 'LoginStatus';
	var req = new Ajax.Request('./api/Api_Locate.php',
								{method: 'post',
								 parameters: hash.toQueryString(),
								 onComplete: InitEns
								});
}

function InitEns(r) {
	//Login
	var DataArray = r.responseText.split('\n').without('\r', '\n', '');
	LoginArray = DataArray[0].split('[DataBr]');
	$('login').innerHTML = LoginArray[0] + ((LoginArray[1] == 'NoLogin') ? ' <a href="https://member.pchome.com.tw/login.html?ref=http://map.pchome.com.tw/">登入</a>' : '<b>'+LoginArray[1]+'</b>( <a href="http://member.pchome.com.tw/logout.html">登出</a> )');
	if (LoginArray[1] == 'NoLogin') {
		CurrentStatus.isMember = false;
		CurrentStatus.pc_id = '';
		CurrentStatus.uid = '';
		CurrentStatus.hId = '';
	} else {
		CurrentStatus.isMember = true;
		CurrentStatus.pc_id = LoginArray[1];
		CurrentStatus.uid = LoginArray[2];
		CurrentStatus.hId = LoginArray[3];
	}
	
	//BtnBlind
	$('BtnBlind').onmouseover = function() { this.style.backgroundPosition = '-7px 0px'; };
	$('BtnBlind').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('BtnBlind').onclick = function() { CurrentStatus.SetElementParameter(); };
	
	//map
	CurrentStatus.MapInit();
	
	//SetElementParameter
	//(CurrentStatus.MapEnviSet[3] == 1) ? Element.hide('LSection') : Element.show('LSection');
	Element.hide('LSection');
	CurrentStatus.SetElementParameter();
	
	//SearchSection
	$('Search').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('addressing');
	}
	$('SubmitSearch').onmouseover = function() { this.style.backgroundPosition = '-52px 0px'; };
	$('SubmitSearch').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('SubmitSearch').onclick = function() { CurrentStatus.SearchCheck('addressing'); };
	//WinWarn
	$('WinWarnBtn').onmouseover = function() { this.style.backgroundPosition = '-13px 0px'; };
	$('WinWarnBtn').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('WinWarnBtn').onclick = function() { Element.hide('WinWarn'); };
	//LoadingSection
	if (CurrentStatus.BrowserPNGfix.indexOf(CurrentStatus.Browser) != -1) {
		$('LoadingSection').style.background = 'url(./img/transparent.gif)';
		$('LoadingSection').style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=./img/BackLoading.png, sizingMethod=scale)';
	}
	//FamousMarkSection
	CurrentStatus.FamousMarkMenuInit('FamousMarkMenu#2');
	//AroundMarkSection
	CurrentStatus.AroundMarkMenuInit('AroundMarkMenu#0');
	//UserPOIsSection
	CurrentStatus.UserPOIsMenuInit();
	//RouteSection
	$('RouteSP').onkeydown = function(e) {
		Element.hide('WinWarn');
		CurrentStatus.Map.clearOverlays();
		Element.hide('RouteInfoSection');
		CurrentStatus.Map.removeOverlay(CurrentStatus.SetSPoint);
		CurrentStatus.SetSPoint = '';
		if (CurrentStatus.SetEPoint) CurrentStatus.Map.addOverlay(CurrentStatus.SetEPoint);
		
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('routing');
	}
	$('RouteEP').onkeydown = function(e) {
		Element.hide('WinWarn');
		CurrentStatus.Map.clearOverlays();
		Element.hide('RouteInfoSection');
		CurrentStatus.Map.removeOverlay(CurrentStatus.SetEPoint);
		CurrentStatus.SetEPoint = '';
		if (CurrentStatus.SetSPoint) CurrentStatus.Map.addOverlay(CurrentStatus.SetSPoint);
		
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('routing');
	}
	$('SubmitRoute').onmousedown = function() { this.style.backgroundPosition = '-74px 0px'; CurrentStatus.SearchCheck('routing'); }
	$('SubmitRoute').onmouseup = function() { this.style.backgroundPosition = '0px 0px'; }
	$('SubmitRoute').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; }
	$('RouteTypeA').onclick = function() { CurrentStatus.ClearSearchResult(); };
	$('RouteTypeB').onclick = function() { CurrentStatus.ClearSearchResult(); };
	CurrentStatus.RouteMenuInit();
	
	//MyListSection
	$('ListTypeA').onclick = function() { CurrentStatus.MyListQuery(); };
	$('ListTypeB').onclick = function() { CurrentStatus.MyListQuery(); };
	
	//WinInfoB
	if (CurrentStatus.BrowserPNGfix.indexOf(CurrentStatus.Browser) != -1) {
		$('WinInfoBTopShadow').style.background = 'url(./img/transparent.gif)';
		$('WinInfoBTopShadow').style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=./img/WinInfoB/BackRT.png, sizingMethod=scale)';
		$('WinInfoBBottom').style.background = 'url(./img/transparent.gif)';
		$('WinInfoBBottom').style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=./img/WinInfoB/BackBottom.png, sizingMethod=scale)';
		$('WinInfoBMidShadow').style.background = 'url(./img/transparent.gif)';
		$('WinInfoBMidShadow').style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=./img/WinInfoB/BackRM.png, sizingMethod=scale)';
	}
	$('WinInfoBBtn').onmouseover = function() { this.style.backgroundPosition = '-13px 0px'; };
	$('WinInfoBBtn').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('WinInfoBBtn').onclick = function() { $('WinInfoB').style.left = '-1000px'; };
	CurrentStatus.WinInfoBInit('▲WinInfo');
	
	//WinIntroMainSection
	$('WinIntroTop').onmousedown = CurrentStatus.WinDragStart;
	$('BtnWinIntro').onmouseover = function() { this.style.backgroundPosition = '-11px 0px'; };
	$('BtnWinIntro').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('BtnWinIntro').onclick = function() { CurrentStatus.HideOWin(); };
	Element.show('WinIntroMain');
	disableSelection($('WinIntro'));
	CurrentStatus.getWinIntroTitle = $('WinIntro').getElementsByTagName('div')[3];
	
	//AddMarkerMain
	$('AddMarkerName').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('AddMarker');
	}
	$('AddMarkerAddr').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('AddMarker');
	}
	$('AddMarkerTag').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('AddMarker');
	}
	$('AddMarkerPhoto').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('AddMarker');
	}
	$('AddMarkerLink').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('AddMarker');
	}
	$('SubmitAddMarker').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('SubmitAddMarker').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('SubmitAddMarker').onclick = function() { CurrentStatus.SearchCheck('AddMarker'); };
	$('ClearAddMarker').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('ClearAddMarker').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('ClearAddMarker').onclick = function() { CurrentStatus.ClearAction('AddMarker'); };
	//EditMyPOIMain
	$('EditMyPOIName').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('EditMyPOI');
	}
	$('EditMyPOIAddr').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('EditMyPOI');
	}
	$('EditMyPOIPhoto').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('EditMyPOI');
	}
	$('EditMyPOILink').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('EditMyPOI');
	}
	//CollectMain
	$('SubmitCollect').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('SubmitCollect').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('ClearCollect').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('ClearCollect').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('CollectTag').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('CollectMarker');
	}
	//SaveNavi
	$('SubmitSaveNavi').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('SubmitSaveNavi').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('SubmitSaveNavi').onclick = function() { CurrentStatus.SearchCheck('SaveNavi'); };
	$('ClearSaveNavi').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('ClearSaveNavi').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('ClearSaveNavi').onclick = function() { CurrentStatus.ClearAction('SaveNavi'); };
	$('SaveNaviSP').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('SaveNavi');
	}
	$('SaveNaviEP').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('SaveNavi');
	}
	for (var i=0; i<CurrentStatus.WinIntroContentSet.length; i++) $('WinIntroMainSection').appendChild($(CurrentStatus.WinIntroContentSet[i]));
	//EditMyPOIMain
	$('SubmitEditMyPOI').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('SubmitEditMyPOI').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('ClearEditMyPOI').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('ClearEditMyPOI').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	//Mail2Main
	$('SubmitMail2').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('SubmitMail2').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('ClearMail2').onmouseover = function() { this.style.backgroundPosition = '-90px 0px'; };
	$('ClearMail2').onmouseout = function() { this.style.backgroundPosition = '0px 0px'; };
	$('SenderName').onkeydown = function(e) {
		Element.hide('WinWarn');
		if (CurrentStatus.GetKeyCode(e) != 13) return;
		CurrentStatus.SearchCheck('Mail2');
	}
	$('SenderName').onkeyup = function() { $('ShowSenderName').innerHTML = this.value; };
	
	//Overlay
	var Overlay = document.createElement("div");
	Overlay.id = 'Overlay';
	Overlay.style.display = 'none';
	Overlay.onclick = function() { CurrentStatus.HideOWin(); };
	document.getElementsByTagName('body')[0].appendChild(Overlay);
	
	//MainMenuSection
	CurrentStatus.MenuInit('Menu#2');
	CurrentStatus.SectionClear('MyListInfoMain');
	CurrentStatus.RunMapAction();
	Event.observe(window, "resize", CurrentStatus.ResizeEvent, false);
}
