
//###################################################################
//## Tworzenie markerów
//###################################################################

// funkcja tworzaca marker klasy FPMarker
function createMarker(id, lat, lng, html, options)
{
  if(lat <= -1000 || lat >= 1000){
    lat /= 1000000.0; // skalujemy wsp. z fixedpointa na float 
  } 
  if(lng <= -1000 || lng >= 1000){
    lng /= 1000000.0; // skalujemy wsp. z fixedpointa na float
  }
  
  var marker;
  
  if (null == options) {
    options = {};
  }
  
  //-- p8R - obsluga wartosci domyślnych ustawianych przez funkjcię setOption (rozszerzona funkcjonalnosc)
  if (createMarker.handleDefaults) {
    options = createMarker.handleDefaults( options );
  } 
  //--| 
  
  if(options.icon == null){
    options.icon = G_DEFAULT_ICON; // domyslna ikonka markera w Google Maps
  }
  
  if(typeof options.icon == "string" && '' != options.icon){
    var iconFileName = options.icon;
    options.icon = new GIcon(G_DEFAULT_ICON, options.icon); // zamieniamy sciezke do bitmapki na wlasciwosc obiektu GIcon
    if (!iconFileName.match( /google/ )) {
      /*
      options.icon.iconSize = new GSize(32, 32);
      options.icon.shadow   = options.iconShadow;
      options.icon.shadowSize = new GSize(59, 32);
      options.icon.imageMap   = [0, 0, 0, 32, 32, 32, 32, 0];
      options.icon.infoWindowAnchor = new GPoint(20, 10);
      options.icon.infoShadowAnchor = new GPoint(32, 32);
      //*/
      options.icon.iconSize = new GSize(16, 16);
      options.icon.shadow   = options.iconShadow;
      options.icon.shadowSize = new GSize(0, 0);
      options.icon.imageMap   = [0, 0, 0, 16, 16, 16, 16, 0];
      options.icon.infoWindowAnchor = new GPoint(0, 0);
      options.icon.infoShadowAnchor = new GPoint(0, 0);
    }
  }
  
  marker = new GMarker(new GLatLng(lat,lng), options);
  
  marker.id = id;
  marker.html = html;
  marker.options = options;
  
  // rejestrujemy ewentualne zdarzenia
  if(options != null && options.listeners != null){
    for(var i in options.listeners){
      GEvent.addListener(marker,i,options.listeners[i]);
    }
  }
  
  //-- p8R
  //*
  
  if ('object' == typeof createMarker.prototype) {
    for (var methodName in createMarker.prototype) {
      if ('function' == typeof createMarker.prototype[methodName]) {
        marker[methodName] = createMarker.prototype[methodName];
      }
    }
  }
  //--| p8R
  //*/
  
  return marker;

}

//###################################################################
//## Roszerzona funkcjonalnosc funkcji tworzacej markery
//###################################################################

createMarker.handleDefaults = function( options ) {
  if (null == this.defaults) return options;
  
  for (var key in this.defaults) {
    if ('undefined' != typeof options[key]) continue;
    
    options[key] = this.defaults[key];
  }
  
  return options;
}

createMarker.setOptions = function(options){
  this.defaults = options;
}

createMarker.resetOptions = function() {
  this.defaults = null;
}

createMarker.addOptions = function( options ) {
  for (var key in options) {
    this.setOption( key, options[key], false );
  }
}

/**
 * Metoda ustawia wartość domyślnej opcji markerów. Trzeci argument wywołania określa czy jeśli 
 * podana opcja ma już ustawioną wartość, to czy zostanie ona nadpisana czy nie   
 * @param {Object} key
 * @param {Object} value
 * @param {Object} overwrite - czy wartość ma być nadpisana jeśli istniej; opcjonalny, domyślnie true
 */

createMarker.setOption = function( key, value, overwrite ) {
  if (null == this.defaults) this.defaults = {};
  
  var ovrDefault = (null == overwrite || true == overwrite); 
  
  if (ovrDefault || 'undefined' == this.defaults[ key ]) {
    this.defaults[ key ] = value;
  }
}


//###################################################################
//## Rozszerzona funkcjonalność markerow
//###################################################################

/*
createMarker.prototype.xxxx = function() { ... } 
//*/

createMarker.prototype.addListener = function( eventName, handler ) {
  GEvent.addListener( this, eventName, handler );
}

createMarker.prototype.addListeners = function( functionality ) {
  for (var eventName in functionality) {
    if ('function' != typeof functionality[eventName]) return;
    
    this.addListener( eventName, functionality[eventName] );
  }
}

//########################################################################################################
//###################################################################
//## Tworzenie mapki GMap
//###################################################################
//########################################################################################################

// klasa badaca mapa na stronie
function createMap(map_id, element, lat, lng, zoom, options)
{
  var map = null;
  
  if(lat == null || lng == null){
    lat = 50.296797;
    lng = 18.673325;
  }
  if(zoom == null){
    zoom = 13;
  }
  
  if (null == options) {
    options = {};
  }
  
  if (GBrowserIsCompatible() && document.getElementById( element )) {
    map = new GMap2(document.getElementById( element ),options);
    map.setCenter( new GLatLng( lat, lng ), zoom );
    map.disableDoubleClickZoom();
    map.map_id = map_id;
    
    if (options && options.disableDragging && true == options.disableDragging) {
      map.disableDragging();
    }
    
    if (!(options && options.hideMapControl && true == options.hideMapControl)) {
      map.addControl(new GSmallMapControl());
    }
    
  }
  else {
    //--- jesli w configu jawnie nie zazadano ukrywania komunikato w bledach to je wyswietlamy
    if (!(options && options.suppressErrorMessages && true == options.suppressErrorMessages)) {
      if (null == document.getElementById( element )) {
        //alert( 'Nie znaleziono elementu kontenera dla obiektu GMap!' );
        return null;
      }
    }
  }
  
  //-- p8R
  
  //-- obsługa rejestracji listenerów wg. tutoriala ?? !!
  if (map && options && options.listeners) {
    for (var listenerName in options.listeners) {
      GEvent.addListener( map, listenerName, options.listeners[listenerName] );
    }
  }
  
  if (map && 'object' == typeof createMap.prototype) {
    for (var methodName in createMap.prototype) {
      if ('function' == typeof createMap.prototype[methodName]) {
        map[methodName] = createMap.prototype[methodName];
      }
    }
  }
  //--| p8R
  
  return map;
  
}

//###################################################################
//## Rozszerzona funkcjonalność mapki
//###################################################################

createMap.prototype.addMarker = function( markerOrId, lat, lng, html, options ) {
  var marker = ('object' == typeof markerOrId)
                  ? markerOrId
                  : createMarker( markerOrId, lat, lng, html, options );
    
  var overlay = this.addOverlay( marker );
  
  if (marker.id) {  //-- jeśli określone id markera to zapisujemy overlay w tablicy _overlays obiektu mapy 
    if ('undefined' == typeof this._overlays) { this._overlays = {}; }
    this._overlays[ marker.id ] = marker;
  }
  
  if(marker.options != null && 'undefined' != typeof marker.options.visible && false == marker.options.visible){
    marker.hide();
  }
  
  return marker;
}


createMap.prototype.addMarkers = function( markers ) {
	if ('object' != typeof markers || 'undefined' == typeof markers.length || 0 >= markers.length) {
    return false;
  }
  
  if ('undefined' == typeof this._overlays) { this._overlays = {}; }
  
  for(i = 0; i < markers.length; i++) {
    this.addOverlay(markers[i]); 
    
    if (markers[i].id) { //-- jesli okreslony id to zapisujemy overlaya w tablicy _overlays mapy
      this._overlays[markers[i].id] = markers[i];  
    }
  }
  
  return this._overlays;
}


createMap.prototype.removeMarker = function( markerId ) {
  if (!this._overlays || 'undefined' == this._overlays[markerId]) {
    return false;
  }
  
  var ovr = this._overlays[markerId];
  
  this.removeOverlay( ovr );
  this._overlays[markerId] = null;
  
  return ovr;
}


createMap.prototype.getMarker = function( markerId ) {
  if (!this._overlays || 'undefined' == this._overlays[markerId]) {
    return null;
  }
  
  return this._overlays[markerId];
}

createMap.prototype.getMarkers = function() {
  return (this._overlays) ? this._overlays : null;
}

createMap.prototype.getMarkersCount = function() {
  return (this._overlays) ? this._overlays.length : 0;
}
