/* $Header: /var/home/dev/dzf/dzf_src/RCS/finder_funcs.js,v 1.20 2007/08/27 02:39:15 dev Exp dev $ */

if( typeof( Finder ) === 'undefined' ) {
    Finder = {};
}


Finder.Init = function() {
    Finder.InitMap();
}

if( window.onload ) {
    Finder.window_onload = window.onload;
    window.onload = function() {
        Finder.Init();
        Finder.window_onload();
    }
} else {
    window.onload = Finder.Init;
}

window.onunload = function() {
    if( typeof( GUnload ) !== 'undefined' ) {
        GUnload();
    }
};


if( typeof( Finder.addControls ) !== 'function' ) {
    Finder.addControls = function() {
        var size = Finder.map.getSize();
        if( size && size.height && size.height <= 300 ) {
            Finder.map.addControl( new GSmallMapControl() );
            Finder.map.addControl( new GMapTypeControl() );
        } else {
            Finder.map.addControl( new GLargeMapControl() );
            Finder.map.addControl( new GMapTypeControl() );
        }
    }
}


Finder.DEFAULT_LAT = 21;
Finder.DEFAULT_LNG = -21;


/*
 * stuff to make cross-platform scripting easier
 */
Finder.W3CDOM_Event = function( target ) {
    this.clientX = event.clientX;
    this.clientY = event.clientY;
    this.currentTarget = target;
    this.preventDefault = function () { window.event.returnValue = false }
    return this;
}

Finder.add_listener = function( object, event, callback ) {
    if (object !== null) {
        if (object.addEventListener) {  // w3c DOM
            object.addEventListener (event, callback, false);
        } else if (object.attachEvent) { // IE
            object.attachEvent ('on' + event, function () { callback( new Finder.W3CDOM_Event( object ) ); } );
        }
    }
}


Finder.createMarker = function( place ) {
    var icon = new GIcon( Finder.baseIcon );
    icon.image = Finder.img_url + "/marker" + place.let + ".png";
    if( typeof( place.gll ) === 'undefined' ) {
        place.gll = new GLatLng( place.lat, place.lng );
    }

    place.marker = new GMarker( place.gll, icon );
    place.htmlBlock = "<div class='flyout'><h2><a href='" + place.url + "'>" + place.name + "</a></h2>";
    if( place.dist ) {
        place.htmlBlock += "<span>" + place.dist + " miles from your location</span>";
    }
    place.htmlBlock += "</div>";

    GEvent.addListener( place.marker, "click", function() {
        place.marker.openInfoWindowHtml( place.htmlBlock );
    } );
    return place.marker;
}

Finder.containsPlace = function( place ) {
    for( p in Finder.places ) {
        if(   ( Finder.places[p].lat === place.lat )
           && ( Finder.places[p].lng === place.lng )
           && ( Finder.places[p].name === place.name ) ) {
            return true;
        }
    }
    return false;
}

function dump( obj ) {
    var msg = "";
    for( p in obj ) {
        if( typeof( obj[p] ) === 'function' ) {
            // noop
        } else if( typeof( obj[p] ) === 'object' ) {
            msg += dump( obj[p] );
        } else {
            msg += p + "=" + obj[p] + " (" + typeof( obj[p] ) + ")\n";
        }
    }
    return msg;
}

if( typeof( Finder.InitMap ) !== 'function' ) {
    Finder.InitMap = function() {
        var gll, i, ll, lat, lng, bZoom,
            mapCont = document.getElementById( 'ovmap' );

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        if( typeof( GIcon ) !== 'undefined' ) {
            Finder.baseIcon = new GIcon();
            Finder.baseIcon.shadow = Finder.img_url + "/shadow50.png";
            Finder.baseIcon.iconSize = new GSize( 20, 34 );
            Finder.baseIcon.shadowSize = new GSize( 37, 34 );
            Finder.baseIcon.iconAnchor = new GPoint( 9, 34 );
            Finder.baseIcon.infoWindowAnchor = new GPoint( 9, 2 );
            Finder.baseIcon.infoShadowAnchor = new GPoint( 18, 25 );
        }

        if( typeof( Finder.ieInit ) !== 'undefined' ) {
            Finder.ieInit();
        }

        if ( mapCont ) {
            if ( mapCont.className.match( /google/ ) ) {
                Finder.mapCont = mapCont;
                // Google maps case
                if ( GBrowserIsCompatible() ) { //TODO: pass this (?) from previous page
                    Finder.map = new GMap2( mapCont );
                    Finder.places = [];

                    if( "points" in Finder && Finder.objectContains( Finder.points ) ) {
                        Finder.bounds = new GLatLngBounds(); // init with centerpoint?
                        for( i in Finder.points ) {
                            Finder.points[i].gll = gll = new GLatLng( Finder.points[i].lat, Finder.points[i].lng );
                            Finder.bounds.extend( gll );
                        }
                        bZoom = Finder.map.getBoundsZoomLevel( Finder.bounds );
                        Finder.map.setCenter( Finder.bounds.getCenter(), ( bZoom > 14 ? 14 : bZoom ) );
                    } else {
                        Finder.recenterAndZoom( Finder.findLatLng() );
                    }


                    // controls
                    if( typeof( Finder.addControls ) === 'function' ) {
                        Finder.addControls();
                    }

                    // map events
                    GEvent.addListener( Finder.map, "moveend", Finder.mapChangeFactory( "move" ) );
                    GEvent.addListener( Finder.map, "zoomend", Finder.mapChangeFactory( "zoom" ) );

                    // crosshair
                    GEvent.addListener( Finder.map, "dragstart", Finder.mapDragFactory( "start" ) );
                    GEvent.addListener( Finder.map, "dragend", Finder.mapDragFactory( "end" ) );

                    if( typeof( Finder.addPlaces ) !== 'undefined' ) {
                        Finder.addPlaces( Finder.points );
                    }

                } else {
                    var h = document.createElement( 'h2' );
                    h.appendChild( document.createTextNode( "Sorry, your browser doesn't support Google Maps." ) );
                    mapCont.appendChild( h );
                }

                if( typeof( Finder.attachBubbles ) === 'function' ) {
                    Finder.attachBubbles();
                }

                if( typeof( Finder.addCrosshairs ) === 'function' ) {
                    Finder.addCrosshairs();
                }

                if( typeof( Finder.latLngUpdateInit ) === 'function' ) {
                    Finder.latLngUpdateInit();
                }
            }
        }
    }
}

Finder.recenterAndZoom = function( latLng, suppressZoomChange ) {
    var bZoom;

    Finder.bounds = new GLatLngBounds();
    if( typeof( latLng ) === 'object' ) {
        Finder.bounds.extend( new GLatLng( latLng.lat, latLng.lng ) );
        bZoom = Finder.map.getBoundsZoomLevel( Finder.bounds );
        var zl = ( suppressZoomChange
                   ? Finder.map.getZoom()
                   : ( latLng.lat === Finder.DEFAULT_LAT && latLng.lng === Finder.DEFAULT_LNG
                       ? 1
                       : ( bZoom > 14 ? 14 : bZoom ) ) );
        Finder.map.setCenter( Finder.bounds.getCenter(), zl );
    }
}

Finder.findLatLng = function() {
    var lat, lng, res;
    if(   ( ( lat = document.getElementById( 'lat' ) ) )
       && ( ( lng = document.getElementById( 'lng' ) ) ) ) {
        lat = parseFloat( lat.value );
        lng = parseFloat( lng.value );
        if(   ( ! isNaN( lat ) )
           && ( ! isNaN( lng ) ) ) {
            return { lat:lat, lng:lng }
        }
    }
    return { lat:Finder.DEFAULT_LAT, lng:Finder.DEFAULT_LNG }
}


function Place() {
    for( var i=0; i<arguments.length; ++i ) {
        this[arguments[i]] = arguments[++i];
    }
    return this;
}
Place.prototype.toString = function() {
    return dump( this );
}

Finder.mapChangeFactory = function( type ) {
    if( typeof( Finder.mapChanged ) === 'function' ) {
        return function() { Finder.mapChanged( type ); }
    } else {
        return function() {};
    }
}

Finder.mapDragFactory = function( type ) {
    if( typeof( Finder.mapDrag ) === 'function' ) {
        return function() { Finder.mapDrag( type ); }
    } else {
        return function() {};
    }
}

Finder.getDZUrl = function( id ) {
    return '/dropzone/Detailed/' + id + '.shtml';
}

Finder.objectContains = function( obj ) {
    var i=0;
    for( key in obj ) {
        if( obj.hasOwnProperty( key ) ) {
            ++i;
        }
    }
    return i>0;
}

Finder.log = function( msg ) {
    if( 'console' in window && 'log' in console ) console.log( msg );
}
