/* 
 * library for google maps, can be extends by external methods
 * author: Jakub Krajniak
 * 
 */

gmapsLib = {
    config: {
        whichControl: 'large',
        showSmallMap: true,
        showControlMap: true,
        mapType: G_HYBRID_MAP
    },
    defaulConfig: {
        css: {
            windowClass: "mapWindow"
        }
    },
    map: {
        map:0,
        control:0,
        mlat:0,
        mManager:0,
        defaultZoom: 13,
        markersListParm: [],
        markersList: []
    },
    init: function(config) {
        if(config)
            jQuery.extend(gmapsLib.config,config)
        
        mapElement = $(this.config.mapId).css(this.config.mapCss).addClass(this.config.mapClass)
        this.map.map = new GMap2(document.getElementById(mapElement.attr('id')))

        if(this.config.whichControl == 'small')
            this.map.control = new GSmallMapControl();
        else if(this.config.whichControl == 'large')
            this.map.control = new GLargeMapControl()
        this.map.map.addControl(this.map.control)
        
        if(this.config.showSmallMap)
            this.map.map.addControl(new GOverviewMapControl());
        
        if(this.config.showControlMap) 
            this.map.map.addControl(new GMapTypeControl());
        
        this.map.map.setMapType(this.config.mapType)
        this.map.map.enableScrollWheelZoom()
        
        this.map.mlat = new GLatLng(52.408650024616556, 16.934995651245117); // center of Poznan
        this.map.map.setCenter(this.map.mlat,this.map.defaultZoom)
        this.map.mManager = new MarkerManager(this.map.map, {trackMarkers:true});
    },
    randomIcon: function() {
      /** random color of marker */
      var rand_no = Math.floor(10*Math.random());
      var randIdx = 0;
      for(var colorName in gmapsLib.iconColors) {
        if(randIdx == rand_no) {
          break;
        }
        randIdx++;
      }
      iconPath = gmapsLib.iconColors[colorName]
      return [colorName, iconPath]
    },
    iconColors: {
      "purple": "http://labs.google.com/ridefinder/images/mm_20_purple.png",
      "yellow": " http://labs.google.com/ridefinder/images/mm_20_yellow.png",
      "blue": " http://labs.google.com/ridefinder/images/mm_20_blue.png",
      "white": " http://labs.google.com/ridefinder/images/mm_20_white.png",
      "green": " http://labs.google.com/ridefinder/images/mm_20_green.png",
      "red": " http://labs.google.com/ridefinder/images/mm_20_red.png",
      "black": " http://labs.google.com/ridefinder/images/mm_20_black.png",
      "orange": " http://labs.google.com/ridefinder/images/mm_20_orange.png",
      "gray": " http://labs.google.com/ridefinder/images/mm_20_gray.png",
      "brown": "http://labs.google.com/ridefinder/images/mm_20_brown.png"
    },
    createIco: function(color,path) {
        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);
        if(color) {
            baseIcon.image = '/js/marker_34_'+color+'.png';
        } else {
            baseIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
        }
        if(path) {
          baseIcon.image = path
        }
        return baseIcon;
    },
    createMarker: function() {
        
    },
    addMarker: function(marker,zoom) {
        gmapsLib.map.mManager.addMarker(marker,3);
        gmapsLib.map.mManager.refresh();
        gmapsLib.map.markersList.push(marker);
        gmapsLib.map.map.setCenter(marker.getLatLng(),zoom);
        gmapsLib.map.map.setZoom(zoom)
    },
    loadMarkers: function(data) {
        xml = $(data)
        markers = xml.find('place')
        gmapsLib.map.markersList = []
        gmapsLib.map.mManager.clearMarkers()
        gmapsLib.map.markersListParm = []
        markers.each(function(){
            place = $(this)
            try {
                lon = place.find('place_lon').eq(0).text()
                lat = place.find('place_lat').eq(0).text()
                pname = place.find('place_name').eq(0).text()
                pnameshort = place.find('place_name_short').eq(0).text()
                try {
                    var paddress = place.find('place_address').eq(0).text()
                } catch(e) {
                    var paddress = "nieznany";
                }
                pid = place.find('place_id').eq(0).text()
                safe = place.find('place_safe_name').eq(0).text()
                parm = { 
                    pllon: lon, 
                    pllat: lat, 
                    plname: pname,
                    plsafe: safe,
                    pladdress: paddress,
                    plpid: pid
                }
                
                if(gmapsLib.config.beforeMarkerCreate)
                    gmapsLib.config.beforeMarkerCreate(parm)
                
                marker = gmapsLib.config.createMarker(parm);
                
                if(gmapsLib.config.afterMarkerCreate)
                    gmapsLib.config.afterMarkerCreate(parm)
                    
                gmapsLib.map.markersList.push(marker)
                gmapsLib.map.markersListParm.push({
                    'marker': marker,
                    'parm': parm
                })
            } catch(e){
                
            }
        })
        if(this.config.afterMarkersCreate)
            this.config.afterMarkersCreate(gmapsLib.map.markersListParm)
        this.map.mManager.addMarkers(gmapsLib.map.markersList,10);
        this.map.mManager.refresh();
    },
    afterCreate: function() { },
    addInfoWindows: function(data) {
        $.each(data,function(){
            var infoWindow = "<b><a href=\"/miejsca/"+this.parm.plsafe+"\">" + this.parm.plname + 
                         "</a></b> <br />"+ this.parm.pladdress + '<br/>' + 
                         "<a href=\"/miejsca/"+this.parm.plsafe+"\">czytaj więcej...</a>"
            var infoWindow = '<div class="' + gmapsLib.defaulConfig.css.windowClass +'">' + infoWindow + '</div>'
            var marker = this.marker
            GEvent.addListener(marker, 'click', function(){
                gmapsLib.map.map.setZoom(17)
                gmapsLib.map.map.panTo(marker.getLatLng())
                marker.openInfoWindowHtml(infoWindow)
            })
        })
    }
}
