﻿/**
 * IE versionチェック用
 */
var ie = (function(){
    var undef, v = 3, div = document.createElement('div');
    while (
        div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
        div.getElementsByTagName('i')[0]
    );
    return v> 4 ? v : undef;
}());

/**
 * map
 */
// 初期視点
// CENTERPARK
var initPos = new google.maps.LatLng(36.86934768196399,137.49459396874997);
var zoomLevel = 12;
var mapOpts = {
	zoom: zoomLevel,
	center : initPos,
	//mapTypeId: google.maps.MapTypeId.ROADMAP,
	mapTypeId: google.maps.MapTypeId.HYBRID,
	scaleControl: true,
	//streetViewControl: false
	scrollwheel: false
}

// IE6のみ、API3だと、ズームバーなどが表示されないので、
// Click時のズームレベルを固定する
if( ie <= 6 )
	zoomLevel = 16;

var map = new google.maps.Map(document.getElementById("map"), mapOpts);

/**
 * makeMarker()
 */
var infoWindow = new google.maps.InfoWindow();
var markerBounds = new google.maps.LatLngBounds();
var markerArray = [];
 
function makeMarker(options){
	
	var pushPin = new google.maps.Marker({map:map});
	pushPin.setOptions(options);
	if(!options.sidebarParent){ //親以外はマップイベントリスナに追加
		google.maps.event.addListener(pushPin, "click", function(){
			infoWindow.setOptions(options);
			infoWindow.open(map, pushPin);

			if( !$.Mobile)			
				try{if(this.sidebarButton)this.sidebarButton.button.focus();}catch(e){}
			map.panTo(options.position);
			
			if( ie <= 6 )
				map.setZoom(zoomLevel);
		});
		var idleIcon = pushPin.getIcon();
	}
	if(options.sidebarItem){
		pushPin.sidebarButton = new SidebarItem(pushPin, options);
		pushPin.sidebarButton.addIn("sidebar");
	}
	markerBounds.extend(options.position);
	markerArray.push(pushPin);
	return pushPin;
}

google.maps.event.addListener(map, "click", function(){
	infoWindow.close();
});

/**
 * Creates an sidebar item 
 */
 var btnCnt = 0;
 var btnWidth = 200;
 var btnHeight = 30;
 var btnHeight2 = 50;
 
function SidebarItem(marker, opts){
	
	var tag = opts.sidebarItemType || "button";
	var row = document.createElement(tag);
	row.className = opts.sidebarItemClassName;	
	if(opts.sidebarParent){
		row.id = opts.sidebarId;
		row.innerHTML = "<span>" + opts.sidebarItem + "</span>";
	} else {
		row.innerHTML = "<span class='" + opts.sidebarItemIcon + "'>"+ opts.sidebarItem + "</span>";
	}
	row.style.display = "block";
	row.style.width = btnWidth + "px";
	if(opts.sidebarItem.match(/<br/i) != null){/*改行タグがあるときは2行扱い*/
		row.style.height = btnHeight2 + "px";
		row.style.paddingLeft = "22px";
	}else{
		row.style.height = btnHeight + "px";
	}
	row.onclick = function(){
		google.maps.event.trigger(marker, 'click');
	}
	row.onmouseover = function(){
		google.maps.event.trigger(marker, 'mouseover');
	}
	row.onmouseout = function(){
		google.maps.event.trigger(marker, 'mouseout');
	}
	this.button = row;
	
	btnCnt ++;
	
}
// adds a sidebar item to a <div>
SidebarItem.prototype.addIn = function(block){

	if(block && block.nodeType == 1)this.div = block;
	else
		this.div = document.getElementById(block)
		|| document.getElementById("sidebar")
		|| document.getElementsByTagName("body")[0];
	this.div.appendChild(this.button);	
}
// deletes a sidebar item
SidebarItem.prototype.remove = function()
{
	if(!this.div) return false;
	this.div.removeChild(this.button);
	return true;
}

/**
 * sidebar open/close controller
 */
var currentSelect = "km01";  //初期はYKK関連を表示
$j = jQuery;
$j(function(){ 
	$j('button.btn_parent_child').hide();
	$j('button.btn_parent:first span').addClass("open");
	$j('button.' + currentSelect).show();

	$j('button.btn_parent').click(function () {
		var openTeam = $j(this).attr("id");
		if(openTeam != currentSelect) {
			$j('button.btn_parent span').removeClass("open");
			$j('span', this).addClass("open");
			$j('button.btn_parent_child').hide();
			setTimeout(function() {
				$j('button.btn_parent_child.'+openTeam).css({opacity: 0, height:"1px"}).show().animate({ height: btnHeight+"px",opacity: 1}, 400 );
				$j('button.btn_parent_child.'+openTeam).each(function(){
					if ($j(this).html().match(/<br/i) != null){ //改行タグがある時の追加処理
						$j(this).animate({ height: btnHeight2+"px",opacity: 1}, 400 ); 
					}
				});
			}, 100);
			currentSelect = openTeam;
		}
		//alert(currentSelect);
	});

	$j('button.btn_parent_child').mouseover(function(){
		$j('span', this).toggleClass("ro");
	})
	.mouseout(function(){
		$j('span', this).toggleClass("ro");
	});

});

