﻿var StreetViewControl = function(){
    var m_MapWindow;
    var svClient;
    var svPanorama;
    var m_gCoord;
    var m_firstView = true;
    return {
        Initialize:function(){
            m_MapWindow = new Ext.Window({
                height:415,
                width:500,
                contentEl:"pano",
                modal:true,
                closeAction:'hide',
                title:'Google Street View (requires Adobe Flash Player 10)',
                resizable:false
            });
            Ext.get('pano').setVisibilityMode(Ext.Element.DISPLAY);
            svClient = new GStreetviewClient();
            svPanorama = new GStreetviewPanorama(document.getElementById("pano"));
            GEvent.addListener(svPanorama, 'error', function(msg){Ext.Msg.alert('Panorama Error', msg); });
            m_MapWindow.addListener('show', function(){Ext.get('pano').setVisible(true);});
            m_MapWindow.addListener('hide', svPanorama.hide);
            
        },
        StartStreetView:function(){
            var map = $find('Map1');
            map.getGeometry(ESRI.ADF.Graphics.ShapeType.Point, StreetViewControl.MapClick, null, 'black', null, 'pointer', true);
        },
        MapClick:function(geom,e) {
            Ext.Msg.wait('Querying Street View');
            var map = $find('Map1');
	        var x = geom.get_x();
	        var y = geom.get_y();
	        
            var context = map.get_id();
            var message = 'ControlID=' + map.get_id() + '&EventArg=ConvertCoordinate&CoordSysID=' + geom.get_spatialReference().id + '&X=' + x + '&Y=' + y;
    
            eval("WebForm_DoCallback('__Page',message,StreetViewControl.GetCoordinateCallback,context,null,true)");
        },
        GetCoordinateCallback:function(coord){
         coord = eval(coord);
            var gCoord = new GLatLng(coord.Latitude, coord.Longitude);
            svClient.getNearestPanoramaLatLng(gCoord, StreetViewControl.StreetViewCallback)
        },
        StreetViewCallback:function(gCoord){
            Ext.Msg.hide();

            if (gCoord != null)
            {
	            m_MapWindow.show();
	            m_gCoord = gCoord;
                svPanorama.checkResize();
                svPanorama.show();
                //need a delay before setting the location.
                if (m_firstView)
                {
                    if (Ext.isIE || Ext.isChrome)
	                    setTimeout('StreetViewControl.StreetViewCallback2()',600);
	                else
	                    setTimeout('StreetViewControl.StreetViewCallback2()',800);
	                m_firstView=false;
	            }
	            else
	            {
                    if (Ext.isIE || Ext.isChrome)
	                    setTimeout('StreetViewControl.StreetViewCallback2()',100);
	                else
	                    setTimeout('StreetViewControl.StreetViewCallback2()',200);
	            }
	        }
	        else
	        {
	            Ext.Msg.alert('Alert','There is not street view imagery at this location.');
	        }
        },
        StreetViewCallback2:function(){
            svPanorama.setLocationAndPOV(m_gCoord);

        }
    };
}();