// POPUP
function popup1(url,w,h,winName){
	var subWin = window.open(url, winName, 'toolbar=1,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width='+w+',height='+h+'');
	subWin.focus();
}

function popup2(url,w,h,winName){
	var latitude = document.getElementById("latitude").value;
	var longitude = document.getElementById("longitude").value;
	var mapMarkerFlag = document.getElementById("mapMarkerFlag").value;
	var subWin = window.open(url + "?latitude=" + latitude + "&longitude=" + longitude + "&mapMarkerFlag=" + mapMarkerFlag,
			winName, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+w+',height='+h+'');
	subWin.focus();
}


// ロールオーバー用
function smartRollover() {  
	if(document.getElementsByTagName) {  
		var tags = ["img","input"];  
		var len = tags.length;  
		for( var i=0; i<len; i++ ) {  
			var el = document.getElementsByTagName(tags[i]);  
			var len2 = el.length;  
			for(var j=0; j<len2; j++) {  
				var attr = el[j].getAttribute("src");  
				if( attr ) {  
					if(el[j].getAttribute("src").match(/_off\./))  
					{  
						el[j].onmouseover = function() {  
							this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));  
						}  
						el[j].onmouseout = function() {  
							this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));  
						}  
					}  
				}  
			}  
		}  
	}  
} 


// 背景色ストライプ
function stripeColor() {
	var elem = "tr";
	if(document.getElementsByTagName) {
		var el = document.getElementsByTagName(elem);

		for(var i=0; i<el.length; i++) {
			if(el[i].childNodes[0].tagName != "th"	&& el[i].parentNode.parentNode.className.indexOf("stripe") != -1) {
				if(i%2 == 1) {
					el[i].className = "odd";
				} else {
					el[i].className = "even";
				}
			}
		}
	}
}


// ページトップへのスクリプト
function backToTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;

	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}

	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}

	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;

	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));

	window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

	if (x > 0 || y > 0) {
		window.setTimeout("backToTop()", 25);
	}
}


// 実行関数
function addEvent(elem,event,func){
	if(elem.addEventListener) {
		elem.addEventListener(event, func, false);
	}else if(elem.attachEvent) {
		elem.attachEvent("on" + event, func);
	}
}
addEvent(window, "load", smartRollover);
addEvent(window, "load", stripeColor);

// オーバーレイ  + フローティングバーの検索条件部分を作成する
var searchPanel = function() {
	function isIE6() {
		return navigator.userAgent.toLowerCase().indexOf("msie 6") !== -1;
	}
	
	function getScreenSize(){
		var isWin9X = (navigator.appVersion.toLowerCase().indexOf('windows 98') + 1);
		var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer') + 1 ? 1 : 0);
		var isOpera = (navigator.userAgent.toLowerCase().indexOf('opera') + 1 ? 1 : 0);
		if (isOpera) {
			isIE = false;
		}
		var isSafari = (navigator.appVersion.toLowerCase().indexOf('safari') + 1 ? 1 : 0);
		
		var size = {};
		
		if (!isSafari && !isOpera) {
			size.x = document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth;
			size.y = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
		}
		else {
			size.x = window.innerWidth;
			size.y = window.innerHeight;
		}
		
		return size;
	}
	
	function getScrollTop() {
		return document.documentElement.scrollTop || document.body.scrollTop;
	}
	
	function getDocumentSize() {
		size = {}
		size.y = document.documentElement.scrollHeight || document.body.scrollHeight;
		size.x = document.documentElement.scrollWidth || document.body.scrollWidth;
		return size;
	}
	
	var SEARCH_PANEL_MARGIN = 100;
			
	function getSearchPanelInnerHeight(){
		var MAX_SEARCH_PANEL_INNER_HEIGHT = 600;
		var searchPanelInnerHeight = getScreenSize().y - SEARCH_PANEL_MARGIN;
		if (searchPanelInnerHeight < 0) {
			searchPanelInnerHeight = 0;
		} else if (searchPanelInnerHeight > MAX_SEARCH_PANEL_INNER_HEIGHT) {
			searchPanelInnerHeight = MAX_SEARCH_PANEL_INNER_HEIGHT;
		}
		return searchPanelInnerHeight;
	}
				
	function getSearchPanelTop() {
		var searchPanelInnerHeight = getSearchPanelInnerHeight();
		var searchPanelHeight = searchPanelInnerHeight + SEARCH_PANEL_MARGIN;
		var scrollTop  = getScrollTop();
		var screenHeight = getScreenSize().y;
		var searchPanelTop = scrollTop + Math.floor((screenHeight - searchPanelInnerHeight - SEARCH_PANEL_MARGIN) / 2);
		if (searchPanelHeight > screenHeight) {
			searchPanelTop = scrollTop;
		}
		return searchPanelTop;
	}
	
	function getFloatingBarY() {
		var topY = getScreenSize().y;
		var bottomY = (isIE6()) ? topY + getScrollTop() : topY;
		return bottomY - 30;
	}
	
	return {
		init : function() {
			var searchForm = document.getElementById('searchForm');
			if (!searchForm) {
				// searchFormがなければ処理を中止
				return false;
			}
			
			// 検索部分作成
			var searchPanel = document.createElement('div');
			searchPanel.className = 'searchPage';
			searchPanel.appendChild(searchForm);
			var objBody = document.getElementsByTagName('body').item(0);
			objBody.insertBefore(searchPanel, objBody.firstChild);

			// 検索部分のヘッダ部(閉じるボタン付)
			var searchConditinTitle = document.createElement('div');
			searchConditinTitle.className = 'titleSearchCondition';
			searchConditinTitle.innerHTML = '<h4>検索条件を選択</h4><p><a href="javascript:searchPanel.hide();">閉じる</a></p>';
			searchForm.insertBefore(searchConditinTitle, searchForm.firstChild);

			// 検索部分表示用フローティングバー作成
			var floatingBar = document.createElement('a');
			floatingBar.id = 'floatingBar';
			floatingBar.href = 'javascript:searchPanel.show();';
			if (isIE6()) {
				floatingBar.style.position = 'absolute';
				addEvent(window, 'scroll', function() {
					floatingBar.style.top = getFloatingBarY() + 'px';
				});
			} else {
				floatingBar.style.position = 'fixed';
			}
			floatingBar.style.top = getFloatingBarY() + 'px';
			objBody.appendChild(floatingBar);
			// ウィンドウリサイズ時のフローティングバーと検索ウィンドウの挙動を定義
			addEvent(window, 'resize', function(){
				var floatingBar = document.getElementById('floatingBar');
				if (floatingBar) {
					floatingBar.style.top = getFloatingBarY() + 'px';
					var overlay = document.getElementById('overlay');
					if (overlay) {
						overlay.style.width = getDocumentSize().x + 'px';
					}
				}

				if (searchForm) {
					var searchConditionChoices = document.getElementById('searchConditionChoices');
					if (searchConditionChoices) {
						searchForm.style.top = getSearchPanelTop() + 'px';
						searchConditionChoices.style.height = getSearchPanelInnerHeight() + 'px';
					}
				}
			});
			// スクロール時に検索ウィンドウも移動させる
			addEvent(window, 'scroll', function() {
				if (searchForm) {
					var doucumentHeight = getDocumentSize().y;
					var scrollTop = getScrollTop();
					var screenHeight = getScreenSize().y;
					if (scrollTop + screenHeight < doucumentHeight) {
						searchForm.style.top = getSearchPanelTop() + 'px';
					}
				}
			});
			
			// 沿線・エリアの絞り込みボタンを検索ボタンの横に並べる
			esLineAreaSearch.setAddtionalCallback(function() {
				var rangeBtns = document.getElementsByClassName('btnChangeRange');
				if (!rangeBtns || rangeBtns.length === 0) {
					return false;
				}
				var searchBtn = document.getElementById('btnSearch');
				var searchBtnContainer = searchBtn.parentNode;
				// デフォルトで町村が指定されている場合にはrangeBtns[0]の内部にタグが存在しないので
				// 以下の処理は行われない
				var rangeBtnsChildren = rangeBtns[0].innerHTML.match(/<.*?>/g);
				if (rangeBtnsChildren && rangeBtnsChildren.length > 0) {
					if (rangeBtns.length > 1) {
						searchBtnContainer.removeChild(rangeBtns[1]);
					}
					searchBtnContainer.insertBefore(rangeBtns[0], searchBtn);
					// ボタンのwidthを指定しているclassを除去し，2つのボタンを横並びにできるようにする
					var classNames = searchBtnContainer.className.split(' ');
					var newClassNames = [];
					for (var i = 0; i < classNames.length; i++) {
						if (classNames[i] !== 'btnSingle') {
							newClassNames.push(classNames[i]);
						}
					}
					searchBtnContainer.className = newClassNames.join(' ');
				}
			});
		},
		
		show : function() {
			// 検索部分を表示
			var searchConditionChoices = document.getElementById('searchConditionChoices');
			searchConditionChoices.style.height = getSearchPanelInnerHeight() + 'px';
			// ホイールによる検索条件部分スクロール時に，ウィンドウのスクロールをさせない
			function scrollSearchCondition (event){
				if (!event) {
					event = window.event;
				}
				var delta = 0;
				if (event.wheelDelta) {
					//IE
					delta = event.wheelDelta;
				} else if (event.detail) {
					delta = -event.detail * 20;
				}
				if (delta) {
					searchConditionChoices.scrollTop -= delta;
				}
				if (event.preventDefault) {
					event.preventDefault();
					event.stopPropagation();
				} else {
					//IE
					event.returnValue = false;
					event.cancelBubble = true;
				}
			}
			if (searchConditionChoices.onmousewheel !== scrollSearchCondition) {
				if (searchConditionChoices.addEventListener) {
					// Mozilla
					searchConditionChoices.addEventListener('DOMMouseScroll', scrollSearchCondition, false);
				}
				searchConditionChoices.onmousewheel = scrollSearchCondition;
			}
			
			var searchPanel = document.getElementById('searchForm');
			searchPanel.style.display = 'block';
			searchPanel.style.top = getSearchPanelTop() + 'px';

			// オーバーレイを表示
			var overlay = document.getElementById('overlay');
			if (overlay) {
				overlay.style.display = 'block';
			} else {
				overlay = document.createElement('div');
				overlay.id = "overlay";
				var documentSize = getDocumentSize();
				overlay.style.height = documentSize.y + 'px';
				overlay.style.width = documentSize.x + 'px';
				var objBody = document.getElementsByTagName('body').item(0);
				objBody.insertBefore(overlay, objBody.firstChild);
			}
			
			// IE6でselect boxのzindexが効かない問題への対策
			if (isIE6()) {
				var selects = document.getElementsByTagName('select');
				for (var i = 0; i < selects.length; i++) {
					var select = selects[i];
					if (!searchPanel.contains(select)) {
						select.style.display = 'none';
					}
				}
			}

			// フローティングバーを非表示
			var floatingBar = document.getElementById('floatingBar');
			if (floatingBar) {
				floatingBar.style.display = 'none';
			}
		},
		
		hide : function() {
			document.getElementById('searchForm').style.display = 'none';

			var overlay = document.getElementById('overlay');
			if (overlay) {
				overlay.style.display = 'none';
			}

			var floatingBar = document.getElementById('floatingBar');
			if (floatingBar) {
				floatingBar.style.display = 'block';
			}
			
			// IE6でselect boxのzindexが効かない問題への対策
			if (isIE6()) {
				var selects = document.getElementsByTagName('select');
				for (var i = 0; i < selects.length; i++) {
					selects[i].style.display = '';
				}
			}
		}
	};
}();
