AjaxPlugins.Layers = {

    /* HTML element's id definitions */
    layersFolderId: 'ajaxLayersId',
    recenterScaleId: 'recenter_scale',  
    switchesId:'switches',
    layersContinentalLocalId: 'ajaxContinentalLocalId',
	 legends:'legends',
    handleResponse: function(pluginOutput, argObject) {
        /* Plugin general behaviour */
        
        /* Redraws layers HTML Code */     
        AjaxHandler.updateDomElement(this.switchesId, 'innerHTML',
                                     pluginOutput.htmlCode.switches);                             +    
        AjaxHandler.updateDomElement(this.layersFolderId, 'innerHTML',
                                     pluginOutput.htmlCode.layers);
         AjaxHandler.updateDomElement(this.layersContinentalLocalId, 'innerHTML',
                                     pluginOutput.htmlCode.continentalLocal
                                     );
        AjaxHandler.updateDomElement(this.legends, 'innerHTML',
                                     pluginOutput.htmlCode.legends
                                     );
        
        
        /* Reopen open nodes */
        if ($(this.layersFolderId) != null) {
            startOpenNodes = pluginOutput.variables.startOpenNodes;
            // Uses layers.tpl + layers.js mecanism
            eval("var openNodes = new Array(" + startOpenNodes + ");");
            writeOpenNodes(true);
        }
        
        
        if (argObject.changeExtent == true){
        var extents=pluginOutput.variables.extent.split(new RegExp("[,]+", "g"));
        mainmap.setExtent(extents[0],extents[1],extents[2],extents[3]);
        }
    }  
};

/*
 * Images plugin's Actions
 */
 
AjaxPlugins.Layers.Actions = {};

AjaxPlugins.Layers.Actions.LayerShowHide = {
    buildPostRequest: function(argObject) {
        return AjaxHandler.buildPostRequest();
    }
};

AjaxPlugins.Layers.Actions.SwitchCountry = {
    buildPostRequest: function(argObject) {
    var select_switch_id=document.carto_form.switch_id;
    var switch_value=select_switch_id.options[select_switch_id.selectedIndex].value;
    var switch_value_substring=switch_value.substring(6);
    document.carto_form.shortcut_id.value=switch_value_substring;
    document.carto_form.shortcut_doit.value=1;
    select_switch_id.options[0].value=switch_value;
    return AjaxHandler.buildPostRequest();
    },
    onAfterAjaxCall: function(argObject) {
    document.carto_form.shortcut_doit.value=0;
    
    }
};

AjaxPlugins.Layers.Actions.LayerDropDownChange = {
    buildPostRequest: function(argObject) {
        return AjaxHandler.buildPostRequest();
    }
};

function nationalView()  {
     var current_switch=document.carto_form.switch_id.options[0].value;
    var current_switch_index=current_switch.substring(6);
    document.carto_form.shortcut_id.value=current_switch_index;
    document.carto_form.shortcut_doit.value=1;
    location.href=document.carto_form.action+'?reset_session&shortcut_doit=1&switch_id='+current_switch+'&shortcut_id='+current_switch_index;
   
}

AjaxPlugins.Layers.Actions.NationalViewReset = {
    buildPostRequest: function(argObject) {
    //reset_session doesn't work in ajax mode
    //var current_switch=document.carto_form.switch_id.options[0].value;
    //var current_switch_index=current_switch.substring(6);
    //document.carto_form.shortcut_id.value=current_switch_index;
    //return '&reset_session=yes&shortcut_doit=1&switch_id='+current_switch+'&shortcut_id='+current_switch_index;
    },
    onAfterAjaxCall: function(argObject) {
        document.carto_form.shortcut_doit.value=0;
    }
};