 // ============================ Ajax Class ============================ //

// Copyright Paul Tesar 2008 
// Copyright Google Inc 
// paultesar@gmail.com

// ============== Main function / Fonction Principal / Hlavni funkce ============== // 

function gmapsClass(holdinObjId,initLat,initLong,initZoom,noControls,params)
{
	this.op         = params ? params : {};    
	this.holdingObj = this.op.holdingObj ? this.op.holdingObj : document.getElementById(holdinObjId);
	this.longitude  = initLong ? initLong : 14.428095817565918; 
	this.latitude   = initLat  ? initLat  : 50.08088328469407;
	this.zoom       = initZoom ? ( (initLat && initLong) ? initZoom : 3 ) : 5;
	   
	this.controlsOn = noControls ? false : true;   
	
	if(this.holdingObj && GBrowserIsCompatible())
	{
	   this.mapobj        = new GMap2(this.holdingObj);
	   this.setPosition(this.latitude,this.longitude,this.zoom);
	   
	   if(this.controlsOn)
	   {
		   this.addControls();
	   }
	}
	   
}

gmapsClass.prototype.setPosition = function (latitude,longitude,zoom)
{        
	var position = new GLatLng(latitude,longitude);
	var zoom     = zoom ? zoom : this.mapobj.getZoom();
	
	this.longitude  = longitude; 
	this.latitude   = latitude;
	this.zoom       = zoom;

	this.mapobj.setCenter(position, zoom);       
}

gmapsClass.prototype.addGeocoder = function ()
{
    this.geocoder = new GClientGeocoder( );
}

gmapsClass.prototype.resolveGeoLocation = function( query, callback )
{
    if( !this.geocoder )
        this.addGeocoder( );
    
    var pointer = this;
    
    this.geocoder.getLatLng( query, function( point )
    { 
        if (!point) 
        { 
            //alert(address + " not found");
        } 
        else 
        {
            callback( point.lat(), point.lng() );           
        }
    });  
}

gmapsClass.prototype.addControls = function ()
{     
	 var mapControl  = new GMapTypeControl();
	 var topRight    = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
	 var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
	 
	 this.mapobj.addControl(mapControl, topRight);
	 this.mapobj.addControl(new GSmallMapControl());
}

gmapsClass.prototype.addMarker = function (latitude,longitude,isMoveable, outputLatObjId,outputLongObjId)
{     
		latitude                  = latitude             ? latitude  : this.latitude;
		longitude                 = longitude            ? longitude : this.longitude;
	var longObj                   = outputLongObjId      ? document.getElementById(outputLongObjId) : null;   
	var latObj                    = outputLatObjId       ? document.getElementById(outputLatObjId)  : null;
	var exportCoordinates         = (longObj && longObj) ? true : false;
	var draggable                 = isMoveable           ? true : false;
	  
	var html                      = "";
	var point                     = new GLatLng(latitude,longitude);
	var marker                    = new GMarker(point,{draggable: draggable}); 
	
	this.mapobj.addOverlay(marker); 
	
	var map                       = this;
	var dragEndFunction = function() {
										 var point = marker.getLatLng();
										 var lat   = point.y;
										 var lon   = point.x;
										 
										 if(exportCoordinates)
										 {
											 latObj.value  = lat; 
											 longObj.value = lon;
										 }
										 map.setPosition(lat,lon);
										 
										 return true;
									 };
	
    if( longObj != null && latObj != null )
    {
        longObj.onchange = latObj.onchange = function( ev ){ var lat = latObj.value; var lon = longObj.value; map.moveMarker( marker, lat, lon ); map.setPosition( lat, lon ); };
    }
    
	GEvent.addListener(marker, "dragend", dragEndFunction);
	
	return marker;
}

gmapsClass.prototype.moveMarker = function(marker,lat,long)
{
	 var point = new GLatLng(lat,long);
	 
	 marker.setPoint(point);
	 
	 return true;
}

gmapsClass.prototype.addInfoWindowToMarker = function (marker,HTML)
{
	var map     = this.mapobj;
	var point   = marker.getLatLng();
	var clicked = function() {
								map.openInfoWindowHtml(point, HTML);
							 }
	 GEvent.addListener(marker, "click",clicked);
	 
	 return true;
} 

gmapsClass.prototype.addInfoWindowToMarkerNoClick = function (marker,HTML)
{
	var point   = marker.getLatLng();
	this.mapobj.openInfoWindowHtml(point, HTML);
	 
	 return true;
}

gmapsClass.prototype.addPolyLine = function (points,color,isArch,thickness)
{
	var isArc      = isArch    ? true      : false;
	var lineColor  = color     ? color     : "#3333cc";
	var lThickness = thickness ? thickness : 2;
	var linePoints = new Array();
	
	for(x in points)
	{
		var point    = points[x];
		linePoints.push( new GLatLng( point.lat ,point.lon ) ) ;
	}
	
	var newLine = new GPolyline(linePoints, lineColor, lThickness, 0.2, { geodesic: isArc });
	
	this.mapobj.addOverlay(newLine); 
	
	return newLine;
}





function fss_googleMap( holdinObjId, initLat,initLong,initZoom,noControls,params )
{    
    this.op         = params ? params : {};    
    this.holdinObjId = holdinObjId;
    this.holdingObj = this.op.holdingObj ? this.op.holdingObj : document.getElementById(holdinObjId);
    this.longitude  = initLong ? initLong : 14.428095817565918; 
    this.latitude   = initLat  ? initLat  : 50.08088328469407;
    this.zoom       = initZoom ? ( (initLat && initLong) ? initZoom : 3 ) : 5;
       
    this.controlsOn = noControls ? false : true;   
    
    var initPosition  = new google.maps.LatLng( this.latitude, this.longitude);
    var mapOptions    = { zoom: this.zoom, mapTypeId: google.maps.MapTypeId.ROADMAP, center: initPosition };
    
    this.mapobj  = new google.maps.Map( document.getElementById( holdinObjId ), mapOptions );
    //this.setPosition(this.latitude,this.longitude,this.zoom);
    
   
    if(this.controlsOn)
    {
       // this.addControls();
    }  
}

fss_googleMap.prototype.setPosition = function (latitude,longitude,zoom)
{        
    var position = new GLatLng(latitude,longitude);
    var zoom     = zoom ? zoom : this.mapobj.getZoom();
    
    this.longitude  = longitude; 
    this.latitude   = latitude;
    this.zoom       = zoom;

    this.mapobj.setCenter(position, zoom);       
}

fss_googleMap.prototype.addGeocoder = function ()
{
    this.geocoder = new GClientGeocoder( );
}

fss_googleMap.prototype.resolveGeoLocation = function( query, callback )
{
    if( !this.geocoder )
        this.addGeocoder( );
    
    var pointer = this;
    
    this.geocoder.getLatLng( query, function( point )
    { 
        if (!point) 
        { 
            //alert(address + " not found");
        } 
        else 
        {
            callback( point.lat(), point.lng() );           
        }
    });  
}

fss_googleMap.prototype.addControls = function ()
{     
     var mapControl  = new GMapTypeControl();
     var topRight    = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
     var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
     
     this.mapobj.addControl(mapControl, topRight);
     this.mapobj.addControl(new GSmallMapControl());
}

fss_googleMap.prototype.addMarker = function (latitude,longitude,isMoveable, outputLatObjId,outputLongObjId)
{     
        latitude                  = latitude             ? latitude  : this.latitude;
        longitude                 = longitude            ? longitude : this.longitude;
    var longObj                   = outputLongObjId      ? document.getElementById(outputLongObjId) : null;   
    var latObj                    = outputLatObjId       ? document.getElementById(outputLatObjId)  : null;
    var exportCoordinates         = (longObj && longObj) ? true : false;
    var draggable                 = isMoveable           ? true : false;
      
    var html                      = "";
    var point                     = new GLatLng(latitude,longitude);
    var marker                    = new GMarker(point,{draggable: draggable}); 
    
    this.mapobj.addOverlay(marker); 
    
    var map                       = this;
    var dragEndFunction = function() {
                                         var point = marker.getLatLng();
                                         var lat   = point.y;
                                         var lon   = point.x;
                                         
                                         if(exportCoordinates)
                                         {
                                             latObj.value  = lat; 
                                             longObj.value = lon;
                                         }
                                         map.setPosition(lat,lon);
                                         
                                         return true;
                                     };
    
    if( longObj != null && latObj != null )
    {
        longObj.onchange = latObj.onchange = function( ev ){ var lat = latObj.value; var lon = longObj.value; map.moveMarker( marker, lat, lon ); map.setPosition( lat, lon ); };
    }
    
    GEvent.addListener(marker, "dragend", dragEndFunction);
    
    return marker;
}

fss_googleMap.prototype.moveMarker = function(marker,lat,long)
{
     var point = new GLatLng(lat,long);
     
     marker.setPoint(point);
     
     return true;
}

fss_googleMap.prototype.addInfoWindowToMarker = function (marker,HTML)
{
    var map     = this.mapobj;
    var point   = marker.getLatLng();
    var clicked = function() {
                                map.openInfoWindowHtml(point, HTML);
                             }
     GEvent.addListener(marker, "click",clicked);
     
     return true;
} 

fss_googleMap.prototype.addInfoWindowToMarkerNoClick = function (marker,HTML)
{
    var point   = marker.getLatLng();
    this.mapobj.openInfoWindowHtml(point, HTML);
     
     return true;
}

fss_googleMap.prototype.addPolyLine = function (points,color,isArch,thickness)
{
    var isArc      = isArch    ? true      : false;
    var lineColor  = color     ? color     : "#3333cc";
    var lThickness = thickness ? thickness : 2;
    var linePoints = new Array();
    
    for(x in points)
    {
        var point    = points[x];
        linePoints.push( new GLatLng( point.lat ,point.lon ) ) ;
    }
    
    var newLine = new GPolyline(linePoints, lineColor, lThickness, 0.2, { geodesic: isArc });
    
    this.mapobj.addOverlay(newLine); 
    
    return newLine;
}



