/*

    Copyright 2003-2007 Purdue University. 

    Developed by: Peter Turbek, Department of Mathematics, Computer Science, and Statistics, Purdue University Calumet.


    This file is part of CaluMath.

    CaluMath is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    CaluMath is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

//Comment: This file contains the routines for the Main and Sub-menus. These are the items that are visible in the window when the page is loaded CAS Page Maker is loaded. //

CaluMath.PM.MENUSTABLEFORHTMLCASMATHOPTIONS = document.getElementById("menustable");

CaluMath.PM.MainMenuElementOnMouseOver=function(){
   this.style.color="cornsilk";
};
CaluMath.PM.MainMenuElementOnMouseOut=function(){
   this.style.color="";
};

CaluMath.PM.MainMenuElementOnClick=function(){
   for(var t=0; t<CaluMath.PM.MASTERLIST.length; t++){
      document.getElementById(CaluMath.PM.MASTERLIST[t][1].replace(/SELECT/, "LIST")+"TABLE").style.display="none";
      document.getElementById(CaluMath.PM.MASTERLIST[t][1].replace(/SELECT/, "LIST")+"ARROW").style.visibility="hidden";
   };
   document.getElementById(this.id.replace(/ROW/, "TABLE")).style.display="";
   document.getElementById(this.id.replace(/ROW/, "ARROW")).style.visibility="";
   this.style.color="cornsilk";
};

CaluMath.PM.SubMenuElementOnClick=function(){
   document.getElementById("menustable").style.visibility="hidden";
   CaluMath.PM.MATHBUTTONFUNCTION(this.cm_objectname,"table");
   this.parentNode.parentNode.parentNode.style.display="none";
   this.style.color="";
};

CaluMath.PM.SubMenuElementOnMouseOver=function(){
   this.style.color="#990000";
   //Comment: In Opera, if we created a text node with no data before the page loaded, when the data in the node was changed after the page loaded, Opera would not display the new data. Therefore we create the text node after the page loads and then must always check to see if the node has been created or not. 
   if(document.getElementById("submainrowdescription") != null && document.getElementById("submainrowdescription").childNodes.length==0){
      document.getElementById("submainrowdescription").appendChild(document.createTextNode(""));
   }
   //Comment: This controls the description of the sub-menu item that appears to the right of the sub-menu. 
   if(CaluMath.PM["PageMaker"+this.cm_objectname+"Default"].description != null){
      document.getElementById("submainrowdescription").childNodes[0].data=CaluMath.PM["PageMaker"+this.cm_objectname+"Default"].description;
   }
   else{
      document.getElementById("submainrowdescription").childNodes[0].data="";
   };
};

CaluMath.PM.SubMenuElementOnMouseOut=function(){
   this.style.color="";
};

CaluMath.PM.SubMenuTableOnMouseOut=function(){
   this.style.cursor="pointer";
   this.style.display="";
   document.getElementById(this.id.replace(/TABLE$/,"")+"ARROW").style.visibility="";
   CaluMath.PM.CurrentlyActivatedMenu=this;
   CaluMath.PM.CurrentlyActivatedArrow=document.getElementById(this.id.replace(/TABLE$/,"")+"ARROW");
   document.body.onclick=function(){
      CaluMath.PM.CurrentlyActivatedMenu.style.display="none";       
      CaluMath.PM.CurrentlyActivatedArrow.style.visibility="hidden";
      document.getElementById("submainrowdescription").childNodes[0].data="";
      document.body.onclick=null;
   };
};

CaluMath.Html.MemoryLeakArray.push(document.body);

CaluMath.PM.MainMenuTableOnMouseOver=function(){
   this.style.cursor="pointer";
};
CaluMath.PM.MainMenuTableOnMouseOut=function(){
   this.style.cursor="";
};





//Comment: This function creates the Main Menu, the Sub-Menu tables associated to each item in the Main Menu, and the associate onclick and onmouseover event handlers
(function(){
   //Comment: This is placed inside an unnamed, executed function to avoid using global variables. 
   //Comment: This is the Main Menu table. 
   var tableElement = document.createElement("table");
   tableElement.setAttribute("border","5");
   tableElement.setAttribute("cellspacing","0");
   tableElement.setAttribute("cellpadding","0");
   tableElement.style.borderStyle="none";
   var tbodyElement= document.createElement("tbody");

   //Comment: The CaluMath.PM.MASTERLIST is a list of all the classes of objects that can be created. Each entry in the list becomes a row in the Main Menu. 
   for(var i=0; i< CaluMath.PM.MASTERLIST.length; i++){
      var trElement = document.createElement("tr");
      var tdElement = document.createElement("td");
      tdElement.id= CaluMath.PM.MASTERLIST[i][1].replace(/SELECT/, "LIST")+"ROW";

      //Comment: For each item in the Main Menu, there is a sub-menu table (which will be created below), and an arrow gif pointing to the  sub-menu. The onclick command reveals the corresponding sub-menu. 
      tdElement.onclick=CaluMath.PM.MainMenuElementOnClick;
      tdElement.onmouseover=CaluMath.PM.MainMenuElementOnMouseOver;
      tdElement.onmouseout=CaluMath.PM.MainMenuElementOnMouseOut;
      // CaluMath.Html.MemoryLeakArray.push(tdElement);
      var dataElement = document.createTextNode(CaluMath.PM.MASTERLIST[i][0]);

      //Comment: The Main menu consists of two columns. The first column consists of entries in the Main Menu. The second column consists of arrows pointing to the corresponding sub-menu. 
      var tdarrowElement = document.createElement("td");
      tdarrowElement.style.visibility = "hidden";
      tdarrowElement.id= CaluMath.PM.MASTERLIST[i][1].replace(/SELECT/, "LIST")+"ARROW";

      var imgElement = document.createElement("img");
//      imgElement.src="../images/sidebaritem_arrow.gif";
      imgElement.src="pagemakerimages/sidebaritem_arrow.gif";
      imgElement.style.height="11px";
      imgElement.style.width="12px";
      imgElement.style.textAlign="right";

      tdElement.appendChild(dataElement);
      tdarrowElement.appendChild(imgElement);
      trElement.appendChild(tdElement);
      trElement.appendChild(tdarrowElement);
      tbodyElement.appendChild(trElement);

      //Comment All the elements with 1 in their name correspond to the Sub-Menu table associated to the item in the Main Menu
      var table1Element = document.createElement("table");
      table1Element.setAttribute("border","5");
      table1Element.style.borderStyle="groove";    
      table1Element.setAttribute("cellspacing","0");
      table1Element.setAttribute("cellpadding","0");
      table1Element.style.borderStyle="none";
      table1Element.style.borderColor="#c6b89e";
      //Comment: We give the Sub-Menu table a name that cooresponds with the item in the Main Menu it is associated with. This ensures that the item can be hidden or visible when the Main Menu item is clicked. 
      table1Element.id=CaluMath.PM.MASTERLIST[i][1].replace(/SELECT/, "LIST")+"TABLE";
      table1Element.style.display="none";
      var tbody1Element= document.createElement("tbody");
      var submenulist=CaluMath.PM[CaluMath.PM.MASTERLIST[i][1].replace(/SELECT/, "LIST")];
      var submenulistdescription=CaluMath.PM[CaluMath.PM.MASTERLIST[i][1].replace(/SELECT/, "LISTDESCRIPTION")];
      var topemptyspaces= Math.max(0,i+1-submenulist.length);
      for(var j=0; j<topemptyspaces; j++){
         var tr1Element = document.createElement("tr");
         var td1Element = document.createElement("td");
         var data1Element = document.createTextNode("placeholder");
         td1Element.style.visibility="hidden";
         td1Element.appendChild(data1Element);
         tr1Element.appendChild(td1Element);
         tbody1Element.appendChild(tr1Element);
      };
      for(var j=0; j<submenulist.length; j++){
         var tr1Element = document.createElement("tr");
         var td1Element = document.createElement("td");
         td1Element.cm_objectname=submenulist[j][1];
         td1Element.onclick= CaluMath.PM.SubMenuElementOnClick;
         td1Element.onmouseover= CaluMath.PM.SubMenuElementOnMouseOver;
         td1Element.onmouseout= CaluMath.PM.SubMenuElementOnMouseOut;
         // CaluMath.Html.MemoryLeakArray.push(td1Element);

         var data1Element = document.createTextNode(submenulist[j][0]);
         td1Element.appendChild(data1Element);
         tr1Element.appendChild(td1Element);
         tr1Element.id = submenulist[j][1]+"tablerow";
         tbody1Element.appendChild(tr1Element);

         table1Element.appendChild(tbody1Element);
         table1Element.onmouseover=CaluMath.PM.SubMenuTableOnMouseOut;
         // CaluMath.Html.MemoryLeakArray.push(table1Element);

         document.getElementById("submainrow").appendChild(table1Element);
      };
   };
   tableElement.appendChild(tbodyElement);
   document.getElementById("mainrow").appendChild(tableElement);

   tableElement.onmouseover=CaluMath.PM.MainMenuTableOnMouseOver;
   tableElement.onmouseout=CaluMath.PM.MainMenuTableOnMouseOut;
   // CaluMath.Html.MemoryLeakArray.push(tableElement);

   CaluMath.PM.MainMenu=tableElement;
})();      




CaluMath.PM.texttagSELECT= document.getElementById("texttagoptions");
CaluMath.PM.stylefontSizeSELECT= document.getElementById("fontsizesoptions");
CaluMath.PM.stylecolorSELECT= document.getElementById("fontcoloroptions");
CaluMath.PM.stylefontFamilySELECT= document.getElementById("fontfamilyoptions");
CaluMath.PM.stylebackgroundColorSELECT= document.getElementById("backgroundcoloroptions");
CaluMath.PM.stylefontWeightSELECT= document.getElementById("boldoptions");
CaluMath.PM.stylefontStyleSELECT= document.getElementById("italicsoptions");
CaluMath.PM.styletextAlignSELECT= document.getElementById("alignmentoptions");
CaluMath.PM.styledisplaySELECT= document.getElementById("displayoptions");
CaluMath.PM.stylewidthSELECT= document.getElementById("widthoptions");
CaluMath.PM.textcaswindowSELECT= document.getElementById("textcaswindow");
CaluMath.PM.textinsertoptionSELECT= document.getElementById("textinsertoption");
CaluMath.PM.textinserttargetSELECT= document.getElementById("textinserttarget");
CaluMath.PM.textcm_onlyforlistsoptionsSELECT= document.getElementById("textcm_onlyforlistsoptions");

CaluMath.PM.PageMakerhtmltextDefault.required= new Object();
CaluMath.PM.PageMakerhtmltextDefault.options= new Object();


CaluMath.PM.PageMakerhtmltextDefault.required["tag"]= new Object();
CaluMath.PM.PageMakerhtmltextDefault.required["name"]= new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontSizeSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["stylecolorSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontFamilySELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["stylebackgroundColorSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontWeightSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontStyleSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["styletextAlignSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["styledisplaySELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["stylewidthSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["textcaswindowSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["textinsertoptionSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["textinserttargetSELECT"]=new Object();
CaluMath.PM.PageMakerhtmltextDefault.options["textcm_onlyforlistsoptionsSELECT"]=new Object();


CaluMath.PM.PageMakerhtmltextDefault.required["tag"].menu=CaluMath.PM.texttagSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontSizeSELECT"].menu=CaluMath.PM.stylefontSizeSELECT;
//CaluMath.PM.PageMakerhtmltextDefault.options["stylecolorSELECT"].menu=CaluMath.PM.stylecolorSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["stylecolorSELECT"].input=CaluMath.PM.stylecolorSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontFamilySELECT"].menu=CaluMath.PM.stylefontFamilySELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["stylebackgroundColorSELECT"].input=CaluMath.PM.stylebackgroundColorSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontWeightSELECT"].menu=CaluMath.PM.stylefontWeightSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["stylefontStyleSELECT"].menu=CaluMath.PM.stylefontStyleSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["styletextAlignSELECT"].menu=CaluMath.PM.styletextAlignSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["styledisplaySELECT"].menu=CaluMath.PM.styledisplaySELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["stylewidthSELECT"].menu=CaluMath.PM.stylewidthSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["textcaswindowSELECT"].menu=CaluMath.PM.textcaswindowSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["textinsertoptionSELECT"].menu=CaluMath.PM.textinsertoptionSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["textinserttargetSELECT"].menu=CaluMath.PM.textinserttargetSELECT;
CaluMath.PM.PageMakerhtmltextDefault.options["textcm_onlyforlistsoptionsSELECT"].menu=CaluMath.PM.textcm_onlyforlistsoptionsSELECT;
//Comment: We can only put in the "name" input box after it is constructed below. It is called CaluMath.PM.htmloptionnamebox

CaluMath.PM.textcm_onlyforlistsoptionsSELECT.onchange=function(){
   alert("Use Only For Lists is an Advanced Technique that has to do with Programming and CaluMath. Only select yes if you know what you are doing.");
   this.selectedIndex=0;
   this.onchange=null;
};
// CaluMath.Html.MemoryLeakArray.push(CaluMath.PM.textcm_onlyforlistsoptionsSELECT);


if(CM_TopWindow !=window){
   document.getElementById("textinsertoption").style.display="none";
   document.getElementById("textinserttarget").style.display="none";
   document.getElementById("textinsertoptionlabel").style.display="none";
   document.getElementById("textinserttargetlabel").style.display="none";
};

CaluMath.PM.specialsymbolsSELECT= document.getElementById("specialsymbolsoptions");
CaluMath.PM.specialsymbolsSELECTwithbrbr= document.getElementById("specialsymbolsoptionswithbrbr");

//Comment: Containers for buttons:
CaluMath.PM.BUTTONCONTAINER= document.getElementById("buttoncontainer");
CaluMath.PM.PAGECONTROLCONTAINER= document.getElementById("pagecontrolcontainer");
CaluMath.PM.PAGEASSISTCONTAINER=document.getElementById("pageassistcontainer");

CaluMath.PM.MakeSelectMenu(CaluMath.PM.texttagSELECT,CaluMath.PM.texttagLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.stylefontSizeSELECT,CaluMath.PM.stylefontSizeLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.stylefontFamilySELECT,CaluMath.PM.stylefontFamilyLIST);
//CaluMath.PM.MakeSelectMenu(CaluMath.PM.stylebackgroundColorSELECT,CaluMath.PM.stylebackgroundColorLIST);
//CaluMath.PM.MakeSelectMenu(CaluMath.PM.stylecolorSELECT,CaluMath.PM.stylecolorLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.stylefontWeightSELECT,CaluMath.PM.stylefontWeightLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.stylefontStyleSELECT,CaluMath.PM.stylefontStyleLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.styletextAlignSELECT,CaluMath.PM.styletextAlignLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.styledisplaySELECT,CaluMath.PM.styledisplayLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.stylewidthSELECT,CaluMath.PM.stylewidthLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.specialsymbolsSELECT,CaluMath.PM.specialsymbolsunicodeLIST);
CaluMath.PM.MakeSelectMenu(CaluMath.PM.textcm_onlyforlistsoptionsSELECT,["no","yes"]);
CaluMath.PM.MakeDefaultOption(CaluMath.PM.specialsymbolsSELECT, CaluMath.Html.UnicodeObject["pi"]);


CaluMath.PM.htmloptionnamebox=new CaluMath.Html.InputText(CM_ParentObject,"size=50,cm_insertin=optionalnametableinput");
CaluMath.PM.namesymbolsSELECT=CaluMath.PM.htmloptionnamebox.cm_node;
'<br><br>'.cm_html("cm_insertin=htmlselectchoices");

//Comment: This was completed for all of the other text menus above. However we could not do this one until after the textbox was created. 
CaluMath.PM.PageMakerhtmltextDefault.required["tag"].menu=CaluMath.PM.texttagSELECT;
CaluMath.PM.PageMakerhtmltextDefault.required["name"].input=CaluMath.PM.htmloptionnamebox.cm_node;

CaluMath.PM.HTMLSELECTCHOICES= document.getElementById("htmlselectchoices");

if(CM_TopWindow==window){
(function(){
   '<table id="edittable" border="5" cellspacing="0" cellpadding="0" style="border-style:groove; border-color:#c6b89e"> <tbody><tr><td><input id="editinlinebutton" type="button" value="Edit Selected Item" /></td><td id="cellforinlineeditselectmenu"></td></tr></tbody></table>'.cm_html("cm_insertbefore=inputandoutputcontainer");
})() 
};

CaluMath.PM.InLineEditButtonOnclickFunction= function(){  
   if(document.getElementById("inlineselectmenu") != null && document.getElementById("inlineselectmenu").options != null && document.getElementById("inlineselectmenu").options.selectedIndex != null && CaluMath.PM.InLineEditArray[document.getElementById("inlineselectmenu").options.selectedIndex] != null){
      CaluMath.PM.ReCreateObject(CaluMath.PM.InLineEditArray[document.getElementById("inlineselectmenu").options.selectedIndex][2]);  
   };
};

if(document.getElementById("editinlinebutton") != null){
   document.getElementById("editinlinebutton").onclick=CaluMath.PM.InLineEditButtonOnclickFunction;
};

CaluMath.PM.CreateInLineEditSelectMenu = function(){

      if(document.getElementById("inlineselectmenu") != null){
         document.getElementById("inlineselectmenu").parentNode.removeChild(document.getElementById("inlineselectmenu"));
      };

      var childlist=[];
      for(var i=0; i<CaluMath.PM.PageMakerObjectArray.length; i++){
         childlist[i]=[CaluMath.PM.PageMakerObjectLabelName(CaluMath.PM.PageMakerObjectArray[i])+' named '+CaluMath.PM.PageMakerObjectArray[i].cm_getsetting("name"), CaluMath.PM.PageMakerObjectArray.type, CaluMath.PM.PageMakerObjectArray[i]];

      };
      var childdropdown=document.createElement("select");
      document.getElementById("cellforinlineeditselectmenu").appendChild(childdropdown); 
      CaluMath.PM.MakeSelectMenu(childdropdown, childlist);
      childdropdown.options.selectedIndex=0;
      childdropdown.id="inlineselectmenu";
      CaluMath.PM.InLineEditArray= childlist;
};

if(CM_TopWindow==window){
   CaluMath.PM.CreateInLineEditSelectMenu();
};