//var globoLayerList = new Array();
var onEditing = false; // indica se si e' in fase di editing
var pluginlist;


//This script detects the following:
//Flash
//Windows Media Player
//Java
//Shockwave
//RealPlayer
//QuickTime
//Acrobat Reader
//SVG Viewer

function detectPlugin() {
    var agt=navigator.userAgent.toLowerCase();
    var ie  = (agt.indexOf("msie") != -1);
    var ns  = (navigator.appName.indexOf("Netscape") != -1);
    var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
    var mac = (agt.indexOf("mac")!=-1);

    if (ie && win) {    pluginlist = detectIE("Adobe.SVGCtl","SVG Viewer") + detectIE("SWCtl.SWCtl.1","Shockwave Director") + detectIE("ShockwaveFlash.ShockwaveFlash.1","Shockwave Flash") + detectIE("rmocx.RealPlayer G2 Control.1","RealPlayer") + detectIE("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime") + detectIE("MediaPlayer.MediaPlayer.1","Windows Media Player") + detectIE("PDF.PdfCtrl.5","Acrobat Reader"); }
    if (ns || !win) {
            nse = ""; for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
            pluginlist = detectNS("image/svg-xml","SVG Viewer") + detectNS("application/x-director","Shockwave Director") + detectNS("application/x-shockwave-flash","Shockwave Flash") + detectNS("audio/x-pn-realaudio-plugin","RealPlayer") + detectNS("video/quicktime","QuickTime") + detectNS("application/x-mplayer2","Windows Media Player") + detectNS("application/pdf","Acrobat Reader");
    }

    function detectIE(ClassID,name) { result = false; document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n result = IsObject(CreateObject("' + ClassID + '"))</SCRIPT>\n'); if (result) return name+','; else return ''; }
    function detectNS(ClassID,name) { n = ""; if (nse.indexOf(ClassID) != -1) if (navigator.mimeTypes[ClassID].enabledPlugin != null) n = name+","; return n; }

    pluginlist += navigator.javaEnabled() ? "Java," : "";
    if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);

}

detectPlugin();




// costruttore
function GloboLayer() {
    this.nome = "";
    this.editing = -1; // -1 mai, 0 non in editazione, 1 in editazione
}

// aggiorna layer corrente in editazione nell'array globoLayerList
function setEditingLayer(name,globoLayerList) {
    for (var i=0; i < globoLayerList.length; i++) {
        if (globoLayerList[i].nome == name) globoLayerList[i].editing = 1;
        else {
            // se e' editabile
            if (globoLayerList[i].editing != -1) globoLayerList[i].editing = 0;
        }
    }
}

// attiva flash
function FlashInit(layer,globoLayerList,edit_where,frozenpars)
{

  if (wantFlash) {
    if (flashIsDetected()) {
      var strNomiLayer="",strEditingLayer="",strIdLayer="";
      
      // servizio corrente
      service = getEditingServiceName();
      
      var IE = navigator.appName.indexOf("Microsoft") != -1;
      var filmato = IE ? window.objEdit : window.document.objEdit;
      //var filmato = document.getElementById('objEdit');
      
      //if (filmato!=null)    {
      //alert("1");
      filmato.SetVariable("_root.html_service",service);
      //filmato.SetVariable("_root.html_service","");
      filmato.SetVariable("_root.html_numLayer",globoLayerList.length);
      for (var i=0; i < globoLayerList.length; i++) {
        //alert(globoLayerList[i].nome);
        strNomiLayer += globoLayerList[i].nome;
        //alert(strNomiLayer);
        strEditingLayer += globoLayerList[i].editing;
        strIdLayer += globoLayerList[i].id;
        if (i != (globoLayerList.length-1)) {
          strNomiLayer += ",";
          strEditingLayer += ",";
          strIdLayer += ",";
        }
      }

      filmato.SetVariable("_root.html_globoLayerList_nome",strNomiLayer);
      filmato.SetVariable("_root.html_globoLayerList_id",strIdLayer);
      filmato.SetVariable("_root.html_globoLayerList_editing",strEditingLayer);
      filmato.SetVariable("_root.html_frozenpars",frozenpars);
      filmato.SetVariable("_root.html_edit_where",edit_where);
      filmato.TCallFrame("_root.init",2);
      //filmato.TCallFrame("_root.showMsgBox",0);
    }
    else alert("Non hai installato o non e' abilitato il plugin flash!");
  }
}

//var flashAggiornaTimeoutId=0;

function flashAggiornaStatoFlash() {
  //clearTimeout(flashAggiornaTimeoutId);
  //window.status = "flashAggiornaTimeoutId="+flashAggiornaTimeoutId;
  //alert("flashAggiornaTimeoutId="+flashAggiornaTimeoutId);
  if (toolIsLoading()) {
      //flashAggiornaTimeoutId = setTimeout("flashAggiornaStatoFlash()", 2000);
      return;
  }

  if (flashIsDetected() && wantFlash) {
  //window.status = "1.";

  var IE = navigator.appName.indexOf("Microsoft") != -1;
  var filmato = IE ? window.objEdit : window.document.objEdit;
  //alert(filmato.width);
    //alert(filmato.id);

    //if (filmato!=null) {
        //alert("2");
    var mapext = mapGetExtent();
    filmato.SetVariable("_root.html_minx", mapext[0]);
    filmato.SetVariable("_root.html_maxx", mapext[1]);
    filmato.SetVariable("_root.html_miny", mapext[2]);
    filmato.SetVariable("_root.html_maxy", mapext[3]);
    filmato.SetVariable("_root.html_MapImageX",document.processa.MapImageX.value);
    filmato.SetVariable("_root.html_MapImageY",document.processa.MapImageY.value);
       //window.status += "2.["+document.laMappa.src+"]";
    filmato.SetVariable("_root.html_ImageURL", document.laMappa.src);
    //filmato.SetVariable("_root.html_ImageURL",dummy);
    //filmato.SetVariable("_root.html_ImageURL","http://localhost:8080/StuGeoMap/img/map/flashbug.jpg");
       //window.status += "3.";

    //var js_refresh="javascript:toolSalvaStatoPreLocator();javascript:toolSetOperazione('refresh');javascript:toolLoad();javascript:toolRipristinaStatoPreLocator();";
    //var js_refresh="javascript:toolRefreshStart('refresh');";
    var js_refresh="javascript:dummyFunction(1);javascript:toolRefreshDoOp();javascript:dummyFunction(2);";
    //var js_refresh="javascript:alert('doing');javascript:toolDoRefreshImage();javascript:alert('done');";
    filmato.SetVariable("_root.html_js_refresh",js_refresh);
       //window.status += "4.";
    filmato.TCallFrame("_root.inserisci",2);
       //window.status += "5";
    }
}

function dummyFunction(arg) {
  mapDebugMessage("dummyFunction:"+arg)
}

// funzione per il ridimensionamento dell'oggetto flash
function flashResize(flashDivName,flashObjName,mapDivName) {

    //alert("222");
    if (flashIsDetected() && wantFlash) {
        //alert(iHeight);
        layerGetStyle(flashDivName).left = mappaLeft;
        layerGetStyle(flashDivName).top = mappaTop;
          layerGetStyle(flashObjName).width = mappaWidth;
          layerGetStyle(flashObjName).height = mappaHeight;
    }
    flashAggiornaStatoFlash();
}


    // rilevo la presenza di flash
function flashIsDetected() {
    if (pluginlist.indexOf("Flash")!=-1) return true;
    else return false;
}



// inizializzazione temporanea di flash
function doFlashInit() {
    // carico informazioni dalla pagina HTML
    var htmlpage_editwhere = document.processa.edit_where.value;
    var htmlpage_frozenpars = document.processa.frozenpars.value;
    // g_editableLayersNames = globale della pagina
    // la lista la leggo dal template
    var flashLayerList = new Array();
    //alert(g_editableLayersNames.length);
    for (c=0; c<g_editableLayersNames.length; c++)
    {
        //alert(g_editableLayersNames[c]);
        var layer = new GloboLayer();
        layer.nome = g_editableLayersNames[c];
        layer.editing = 0;
        layer.id = g_editableLayersIds[c];
        flashLayerList[c]=layer;
    }

    /*
    layer = new GloboLayer();
    layer.nome = "Frane lineari (proposta)";
    layer.editing = 0;
    globoLayerList[1] = layer;
    layer = new GloboLayer();
    layer.nome = "Frane PIFF (proposta)";
    layer.editing = 0;
    globoLayerList[2] = layer;
    // inizializzo flash
    */

    FlashInit("",flashLayerList, htmlpage_editwhere, htmlpage_frozenpars);
}

// imposta geometrie di snap
function setSnapGeometries(geometries) {
  var IE = navigator.appName.indexOf("Microsoft") != -1;
  var filmato = IE ? window.objEdit : window.document.objEdit;
  filmato.SetVariable("applicationInterfaceJS.setSnapGeometries_par0",geometries);
  filmato.TCallLabel("applicationInterfaceJS","setSnapGeometries");
}
