//// PlaceEngine support functions
//// Copyright(C) 2006 Jun Rekimoto
//// $Id: pe.js,v 1.11 2007/07/27 14:21:53 rekimoto Exp $
function from10(x){
  var s1 = Math.floor(x);
  var ss = x - s1;
  var s2 = Math.floor(ss*60.0);
  var s3 = (ss - s2/60)*3600;
  return "" + s1 + "/" + s2 + "/" + (Math.floor(s3*100)/100);
}
function from10dot(x){
  var s1 = Math.floor(x);
  var ss = x - s1;
  var s2 = Math.floor(ss*60.0);
  var s3 = (ss - s2/60)*3600;
  return "" + s1 + "." + s2 + "." + (Math.floor(s3*100)/100);
}
function maplink(name, mapx, mapy, mapz){
  var cx = Math.floor(mapx * 100000) / 100000;
  var cy = Math.floor(mapy * 100000) / 100000;
  var wy  = cy  + 0.00010696 *cy - 0.000017467*cx - 0.0046020;
  var wx = cx + 0.000046047*cy + 0.000083049*cx - 0.010041;
  var wy1 = from10(wy);
  var wx1 = from10(wx);
  var wy2 = from10dot(wy);
  var wx2 = from10dot(wx);
  var z = mapz;
  if (name=='googlelocal') {
     location.href = "http://local.google.co.jp/local?f=l&hl=ja&sll=" + cy + "," + cx; //   + "&spn=" + sy + "," + sx;
  } else if (name=='yahoo') {
     location.href = "http://area-search.yahoo.co.jp/bin/search?nl=" + wy1 + "&el=" + wx1;
  } else if (name=='richtags') {
     location.href = "http://www.richtags.com/bookmark/map?x=" + cx + "&y=" + cy + "&sz=" + z;
  } else if (name=='alps') {
     location.href = "http://base.alpslab.jp/?s=25000;p=" + wy1 + "," + wx1;
  } else if (name=='mapion') {
location.href = 'http://www.mapion.co.jp/c/f?uc=1&grp=all&nl=' + wy1 + '&el=' + wx1 + '&scl=25000&bid=Mlink'
  } else if (name=='mapfan') {
location.href = 'http://www.mapfan.com/index.cgi?MAP=E' + wx2 + 'N' + wy2 + "&ZM=9"
  } else if (name=='mapion_station') {
    location.href = 'http://www.mapion.co.jp/c/f?uc=18&vp=5&leg=20000&nl=' + wy1 + '&el=' + wx1 + '&grp=station';
  } else if (name=='mapion_bus') {
    location.href = 'http://www.mapion.co.jp/c/f?uc=17&vp=5&leg=20000&nl=' + wy1 + '&el=' + wx1 + '&grp=all&gnr=241&BT=Ngok01_02';
  } else if (name=='chizumaru') {
    location.href = 'http://www.chizumaru.com/maplink.asp?SER=all&D=all&E=' + wx2 + '&N=' + wy2 + '&SCL=500';
  } else if (name=='goo') {
    location.href = 'http://map.goo.ne.jp/map.php?MAP=E' + wx2 + 'N' + wy2 + '&MT=%C5%EC%B5%FE%C5%D4%C9%CA%C0%EE%B6%E8%C5%EC%B8%DE%C8%BF%C5%C4%A3%B3%C3%FA%CC%DC&ZM=9'
  } else if (name=='gnavi') {
    location.href = 'http://map.gnavi.co.jp/search.php?nl=' + wy + '&el=' + wx + '&scl=25000&map1=13&map2=13109&pg=10&lk=0&kencode='
  } else if (name=='tabelog') {
    location.href = 'http://r.tabelog.com/rstmap.asp?clat=' + cy + '&clon=' + cx + '&lz=1';
  } else if (name=='ekitanlab') {
      location.href = 'http://lab.ekitan.com/scroll/index.shtml?nl=' + cy + '&el=' + cx + '&gs=wgs';
  } else if (name=='petamap') {
     location.href = "http://petamap.jp/?lng=" + cx + "&lat=" + cy + "&zoom=" + (17 - z);
  } 
      
}

function ll2str(x,y) {
    var cx = Math.floor(x * 10000) / 10000;
    var cy = Math.floor(y * 10000) / 10000;
    return (cy>0?"N":"S") + Math.abs(cy) + " " + (cx>0?"E":"W") + Math.abs(cx);
}
///// cookies
function getCookie(name) {
    var ca = document.cookie.split(";");
    name += '='
	for (i=0; i<ca.length;i++) {
	    c=ca[i];
	    while (c.charAt(0)==' ') c=c.substring(1);
	    if (c.indexOf(name)==0) return c.substring(name.length,c.length)
	}
    return null;
}
//// set cookie
function setCookie(name, val) {
    var date = new Date();
    date.setTime(date.getTime()+(14*24*3600*1000));
    var expire = "; expires=" + date.toGMTString();
    document.cookie = name +"="+val + expire + "; path=/"
	}
//// startsWith, endsWith
String.prototype.startsWith = function(match) {
    if (match.length > this.length) return false;
    return match == this.substring(0, match.length);
}
String.prototype.endsWith = function(match) {
    if (match.length > this.length) return false;
    return match == this.slice(~match.length + 1);
}
// ctrl: add map control
function mkMap(name, ctrl, x, y, s, pin){
    var map = new GMap2($(name));
    if (ctrl==2) {
       map.addControl(new GLargeMapControl());
       map.addControl(new GScaleControl());
    } else if (ctrl>0) {
       map.addControl(new GSmallMapControl());
    } 
    map.setCenter(new GLatLng(y, x), 17-s);
    if (pin) map.addOverlay(new GMarker(new GLatLng(y,x)));
    //    GEvent.addListener(map, "click",
    //		       function(marker,point){map.panTo(point);});
    
    //  map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
    return map;
}
// control visibility
function toggleShow(id) {
    var d = $(id);
    if (d.style.visibility!="visible") {
	d.style.visibility="visible";
    } else {
	d.style.visibility="hidden";
    }
}
//// geographical distance 
function geoDist(x,y,x2,y2){
    with(Math) {
	var x = (x * PI)  / 180.0;
	var y = (y * PI)  / 180.0;
	var x2 = (x2 * PI)  / 180.0;
	var y2 = (y2 * PI)  / 180.0;
	var cs = cos(y)*cos(y2)*cos(x-x2) + sin(y)*sin(y2);
	return acos(cs) * 6378.1; // 6369.0  Km
    }
}
//// geocoding
function geocode(map, en) {
    var str = prompt(en ? "Address Search (ex: 'New York')" : "住所検索 (ex:銀座 東京タワー Paris)");
    if (str) {
	var g = new GClientGeocoder();
	var p = g.getLatLng(str, function(p){
	    if (p) {
		map.setCenter(p);
	    } else {
		alert(en ? '"' + str + "\" did not mach any locations." : "住所検索に失敗しました");
	    }
	});
    }
}
