/*
 * 本著作係依據創用 CC 姓名標示-相同方式分享 2.5 台灣 授權條款進行授權。
 * 如欲瀏覽本授權條款之副本，請造訪 http://creativecommons.org/licenses/by-sa/2.5/tw/
 * 或寄信至 Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA。
 *
 * This work is licensed under the Creative Commons Attribution-Share Alike 2.5 Taiwan License.
 * To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/tw/
 * or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
 *
 * tinyMap - 自訂個人 Google Map 的 jQuery 擴充套件
 * http://app.essoduke.org/tinyMap/
 *
 * Changelog
 *  1.0.1:
 *    #修正部份程式錯誤
 *    #改以 Google AJAX API 呼叫 map
 *  1.0:
 *    # Release
 *
 * Fri, 30 October 2009 04:23:05 GMT
 */
(function($) {
  $.fn.extend({
    tinymap: function(options) {
      var _ = $(this);
      var o = $.extend({
        width: 640,
        height: 480,
        latlng: [22, 32],
        control: true,
        zoom: true,
        zoomLevel: 13,
        mapType: '',
        marker: []
      }, options || {});
      if ( google.maps.BrowserIsCompatible && google.maps.Map2 ){
        if( o.latlng.constructor === Array ){
          var point = new google.maps.LatLng( o.latlng[0], o.latlng[1] );
          if( o.latlng[0] == 22 && o.latlng[1] == 32){ o.zoomLevel = 2; }
          $.fn.tinymap.init( _, point, o );
        }
        else{
          var geocoder = new google.maps.ClientGeocoder();
          if (geocoder) {
            geocoder.getLatLng( o.latlng, function(point) {
              if(typeof point === 'object'){
               $.fn.tinymap.init( _, point, o );
              }
            });
          }
        }
      }
      else {
        alert( 'Google Map API isn\' loaded' );
      }
      $(window).unload(function(){ google.maps.Unload(); });
    }
  });
  
  /* initialize the map */
  $.fn.tinymap.init = function(_, point, o){
    var gmap = _.css({'width': o.width, 'height': o.height})[0];
    var map = new google.maps.Map2(gmap);
    var mapType = '';
    var c = o.marker.length;
    switch( o.mapType ){
      case 'default': mapType = G_DEFAULT_MAP_TYPES; break;
      case 'hybrid' : mapType = G_HYBRID_MAP ; break;
      case 'satellite': mapType = G_SATELLITE_MAP; break;
      default: mapType = ''; break;
    }
    if( o.control ){ map.addControl(new google.maps.LargeMapControl()); }
    if( o.zoom ){ map.addControl(new google.maps.MapTypeControl()); }
    
    map.addMapType( G_PHYSICAL_MAP );
    
    if( mapType !== '' ){ map.setMapType( mapType ); }
    if( c > 0 ){
      //js closure to pass arguments
      var draw = function(i){
        return function( point ){
          if(typeof point === 'object'){
            var ico = new google.maps.Icon(G_DEFAULT_ICON);
            var icon = o.marker[i].icon;
            if (icon){
              ico.image = ( icon ) ? icon[2] : 'http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png';
              ico.iconSize = new google.maps.Size( icon[0], icon[1] );
            }
            var markerOptions = {'icon':ico};
            var ico_mark  = new google.maps.Marker( point, markerOptions);
            google.maps.Event.addListener(ico_mark, 'click', function(){
              ico_mark.openInfoWindowHtml( o.marker[i].text );
            });
            map.addOverlay( ico_mark );
            ico = null;
          }
        };
      };
      for(var i=0; i<c; i++){
        var geocoder = new google.maps.ClientGeocoder();
        var addr = o.marker[i].addr;
        var latlng = o.marker[i].latlng;
        var location = (addr !== 'underfined' & addr !== '' ) ? addr : ( latlng !== 'underfined' && latlng.length == 2 ? latlng : null);
        if(geocoder){
          geocoder.getLatLng(location, draw(i));
        }
      }
    }
    map.setCenter(point, o.zoomLevel);
  };
  $.fn.tinymap.addPointText = function(point, text){
    GEvent.addListener(point, 'click', function() {
      point.openInfoWindowHtml( text );
    });
    return point;
  };
})(jQuery);

