WaMap = function(){}

WaMap.prototype =
{
	map: null,
	marker: null,
	point: null,
	sizeControl: null,
	typeControl: null,
	overviewControl: null,
	scaleControl: null,
	zoom: 14,
	messages: null,
	drag: true,
	geocoder: new GClientGeocoder()
}

WaMap.prototype.base = function(divName){
    if(!divName) divName='map';
  	this.map = new GMap2(document.getElementById( divName ));
  	
  	var defaultPoint = new GLatLng(25.03314, 121.561838);
  	this.map.setCenter(defaultPoint,this.zoom);
}

WaMap.prototype.getLatLng = function(address,marker,markerSrc,information){
    var mm = this;
    switch(address){
    	case 'wabow':
    	case '哇寶':
    	case 'neo':
    		address = '台北市信義區基隆路一段143號';
    		break;
    	case 'abu':
    		address = '台北縣板橋市貴興路42號';
    		break;
    	case 'stilla':
    	case 'pipi':
    	case 'wawa':
    		address = '台北縣基隆市';
    		break;
    	default:
    		break;    
    }
    if(address){
        this.geocoder.getLatLng( address , function(point) {
                               if (!point) {	
                                      alert("搜尋"+ address +"失敗！");
                               }else{
                                    mm.point = point;
                                    mm.map.setCenter(point);
                                    if(mm.marker) mm.removeMarker();  
                                    if(information!=null) mm.messages = information;
                                    if(marker=='setup' && markerSrc!=null){
                                        mm.addMarkerPersonalization(markerSrc);
                                    }
                                    else{
                                        mm.addMarker();
                                    }
                                    $('#load').html('搜尋成功');
                                    $('#load').show(); //顯示load完畢DIV，並設TimeOut 2秒後隱藏
                                    $('#address').val(address);
                                    setTimeout("$('#load').hide()",1000);
                               }
        });
    }   
}

WaMap.prototype.addControl = function(size,delObj){
    if(!size) size='1';
    var addObj=null;
    switch (size) {
        case '0' :
            if(!delObj) delObj = this.sizeControl;
            else delObj = eval('this.'+delObj);            
            this.map.removeControl(delObj);
            break;
        case '1' :
            if(this.sizeControl!=null) this.addControl('0');
            this.sizeControl = new GSmallMapControl(); addObj = this.sizeControl; break;
        case '2' :               
            if(this.sizeControl!=null) this.addControl('0');      
            this.sizeControl = new GLargeMapControl(); addObj = this.sizeControl; break;
        case '3' :       
            if(this.sizeControl!=null) this.addControl('0');      
            this.sizeControl = new GSmallZoomControl(); addObj = this.sizeControl; break; 
        case '4' :                     
            if(this.scaleControl!=null) this.addControl('0');      
            this.scaleControl = new GScaleControl(); addObj = this.scaleControl; break; 
        case '5' :                     
            if(this.typeControl!=null) this.addControl('0','typeControl');
            this.typeControl = new GMapTypeControl(); addObj = this.typeControl; break;
        case '6' :                     
            if(this.overviewControl!=null) this.addControl('0','overviewControl');
            this.overviewControl = new GOverviewMapControl(); addObj = this.overviewControl; break;
       default:break;
    } 
    if(addObj!=null) this.map.addControl(addObj);   
    
           
}

WaMap.prototype.addMarker = function() {
    $(".marker[@value='1']").attr('checked','1');    //對應主頁面指標選項，強迫選取為"預設"之選項
    $('#div_mapinfo').show();                        //顯示訊息輸入框


    if(this.marker){
        this.removeMarker();
    }

    if(this.point==null) this.point = this.map.getCenter();
    
    this.marker = new GMarker(this.point, {draggable: true});        
    this.map.addOverlay(this.marker);
//     this.map.setCenter(this.point);  
    if(this.drag == true){
        this.__dragstart();
        this.__dragend();
    }else{
        this.marker.disableDragging();        
    }
    this.addInfo(this.messages);
}

WaMap.prototype.removeMarker = function() {
    if(this.marker){
        this.map.removeOverlay(this.marker);
        this.marker = null;
    }  
}

//自訂指標圖樣
imgProcessing = false;
WaMap.prototype.addMarkerPersonalization = function(imgSrc){
    if(imgProcessing == true){
        $('#markerSrc').val('');
        alert("系統忙碌中，請稍後再試");
        return;
    }
    me = this;
    var img = new Image();
    imgProcessing = true;
    
    if(this.marker){
        this.map.removeOverlay(this.marker);
        this.marker = null;
    }
    
    if(this.point==null) this.point = this.map.getCenter();
    
    img.onload = function(){
        widthAnchor  = img.width/2;
        heightAnchor = img.height/2;
        
        var resetIcon = new GIcon();    
        resetIcon.image = img.src;
        resetIcon.iconSize   = new GSize(img.width,img.height);
        resetIcon.iconAnchor = new GPoint(widthAnchor,heightAnchor);
        resetIcon.infoWindowAnchor = new GPoint(img.width, 1);
        markerOptions = { icon:resetIcon , draggable: true };
        
        me.marker = new GMarker(me.point,markerOptions);   
        me.map.addOverlay(me.marker);
//         me.map.setCenter(me.point);  
        
        if(me.drag == true){
            me.__dragstart();
            me.__dragend();
        }else{
            me.marker.disableDragging();        
        }
        
        me.addInfo(me.messages);
        
        imgProcessing = false;
    };
    
    img.onerror = function () {
        imgProcessing = false; 
    };
    
    img.src = imgSrc;
}

WaMap.prototype.__dragstart = function(){
    var mm = this;
    if(this.marker)
        GEvent.addListener(this.marker, "dragstart", function() {                  
                                                       mm.map.closeInfoWindow();  
                                                     }
        );
}

WaMap.prototype.__dragend = function(){
    var mm = this;
    if(this.marker)
         GEvent.addListener(this.marker, "dragend", function() {             
                                                        mm.map.panTo(mm.marker.getPoint());  
                                                        mm.point = mm.marker.getPoint();
														  														                                    
//                                                         mm.addInfo(mm.messages);
														//使用timeout方式，delay0.5秒，使指標和訊息能夠順利置中   
                                                        mm.sleepInfo();
                                                    }
        );    

}

WaMap.prototype.sleepInfo = function(){
	setTimeout("wa.addInfo(wa.messages);",500);
}

WaMap.prototype.addInfo = function(messages){
    this.map.closeInfoWindow() 
    
    if(this.marker && messages){ 
        this.marker.openInfoWindowHtml(messages);
    }
}

WaMap.prototype.__moveend = function(){
    var mm = this;
    if(this.map)
        GEvent.addListener(this.map, "moveend", function() {  
        //                              
                                                }
        );
}

WaMap.prototype.__click = function(remove){
    var mm = this;
    if(this.map)
       GEvent.addListener(this.map, "click", function(marker, point) {  
                                                if (marker) {    
                                                    mm.map.removeOverlay(marker);  
                                                } else {        
                                                    mm.marker.setLatLng(point);                                                      
                                                }
                                             }       
       );
}

WaMap.prototype.__dblclick = function(){    
    var mm = this;
    if(this.map)
       GEvent.addListener(this.map, "dblclick", function(marker, point) {  
                                                    if(mm.marker){
                                                       // mm.point = mm.map.getCenter(); 
                                                        mm.marker.setLatLng(point);
                                                        mm.map.panTo(point);
                                                        mm.addInfo();  
                                                    } 
                                                }       
       );
}

