/**
 * This is essentially the public static void main of this page (or so I like to think)
 * Should be the first method called when the page loads.  It creates the SWF object, and then 
 * the MapController JS object which then takes charge of the page and listens for events from the SWF object
 * 
 * The values of the mapString and controllerString are hard-coded in here
 */
function main() {
    player = new ActivityPlayer("activityMap", "activityPlayer");
}
var player = null;
Behaviour.addLoadEvent(main);


var ActivityPlayer = Class.create();
ActivityPlayer.prototype = {
    initialize: function(mapElement, swfElement) {
        this.mapElement = $(mapElement);
        this.swfElement = $(swfElement);
        
        //setup listener for resize of window
        if (window.attachEvent) {
            window.attachEvent("onresize",
                function() {this.mc.map.checkResize()}.bind(this));
        } else { 
            window.addEventListener("resize",
                function() {this.mc.map.checkResize()}.bind(this), false);
        }
    
        this.addFlashObject();
        this.mc = new MapController(mapElement, swfElement);
        this.addControls();
    },

    /**
     * Adds the flash based map controller to the page
     */
    addFlashObject: function() {
        //create flash object
        var so = new SWFObject("/trail/player2/ActivityPlayer.swf?version=070706", "activityPlayer", "950", "170", "8.0.0", "#FFFFFF", true);
    	so.addParam("wmode", "transparent");
    	so.addVariable("server", $F("server")); 
    	so.addVariable("file_pk", $F("episodePkValues"));
    	
    	//if flash isn't installed, display the Get Flash picture instead
    	if( !so.write("controllerContainer") ){
    		var img = document.createElement("img");
    		img.src = "/trail/site/images/get_flash.jpg";
    		img.alt = "Download the Free Flash Player now!";
    
    		var link = document.createElement("a");
    		link.href = "http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
    		link.appendChild( img );
    		
    		$("controllerContainer").appendChild(link);
    	}
    },

    /**
     * Add the appropriate controls to the Map
     */
    addControls: function() {
        this.mc.addClickToMove();
        this.mc.map.addControl(new GMapTypeControl());
        this.mc.map.addControl(new GLargeMapControl());
        this.mc.map.addControl(new PlayerModeControl());
        this.mc.map.addControl(new GScaleControl());
        this.mc.map.addControl(new GOverviewMapControl());
        this.mc.map.enableDoubleClickZoom();
        this.mc.map.enableContinuousZoom();
    },
    
    toString: function() {
        return "ActivityPlayer.  Map: " + this.mapElement.id + " swf: " + this.swfElement;
    }
};


/************************ CALLS FROM FLASH OBJECT *************************/
/**
 * Flash calls this method to inform the JS controller that it's been initialized and is ready
 * for whatever might be coming it's way
 */
function controllerInitialized() {
    log('swf is initialized');
    isControllerInitialized = true;
    if($('ad') != undefined) {
        $('ad').style.display = 'none';
    }
    $('activityMap').style.visibility = 'visible';
}
/**
 * Flash calls this to let the map know it should center and Zoom on the point
 */
function centerAndZoom(lat, lon) {
    log('flash called cAndZ: ' + lat + ' ' + lon);
    player.mc.centerAndScale(lat, lon);
}
/**
 * Flash calls this to let it know it should draw the track on the map
 */
function drawTrack(points, color) {
    player.mc.drawTrack(points, color);
}
/**
 * Flash calls this to let the controller know that the marker has moved
 */
function moveMarkers(index) {
    player.mc.moveMarkers(index);
}
