if(typeof(__realtime_nmzu)=='undefined'){
	var __realtime_nmzu={
		loadJS:function(url,callback){
			var sc=document.createElement('script');
			sc.type='text/javascript';
			sc.charset='UTF-8';
			if (window.ActiveXObject) {
				sc.onreadystatechange=function(){
					if (sc.readyState=='complete') callback(sc.readyState);
					if (sc.readyState=='loaded') callback(sc.readyState);
				};
			} else {
				sc.onload=function(){
					callback('onload');
				};
			}
			sc.src = url;
			document.getElementsByTagName('head')[0].appendChild(sc);
		},
		loadCSS:function(url){
			var link = document.createElement('link');
			link.href=url;
			link.type='text/css';
			link.rel='stylesheet';
			document.getElementsByTagName('head')[0].appendChild(link);
		},
		loadedJSCnt:0,
		loadedJSCallback:function(loadState){
			if (__realtime_nmzu.loadModules.length<=++__realtime_nmzu.loadedJSCnt){
				__realtime_nmzu.loadedJSCnt=0;
				__realtime_nmzu.loadModules=['http://realtime.nmzu.jp/lib/jquery.easing.js'];
//				if (typeof(jQuery.cookie)==='undefined') {
//					__realtime_nmzu.loadModules.push('http://realtime.nmzu.jp/lib/jquery.cookie.js');
//				}
				if (typeof(jQuery.easing.easeInQuad)==='undefined') {
					__realtime_nmzu.loadModules.push('http://realtime.nmzu.jp/lib/jquery.easing.js');
				}
				
				if (__realtime_nmzu.loadModules.length===0){
					__realtime_nmzu.readyModules();
				}else{
					for (var jsidx=0;jsidx<__realtime_nmzu.loadModules.length;jsidx++){
						__realtime_nmzu.loadJS(__realtime_nmzu.loadModules[jsidx],__realtime_nmzu.readyModules);
					}
				}
			}
		},
		loadModules:['http://realtime.nmzu.jp/lib/socket.io.js',
	                 'http://nmzu.jp/js/hamagri/nmzu.js',
	                 ],
		readyModules:function(){
			if (__realtime_nmzu.loadModules.length<=++__realtime_nmzu.loadedJSCnt){
				nmzu.init({}, function(){
					__realtime_nmzu.initAlertbox();
					__realtime_nmzu.startLoadConfig();
					__realtime_nmzu.startListen();
				});
			}
		},
		notify_config:{
			current_location_lat: 35.6894875,
			current_location_lng: 139.6917064,
			notify_epicenter_intensity: 3,
			notify_voice: 60,
			notify_sound: 60,
			current_location_mode: 'auto',
		},
		alertbox:null,
		initAlertbox:function(){
			__realtime_nmzu.alertbox=new nmzu.UIService.alertbox({
				id:'nmz_notify_alertbox',
				title:'地震速報',
				position:nmzu.UIService.alertbox.SUPPORT_POSITION.TOP,
				showEasing:'easeOutBounce',
				onclosing:__realtime_nmzu.closingAlertbox,
				style:{
					caution_level_class:'alertbox_level_warning',
				},
			});
		},
		startLoadConfig:function(){
			jQuery('body').append('<iframe id="nmznotify_config" seamless="seamless" src="http://nmzu.jp/nmznotify_cfg_inner.html" name="nmznotify_iframe" width="1px" height="1px" frameborder="0" style="position:absolute; left:0px; top:0px; z-index:0; border:0px; background-color:transparent; overflow:hidden; visibility:hidden;" allowtransparency="true"></iframe>');
			if (window.addEventListener) {
				window.addEventListener('message', __realtime_nmzu.receiveNamazuMessage, true);
			} else if (window.attachEvent) {
				window.attachEvent('onmessage', __realtime_nmzu.receiveNamazuMessage);
			}
			jQuery('#nmznotify_config').bind('load', function() {
				var cfgframe = jQuery('#nmznotify_config').get(0);
				var cfgwindow = cfgframe.contentWindow;
				var loadparam = {method:'load'};
				cfgwindow.postMessage(JSON.stringify(loadparam), 'http://nmzu.jp');
			});
		},
		receiveNamazuMessage:function(eventMessage) {
			if (eventMessage.origin !== 'http://nmzu.jp') return;
//			if (window.removeEventListener) {
//				window.removeEventListener('message', __realtime_nmzu.receiveNamazuMessage);
//			} else if (window.detachEvent) {
//				window.detachEvent('onmessage', __realtime_nmzu.receiveNamazuMessage);
//			} 
			if (eventMessage && eventMessage.data) {
				var receivedData = JSON.parse(eventMessage.data);
				switch (receivedData.method) {
					case 'loaded':
						if (receivedData.parameter && receivedData.parameter.config) {
							__realtime_nmzu.loadNotifyConfig(receivedData.parameter.config);
						}
						break;
				}
			}
		},
		welcomebox:null,
		loadNotifyConfig:function(config) {
			__realtime_nmzu.notify_config = config;
			__realtime_nmzu.ignore_map = config.ignore_map ? config.ignore_map : {};
			if (typeof(config.exist_configure) !== 'undefined' && parseInt(config.exist_configure) === 0) {
				__realtime_nmzu.welcomebox=new nmzu.UIService.alertbox({
					id:'nmz_config_message',
					title:'【地震情報配信】なまずが有効です。',
					position:nmzu.UIService.alertbox.SUPPORT_POSITION.BOTTOM,
					showEasing:'easeOutBounce',
					style:{
						caution_level_class:'alertbox_level_information',
					},
				});
				__realtime_nmzu.welcomebox.addInfo('welcome_message', '<div><a href="http://nmzu.jp/nmznotify_cfg.html" target="_blank" onclick="if (location.host==\'nmzu.jp\' && jQuery(\'#slidemenu_setting\').size()>0) {jQuery(\'#slidemenu_setting\').trigger(\'click\');}else{window.open(\'http://nmzu.jp/nmznotify_cfg.html\',\'namazu notification preferences\',\'width=560,height=660,menubar=false,toolbar=false,location=false,status=false,resizable=false,scrollbars=true\');} __realtime_nmzu.welcomebox.hide(); return false;"><img src="http://nmzu.jp/images/subscribe_icon.png" /></a></div><div>【地震情報配信なまず】が有効です。地震が発生した際に、地震情報がリアルタイム表示されます。<br />アイコンをクリックして、地震が発生時の通知設定を行ってください。</div>');
				__realtime_nmzu.welcomebox.show();
			}
		},
		closeTimerId:null,
		closingAlertbox:function(){
			__realtime_nmzu.notifyInfoFlag = false;
			jQuery.each(__realtime_nmzu.dispInfolist, function(status, id) {
				//TODO
				__realtime_nmzu.ignore_map[status] = id;
				var cfgframe = jQuery('#nmznotify_config').get(0);
				var cfgwindow = cfgframe.contentWindow;
				var loadparam = {method:'ignorecode',parameter:{code:id,status:status}};
				cfgwindow.postMessage(JSON.stringify(loadparam), 'http://nmzu.jp');
				//jQuery.cookie("eminfo_ignore_eminfoId_"+status,id);
				delete __realtime_nmzu.dispInfolist[status];
			});
			for (var i=0; i<__realtime_nmzu.alertInfoIDList.length; i++) delete __realtime_nmzu.alertInfoIDList[i];
			__realtime_nmzu.alertbox.clearInfo();
			if (__realtime_nmzu.titleBlink) {
				__realtime_nmzu.titleBlink.stop();
			}
			if (__realtime_nmzu.closeTimerId) {
				clearTimeout(__realtime_nmzu.closeTimerId);
				__realtime_nmzu.closeTimerId = null;
			}
			return true;
		},
		dispInfolist:{},
		startListen:function(){
			var RTS=new nmzu.RTNotificationService();
			RTS.initialize(function(){
				RTS.connectServer('http://realtime.nmzu.jp', 7000);
				RTS.listen('news msg', function(data){
					nmzu.Utility.Logging.debug(data);
					if (data && data.status && data.message && data.id && data.detection_date) {
						var detectTimeSpan = new Date().getTime() - new Date(data.detection_date).getTime();
						if (detectTimeSpan <= 60*10*1000) {
							__realtime_nmzu.alertProcess(data);
						}
					}
				});
				RTS.listen('notify msg', function(data){
					nmzu.Utility.Logging.debug(data);
					if (data && data.status && data.message && data.id) {
						__realtime_nmzu.alertProcess(data);
					}
				});
				RTS.send('request last news',{});
			});
		},
		alertInfoIDList:[],
		ignore_map:{},
		alertProcess:function(data){
			if (!__realtime_nmzu.isEnableAlert(data)) {
				return false;
			}
			if (data.status != 'jishin'){
				return false;
			}
			var newinfo = __realtime_nmzu.dispAlert(data);
			if (!__realtime_nmzu.titleBlink){
				__realtime_nmzu.titleBlink = new nmzu.UIService.blinktitle('新着通知があります - 【地震情報配信なまず】');
			}
			if (newinfo && !__realtime_nmzu.titleBlink.isEnabled()) __realtime_nmzu.titleBlink.start();
			data = this.exchangeReceiveDataType(data);
			__realtime_nmzu.notifyReachInfo.startNotify(data);
			__realtime_nmzu.closeTimerId = setTimeout(function(){
				if (__realtime_nmzu.closeTimerId) {
					__realtime_nmzu.closingAlertbox();
					__realtime_nmzu.alertbox.hide();
				}
			}, 5*60*1000);
			return true;
		},
		exchangeReceiveDataType:function(data) {
			switch(data.status) {
			case 'jishin':
				data.depth = parseInt(data.depth);
				data.epicenter_lat = parseFloat(data.epicenter_lat);
				data.epicenter_lng = parseFloat(data.epicenter_lng);
				data.intensity = parseInt(data.intensity);
				data.magnitude = parseInt(data.magnitude);
			}
			return data;
		},
		titleBlink:null,
		isEnableAlert:function(data){
			if (data.status != 'jishin') return true;
			if (typeof(nmzu.current_jishin_id)!='undefined' && nmzu.current_jishin_id==data.id) return false;
			var notify_epicenter_intensity = __realtime_nmzu.notify_config.notify_epicenter_intensity;
			if (data.intensity >= notify_epicenter_intensity) return true;
			return false;
		},
		notifyReachInfo:{
			quakeInfo:null,
			currentCoordinate:null,
			startNotify:function(data){
				__realtime_nmzu.notifyInfoFlag = true;
				__realtime_nmzu.notifyReachInfo.additionInfoCountdown='';
				__realtime_nmzu.notifyReachInfo.additionInfoIntensity='';
				if (__realtime_nmzu.alertbox.getInfoObject(__realtime_nmzu.notifyReachInfo.notifyReachInfoAlertBoxID)) {
					__realtime_nmzu.alertbox.removeInfo(__realtime_nmzu.notifyReachInfo.notifyReachInfoAlertBoxID);
				}
				if (data.detection_date && (new Date().getTime()-new Date(data.detection_date).getTime())<120000 &&
					data.epicenter_lat && data.epicenter_lng){
					__realtime_nmzu.notifyReachInfo.quakeEstimationService=new nmzu.QuakeEstimationService();
					__realtime_nmzu.notifyReachInfo.locationService=new nmzu.LocationService({timeoutInterval:60});
					__realtime_nmzu.notifyReachInfo.audioService=new nmzu.AudioService();
					__realtime_nmzu.notifyReachInfo.quakeInfo=new nmzu.Common.Quake(data.id);
					__realtime_nmzu.notifyReachInfo.quakeInfo.detectionDate=new Date(data.detection_date);
					if (data.epicenter_lat && data.epicenter_lng && data.intensity && data.magnitude && data.depth) {
						__realtime_nmzu.notifyReachInfo.quakeInfo.epicenterLocation=new nmzu.Common.Location(new nmzu.Common.Coordinate(data.epicenter_lat, data.epicenter_lng),null);
						__realtime_nmzu.notifyReachInfo.quakeInfo.intensity=nmzu.Common.Intensity.getInstanceFromNo(data.intensity);
						__realtime_nmzu.notifyReachInfo.quakeInfo.magnitude=data.magnitude;
						__realtime_nmzu.notifyReachInfo.quakeInfo.depth=data.depth;
					}
					if (__realtime_nmzu.notifyReachInfo.currentCoordinate){
						__realtime_nmzu.notifyReachInfo.notifyEstimationInfo(__realtime_nmzu.notifyReachInfo.currentCoordinate);
					}else{
						__realtime_nmzu.notifyReachInfo.startCurrentLocationSearch();
					}
				}
			},
			locationService:null,
			quakeEstimationService:null,
			audioService:null,
			startCurrentLocationSearch:function(){
				var location_mode=__realtime_nmzu.notify_config.current_location_mode;
				if (location_mode != 'manual') {
					__realtime_nmzu.notifyReachInfo.locationService.addAPI(new nmzu.LocationService.APIS.GOOGLE_AJAX(),{priority:20});
					__realtime_nmzu.notifyReachInfo.locationService.addAPI(new nmzu.LocationService.APIS.BROWSER_LOCATION(),{priority:40,minFoundCount:1,minAccurancy:false,timeoutSeconds:60,findingRepeat:true});
					__realtime_nmzu.notifyReachInfo.locationService.setFindLocationCallback(__realtime_nmzu.notifyReachInfo.notifyEstimationInfo);
					__realtime_nmzu.notifyReachInfo.locationService.startSearchCurrentLocation();
				} else {
					var location_lat=__realtime_nmzu.notify_config.current_location_lat;
					var location_lng=__realtime_nmzu.notify_config.current_location_lng;
					var coordinate = new nmzu.Common.Coordinate(parseFloat(location_lat), parseFloat(location_lng));
					__realtime_nmzu.notifyReachInfo.notifyEstimationInfo(coordinate);
				}
			},
			currentLocationReachDateTime:null,
			notifyEstimationInfo:function(coordinate){
				__realtime_nmzu.notifyReachInfo.currentCoordinate=coordinate;
				__realtime_nmzu.notifyReachInfo.currentLocationReachDateTime=__realtime_nmzu.notifyReachInfo.quakeEstimationService.estimateReachDate(__realtime_nmzu.notifyReachInfo.quakeInfo,coordinate);
				var estimationIntensity = __realtime_nmzu.notifyReachInfo.quakeEstimationService.estimateReachIntensity(__realtime_nmzu.notifyReachInfo.quakeInfo,__realtime_nmzu.notifyReachInfo.currentCoordinate);
				__realtime_nmzu.notifyReachInfo.updateCurrentLocationIntensity(estimationIntensity);
				__realtime_nmzu.notifyReachInfo.setCountdownAudio(__realtime_nmzu.notifyReachInfo.currentLocationReachDateTime.getTime()-new Date().getTime());
				__realtime_nmzu.notifyReachInfo.updateAdditionCountdown();
			},
			prePlayId:null,
			setCountdownAudio:function(reachTimeMillis){
				var notify_sound_volume = __realtime_nmzu.notify_config.notify_sound;
				var notify_voice_volume = __realtime_nmzu.notify_config.notify_voice;
				var reachTime=reachTimeMillis/1000;
				var offsetTime=function(countTime){
					return (reachTime>countTime)?0:(countTime-reachTime)*1000;
				};
				var delayTime=function(countTime){
					return (reachTime>countTime)?(reachTime-countTime)*1000:0;
				};
				var reachTimeIdx;
				for (reachTimeIdx=10;reachTimeIdx<=60;reachTimeIdx+=10){
					if (__realtime_nmzu.notifyReachInfo.audioService.isRegistered('countdown'+reachTimeIdx)) {
						__realtime_nmzu.notifyReachInfo.audioService.stopAudio('countdown'+reachTimeIdx);
					}
				}
				if (reachTime>0){
					// NotifyAlert
					if (__realtime_nmzu.notifyReachInfo.prePlayId !== __realtime_nmzu.notifyReachInfo.quakeInfo.id){
						if (!__realtime_nmzu.notifyReachInfo.audioService.isRegistered('notify_sound')) {
							__realtime_nmzu.notifyReachInfo.audioService.registAudio('notify_sound','http://nmzu.jp/audios/notify/notifyalarm',function(){
								__realtime_nmzu.notifyReachInfo.audioService.setVolume('notify_sound', notify_sound_volume/100);
								__realtime_nmzu.notifyReachInfo.audioService.playAudio('notify_sound');
							});
						}else{
							__realtime_nmzu.notifyReachInfo.audioService.setVolume('notify_sound', notify_sound_volume/100);
							__realtime_nmzu.notifyReachInfo.audioService.playAudio('notify_sound');
						}
						__realtime_nmzu.notifyReachInfo.prePlayId=__realtime_nmzu.notifyReachInfo.quakeInfo.id;
					}
					
					// Countdown voice
					for (reachTimeIdx=20;reachTimeIdx<=50;reachTimeIdx+=10){
						if (reachTime>=reachTimeIdx+1) {
							if (__realtime_nmzu.notifyReachInfo.audioService.isRegistered('countdown'+reachTimeIdx)){
								__realtime_nmzu.notifyReachInfo.audioService.setVolume('countdown'+reachTimeIdx,notify_voice_volume/100);
								__realtime_nmzu.notifyReachInfo.audioService.playAudio('countdown'+reachTimeIdx,offsetTime(reachTimeIdx),delayTime(reachTimeIdx+1));
							} else {
								__realtime_nmzu.notifyReachInfo.audioService.registAudio('countdown'+reachTimeIdx,'http://nmzu.jp/audios/notify/voices/f_e'+reachTimeIdx,(function(reachTimeIdx){
									return function(){
										__realtime_nmzu.notifyReachInfo.audioService.setVolume('countdown'+reachTimeIdx,notify_voice_volume/100);
										__realtime_nmzu.notifyReachInfo.audioService.playAudio('countdown'+reachTimeIdx,offsetTime(reachTimeIdx),delayTime(reachTimeIdx+1));
									};
								})(reachTimeIdx));
							}
						}
					}
					if (__realtime_nmzu.notifyReachInfo.audioService.isRegistered('countdown10')){
						__realtime_nmzu.notifyReachInfo.audioService.setVolume('countdown10',notify_voice_volume/100);
						__realtime_nmzu.notifyReachInfo.audioService.playAudio('countdown10',offsetTime(10.8),delayTime(11.8),true);
					} else {
						__realtime_nmzu.notifyReachInfo.audioService.registAudio('countdown10','http://nmzu.jp/audios/notify/voices/f_e10cnt',function(){
							__realtime_nmzu.notifyReachInfo.audioService.setVolume('countdown10',notify_voice_volume/100);
							__realtime_nmzu.notifyReachInfo.audioService.playAudio('countdown10',offsetTime(10.8),delayTime(11.8),true);
						});
					}
					if (reachTime>=63) {
						if (__realtime_nmzu.notifyReachInfo.audioService.isRegistered('countdown60')){
							__realtime_nmzu.notifyReachInfo.audioService.setVolume('countdown60',notify_voice_volume/100);
							__realtime_nmzu.notifyReachInfo.audioService.playAudio('countdown60',offsetTime(60),3000,true);
						} else {
							__realtime_nmzu.notifyReachInfo.audioService.registAudio('countdown60','http://nmzu.jp/audios/notify/voices/f_e60',function(){
								__realtime_nmzu.notifyReachInfo.audioService.setVolume('countdown60',notify_voice_volume/100);
								__realtime_nmzu.notifyReachInfo.audioService.playAudio('countdown60',offsetTime(60),3000,true);
							});
						}
					}
				}
			},
			additionInfoIntensity:'',
			updateCurrentLocationIntensity:function(intensityInfo){
				var estText;
				if (intensityInfo.min.intensityNo == intensityInfo.max.intensityNo) {
					estText=intensityInfo.min.name;
				} else {
					estText=intensityInfo.min.name + " 〜 " + intensityInfo.max.name;
				}
				__realtime_nmzu.notifyReachInfo.additionInfoIntensity = '(到達予想震度：'+estText+')';;
				__realtime_nmzu.notifyReachInfo.updateNotifyInfoAlertBox();
			},
			additionInfoCountdown:'',
			countdownTimerId:null,
			updateAdditionCountdown:function(){
				var nowTime=new Date().getTime();
				var reachTime=__realtime_nmzu.notifyReachInfo.currentLocationReachDateTime.getTime();
				var reachSec=Math.floor((reachTime-nowTime)/1000);
				if (reachSec<=0){
					if (__realtime_nmzu.notifyReachInfo.countdownTimerId){
						clearInterval(__realtime_nmzu.notifyReachInfo.countdownTimerId);
					}
					__realtime_nmzu.notifyReachInfo.additionInfoCountdown='現在地にはすでに到達';
					__realtime_nmzu.notifyReachInfo.countdownTimerId=null;
				}else{
					__realtime_nmzu.notifyReachInfo.additionInfoCountdown='到達まで'+reachSec+'秒';
					if (!__realtime_nmzu.notifyReachInfo.countdownTimerId){
						__realtime_nmzu.notifyReachInfo.countdownTimerId=setInterval('__realtime_nmzu.notifyReachInfo.updateAdditionCountdown()', 500);
					}
				}
				__realtime_nmzu.notifyReachInfo.updateNotifyInfoAlertBox();
			},
			notifyReachInfoAlertBoxID:'notify_reach_info_id',
			updateNotifyInfoAlertBox:function(){
				if (!__realtime_nmzu.notifyInfoFlag) return;
				var msg = __realtime_nmzu.notifyReachInfo.additionInfoCountdown + __realtime_nmzu.notifyReachInfo.additionInfoIntensity;
				if (__realtime_nmzu.alertbox.getInfoObject(__realtime_nmzu.notifyReachInfo.notifyReachInfoAlertBoxID)) {
					__realtime_nmzu.alertbox.updateInfo(__realtime_nmzu.notifyReachInfo.notifyReachInfoAlertBoxID, msg);
				} else {
					__realtime_nmzu.alertbox.addInfo(__realtime_nmzu.notifyReachInfo.notifyReachInfoAlertBoxID, msg);
				}
			},
		},
		notifyInfoFlag:false,
		allowStatus:['jishin','tsunami'],
		dispAlert:function(data) {
			var result = false; // new info is true, else false
			if (!nmzu.Utility.ObjUtil.containsArray(__realtime_nmzu.allowStatus,data.status)){
				return false;
			}
			if (__realtime_nmzu.dispInfolist[data.status]!==data.id) {
				var showAlertFlag = false;
				//var ignoreId = jQuery.cookie("eminfo_ignore_eminfoId_"+data.status);
				var ignoreId = __realtime_nmzu.ignore_map[data.status];
				if (ignoreId != data.id) {
					__realtime_nmzu.alertbox.addInfo(data.id, data.message);
					__realtime_nmzu.alertInfoIDList.push(data.id);
					if (__realtime_nmzu.alertbox.getRegistedInfoIDList().length > 5) {
						var removeInfoID = __realtime_nmzu.alertInfoIDList.shift();
						__realtime_nmzu.alertbox.removeInfo(removeInfoID);
					}
					showAlertFlag = true;
					result = true;
				}
				
				if (showAlertFlag) {
					__realtime_nmzu.alertbox.show();
				}
				__realtime_nmzu.dispInfolist[data.status] = data.id;
			} else {
				__realtime_nmzu.alertbox.updateInfo(data.id, data.message);
			}
			return result;
		},
		startLoad:function(){
			__realtime_nmzu.loadCSS('http://nmzu.jp/style/alertbox/default.css');
			if (typeof(jQuery)==='undefined') {
				__realtime_nmzu.loadModules.push('https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js');
			}
			for (var jsidx=0;jsidx<__realtime_nmzu.loadModules.length;jsidx++){
				__realtime_nmzu.loadJS(__realtime_nmzu.loadModules[jsidx],__realtime_nmzu.loadedJSCallback);
			}
		},
	};
	setTimeout('__realtime_nmzu.startLoad()', 0);
}
