﻿
Ext.namespace('Ext.ux');

Ext.ux.StreetViewPanel = function(pnl, mapObj,noflashCallback){


    var thePanel = pnl;
    var panoObj = {
        gmap: null,
        panel: thePanel,
        mapObj: mapObj,
        mapHandlers:[],
        roundLL: function(latlng, decimals){
            var num = 10000;
            if (decimals) {
                num = Math.pow(10, decimals)
            }
            var lat = (Math.round(latlng.lat() * num)) / num;
            var lng = (Math.round(latlng.lng() * num)) / num;
            return new GLatLng(lat, lng);
        },
        panoResize: function(pnl,adjWidth,adjHeight,rawWidth,rawHeight){
           
            if (pnl) {
                pnl.svPano.checkResize();
            }
        },
        onCollapse: function(){
            if (this.gmap) {
                this.gmap.checkResize();
            }
        },
        lastPoint: null,
        setPanoCenter: function(){
            var latlng = this.gmap.getCenter();
            this.setPanoCenterLL(latlng)
        },
        setPanoCenterLL: function(latlng){
            latlng = this.roundLL(latlng);
            //console.log(latlng.lat() + "," + latlng.lng());
            this.lastPoint = latlng;
            this.panel.svPano.setLocationAndPOV(latlng);
            this.panel.setTitle(latlng.lat() + "," + latlng.lng());
            if(this.tempMarker){
            this.tempMarker.hide();
            }
        },
        tempMarker: null,
        showTempMarker: function(){
            if (!this.tempMarker) {
                this.tempMarker = new GMarker(this.lastPoint);
                this.gmap.addOverlay(this.tempMarker);
            }
            else {
                this.tempMarker.setLatLng(this.lastPoint);
            }
            this.tempMarker.show();
            
        },
        panoTrys: 0,
        panoError: function(response){
            var msg = "";
            if (response == 600) {
                msg = "No Nearby Pano";
                if (this.panoTrys < 1) {
                    this.lastPoint = this.roundLL(this.lastPoint, 2);
                    this.setPanoCenterLL(this.lastPoint);
                    this.panoTrys++;
                }else{
                  this.panel.setTitle(msg);
                }
                //this.showTempMarker();
            }
            else {
                if (response == 603) {
                    msg = "Flash doesn't appear to be supported by your browser";
                    noflashCallback();
                    this.panel.hide();
                    for (var i=0;i< this.mapHandlers.length;i++){
                       GEvent.removeListener(this.mapHandlers[i]);
                    }
                }
                else {
                    msg = "Error code: " + response;
                }
               
                //Ext.MessageBox.alert('Error', msg);
                
            }
        },
        onPanelRender: function(){
            this.panel.svPano = new GStreetviewPanorama(this.panel.body.dom);
            var h1 =GEvent.bind(this.panel.svPano, "error", this, this.panoError);
            this.gmap = this.mapObj.gmap;
            var LL = this.gmap.getCenter();
            this.panel.svPano.setLocationAndPOV(LL);
            
            this.panel.on('expand', this.setPanoCenter, this);
            this.panel.on('collapse', this.onCollapse, this);
            this.panel.on('resize', this.panoResize);
            var h2 = GEvent.bind(this.gmap, "click", this, function(overlay, latlng){
                this.panel.svPano.setLocationAndPOV(latlng);
                });
            var h3 = GEvent.bind(this.gmap, "moveend", this, function(){
                if (this.panel.isVisible()) {
                    this.panoTrys = 0;
                    this.setPanoCenter();
                }
                else {
                    //console.log("not visible");
                }
            });
            
            this.mapHandlers = [h1,h2,h3]; 
            
        },
        
        init: function(){
            if (this.panel.rendered == true) {
                this.onPanelRender();
            }
            else {
                this.panel.addListener('render', this.onPanelRender,this);

            }
            
            
        }
    }
    
    panoObj.init();
    
};
