﻿// JScript File

Parts.ToolBar = function(id){
    Parts.ToolBar.initializeBase(this, [id]);
    this.stateButtons = false;
    this.initialize();
}

Parts.ToolBar.prototype.initialize = function(){
    //make sure toolbar reflect mode changes done by right-click menu
    Global.EventController().subscribe("evtMapModeChanged", Function.createDelegate(this, this.onMapActionChanged));

    
    this.stateButtons = Global.DataController().getData(this.getId() + ".stateButtons");

    //hook up Toolbar actions
    var oElement;
    
    //get image button
    oElement = this.elm("imgSelect"); 
    //if image button exists..
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onSelect));
    
    oElement = this.elm("imgZoomIn");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onZoomIn));
    oElement = this.elm("imgZoomOut");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onZoomOut));
    oElement = this.elm("imgZoomStart");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onZoomStart));
    oElement = this.elm("imgPan");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onPan));
    oElement = this.elm("imgPrint");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onPrint));
    oElement = this.elm("imgPrintPreview");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onPrintPreview));
    oElement = this.elm("imgPolygonSelect");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onPolygonSelect));
    oElement = this.elm("imgFullScreen");
    if (oElement) $addHandler(oElement, 'click', Function.createDelegate(this, this.onFullScreen));
    
}

/**
    Make sure selections in rightclick menu are reflected in the toolbar
**/
Parts.ToolBar.prototype.onMapActionChanged = function(){
    var action;
    if(MapApi){
        action = MapApi.getMapMode();
    
//    MapApiClient.EnumMapMode:
//    MAP_MODE_ZOOM_RECT: 1,
//    MAP_MODE_ZOOM_OUT: 2,
//    MAP_MODE_MOVE: 3, 
//    MAP_MODE_SELECT: 4,
//    MAP_MODE_ZOOM_START: 5,
//    MAP_MODE_DIGITIZE_RECT: 6,
//    MAP_MODE_DIGITIZE_POLYGON: 7,
//    MAP_MODE_SELECT_BY_POLYGON: 8
    
        switch(action){
        case MapApi.EnumMapMode().MAP_MODE_ZOOM_RECT :
            this.selectButton("imgZoomIn");
        break    
        case MapApi.EnumMapMode().MAP_MODE_ZOOM_OUT :
            this.selectButton("imgZoomOut");
        break
        case MapApi.EnumMapMode().MAP_MODE_MOVE :
            this.selectButton("imgPan");
        break    
        case MapApi.EnumMapMode().MAP_MODE_SELECT :
            this.selectButton("imgSelect");
        break
        case MapApi.EnumMapMode().MAP_MODE_ZOOM_START :
            this.selectButton("imgZoomStart");
        break
                
        case MapApi.EnumMapMode().MAP_MODE_DIGITIZE_RECT :
        case MapApi.EnumMapMode().MAP_MODE_DIGITIZE_POLYGON : 
            //do nothing
        break
        case MapApi.EnumMapMode().MAP_MODE_SELECT_BY_POLYGON :
            this.selectButton("imgPolygonSelect");
        break
        }
        
    }
}

Parts.ToolBar.prototype.onZoomIn = function(){
    this.selectButton("imgZoomIn");
    MapApi.setMapMode(MapApi.EnumMapMode().MAP_MODE_ZOOM_RECT);
}

Parts.ToolBar.prototype.onZoomOut = function(){
    this.selectButton("imgZoomOut");    
    MapApi.setMapMode(MapApi.EnumMapMode().MAP_MODE_ZOOM_OUT);
}

Parts.ToolBar.prototype.onPan = function(){
    this.selectButton("imgPan");
    MapApi.setMapMode(MapApi.EnumMapMode().MAP_MODE_MOVE);
}

Parts.ToolBar.prototype.onSelect = function(){
    this.selectButton("imgSelect");
    MapApi.setMapMode(MapApi.EnumMapMode().MAP_MODE_SELECT);
}

Parts.ToolBar.prototype.onZoomStart = function(){
    //this.selectButton("imgZoomStart");
    MapApi.setMapMode(MapApi.EnumMapMode().MAP_MODE_ZOOM_START);
}

Parts.ToolBar.prototype.onPrint = function(){
    //this.selectButton("imgPrint");
    MapApi.printMap();
    
}
Parts.ToolBar.prototype.onPrintPreview = function(){
    //this.selectButton("imgPrintPreview");
    MapApi.printPreview();
}
Parts.ToolBar.prototype.onPolygonSelect = function(){
    this.selectButton("imgPolygonSelect");
    MapApi.setMapMode(MapApi.EnumMapMode().MAP_MODE_SELECT_BY_POLYGON);
}
Parts.ToolBar.prototype.onFullScreen = function(){
    //Hotlink to make sure fullscreen map shows the same area as the embedded version.
    if(MapApi && MapApi.getMapRect()){
        var width = MapApi.getMapWidthMeter();
        var rect = MapApi.getMapRect();
        var x = rect.LLX + ((rect.URX - rect.LLX) / 2);
        var y = rect.LLY + ((rect.URY - rect.LLY) / 2);
        
        //get language
        var language = "";
        language = Utils.getPageLanguage();
        
        //get view part
        var viewName = "";
        viewName = Global.DataController().getData(this.getId() + ".ViewPart");
        
        var fullscreenOptions = "";
        fullscreenOptions = Global.DataController().getData(this.getId() + ".FullScreenOptions");
        //build url
        var url = "Default.aspx?MapFrame_resizable=true&MapClient_mapCenterXworld=" + x 
            + "&MapClient_mapCenterYworld=" + y 
            + "&MapClient_mapWidthWorld=" + width;
        //add language
        if(language != "") {
            url += "&language=" + language;
        }
        //add selected view options
        if(viewName){
            var view = eval(viewName);
            var service = view.getSelectedService();
            var options = view.getSelectedOptions();
            var strOptionsOn = "";
            var strOptionsOff = "";
            
            for(i = 0; i < options.length; i++){
                if(options[i][1] == "ON"){ strOptionsOn += options[i][0] + "|";}
                if(options[i][1] == "OFF"){ strOptionsOff += options[i][0] + "|";}        
              
            }
            
            //add services, selected options.
            url += "&" + viewName + "_selectedService=" + service;
            url += "&" + viewName + "_selectedOptionsOn=" + strOptionsOn.substring(0, strOptionsOn.length -1);
            url += "&" + viewName + "_selectedOptionsOff=" + strOptionsOff.substring(0, strOptionsOff.length -1);
            
            //add filters
            var overWater = view.getSelectedOverWater(); //true/false
            var timeLevel = view.getSelectedTimeLevel();
            var covLevel  = view.getSelectedEnvironment(); 
            if(covLevel && covLevel.length > 0){url += "&" + viewName + "_covLevel=" + covLevel;}
            if(timeLevel && timeLevel.length > 0){url += "&" + viewName + "_timeLevel=" + timeLevel;}
            if(overWater){
                url += "&" + viewName + "_overWater=WATER";
            }else{
                url += "&" + viewName + "_overWater=LAND_ONLY";
            }
        }
        
        //add other fullscreen options
        if(fullscreenOptions){
            url += fullscreenOptions;
        }
        
        //alert(url);
        window.open(url, "_blank", "toolbar=no, scrollbars=yes, location=no, directories=no, status=no, menubar=no, resizable=yes, scrollbars=yes, height=750, width=750");
    }
}


Parts.ToolBar.prototype.selectButton = function(buttonOn){
    //deselect all, then select the One.
    
    //if STATE, set activebutton url also.
    
    
    var button;
    button = this.elm("imgZoomIn");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgZoomOut");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgZoomStart");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgPan");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgSelect");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton" ;
    button = this.elm("imgPrint");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgPrintPreview");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgPolygonSelect");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgFullScreen");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgLinkInt");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    button = this.elm("imgLinkExt");
    if(button) { this.toggleButton(button, false); } //button.className = "ToolBar_clsButton";
    
    button = this.elm(buttonOn);
    if(button) { this.toggleButton(button, true); } // button.className = "ToolBar_clsButtonSelected";
}

/*
* Toggle button image when using state buttons
*/
Parts.ToolBar.prototype.toggleButton = function(button, bOn){
       
    if(bOn){
        button.className = "ToolBar_clsButtonSelected"
        if(this.stateButtons){
            button.src = button.src.replace(".gif", "_on.gif");
        }
    }
    else{
        button.className = "ToolBar_clsButton";
        if(this.stateButtons){
            button.src = button.src.replace("_on.gif", ".gif");
        }
    }
}

/*
*  Inherit from part base class.
*/
Parts.ToolBar.registerClass('Parts.ToolBar', Parts.Part, Sys.IDisposable);
