/**
 * map.js
 *
 * Version history (please keep backward compatible):
 * 1.0, 2009-07-01: Cornelius Hansjakob
 *
 * @author Cornelius Hansjakob <cha@massiveart.com>
 * @version 1.0
 */

GMap = Class.create({

  initialize: function() {
	google.load("earth", "1");
	
	this.map = null;
    this.geocoder = null;
    this.marker = null;
    
    this.blnValid = false;
    
    this.xmlString = '';
    
    this.singleLat;
    this.singleLng;
    this.singleIcon;
    this.singleTitle;
    
    this.initHeaderMap();   
  },
  
  initEarth: function() {
  	if($('mapEarth')){
      $('mapEarth').innerHTML = ''; 
  	  google.earth.setLanguage('de');	
  	  google.earth.createInstance('mapEarth', this.initCallback, this.failureCallback);	
  	}
  },
		
  initCallback: function(object) {
    this.ge = object;
  	this.ge.getWindow().setVisibility(true);	
  
  	// add a navigation control
  	this.ge.getNavigationControl().setVisibility(this.ge.VISIBILITY_AUTO);
  
  	// add some layers
  	this.ge.getLayerRoot().enableLayerById(this.ge.LAYER_BORDERS, true);
  	this.ge.getLayerRoot().enableLayerById(this.ge.LAYER_ROADS, true);
  	
  	// show terrain
  	var layerRoot = this.ge.getLayerRoot();
  	var terrainLayer = layerRoot.getLayerById(this.ge.LAYER_TERRAIN);
  	terrainLayer.setVisibility(true);
  	
  	// create the placemark
  	var placemark = this.ge.createPlacemark('');
  	
  	var point = this.ge.createPoint('');	
  	point.setLatitude(myGMap.singleLat);
  	point.setLongitude(myGMap.singleLng);
  	if($('mapEarthTitle')) placemark.setName($('mapEarthTitle').innerHTML);
  	placemark.setGeometry(point);
  
  	// add the placemark to the earth DOM
  	this.ge.getFeatures().appendChild(placemark);
  
  	// look at the placemark we created
  	var la = this.ge.createLookAt('');
  	la.set(myGMap.singleLat, myGMap.singleLng,
  	  0, // altitude
  	  this.ge.ALTITUDE_RELATIVE_TO_GROUND,
  	  0, // heading
  	  0, // straight-down tilt
  	  800 // range (inverse of zoom)
  	);
  	this.ge.getView().setAbstractView(la);
  },
		
  failureCallback: function(object) {
	  
  },
  
  initHeaderMap: function(){
    /**
  	 * init google map
  	 */   
  	if($('headerGMap')){
  	  if(GBrowserIsCompatible()) {
  	    this.map = new GMap2($('headerGMap'));
  	  	var center = new GLatLng(47.22982711058905, 9.897308349609375);
  	  	this.map.setCenter(center, 9);  	    
  	  	this.geocoder = new GClientGeocoder();
  	  	    
  	  	var customUI = this.map.getDefaultUI();
  	  	customUI.controls.scalecontrol = false;
  	  	customUI.controls.maptypecontrol = false;
  	  	customUI.controls.menumaptypecontrol = false;
  	  	customUI.controls.largemapcontrol3d = true;
  	  	customUI.controls.smallzoomcontrol3d = false;
  	  	customUI.zoom.doubleclick = true; 
  	  	this.map.setUI(customUI);
  	  	this.map.disableScrollWheelZoom();
  	  }
  	}  
  },
  
  /**
   * showAdresses
   */
  showAdresses: function(xmlString){	 
  	var map = this.map;
  	var marker = this.marker;
  
  	if(typeof(xmlString) == 'undefined'){
  	  xmlString = this.xmlString;	
  	}
	
    new Ajax.Request('/zoolu-website/realestate/xml', {
      parameters: { xmlData: xmlString },
  	  evalScripts: true,
  	  onComplete: function(transport) {
        map.clearOverlays();
		 
        var xml = GXml.parse(transport.responseText);	    
        var markers = xml.documentElement.getElementsByTagName('marker');		            

  	    var bounds = new GLatLngBounds();
  	    
  	    for (var i = 0; i < markers.length; i++) {
  	      var name = markers[i].getAttribute('name');		          
  	      
  	      if(markers[i].getAttribute('lat') != 0 && markers[i].getAttribute('lng') != 0){
  	        var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                    parseFloat(markers[i].getAttribute('lng')));
                    
            var customIcon = new GIcon(G_DEFAULT_ICON);
            /**
             * create own marker icon
             */
            if(markers[i].getAttribute('icon') != ''){
              customIcon.image = markers[i].getAttribute('icon');
              customIcon.shadow = '/website/themes/schertler-alge/images/map_point.png';
              customIcon.iconSize = new GSize(40, 40);
              customIcon.shadowSize = new GSize(44, 58);
              customIcon.imageMap = [0,0,40,0,40,40,0,40];  
            }
          
            markerOptions = { icon:customIcon, title:name, draggable: false, bouncy:true};
            
            var projMarker = myGMap.createMarker(point, markers[i], markerOptions);
    
            map.addOverlay(projMarker);                   
            bounds.extend(point);  
  	      }
        }	    
        map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
      }.bind(this)
    });
  },
  
  /**
   * showSingleAddress
   */
  showSingleAddress: function(elementId){
    var lat = this.singleLat;
  	var lng = this.singleLng;
  	var icon = this.singleIcon;

  	/**
  	 * init google map
  	 */   
  	if($(elementId) && (lat != 0 && lng != 0)){
  	  if(GBrowserIsCompatible()) {
  	    this.map = new GMap2($(elementId));
  	  	var center = new GLatLng(lat, lng);
  	  	this.map.setCenter(center, 16);  	    
  	  	this.geocoder = new GClientGeocoder();
  	 	    
  	  	var customUI = this.map.getDefaultUI();
  	  	customUI.controls.scalecontrol = false;
  	  	customUI.controls.maptypecontrol = false;
  	  	customUI.controls.menumaptypecontrol = false;
  	  	customUI.controls.largemapcontrol3d = false;
  	  	customUI.controls.smallzoomcontrol3d = true;
  	  	customUI.zoom.doubleclick = true; 
  	  	this.map.setUI(customUI);
  	  	this.map.disableScrollWheelZoom();
  	  	
  	  	this.map.clearOverlays();
  	  	
  	    var customIcon = new GIcon(G_DEFAULT_ICON);
  	    /**
  	     * create own marker icon
  	     */
  	    customIcon.image = icon;
  	    customIcon.shadow = '/website/themes/schertler-alge/images/map_point.png';
    		customIcon.iconSize = new GSize(40, 40);
    		customIcon.shadowSize = new GSize(44, 58);
  	    customIcon.imageMap = [0,0,40,0,40,40,0,40];  
  	        
  	    markerOptions = { icon:customIcon, title:name, draggable: false, bouncy:true};
  	      
  	    var marker = new GMarker(center, markerOptions);
  	    this.map.addOverlay(marker);	    
  	  }
  	}	  
  },
  
  /**
   * createMarker
   */
  createMarker: function(point, xml, markerOptions) {
  	var marker = new GMarker(point, markerOptions);
  	var url = '/de/'+xml.getAttribute('url');
  	GEvent.addListener(marker, 'click', function() {
  	  window.location.href = url;
  	});
    return marker;
  }

});