var map;
var numberArray = new Array();
var lendingArray = new Array();
var icon = new GIcon();

var markerArray =[];
var lastMarker;

var debugFlag;

var icon;
icon = new GIcon();
icon.image = "/images/map/marker.png";
icon.shadow = "/images/map/marker_shadow.png";
icon.iconSize = new GSize(26, 37);
icon.shadowSize = new GSize(38, 37);
icon.iconAnchor = new GPoint(26, 0);

var iconActive;
iconActive = new GIcon();
iconActive.image = "/images/map/marker_over.png";
iconActive.shadow = "/images/map/marker_shadow.png";
icon.iconSize = new GSize(26, 37);
icon.shadowSize = new GSize(38, 37);
icon.iconAnchor = new GPoint(13, 35);


function mapload (){
	debugFlag = false;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map")); 
		map.setCenter(new GLatLng(38,139), 9);
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMenuMapTypeControl(true, false));
		map.setMapType(G_PHYSICAL_MAP);

		createArray();
		
		lendingPoints();
	}
}

function createMarks(){
	for (var i=0; i < numberArray.length; i++) {
		var tempNum = numberArray[i];
		markPoint(tempNum,lendingArray[tempNum].latlng,lendingArray[tempNum].title + "<br />" +lendingArray[tempNum].address);
	}
	
	if(!debugFlag){
		selectCloseup();
		selectIroiro();
	}
}

function selectCloseup(){
	var closeupNum = Math.floor(Math.random()*numberArray.length);
	var tempNum = numberArray[closeupNum];
	var marker = map.getMarkerById(tempNum);
	GEvent.trigger(marker,'click');
}

function markPoint(num,latlng,info){
	
	latlng = latlng.split(',');
	var point = new GLatLng(latlng[1], latlng[0]);
	var markPoint = new PdMarker(point,icon);
	
	var html = '<table width="180" border="0" cellspacing="0" cellpadding="0"><tr><td width="9"><img src="/images/map/top_l.png" width="9" height="6" /></td><td class="markerText_top"></td><td width="9"><img src="/images/map/top_r.png" width="9" height="6" /></td></tr><tr><td class="markerText_left"><img src="/images/map/arrow.gif" width="9" height="5" /></td><td class="markerText">' + info + '</td><td class="markerText_right"></td></tr><tr><td><img src="/images/map/bottom_l.png" width="9" height="8" /></td><td class="markerText_bottom"></td><td><img src="/images/map/bottom_r.png" width="9" height="8" /></td></tr></table>';
	markPoint.setTooltip(html);
	
	markPoint.setId(num);

	map.addOverlay(markPoint);
				
	GEvent.addListener(markPoint,"click",function(){
		if(!debugFlag){
			htmlChange(num, markPoint);
		}
		lastMarker = num;
		});
	GEvent.addListener(markPoint, "mouseover", function() {
		markPoint.topMarkerZIndex();
		markPoint.setImage(iconActive.image);
		}); 
	
	GEvent.addListener(markPoint, "mouseout", function() {
		if (lastMarker != num) {
			markPoint.restoreMarkerZIndex();
			markPoint.restoreImage(icon.image);
			}
		});
		
			
}

function pointInformation(title, address, basename, latlng, category, description, table, url) {
	this.title = title;
	this.address = address;
	this.basename = basename;
	this.latlng = latlng;
	this.category = category;
	this.description = description;
	this.table = table;
	this.url = url;
}

function htmlChange(x,marker){
	$('change-image').src = "/lending/profile/" + lendingArray[x].basename + "/img/basic.jpg";
	$('change-title').href = lendingArray[x].url;
	$('change-title').innerHTML = lendingArray[x].title;
	$('change-category').innerHTML = lendingArray[x].category;
	$('change-description').innerHTML = lendingArray[x].description;
	$('change-excerpt').innerHTML = lendingArray[x].table;
	$('change-url').href = lendingArray[x].url;	
	
	var zoomlevel = map.getZoom();
	if (zoomlevel != 9){
		map.setZoom(9);
	}
	
	var latlng = lendingArray[x].latlng;
	latlng = latlng.split(',');
	var num = Number(latlng[0]);
	num = num + 0.19;
	
	var point = new GLatLng(latlng[1], num);
	map.panTo(point);
	
	var omarker = map.getFirstMarker();
	while (omarker != null)
	{
		if(omarker != marker) {
			omarker.setTooltipHiding(true);
			omarker.hideTooltip();
			omarker.restoreImage(icon.image);
		}
		omarker = map.getNextMarker();
	}
	
	marker.setImage(iconActive.image);
	marker.setTooltipHiding(false);
	marker.showTooltip();
	
}


function selectIroiro(){
	var list = $("change-iroiro");
	
	shuffle(numberArray);
	
	for (var i=0; i < list.childNodes.length; i++) {
			if (list.childNodes[i].nodeName == "DIV") {
				if(document.all){
					//サムネイル画像
					list.childNodes[i].childNodes[0].childNodes[0].childNodes[0].src = "/lending/profile/" + lendingArray[numberArray[i]].basename + "/img/thumbnail.jpg";
					//サムネイルhref
					list.childNodes[i].childNodes[0].childNodes[0].href = lendingArray[numberArray[i]].url;
					//h5リンク先
					list.childNodes[i].childNodes[1].childNodes[0].childNodes[0].href = lendingArray[numberArray[i]].url;
					//h5タイトル
					list.childNodes[i].childNodes[1].childNodes[0].childNodes[0].innerHTML = lendingArray[numberArray[i]].title;
					//カテゴリーリスト
					list.childNodes[i].childNodes[1].childNodes[1].innerHTML = lendingArray[numberArray[i]].category;
					//本文
					list.childNodes[i].childNodes[1].childNodes[2].innerHTML = lendingArray[numberArray[i]].description;
				}else{
					//サムネイル画像
					list.childNodes[i].childNodes[1].childNodes[1].childNodes[0].src = "/lending/profile/" + lendingArray[numberArray[i]].basename + "/img/thumbnail.jpg";
					//サムネイルhref
					list.childNodes[i].childNodes[1].childNodes[1].href = lendingArray[numberArray[i]].url;
					//h5リンク先
					list.childNodes[i].childNodes[3].childNodes[1].childNodes[0].href = lendingArray[numberArray[i]].url;
					//h5タイトル
					list.childNodes[i].childNodes[3].childNodes[1].childNodes[0].innerHTML = lendingArray[numberArray[i]].title;
					//カテゴリーリスト
					list.childNodes[i].childNodes[3].childNodes[3].innerHTML = lendingArray[numberArray[i]].category;
					//本文
					list.childNodes[i].childNodes[3].childNodes[5].innerHTML = lendingArray[numberArray[i]].description;
				}
			}
		}
}


function shuffle(array) {
  var i = array.length;

  while (--i) {
    var j = Math.floor(Math.random() * (i + 1));
    if (i == j) continue;
    var k = array[i];
    array[i] = array[j];
    array[j] = k;
  }

  return array;
}