//==============================================================
// MapF_Scale ------------------------------------------------
MapF_Scale = function() { }
//============> MapF_Scale
//------------> MapF_Scale.create(MapID)
//------------> MapF_Scale.remove(MapID)
//------------> MapF_Scale.Initial(MapID)
//------------> MapF_Scale.getInnerHTML(MapID)
//------------> MapF_Scale.refresh (MapID)
//--------------------------------------------------------------
//MapF_Scale.create --------------------------------------------
MapF_Scale.create = function(MapID, Ocode) {
    var Mcode = Map.getMcodeFromMapID(MapID);
    var Hoptions = Holder.getOptions(MapID, Ocode);
    var FrameHolderSize = { w: 0, h: 0 };
    //----------------------------------
    var innerHTML = MapF_Scale.getInnerHTML(MapID, Ocode);
    document.getElementById(Mcode + "^" + Ocode + "^" + "Holder_MM").innerHTML += innerHTML;
    //----------------------------------
   // MapF_Scale.refresh(MapID);
    if (Hoptions.DefSize.size == "auto"){
        var Elem0 = document.getElementById(Mcode + "^" + Ocode + "^" + "Scale_Table");
        var Size0 = Map.getElem.Size(Elem0);
        FrameHolderSize.w = Size0.w ;
        FrameHolderSize.h = Size0.h ;
    }else {
        FrameHolderSize.w = Hoptions.DefSize.w ;
        FrameHolderSize.h = Hoptions.DefSize.h ;
    }
    return FrameHolderSize;  
}
//--------------------------------------------------------------
//MapF_Scale.getInnerHTML ----------------------------------
MapF_Scale.getInnerHTML = function(MapID, Ocode) {
    var Mcode = Map.getMcodeFromMapID(MapID);
    //----------------------------------
    var innerHTML="";
    innerHTML+="<table id='"+ Mcode + "^" + Ocode + "^" + "Scale_Table'  class='Scale_Table' border='0' cellspacing='0' cellpadding='0'"
    innerHTML+=">"
    innerHTML += "<tr class='Scale_TitleRow' >"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_label0' colspan='11'  class='Scale_Chell_Scale' >"
    innerHTML += "[1:200,000]"
    innerHTML += "</td>"
    innerHTML += "</tr>"
    innerHTML += "<tr class='Scale_LabelRow'>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_label1' colspan='3' class='Scale_Chell_label' >"
    innerHTML += "0"
    innerHTML += "</td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_label2' colspan='3'class='Scale_Chell_label'  >"
    innerHTML += "2"
    innerHTML += "</td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_label3' colspan='2' class='Scale_Chell_label'  >"
    innerHTML += "&nbsp;"
    innerHTML += "</td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_label4' colspan='3' class='Scale_Chell_label'  >"
    innerHTML += "6 km"
    innerHTML += "</td>"
    innerHTML += "</tr>"
    innerHTML += "<tr class='Scale_Row'>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B0' class='Scale_Chell_E' valign='top' style='width: 30px; text-align:right;'>"
    innerHTML += "<img src='Images/ScaleBar_L.png'  />"
    innerHTML += "</td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_I1' class='Scale_Chell_I' valign='top' style='width: 2px; background-image: url(Images/ScaleBar_I.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B1' class='Scale_Chell_B' valign='top' style='width: 30px; background-image: url(Images/ScaleBar_B.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B2' class='Scale_Chell_B' valign='top' style='width: 30px; background-image: url(Images/ScaleBar_W.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_I2' class='Scale_Chell_I' valign='top' style='width: 2px; background-image: url(Images/ScaleBar_I.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B3' class='Scale_Chell_B' valign='top' style='width: 30px; background-image: url(Images/ScaleBar_B.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B4' class='Scale_Chell_B' valign='top' style='width: 30px; background-image: url(Images/ScaleBar_B.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B5' class='Scale_Chell_B' valign='top' style='width: 30px; background-image: url(Images/ScaleBar_W.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B6' class='Scale_Chell_B' valign='top' style='width: 30px; background-image: url(Images/ScaleBar_W.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_I3' class='Scale_Chell_I' valign='top' style='width: 2px; background-image: url(Images/ScaleBar_I.png);'></td>"
    innerHTML += "<td id='" + Mcode + "^" + Ocode + "^" + "SChell_B7' class='Scale_Chell_E' valign='top' style='width: 30px;text-align:left;'>"
    innerHTML += "<img src='Images/ScaleBar_R.png'  />"
    innerHTML += "</td>"
    innerHTML += "</tr>"
    innerHTML += "</table>"
return innerHTML;
}
////--------------------------------------------------------------
////MapF_Scale.remove --------------------------------------------
//MapF_Scale.remove = function(MapID) {
//    var Mcode = Map.getMcodeFromMapID(MapID);
//    var Ocode = "Scale";
//    var Celem = document.getElementById(Mcode + "^Map^" + "MapContainer")
//    var elem = document.getElementById(Mcode + "^" + Ocode + "^" + "MapF_Container")
//    if (elem) {
//        Celem.removeChild(Elem);
//        return true;
//    } else {
//        return false;
//    }
//}
//--------------------------------------------------------------
//MapF_Scale.Initial ---------------------------------------
MapF_Scale.Initial = function(MapID) {
   
    var Mcode = Map.getMcodeFromMapID(MapID);
    var Ocode = "Scale";
//    var Mopt = Map.MapObj[MapID]
//    var Fopt = Mopt.FrameHolder.Scale;
//    var x = Fopt.DefSize.x;
//    var y = Fopt.DefSize.y;
//    var w = Fopt.DefSize.w;
//    var h = Fopt.DefSize.h;   
    //---------------------------------- 
    var Hoptions = Holder.getOptions(MapID, Ocode); 
    var w = 0;
    if (Hoptions.style == 0) { w = 18; } else { w = 2 }
    //----------------------------------
    var elem = document.getElementById(Mcode + "^" + Ocode + "^" + "Holder_Container")
    elem.style.width = Hoptions.DefSize.w + "px";
    elem.style.height = Hoptions.DefSize.h + "px";
    elem.style.left = Hoptions.DefSize.x + "px";
    elem.style.top = Hoptions.DefSize.y + "px";
    //----------------------------------
    //----------------------------------
//    var elem0 = document.getElementById(Mcode + "^" + Ocode + "^" + "Scale_Table")
//    elem0.style.visibility = "visible";
    //----------------------------------
    var elem0 = document.getElementById(Mcode + "^" + Ocode + "^" + "Holder_MM")
    elem0.style.width = (Hoptions.DefSize.w -w) + "px";
    elem0.style.height = (Hoptions.DefSize.h  -w)+ "px";
    //----------------------------------    
    return true;
}

//--------------------------------------------------------------
//MapF_Scale.refresh ----------------------------------
MapF_Scale.refresh = function(MapID) {
    var Mcode = Map.getMcodeFromMapID(MapID);
    var Ocode = "Scale";
    //----------------------------------
    var Mopt =Map.MapObj[MapID];
    var ScaleSizePX=35;
//    var ScaleUnit="";
//    var ScaleLabel1="";
//    var ScaleLabel2="";
    var PxM=350
    var MPx= 0.002
    var Px30=12.5;
    var Px30Km= 10 
    var w =35 ;
    var ScaleLabel1=(Px30Km * 2);
    var ScaleLabel2=(Px30Km * 6);
    var ScaleUnit="Km" 
    //----------------------------------
    PxM= (Mopt.MapSize.w/Mopt.ImageSize.w + Mopt.MapSize.h/Mopt.ImageSize.h)/2
    MPx= (Mopt.ImageSize.w/Mopt.MapSize.w + Mopt.ImageSize.h/Mopt.MapSize.h )/2
    Px30=(PxM/1000)*ScaleSizePX; 
     
    if (PxM==0){
         PxM=350
         MPx= 0.002
         Px30=12.5;
    }

    //----------------------------------
    if (Px30>=0.5){
        Px30Km= Math.round((PxM/1000)*ScaleSizePX)
        w = Px30Km *1000 * MPx
        ScaleLabel1=(Px30Km * 2);
        ScaleLabel2=(Px30Km * 6);
        ScaleUnit="Km"
        }
    if (Px30>0.25 && Px30<0.5){
        Px30Km= Math.round((PxM/100)*ScaleSizePX)
        w = Px30Km *100 * MPx;
        Px30Km=Px30Km/10;
        ScaleLabel1=(Px30Km * 2).toFixed(1);
        ScaleLabel2=(Px30Km * 6).toFixed(1);
        ScaleUnit="Km";
    }
    if (Px30<=0.25){
        Px30Km= Math.round(PxM*ScaleSizePX)
        w = Px30Km  * MPx
        ScaleLabel1=(Px30Km * 2);
        ScaleLabel2=(Px30Km * 6);
        ScaleUnit="m";
    } 
    //----------------------------------
    var SumW=0;
    for (var i=0;i<8;i++){
        Elem = document.getElementById(Mcode + "^" + Ocode + "^" +"SChell_B"+ i)
        Elem.style.width=w+"px"; 
        SumW += w
    }
    SumW += 6
    
    //----------------------------------
    Elem = document.getElementById(Mcode + "^" + Ocode + "^" + "SChell_label0")
    Elem.innerHTML = "<b>Scale</b><tt>[1:" + Map.addCommas(Math.round(Mopt.MapScale)) + "]</tt>";
    Elem = document.getElementById(Mcode + "^" + Ocode + "^" + "SChell_label2")
    Elem.innerHTML = ScaleLabel1;
    Elem = document.getElementById(Mcode + "^" + Ocode + "^" + "SChell_label4")
    Elem.innerHTML = ScaleLabel2 + "<small> " + ScaleUnit + "</small>";

    var Elem0 = document.getElementById(Mcode + "^Map^" + "MapContainer");
    var Size0 = Map.getElem.Size(Elem0);
    
    //Elem1 = document.getElementById(Mcode + "^" + Ocode + "^" + "Holder_Container")
    var Elem1 = document.getElementById(Mcode + "^" + Ocode + "^" + "Holder_MM")
    Elem1.style.width =  parseInt(SumW ) + "px";
    var Size1 = Map.getElem.Size(Elem1);
    
    var Elem11 = document.getElementById(Mcode + "^" + Ocode + "^" + "Scale_Table")
    Elem11.style.width =  Size1.w + "px";
    //----------------------------------
    var Hoptions = Holder.getOptions(MapID, Ocode);
    var w1 = 0;
    if (Hoptions.style == 0) { w1 = 18; } else { w1 = 2 }
    //----------------------------------
    var Elem2 = document.getElementById(Mcode + "^" + Ocode + "^" + "Holder_Container")
    Elem2.style.width =(Size1.w +w1)+ "px";
    Elem2.style.height =(Size1.h +w1)+ "px";
    var Size2 = Map.getElem.Size(Elem2);
    //----------------------------------
    var pos =Holder.GetAngPosition (Hoptions, Size0,Size2);

    Elem2.style.left = pos.x + "px";
    Elem2.style.top = pos.y + "px";
    
    
//    var Size2 = Map.getElem.Size(Elem2);
//    var x = parseInt((Size0.w - Size2.w) / 2)
//    var y = parseInt(Size0.h - Size2.h)

//    Elem2.style.left = x + "px";
//    Elem2.style.top = y + "px";
    
}  