/*
make this possible to compile.and make new html source named "prototypeOfficeGuide.html"
*/
//--------------------------------------------

function SetIcon(){
    var baseIcon = new GIcon();
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20,34);
    baseIcon.shadowSize = new GSize(37,34);
    baseIcon.iconAnchor = new GPoint(9,34);
    baseIcon.infoWindowAnchor = new GPoint(9,2);
    baseIcon.infoShadowAnchor = new GPoint(18,25);
    return baseIcon;
}
//--------------------------------------------
function createMarker(point,str,icon){
    var marker = new GMarker(point,icon);
    GEvent.addListener(marker,"click",function(){
	    marker.openInfoWindowHtml("<pre>"+str+"</pre>");});
    return marker;
}
//--------------------------------------------
var LineDescription = Class.create();
LineDescription.prototype={
    timer:null,
    point:null,
    map:null,
    count:null,
    initialize:function(map,point,timer){
	this.map = map;
	this.point = point;
	this.timer = timer;
	this.count = point.length;
    },
    drawLine:function(){
	if(this.count > 1){
	    var p1 = this.point[0];
	    var p2 = this.point[1];
	    var ptr =[];
	    ptr.push(p1);
	    ptr.push(p2);
	    this.count -= 1;
	    setTimeout(this.drawLine.bind(this),this.timer);
	    this.map.recenterOrPanToLatLng(this.point[1]);
	    this.map.addOverlay(new GPolyline(ptr));
	    this.point.shift();
	}else
	    return;
    }
};
var DefaultMap = Class.create();
DefaultMap.prototype = {
    baseIcon:null,
    map:null,
    home:null,    
    initialize:function(path){
        this.dataPath = path;
        this.setUpMap();
        

    },
    setUpMap:function(){
	this.baseIcon = SetIcon();
	this.map = new GMap($("map"));
	this.home = new GPoint(139.73732,35.654977);
	this.map.centerAndZoom(this.home, 0);
	this.setMarkerData();	
        this.map.addControl(new GMapTypeControl());
        this.map.addControl(new GSmallMapControl());
    },
    setMarkerData:function(){
	var map = this.map;
	var baseIcon = this.baseIcon;
	var markRequest = GXmlHttp.create();

	markRequest.open("GET",this.dataPath,true);
	markRequest.onreadystatechange= function(){
	    if(markRequest.readyState == 4){
		var xmlDoc = markRequest.responseXML;
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");

		for(var i = 0 ; i < markers.length ; i++){
		    var str = markers[i].getAttribute("str");
		    var letter = String.fromCharCode("A".charCodeAt(0)+i);
		    var icon = new GIcon(baseIcon);
		    icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
		    var point = new GPoint(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));		    
		    map.addOverlay(createMarker(point,str,icon));
		}
	    }
	}
	markRequest.send(null);
    },
    getMapData:function(){
	return this.map;
    }
    
}
//--------------------------------------------
var GlucoseMap = Class.create();
GlucoseMap.prototype = {
    filename:null,
    //    baseIcon:null,
    //    map:null,
    //    home:null,
    //    ptr:null, no need ,maybe
    timer:null,
    initialize:function(filename,showFlag){
	//	this.setUpMap();	
	this.timer = 1500;
	var m = new DefaultMap();
	this.map = m.getMapData();
        if(filename != null)
	    this.filename = filename;
	this.showFlag = showFlag;	
    },
    setUpCoord:function(){
	var pointRequest = GXmlHttp.create();
	var coord = [];
	var map = this.map;
	var timer = this.timer;
	pointRequest.open("GET",this.filename,true);
	pointRequest.onreadystatechange=function(){
	    if(pointRequest.readyState == 4){
		var point = new Array();
		var coordXML = pointRequest.responseXML;
		coord = coordXML.documentElement.getElementsByTagName("point");
		for(var i = 0 ; i < coord.length ; i++)
		    point.push(new GPoint(coord[i].getAttribute("lat"),coord[i].getAttribute("lng")));		
		var lineMaker = new LineDescription(map,point,timer);
		map.recenterOrPanToLatLng(point[0]);
		setTimeout(lineMaker.drawLine.bind(lineMaker),3000);
	    }
	}
	pointRequest.send(null);
    },
    execute:function(){	
        if(this.showFlag){
            //this.map.addControl(new GSmallMapControl());
	    //	    this.setMarkerData();	
	    this.setUpCoord();
	}
    }

}
