﻿function PhMapLocation( address, marker )
{
	this.Address	= address;
	this.Marker		= marker;
}

function PhGoogleMap( show_controls, show_types, width, height, map_width, map_height, offset_left, offset_top )
{
	this.m_oAjax		= new PhAjaxJson();
	
	this.m_oMap			= null;
	this.m_oDirections	= null;
	this.m_oBounds		= null;
	this.m_oMapCanvas	= null
	this.m_oWaypoints	= new Array();
	this.m_oDirections	= new GDirections();
	this.m_oMarkers		= new Array();
	
	this.m_bShowControls	= show_controls;
	this.m_bShowTypes		= show_types;
	this.m_iWidth			= width;
	this.m_iHeight			= height;
	this.m_iMapWidth		= map_width;
	this.m_iMapHeight		= map_height;
	this.m_iOffsetLeft		= offset_left;
	this.m_iOffsetTop		= offset_top;
	this.m_iMarkerCount		= 0;
	
	this.m_bValidAddress	= false;
	
	this.OnMarkerAdded		= null;
	this.OnMapClicked		= null;
	
	this.RegisterUnload();
	this.InitObject();
}

PhGoogleMap.prototype = new PhWidget();

PhGoogleMap.prototype.IsValidAddress = function()
{
	return this.m_bValidAddress;
}

PhGoogleMap.prototype.SetInlineStyles = function( styles )
{
	var cur_styles = '';
	
	if( this.m_oWrapper != null )
	{
		if( g_oClient.GetBrowser() == PhClient.BROWSER_IE )
		{
			cur_style = this.m_oWrapper.style.getAttribute( 'cssText' );
			this.m_oWrapper.style.setAttribute( 'cssText', cur_style+';'+styles, 0 );
		}
		else
		{
			cur_style = this.m_oWrapper.getAttribute( 'style' );
			this.m_oWrapper.setAttribute( 'style', cur_style+styles );
		}
	}
}

PhGoogleMap.prototype.RegisterUnload = function()
{
//	var body = document.getElementsByTagName( 'body' )[0];
//	
//	if( body.addEventListener )
//		body.addEventListener( 'unload', function(){alert('hi');GUnload();}, false );
//	else
//		body.attachEvent( 'onunload', function(){alert('hi');GUnload();} );
}

PhGoogleMap.prototype.InitObject = function()
{
	var me = this;
	
	this.m_oContainer	= document.createElement( 'div' );
	//this.m_oContainer.className			= 'png';
	this.m_oContainer.style.width	= this.m_iWidth+'px';
	this.m_oContainer.style.height	= this.m_iHeight+'px';
	this.m_oContainer.style.position	= 'relative';
	
	this.m_oContainer.style.zIndex		= '1';
	
	this.m_oWrapper = document.createElement( 'div' );
	this.m_oWrapper.className = 'png';
	this.m_oWrapper.style.zIndex = '2';
	this.m_oWrapper.style.width = this.m_iWidth+'px';
	this.m_oWrapper.style.height = this.m_iHeight+'px';
	//this.m_oWrapper.style.backgroundColor = 'Green';
	
	this.m_oMapCanvas	= document.createElement( 'div' );
	this.m_oMapCanvas.className			= 'MapCanvas';
	
	this.m_oMapCanvas.style.width		= this.m_iMapWidth+'px';
	this.m_oMapCanvas.style.height		= this.m_iMapHeight+'px';	
	this.m_oMapCanvas.style.left		= this.m_iOffsetLeft+'px';
	this.m_oMapCanvas.style.top			= this.m_iOffsetTop+'px';
	this.m_oMapCanvas.style.zIndex		= '10';

	//this.m_oContainer.appendChild( this.m_oMapCanvas );
	
	this.m_oWrapper.appendChild( this.m_oMapCanvas );
	this.m_oContainer.appendChild( this.m_oWrapper );
	
	if( GBrowserIsCompatible() )
	{
		this.m_oMap = new GMap2( this.m_oMapCanvas, { size: new GSize( this.m_iMapWidth, this.m_iMapHeight ) } );
		this.m_oMap.setCenter( new GLatLng( 37, -122 ), 15 );
		
		this.m_oBounds = new GLatLngBounds();
		
		if( this.m_bShowControls )
			this.m_oMap.addControl( new GSmallMapControl(), new GControlPosition( G_ANCHOR_TOP_RIGHT, new GSize( 5, 5 ) ) );
			
		if( this.m_bShowTypes )
			this.m_oMap.addControl( new GMapTypeControl(), new GControlPosition( G_ANCHOR_BOTTOM_RIGHT, new GSize( 5, 5 ) ) );
			
		GEvent.addListener( this.m_oMap, 'click', function( marker, point ){me.HandleMapClick( marker, point );} );
	}
	else
	{
		this.m_oMapCanvas.innerHTML = 'We\'re sorry, but it seems that Google Maps is not supported by your browser.';
	}
}

PhGoogleMap.prototype.HandleMapClick = function( marker, point )
{
	if( this.OnMapClicked != null )
		this.OnMapClicked( marker, point );
}

PhGoogleMap.prototype.LocateAddress = function( address )
{
	var me = this;
	this.m_oAjax.SetCallback( function( obj ){ me.LocateAddressCallback( obj ); } );
	this.m_oAjax.SendRequest( 'system/widgets/google_map/geocode_request.aspx', 'q='+address+'&output=json', null );
}

PhGoogleMap.prototype.LocateAddressCallback = function( obj )
{	
	var p		= obj.Placemark[0].Point.coordinates;
	var point	= new GLatLng( p[1], p[0] );
	var marker	= new GMarker( point );
	
	this.m_oMap.setCenter( point, 16 );
	this.m_oMap.addOverlay( marker );
	
	this.m_bValidAddress = true;
}

PhGoogleMap.prototype.AddMarkerByAddress = function( address )
{
	var me = this;
	var cb = function( obj ){ me.AddMarkerByAddressCallback( obj ); };
	
	this.m_oAjax.SendRequest( 'system/widgets/google_map/geocode_request.aspx', 'q='+address+'&output=json', null, cb );
}

PhGoogleMap.prototype.AddMarkerByAddressCallback = function( obj )
{	
	if( obj.Placemark == null )
	{
		this.m_bValidAddress = false;
		return;
	}
		
	var p		= obj.Placemark[0].Point.coordinates;
	var point	= new GLatLng( p[1], p[0] );
	var marker	= new GMarker( point );
	var zoom	= (this.m_iMarkerCount == 0)? 20:14;
	
	this.m_oBounds.extend( point );
	zoom = this.m_oMap.getBoundsZoomLevel( this.m_oBounds );
	
	this.m_oMap.setZoom( (zoom > 14)? 14 : zoom );
	this.m_oMap.setCenter( this.m_oBounds.getCenter() );
	
	this.m_oMap.addOverlay( marker );
	this.m_iMarkerCount++;
	
	if( this.OnMarkerAdded != null )
		this.OnMarkerAdded( marker );
	
	this.m_bValidAddress = true;
}

PhGoogleMap.prototype.AddMarkerByLocation = function( location )
{
	var me = this;
	var cb = function( obj ){ me.AddMarkerByLocationCallback( obj, location ); };
	
	this.m_oLocation = location;
	this.m_oAjax.SendRequest( 'system/widgets/google_map/geocode_request.aspx', 'q='+location.Address+'&output=json', null, cb );
}

PhGoogleMap.prototype.AddMarkerByLocationCallback = function( obj, location )
{
	if( obj.Placemark == null )
	{
		this.m_bValidAddress = false;
		return;
	}
		
	var p		= obj.Placemark[0].Point.coordinates;
	var point	= new GLatLng( p[1], p[0] );
	var marker	= new GMarker( point );
	var zoom	= (this.m_iMarkerCount == 0)? 20:14;
	
	location.Marker = marker;
	
	this.m_oBounds.extend( point );
	zoom = this.m_oMap.getBoundsZoomLevel( this.m_oBounds );
	
	this.m_oMap.setZoom( (zoom > 14)? 14 : zoom );
	this.m_oMap.setCenter( this.m_oBounds.getCenter() );
	
	this.m_oMap.addOverlay( marker );
	this.m_iMarkerCount++;
	
	if( this.OnMarkerAdded != null )
		this.OnMarkerAdded( marker );
		
	this.m_bValidAddress = true;	
}

PhGoogleMap.prototype.AddMarkerByCoordinates = function( latitude, longitude )
{
	var point	= new GLatLng( latitude, longitude );
	var marker	= new GMarker( point );
	var zoom	= (this.m_iMarkerCount == 0)? 20:14;
	
	this.m_oBounds.extend( point );
	zoom = this.m_oMap.getBoundsZoomLevel( this.m_oBounds );
	
	this.m_oMap.setZoom( (zoom > 14)? 14 : zoom );
	this.m_oMap.setCenter( this.m_oBounds.getCenter() );
	
	this.m_oMap.addOverlay( new GMarker( point ) );
	this.m_iMarkerCount++;
}

PhGoogleMap.prototype.AddWaypoint = function( waypoint )
{
	this.m_oWaypoints.push( waypoint );
}

PhGoogleMap.prototype.GetMarker = function( index )
{
	return this.m_oMarkers[index];
}
	
PhGoogleMap.prototype.GetRoute = function()
{
	if( this.m_oWaypoints.length <= 0 ) return;
	
	var me				= this;
		
	GEvent.addListener(this.m_oDirections,"error", function(){me.GetRouteError();});
	GEvent.addListener(this.m_oDirections,"load", function(){me.GetRouteCallback();});

	this.m_oDirections.loadFromWaypoints( this.m_oWaypoints, {getPolyline:true} );	
}

PhGoogleMap.prototype.GetRouteError = function()
{
	var s = this.m_oDirections.getStatus();
	
	alert( 'GDirections Failed: '+this.m_oDirections.getStatus().code );
}

PhGoogleMap.prototype.GetRouteCallback = function()
{
	var i		= 0;
	var poly	= this.m_oDirections.getPolyline();
	var bounds	= this.m_oDirections.getBounds();
	var route	= null;
	
	var numberedIcon = new GIcon();

	numberedIcon.image = 'system/resources/global/custom_google_maps_icons/1.png';
	numberedIcon.iconSize = new GSize( 39, 40 );
	numberedIcon.iconAnchor	= new GPoint( 9, 40 );
	numberedIcon.infoWindowAnchor = new GPoint( 18, 8 );
	numberedIcon.infoShadowAnchor = new GPoint( 18, 25 );
	markerOptions = {icon:numberedIcon };
	
	//var start	= new GMarker( poly.getVertex(0),G_START_ICON );
	var start	= new GMarker( poly.getVertex(0), markerOptions );
		
	//var end		= new GMarker( poly.getVertex(poly.getVertexCount()-1),G_END_ICON );
	var stop	= null;
	
	this.m_oMap.addOverlay( poly );
	this.m_oMap.setZoom( this.m_oMap.getBoundsZoomLevel( bounds ) );
	this.m_oMap.setCenter( bounds.getCenter() );
	this.m_oMap.addOverlay( start );
	//this.m_oMap.addOverlay( end );
	
	this.m_oMarkers.push( start );
	
	
	for( i = 0; i < this.m_oDirections.getNumRoutes(); i++ )
	{
		numberedIcon = new GIcon();
		
		numberedIcon.image = 'system/resources/global/custom_google_maps_icons/'+(i+2)+'.png';
		numberedIcon.iconSize = new GSize( 39, 40 );
		numberedIcon.iconAnchor	= new GPoint( 9, 40 );
		numberedIcon.infoWindowAnchor = new GPoint( 16, 8 );
		numberedIcon.infoShadowAnchor = new GPoint( 18, 25 );
		
		markerOptions = {icon:numberedIcon };
		
		route	= this.m_oDirections.getRoute( i );
		//stop	= new GMarker( route.getEndLatLng(), G_PAUSE_ICON );
		stop	= new GMarker( route.getEndLatLng(), markerOptions );
		
		this.m_oMap.addOverlay( stop );
		this.m_oMarkers.push( stop );
	}
	
	//this.m_oMarkers.push( end );	
}

PhGoogleMap.prototype.ClearMarkers = function()
{
	this.m_oMap.clearOverlays();
	this.m_oBounds		= new GLatLngBounds();
	this.m_iMarkerCount = 0;
}

PhGoogleMap.prototype.DisableBorder = function()
{
	this.m_oMapCanvas.style.borderStyle = 'none';
}
