/*
	Copyright (C) Falcom GmbH, GERMANY
	All rights reserved.

	Redistribution and modification of this code is strictly prohibited.

	IN NO EVENT SHALL THE FALCOM GMBH BE LIABLE TO ANY PARTY FOR
	DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
	OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
	FALCOM GMBH HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	FALCOM GMBH SPECIFICALLY DISCLAIMS ANY WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
	ON AN "AS IS" BASIS, AND THE FALCOM GMBH HAS NO OBLIGATION TO
	PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
*/

if ( de == undefined ) var de = {};
if ( de.falcom == undefined ) de.falcom = {};

de.falcom.FEismann = new Class.Singleton(
{
	mMap: null,
	
	mMarkerLayer: null,
	
	mMarker: null,

	isWidget: false,

    initialize: function()
    {
	
    },
    
	initMap: function( pMapDom )
	{
    	this.mMap = new OpenLayers.Map( pMapDom, {
    		controls:[
              		new OpenLayers.Control.Navigation(),
              		new OpenLayers.Control.KeyboardDefaults,
                    new OpenLayers.Control.Attribution()],
            maxExtent:  new OpenLayers.Bounds( -20037508, -20037508, 20037508, 20037508 ),
			numZoomLevels: 19,
			maxResolution: 156543,
			transitionEffect: 'resize',
 			projection: new OpenLayers.Projection("EPSG:900913"),
			displayProjection: new OpenLayers.Projection("EPSG:4326"),
			units: 'meters',
			panMethod: OpenLayers.Easing.Expo.easeOut
    	} );

		var tLayer = new OpenLayers.Layer.OSM.Mapnik( "map.layer.OsmMapnik", { transitionEffect: 'resize' } );
    	
		this.mMap.addLayer( tLayer );
		this.mMap.setBaseLayer( tLayer );
		this.mMap.zoomToMaxExtent();
		
		if ( this.isWidget )
		{
			this.mMap.setCenter( this.getLonLat( 11.0, 51.0 ), 16 );
		}
		else
		{
			this.mMap.setCenter( this.getLonLat( 11.0, 51.0 ), 17 );
		}
		
		this.mMakerLayer = new OpenLayers.Layer.Vector( "Markers", this.mLayerOptions );
		this.mMap.addLayer( this.mMakerLayer );

		
		this.mMarker = new de.falcom.map.FMarker( this.getPoint( 10.980683333333332, 50.67335666666666 ), 0, "", this.isWidget );
 
		this.mMakerLayer.addFeatures( this.mMarker );
		
		
		var tControl = new OpenLayers.Control.SelectFeature(
	            [ this.mMakerLayer ],
	            {
	                clickout: true, 
	                toggle: true,
	                multiple: false, 
	                hover: false
	            }
	        );
			
			this.mMap.addControl( tControl );
			tControl.activate();
			
			this.mMakerLayer.events.on({
	            "featureselected": function( pEvent ) { pEvent.feature.showPopup(); },
	            "featureunselected": function( pEvent ) { pEvent.feature.hidePopup(); }
	        });
		
		
		
	},
	
	updatePosition: function()
	{
		new Request.HTML(
		{	
			url: './exec?action=eismann&function=position',
			evalResponse: true,
			async: true,
			update: new Element( 'div' ),
			onComplete: function() 
			{
				( function() { this.updatePosition(); }.bind( this ) ).delay( 60000, this );
			}.bind( this )
        }).send();
	},	
	
	doUpdateMakerOnMap: function( pImei, pLat, pLng, pHeading, pSpeed, pTimestamp )
	{
		this.mMarker.mPopupText  = '<div><ul><li>Uhrzeit: ' + new Date( pTimestamp ).format( "%d.%m.%Y %H:%M" ) + '</li><li>Richtung: ' + pHeading + '°</li>';
		this.mMarker.mPopupText += '<li>Status: ' + ( new Date().getTime() - pTimestamp > 600000 ? 'steht': 'unterwegs' ) + '</li></ul></div>';
		
		this.mMarker.setRotation( pHeading );
		this.mMarker.moveTo( this.getPoint( pLng, pLat ), this.getLonLat( pLng, pLat ) );
		this.mMap.panTo( this.getLonLat( pLng, pLat ) );
	},
	
	getLonLat: function( pLng, pLat )
	{
		return new OpenLayers.LonLat( pLng, pLat ).transform( this.mMap.displayProjection, this.mMap.projection );
	},
	
	getPoint: function( pLng, pLat )
	{
		return new OpenLayers.Geometry.Point( pLng, pLat ).transform( this.mMap.displayProjection, this.mMap.projection );
	}

    
} );


if ( de.falcom.map == undefined ) de.falcom.map = {};

de.falcom.map.FMarker = new Class(
{		
	Extends: OpenLayers.Feature.Vector,
	
	mLayer: null,
	
	mPopupText: "",
	
	mPopup: null,
	
	mStyle: null,
	
	isWidget: false,
	
	initialize: function( pPoint, pRotation, pPopupText, pIsWidget )
	{
		this.mStyle = OpenLayers.Util.extend( {}, OpenLayers.Feature.Vector.style['default'] );
		this.mStyle.fillOpacity = 1;
		this.mStyle.graphicName = "circle";
		this.isWidget = pIsWidget;
		
		if ( this.isWidget )
		{
			this.mStyle.pointRadius = 24;
		}
		else
		{
			this.mStyle.pointRadius = 48;
		}
		this.mStyle.rotation = pRotation;
		this.mStyle.externalGraphic = "img/marker-left.png";
		this.mStyle.imageLeft = "img/marker-left.png";
		this.mStyle.imageRight = "img/marker-right.png";


		this.parent( pPoint, { 'object': this }, this.mStyle );
		
		this.mPopupText = pPopupText;
	},
	
	showPopup: function()
	{
		this.popup = new OpenLayers.Popup(
				"LivePopup", 
				this.geometry.getBounds().getCenterLonLat(),
                null,
                this.mPopupText,
                false,
                null
		);
		
		this.popup.displayClass = 'Popup';
		this.popup.contentDisplayClass = 'PopupContent';
		this.popup.panMapIfOutOfView = true;
		this.popup.autoSize = true;
		this.popup.minSize = new OpenLayers.Size( 174, 60 );
		this.layer.map.addPopup( this.popup );
		
	},
	
	hidePopup: function()
	{
		if ( this.popup != null )
		{
			this.popup.hide();
			this.layer.map.removePopup( this.popup );
			this.popup = null;
		}
	},
	
	toFront: function()
	{
		var tLayer = this.layer;
		
		this.layer.destroyFeatures( this );
		tLayer.addFeatures( this );
	},
	
	moveTo: function( pPoint, pLonLat )
	{
		this.geometry.x = pPoint.x;
		this.geometry.y = pPoint.y;
		
		this.geometry.calculateBounds();
		
		this.layer.drawFeature( this );

		// Update popup if visible
		if ( this.popup != null )
		{
			this.popup.lonlat = pLonLat;
			this.popup.draw();
		}
	},
	
	setRotation: function( pRotation )
	{
		this.style.rotation = pRotation;
		this.layer.drawFeature( this );
	},
	
	setMarkerImage: function()
	{
		if ( this.mOnline )
		{
			this.style.externalGraphic = this.mActive ? this.mStyle.imageOnlineFocus : this.mStyle.imageOnline; 
		}
		else
		{
			this.style.externalGraphic = this.mActive ? this.mStyle.imageDefaultFocus : this.mStyle.imageDefault; 
		}
		
		this.mCurrentStyle = this.style;
		
		if ( $defined(this.layer) )
			this.layer.drawFeature( this );
	},
	
	setOnlineState: function( pOnline )
	{
		this.mOnline = pOnline;
		this.setMarkerImage();
	},

	getLonLat: function()
	{
		return this.geometry.getBounds().getCenterLonLat();
	},
	
	show: function()
	{
		if ( this.layer == null )
		{
			this.mLayer.addFeatures( [ this ] );		
		}
	},
	
	hide: function()
	{
		if ( this.layer != null )
		{
			this.mLayer = this.layer;
			this.layer.removeFeatures( [ this ] );
		}
	}
});


de.falcom.map.FFramedCloud = new Class(
{		
	Extends: OpenLayers.Popup.FramedCloud,
	
	isAlphaImage: true,
	
    positionBlocks: {
	    "tl": {
		    'offset': new OpenLayers.Pixel( 59, -5 ),
		    'padding': new OpenLayers.Bounds( 25, 12, 25, 20 ),
	        'blocks': [
	            { // top-left
	                size: new OpenLayers.Size( 'auto', 'auto' ),
	                anchor: new OpenLayers.Bounds( 0, 36, 22, 0 ),
	                position: new OpenLayers.Pixel( 0, -301 )
	            },
	            { //top-right
	                size: new OpenLayers.Size(22, 'auto'),
	                anchor: new OpenLayers.Bounds(null, 36, 0, -1 ),
	                position: new OpenLayers.Pixel(-638, 0)
	            },
	            { //bottom-left
	                size: new OpenLayers.Size( 'auto', 33 ),
	                anchor: new OpenLayers.Bounds( 0, 3, 76, null ),
	                position: new OpenLayers.Pixel( 0, -565 )
	            },
	            { //bottom-right
	                size: new OpenLayers.Size( 40, 33 ),
	                anchor: new OpenLayers.Bounds( null, 3, 0, null),
	                position: new OpenLayers.Pixel( -620, -616 )
	            },
	            { // stem
	                size: new OpenLayers.Size( 36, 36 ),
	                anchor: new OpenLayers.Bounds( null, 0, 40, null ),
	                position: new OpenLayers.Pixel( -20, -686 )
	            }
	        ]
	    },
	    
	    "tr": {
		    'offset': new OpenLayers.Pixel( -59, -5 ),
		    'padding': new OpenLayers.Bounds( 25, 12, 25, 20 ),
	        'blocks': [
	            { // top-left
	                size: new OpenLayers.Size( 'auto', 'auto' ),
	                anchor: new OpenLayers.Bounds( 0, 36, 22, 0 ),
	                position: new OpenLayers.Pixel( 0, -301 )
	            },
	            { //top-right
	                size: new OpenLayers.Size(22, 'auto'),
	                anchor: new OpenLayers.Bounds(null, 36, 0, -1 ),
	                position: new OpenLayers.Pixel(-638, 0)
	            },
	            { //bottom-left
	                size: new OpenLayers.Size( 'auto', 33 ),
	                anchor: new OpenLayers.Bounds( 0, 3, 40, null ),
	                position: new OpenLayers.Pixel( 0, -616 )
	            },
	            { //bottom-right
	                size: new OpenLayers.Size( 40, 33 ),
	                anchor: new OpenLayers.Bounds( null, 3, 0, null ),
	                position: new OpenLayers.Pixel(-620, -616)
	            },
	            { // stem
	                size: new OpenLayers.Size( 36, 36 ),
	                anchor: new OpenLayers.Bounds( 40, 0, null, null),
	                position: new OpenLayers.Pixel( -20, -686 )
	            }
	            
	        ]
	    },
	    
	    "bl": {
		    'offset': new OpenLayers.Pixel( 59, 5 ),
		    'padding': new OpenLayers.Bounds( 25, 26, 25, 23 ),
	        'blocks': [
   	            { // top-left
	                size: new OpenLayers.Size('auto', 'auto'),
	                anchor: new OpenLayers.Bounds( 0, 50, 100, 4 ),
	                position: new OpenLayers.Pixel( 0, -301 )
	            },
	            { //top-right
	                size: new OpenLayers.Size( 100, 'auto' ),
	                anchor: new OpenLayers.Bounds( null, 50, 0, 3 ),
	                position: new OpenLayers.Pixel( -560, 0 )
	            },
	            { //bottom-left
	                size: new OpenLayers.Size( 'auto', 33 ),
	                anchor: new OpenLayers.Bounds( 0, 17, 40, null ),
	                position: new OpenLayers.Pixel( 0, -565 )
	            },
	            { //bottom-right
	                size: new OpenLayers.Size( 40, 33 ),
	                anchor: new OpenLayers.Bounds( null, 17, 0, null),
	                position: new OpenLayers.Pixel( -620, -616 )
	            },
	            { // stem
	                size: new OpenLayers.Size( 36, 53 ),
                    anchor: new OpenLayers.Bounds( null, null, 40, 0 ),
                    position: new OpenLayers.Pixel( -118, -672 )
                }
	        ]
	    },
	   
	    "br": {
		    'offset': new OpenLayers.Pixel( -57, 5 ),
		    'padding': new OpenLayers.Bounds( 25, 26, 25, 23 ),
	        'blocks': [
	            { // top-left
	                size: new OpenLayers.Size('auto', 'auto'),
	                anchor: new OpenLayers.Bounds( 0, 50, 22, 3 ),
	                position: new OpenLayers.Pixel( 0, 0 )
	            },
	            { //top-right
	                size: new OpenLayers.Size(22, 'auto'),
	                anchor: new OpenLayers.Bounds( null, 50, 0, 3 ),
	                position: new OpenLayers.Pixel(-638, 0)
	            },
	            { //bottom-left
	                size: new OpenLayers.Size( 'auto', 33 ),
	                anchor: new OpenLayers.Bounds( 0, 17, 40, null ),
	                position: new OpenLayers.Pixel( 0, -565 )
	            },
	            { //bottom-right
	                size: new OpenLayers.Size( 40, 33 ),
	                anchor: new OpenLayers.Bounds( null, 17, 0, null),
	                position: new OpenLayers.Pixel( -620, -616 )
	            },
	            { // stem
	                size: new OpenLayers.Size( 36, 53 ),
                    anchor: new OpenLayers.Bounds( 40, null, null, 0 ),
                    position: new OpenLayers.Pixel( -118, -672 )
                }
            ]
        }
    },

	
	initialize: function( pId, pLonLat, pContentSize, pContentHtml, pAnchor, pImgSrc )
	{
		this.parent( pId, pLonLat, pContentSize, pContentHtml, pAnchor, false, null );
   		this.imageSrc = "img/popup.png";
   		
   		
	}
} );
	

