/**
 * directions.js
 * Author: Jerry Smith
 * Dependencies: base-web.js
 */
 Ext.namespace("web.directions");

web.directions.gmap = Ext.extend(Ext.ux.GMapPanel, {
	initComponent: function(){
		web.directions.gmap.superclass.initComponent.call(this);
		this.addEvents("afterDirectionsLoaded", "resetMap");
	},
    afterRender : function(){
        
        var wh = this.ownerCt.getSize();
        Ext.applyIf(this, wh);
        
        Ext.ux.GMapPanel.superclass.afterRender.call(this);    
        
        if (this.gmapType === 'map'){
        	if(this.mapOptions){
        		this.gmap = new GMap2(this.body.dom, this.mapOptions);
        	}
        	else{
            	this.gmap = new GMap2(this.body.dom);
            }
        }

        GEvent.bind(this.gmap, 'load', this, function(){
            this.onMapReady();
        });
        
        this.setupMap();
    },
    clearOverlays: function(){
    	this.gmap.clearOverlays();
    },
    setupMap: function(){
		if (this.gmapType === 'panorama'){
            this.gmap = new GStreetviewPanorama(this.body.dom);
        }
        
        if (typeof this.addControl == 'object' && this.gmapType === 'map') {
            this.gmap.addControl(this.addControl);
        }
        
        if (typeof this.setCenter === 'object') {
            if (typeof this.setCenter.geoCodeAddr === 'string'){
                this.geoCodeLookup(this.setCenter.geoCodeAddr);
            }else{
                if (this.gmapType === 'map'){
                    var point = new GLatLng(this.setCenter.lat,this.setCenter.lng);
                    this.gmap.setCenter(point, this.zoomLevel);    
                }
                if (typeof this.setCenter.marker === 'object' && typeof point === 'object'){
                    this.addMarker(point,this.setCenter.marker,this.setCenter.marker.clear);
                }
            }
            if (this.gmapType === 'panorama'){
                this.gmap.setLocationAndPOV(new GLatLng(this.setCenter.lat,this.setCenter.lng), {yaw: this.yaw, pitch: this.pitch, zoom: this.zoom});
            }
        }    
    }, 
    onMapReady : function(){
        this.addMarkers(this.markers);
        this.addMapControls();
        this.addOptions();  
        if(this.mapOptions.googleBarOptions){
        	this.gmap.enableGoogleBar();
        }
        this.openGetDirectionsWindow(this.setCenter.lat, this.setCenter.lng + .005, "Northpoint Church of Christ");
    },

    /**
     * Pass in a lng and lat or geoCodeAddr into tmpMarker and if the marker
     * exists on the map, then it will be retrieved.  Otherwise, null
     * is retrieved.
     */
    getMarkerObject: function(tmpMarker){
    	for(var index = 0; index < this.markers.length; index = index + 1){
    		if(this.markers[index].lng === tmpMarker.lng && this.markers[index].lat === tmpMarker.lat)
    			return this.markers[index];
    	}
    	return null;
    },

    loadDirections: function(locationStart, locationEnd){
    	this.getMap().clearOverlays();
    	var dumpToPanelId = arguments[2] || this.directionsPanelId;
    	
    	if(this.directionsPanelId === null || this.directionsPanelId === undefined){
    		directions = new GDirections(this.getMap());
    	}
    	else{
    		var directionsPanel = document.getElementById(this.directionsPanelId);
    		directionsPanel.innerHTML = "";
    		directions = new GDirections(this.getMap(), directionsPanel);
    	}
    	
    	var loadString = "from: " + locationStart + " to: " + locationEnd;
    	this.locationStart = locationStart;
    	this.locationEnd = locationEnd;
    	
  		directions.load(loadString);
  		this.fireEvent('afterDirectionsLoaded', {start: locationStart, end: locationEnd, toPanel: this.directionsPanelId});
    },
    openCurrentDirectionsPrintPreviewWindow: function(){
    	if(this.locationStart === undefined || this.locationStart === null ||
    		this.locationEnd === undefined || this.locationEnd === null)
    		return;
    	
    	this.openDirectionsPrintPreviewWindow(this.locationStart, this.locationEnd);
    },
    openDirectionsPrintPreviewWindow: function(locationStart, locationEnd){
		var printPreviewURL = "http://maps.google.com/maps?f=d&source=s_d&saddr=" + locationStart + "&daddr=" + locationEnd + "&hl=en&geocode=&mra=ls&ie=UTF8&z=14&layer=c&pw=2"    
		web.base.openWindow(printPreviewURL, 'Unable to open print preview window.  Please make sure all popup blockers are turned off and try again.');
    },    
    openInfoWindowHtml: function(lat, lng, html){
    	var map = this.getMap();
		// map.setCenter(new GLatLng(37.4419, -122.1419), 13);
		map.openInfoWindowHtml(new GLatLng(lat, lng),
        	html);    	
    },    
    openGetDirectionsWindow: function(lat, lng, locationName){
    	var htmlDirectionsForm =
    		  "<div>Get Directions: <span id='toHereSelected'>To Here</span><span id='toHereNotSelected' style='display:none;'><a href='#' onclick='directionsFormToFromClicked(true); return false;'>To Here</a></span>&nbsp;&nbsp;<span id='fromHereSelected' style='display:none;'>From Here</span><span id='fromHereNotSelected'><a href='#' onclick='directionsFormToFromClicked(false); return false;'>From Here</a></span></div>"
    		+ "<div id='mapDirectionsToHere'><form name='mapDirectionsToHereForm' onsubmit='mainViewport.findById(\"mapPanel\").loadDirections(document.getElementById(\"addressEntryFrom\").value,\"" + lat +"," + lng + "\"); return false;'>"
    		+ "Start: <input type='text' name='from' id='addressEntryFrom'/><button onclick='mainViewport.findById(\"mapPanel\").loadDirections(document.getElementById(\"addressEntryFrom\").value,\"" + lat +"," + lng + "\"); return false;'>Go</button><br/>"
    		+ "</form></div>"
    		+ "<div id='mapDirectionsFromHere' style='display:none'><form name='mapDirectionsFromHereForm' onsubmit='mainViewport.findById(\"mapPanel\").loadDirections(\"" + lat +"," + lng + "\", document.getElementById(\"addressEntryTo\").value); return false;'>"
    		+ "End: <input type='text' name='to' id='addressEntryTo'/><button onclick='mainViewport.findById(\"mapPanel\").loadDirections(\"" + lat +"," + lng + "\", document.getElementById(\"addressEntryTo\").value); return false;'>Go</button>"
    		+ "</form></div>"
    		+ "(street address,city,state,zip)"
    	this.openInfoWindowHtml(lat, lng, htmlDirectionsForm);
    },
    onResetMap: function(){
    	this.clearOverlays();
    	this.setupMap();
    	this.onMapReady();
    	this.fireEvent("resetMap");
    }
});
Ext.reg('web.gmappanel',web.directions.gmap);

/**
 * Using a brute force way to accomplish this for now...  I'm in a hurry
 * get this working.  Perhaps there's a way to move this method into gmappanel
 * object.
 */
function directionsFormToFromClicked(showToForm){
	var toHereDisplay, toHereNotDisplay, fromHereDisplay, fromHereNotDisplay;
	
	toHereDisplay = fromHereNotDisplay = (showToForm) ? 'inline' : 'none';
	toHereNotDisplay = fromHereDisplay = (showToForm) ? 'none' : 'inline';
	
	var val;
	if(toHereDisplay === "inline"){
		document.getElementById("addressEntryFrom").value = document.getElementById("addressEntryTo").value;
	}
	else if(fromHereDisplay === "inline"){
		document.getElementById("addressEntryTo").value = document.getElementById("addressEntryFrom").value;
	}
	
	// switch to/from text:
	document.getElementById('toHereSelected').style.display=toHereDisplay; 
	document.getElementById('toHereNotSelected').style.display=toHereNotDisplay;
	document.getElementById('fromHereSelected').style.display=fromHereDisplay; 
	document.getElementById('fromHereNotSelected').style.display=fromHereNotDisplay;
	
	// display the to here form and hide the from here form:
	document.getElementById("mapDirectionsToHere").style.display = toHereDisplay;
	document.getElementById("mapDirectionsFromHere").style.display = fromHereDisplay;

}
