/************************************************************
*           web-sn_dropmenuhandlers.js - part of EditAce    *
*           copyright Simon Norris 2002                     *
*           email: si@web-sn.com                            *
*           EditAce home page: http://www.web-sn.com        *
*                                                           *
*           Removal or modification of this notice is       *
*		    a violation of copyright and the terms of use   *
*           Last Modified: July 02 2002                     *
************************************************************/


// included from winmenu scriptlet files

// globals
document.onselectstart = top.editor.focus;
var twda = top.window.document.all;
var da = document.all;
var frm = top.frm_tb == "yes" ? 0 : 1; // state of form toolbar
var tbl = top.tbl_tb == "yes" ? 0 : 1; // state of table toolbar
var bas = top.bas_tb == "yes" ? 0 : 1; // state of basic toolbar
var adv = top.adv_tb == "yes" ? 0 : 1; // state of format toolbar
var fil = top.fil_tb == "yes" ? 0 : 1; // state of side toolbar

function public_setTBVisible() {
  websn_showhideBasToolbar();
  websn_showhideAdvToolbar();
  websn_showhideTblToolbar();
  websn_showhideFrmToolbar();
  websn_showhideSideToolbar();
}

// constructor for menu objects
function genMenuObj(act,el,imgid,func,txt,showsub,hidesub,img) {
  this.exec = act;
  this.el = el;
  this.imgid = imgid;
  this.func = func;
  this.txt = txt;
  this.showsub = showsub;
  this.hidesub = hidesub;
  this.img = img;
}
		   
// mouseover for individual linkIDs
function websn_doMenuLinkOver(linkID, qId) { 
  if (top.editor.Busy != true) {  
	linkID.style.cursor = 'default'; // always use the default cursor
	if (qId == 1) { // do rollover no matter what the current editor status is
	 linkID.style.color = 'highlighttext';
     linkID.style.backgroundColor = 'highlight';
	}
	else if (qId == 0 && top.editor.DOM.selection.type == "Control") {
	  return;  // don't do rollover if a control is selected in the editor (forms etc)
	}
	else if (qId == 2 && top.editor.DOM.selection.type != "Control") {
	  return;  // don't do rollover if a control is selected in the editor (forms etc)
	}
	else if (qId == 0 && top.editor.DOM.selection.type != "Control") {
	 // control is not selected so do rollover
	 linkID.style.color = 'highlighttext';
     linkID.style.backgroundColor = 'highlight';
	}
	else if (qId == 2 && top.editor.DOM.selection.type == "Control") {
	 // control is selected so do rollover - for z-order
	 linkID.style.color = 'highlighttext';
     linkID.style.backgroundColor = 'highlight';
	}	
	else if (top.editor.queryStatus(qId) != 1 && top.editor.queryStatus(qId) != 0) {
	 // if the current action (qID) is allowed, do the rollover
     linkID.style.color = 'highlighttext';
	 linkID.style.backgroundColor = 'highlight';
	}
  }
}
	
// mouseout for individual linkIDs
function websn_doMenuLinkOut(linkID) {
  if (top.editor.Busy != true) {
	// return the item to its normal look
    linkID.style.backgroundColor = 'menu';
	linkID.style.color = 'menutext';
  }
}
	
// show submenus
function websn_doSubMenuMouseOver(subMenuID, qId) {
  if (top.editor.Busy != true) {
     if (qId == 0 && top.editor.DOM.selection.type == "Control") {
	    return; // don't show menu if control is selected
	 }
	 else if (qId == 2 && top.editor.DOM.selection.type != "Control") {
	    return;
	 }
	 else if (qId == 0 && top.editor.DOM.selection.type != "Control") {
	    twda[subMenuID].resetMouseover();
		twda[subMenuID].style.visibility = "visible"; // not a control so show the menu
	 }
	 else if (qId == 2 && top.editor.DOM.selection.type == "Control") {
	    twda[subMenuID].resetMouseover();
		twda[subMenuID].style.visibility = "visible"; // for z-ordering a control must be selected
	 }	 
	 else if (top.editor.queryStatus(qId) != 1 && top.editor.queryStatus(qId) != 0) {
	    twda[subMenuID].resetMouseover();
		twda[subMenuID].style.visibility = "visible"; // action is allowed, show the menu
	 }
  }
}

// hide submenus
function websn_doSubMenuMouseOut(subMenuID) {
   if (top.editor.Busy != true) {
     twda[subMenuID].style.visibility = "hidden"; // hide the menu
   }
}
	   
 // show / hide Basic Toolbar
function websn_showhideBasToolbar() {
    // if it is currently visible
    if (bas == 1)  { // if basic toolbar is visible
	    twda['basic_toolbar'].style.visibility = "hidden"; // hide it
		bas = 0; // set state to 0 for future
		/* set the format toolbars left attribute to 0
		   (this will always be the OTHER toolbar on the same strip
	       ie. there are two pairs - bas/adv and tbl/frm) */
    	twda['forms_toolbar'].style.left = "0px";
	} 
	else if (bas == 0)  { // if it is currently hidden
		if (frm == 1 && (twda['forms_toolbar'].style.left != "0px")) // if format toolbar (adv) is visible and NOT at far left
		  /* set basic toolbars left attribute to 0 so that
		  when it is shown it will appear BEFORE the format toolbar */
	      {twda['basic_toolbar'].style.left = "0px";}
		else if (frm == 1 && (twda['forms_toolbar'].style.left == "0px")) // if format toolbar is visible and IS at far left
		  /* set basic toolbars left attribute to 515 (the width of the format toolbar)
	      so that when it is shown it will appear AFTER the format toolbar */
		  {twda['basic_toolbar'].style.left = twda['forms_toolbar'].offsetWidth;}
		else // if the format toolbar is also hidden
		  {twda['basic_toolbar'].style.left = "0px";} // set basic toolbars left attribute to 0
		 twda['basic_toolbar'].style.visibility = "visible"; // make the basic toolbar visible 
	    bas = 1; // set state to 1 for future
	}
	if (top.useMenus == "yes") {
	  top.toolbar_Menu.chkBASVisible(); // show / hide the tick in the toolbar menu
	}
	top.context_Menu.chkBASVisible(); // show / hide the tick in the context menu
	websn_sortToolbars(); // sort the toolbars and set the proper heights for affected items
	top.context_Menu.style.visibility = "hidden";
}
   
/* This function uses similar logic to websn_showhideBasToolbar
so see that for more extensive comments */
function websn_showhideAdvToolbar() {
     //if it is visible
     if (adv == 1)  {
	      twda['format_toolbar'].style.visibility = "hidden";
		  twda['tFontname'].style.visibility = "hidden";
		  twda['tFontsize'].style.visibility = "hidden";
		  twda['tHeading'].style.visibility = "hidden";
		  twda['tStyles'].style.visibility = "hidden";
		  adv = 0;
		  win = "e";
		  twda['tables_toolbar'].style.left = "0px";	  
	 } 
	 // if it is hidden
	 else if (adv == 0) { 
		  if (tbl == 1 && (twda['tables_toolbar'].style.left != "0px"))
			{twda['format_toolbar'].style.left = "0px";}
		  else if (tbl == 1 && (twda['tables_toolbar'].style.left == "0px"))
		    {twda['format_toolbar'].style.left = twda['tables_toolbar'].offsetWidth;}
		  else 
		    {twda['format_toolbar'].style.left = "0px";}
		 twda['format_toolbar'].style.visibility = "visible";
		 twda['tFontname'].style.visibility = "visible";
		 twda['tFontsize'].style.visibility = "visible";
		 twda['tHeading'].style.visibility = "visible";
		 twda['tStyles'].style.visibility = "visible";
	     adv = 1;
	  }
	  if (top.useMenus == "yes") {
	    top.toolbar_Menu.chkADVVisible(); // show / hide the tick in the toolbar menu
	  }
	  top.context_Menu.chkADVVisible(); // show / hide the tick in the context menu
	  websn_sortToolbars();
	  top.context_Menu.style.visibility = "hidden";
}
	
   
/* This function uses similar logic to websn_showhideBasToolbar
so see that for more extensive comments */
function websn_showhideFrmToolbar() {
     // if it is visible
	 if (frm == 1)  {
	      twda['forms_toolbar'].style.visibility = "hidden";
		  frm = 0;
		  twda['basic_toolbar'].style.left = "0px";
	 } 
	// if it is hidden
	 else if (frm == 0)  { 
		  if (bas == 1 && (twda['basic_toolbar'].style.left != "0px"))
			{twda['forms_toolbar'].style.left = "0px";}
		  else if (bas == 1 && (twda['basic_toolbar'].style.left == "0px"))
		    {twda['forms_toolbar'].style.left = twda['basic_toolbar'].offsetWidth;}
		  else 
		    {twda['forms_toolbar'].style.left = "0px";}
		 twda['forms_toolbar'].style.visibility = "visible";
	     frm = 1;
	  }
	  if (top.useMenus == "yes") {
	    top.toolbar_Menu.chkFRMVisible(); // show / hide the tick in the toolbar menu
	  }
	  top.context_Menu.chkFRMVisible(); // show / hide the tick in the context menu
	  websn_sortToolbars();
	  top.context_Menu.style.visibility = "hidden";
 }
		
/* This function uses similar logic to websn_showhideBasToolbar
so see that for more extensive comments */
function websn_showhideTblToolbar() {
     // if it is visible
     if (tbl == 1)  {
	      twda['tables_toolbar'].style.visibility = "hidden";
		  tbl = 0;
		  twda['format_toolbar'].style.left = "0px";
	 }
	 // if it is hidden
	 else if (tbl == 0)  { 
		  if (adv == 1 && (twda['format_toolbar'].style.left != "0px"))
			{twda['tables_toolbar'].style.left = "0px";}
		  else if (adv == 1 && (twda['format_toolbar'].style.left == "0px"))
		    {twda['tables_toolbar'].style.left = twda['format_toolbar'].offsetWidth;}
		  else
		    {twda['tables_toolbar'].style.left = "0px";}
		 twda['tables_toolbar'].style.visibility = "visible";
	     tbl = 1;
	  }
	  if (top.useMenus == "yes") {
	    top.toolbar_Menu.chkTBLVisible(); // show / hide the tick in the toolbar menu
	  }
	  top.context_Menu.chkTBLVisible(); // show / hide the tick in the context menu
	  websn_sortToolbars();
	  top.context_Menu.style.visibility = "hidden";
} 
	
// clean up the margins and positions of affected elements after a toolbar has been shown or hidden
function websn_sortToolbars() {
     if (top.useMenus == "yes") {
      //var menuHeight = twda['root_file'].offsetHeight + 4;
	  var menuHeight = twda['root_file'].offsetHeight; //+ 4;
	  menuHeight = menuHeight > 26 ? 30 : 25;
	  if ((adv == 1 || tbl == 1) && (frm == 1 || bas == 1)) { // if all top toolbars are visible
	     twda['editor'].style.top = (menuHeight + 58) + "px";
		 twda['source'].style.top = (menuHeight + 58) + "px";
		 twda['preview'].style.top = (menuHeight + 58) + "px";
		 twda['file_toolbar'].style.top = (menuHeight + 58) + "px";
		 twda['editor'].style.height = "82%";
		 twda['source'].style.height = "82%";
		 twda['preview'].style.height = "82%";
		 twda['basic_toolbar'].style.top = menuHeight + "px";
		 twda['forms_toolbar'].style.top = menuHeight + "px";
		 twda['format_toolbar'].style.top = (menuHeight + 29) + "px";
		 twda['tables_toolbar'].style.top = (menuHeight + 29) + "px";
	  }
	  else if ((adv == 1 || tbl == 1) && (frm == 0 && bas == 0)) { 
	     // if form and table toolbars are visible but basic and format toolbars are hidden
	     twda['editor'].style.top = (menuHeight + 29) + "px";
		 twda['source'].style.top = (menuHeight + 29) + "px";
		 twda['preview'].style.top = (menuHeight + 29) + "px";
		 twda['file_toolbar'].style.top = (menuHeight + 29) + "px";
		 twda['editor'].style.height = "87%";
		 twda['source'].style.height = "87%";
		 twda['preview'].style.height = "87%";
		 twda['format_toolbar'].style.top = menuHeight + "px";
		 twda['tables_toolbar'].style.top = menuHeight + "px";
	  }
	  else if ((frm == 1 || bas == 1) && (adv == 0 && tbl == 0)) {
	     // if form and table toolbars are hidden but basic and format toolbars are visible 
	     twda['editor'].style.top = (menuHeight + 29) + "px";
		 twda['source'].style.top = (menuHeight + 29) + "px";
		 twda['file_toolbar'].style.top = (menuHeight + 29) + "px";
		 twda['preview'].style.top = (menuHeight + 29) + "px";
		 twda['editor'].style.height = "87%";
		 twda['source'].style.height = "87%";
		 twda['preview'].style.height = "87%";
	  }
	  else if (frm == 0 && tbl == 0 && adv == 0 && bas == 0) { 
	     // if all top toolbars are hidden
	     twda['editor'].style.top = menuHeight + "px";
		 twda['source'].style.top = menuHeight + "px";
		 twda['preview'].style.top = menuHeight + "px";
		 twda['file_toolbar'].style.top = menuHeight + "px";
		 twda['editor'].style.height = "92%";
		 twda['source'].style.height = "92%";
		 twda['preview'].style.height = "92%"; 
	  }
	 }
	 else { // menus aren't visible
	  if ((adv == 1 || tbl == 1) && (frm == 1 || bas == 1)) { // if all top toolbars are visible
	     twda['editor'].style.top = "58px";
		 twda['source'].style.top = "58px";
		 twda['preview'].style.top = "58px"
		 twda['file_toolbar'].style.top = "58px";
		 twda['editor'].style.height = "86%"; //
		 twda['source'].style.height = "86%";//
		 twda['preview'].style.height = "86%";//
		 twda['basic_toolbar'].style.top = "0px";
		 twda['format_toolbar'].style.top = "29px";
		 twda['forms_toolbar'].style.top = "0px";
		 twda['tables_toolbar'].style.top = "29px";
	   }
	   else if ((adv == 1 || tbl == 1) && (frm == 0 && bas == 0)) { 
	     // if form and table toolbars are visible but basic and format toolbars are hidden
	     twda['editor'].style.top = "29px";
		 twda['source'].style.top = "29px";
		 twda['preview'].style.top = "29px";
		 twda['file_toolbar'].style.top = "29px";
		 twda['editor'].style.height = "91%";//
		 twda['source'].style.height = "91%";//
		 twda['preview'].style.height = "91%";//
		 twda['format_toolbar'].style.top = "0px";
		 twda['tables_toolbar'].style.top = "0px";
	   }
	   else if ((frm == 1 || bas == 1) && (adv == 0 && tbl == 0)) {
	     // if form and table toolbars are hidden but basic and format toolbars are visible 
	     twda['editor'].style.top = "29px";
		 twda['source'].style.top = "29px";
		 twda['file_toolbar'].style.top = "29px";
		 twda['preview'].style.top = "29px";
		 twda['editor'].style.height = "91%";//
		 twda['source'].style.height = "91%";//
		 twda['preview'].style.height = "91%";//
	   }
	   else if (frm == 0 && tbl == 0 && adv == 0 && bas == 0) { 
	     // if all top toolbars are hidden
	     twda['editor'].style.top = "0px";
		 twda['source'].style.top = "0px";
		 twda['preview'].style.top = "0px";
		 twda['file_toolbar'].style.top = "0px";
		 twda['editor'].style.height = "96%";//
		 twda['source'].style.height = "96%";//
		 twda['preview'].style.height = "96%"; //
	  }
	 }
}
	
// show or hide the side toolbar
function websn_showhideSideToolbar() {
	   if (fil == 1) { // if its visible
		   twda['file_toolbar'].style.visibility = "hidden"; // hide it
		   fil = 0;
		   
		   twda['editor'].style.left = "0px"; // move editor to the left
		   twda['editor'].style.width = "99.2%"; // and increase its width
		   top.edwidth = "99.2%";
		   
		   twda['source'].style.left = "0px"; // move source window to the left
		   twda['source'].style.width = "99.2%"; // and increase its width
		   
		   twda['preview'].style.left = "0px"; // move preview window to the left
		   twda['preview'].style.width = "99.2%"; // and increase its width
		   
		   twda['mode'].style.left = "2px";

		   twda['act'].style.left = "302px";
	   }
	   else { // if its hidden
		   twda['file_toolbar'].style.visibility = "visible"; //show it
		   fil = 1;
		   
		   twda['editor'].style.left = "30px"; // move editor to the right
		   twda['editor'].style.width = "96.25%"; // decrease its width
		   top.edwidth = "96.25%";
		   
		   twda['source'].style.left = "30px"; // move source window to the right
		   twda['source'].style.width = "96.25%"; // decrease its width
		   
		   twda['preview'].style.left = "30px"; // move preview window to the right
		   twda['preview'].style.width = "96.25%"; // decrease its width
		   
		   twda['mode'].style.left = "32px";

		   twda['mode'].style.left = "332px";
	   }
	   if (top.useMenus == "yes") {
	     top.toolbar_Menu.chkFILVisible(); // show / hide the tick in the toolbar menu
	   }
	   top.context_Menu.chkFILVisible(); // show / hide the tick in the toolbar menu
	   top.context_Menu.style.visibility = "hidden";
}
	
// get the current classname
function websn_getClass() {
     var el = event.srcElement;
     var tmp = el;
     if (eval("tmp." + "className") == 'tb') {
       el.fireEvent('onmouseover', el.parentElement);
     }
}

// set menu states, used by various menus
function setMenuState(el, img, state) {
 if (top.editor.Busy != true) {
   if (state == "on") {  // menu item is available and active
	  with (img.style) {
		   filter = "alpha(opacity=100)";
	  }
	  with (el.style) {
	       filter = "alpha(opacity=100)";
	  }
   }
   else if (state == "off") { // menu item is available but not active
	  with (img.style) {
		   filter = "alpha(opacity=100)";
	  }
	  with (el.style) {
	       filter = "alpha(opacity=100)";
	  }
   }
   else if (state == "unavailable") { // menu item is not available
	  with (el.style) {
	     filter = "gray(), alpha(opacity=15)";
	  }
	  with (img.style) {
	     filter = "gray(), alpha(opacity=15)";
	  }
  }
 }
}

function websn_createMenu(arr,pth) {
  var imgpath = pth == 1 ? "../../images/" : "../images/";
  var str = "";
  for (j=0; j<arr.length; j++) {
    if (arr[j].exec != "SEPARATOR") {
	  if (arr[j].hidesub == "") {
	    str+= "<tr onmouseout=\"websn_doMenuLinkOut(this);\" onmousedown=\"" + arr[j].func + "\" onmouseover=\"websn_doMenuLinkOver(this, " + arr[j].exec + ")\">";
	    str += "<td valign=\"middle\" colspan=\"2\"><div class=\"web-sn_listItem\" id=\"" + arr[j].el + "\">";
	    str += "&nbsp;&nbsp;<input type=\"image\" width=\"16\" height=\"16\" src=\"" + imgpath + arr[j].img + ".gif\" id=\"" + arr[j].imgid + "\">&nbsp;" + arr[j].txt + "</div></td></tr>";
	  }
	  else if (arr[j].showsub == "" && arr[j].hidesub != "") {
	    str+= "<tr onmouseout=\"websn_doMenuLinkOut(this);\" onmousedown=\"" + arr[j].func + "\" onmouseover=\"websn_doMenuLinkOver(this, " + arr[j].exec + "); websn_doSubMenuMouseOut('" + arr[j].hidesub + "')\">";
        str += "<td valign=\"middle\" colspan=\"2\"><div class=\"web-sn_listItem\" id=\"" + arr[j].el + "\">";
	    str += "&nbsp;&nbsp;<input type=\"image\" width=\"16\" height=\"16\" src=\"" + imgpath + arr[j].img + ".gif\" id=\"" + arr[j].imgid + "\">&nbsp;" + arr[j].txt + "</div></td></tr>";
	  }
	  else {
	    str+= "<tr onmouseout=\"websn_doMenuLinkOut(this);\" onmousedown=\"" + arr[j].func + "\" onmouseover=\"websn_doMenuLinkOver(this, " + arr[j].exec + "); websn_doSubMenuMouseOut('" + arr[j].hidesub + "'); websn_doSubMenuMouseOver('" + arr[j].showsub + "', " + arr[j].exec + ")\">";
        str += "<td valign=\"middle\"><div class=\"web-sn_listItem\" id=\"" + arr[j].el + "\">";
	    str += "&nbsp;&nbsp;<input type=\"image\" width=\"16\" height=\"16\" src=\"" + imgpath + arr[j].img + ".gif\">&nbsp;" + arr[j].txt + "</div></td>";
	    str += "<td valign=\"middle\" align=\"right\"><input type=\"image\" src=\"" + imgpath + "arrow.gif\" id=\"" + arr[j].imgid + "\"></td></tr>";
	  }
	}
	else {
	  if (arr[j].hidesub != "") {
	    str += "<tr><td colspan=\"2\" onmouseover=\"websn_doSubMenuMouseOut('" + arr[j].hidesub + "')\"><div class=\"separator\"></div></td></tr>";
	  }
	  else {
	    str += "<tr><td colspan=\"2\"><div class=\"separator\"></div></td></tr>";
	  }
	}
	document.write(str);
	str = "";
  }
}

function public_resetMouseover() {  // in case a table row gets stuck in the on position
  for (i=0; i<document.all.length; i++) {
     if (document.all[i].tagName == "TR") {
	   document.all[i].style.backgroundColor = "menu"; // reset bgcolour
	   document.all[i].style.color = 'menutext'; // reset text color
	 }
  }
}

function getMenuState(arr) {
  for (i=0; i<arr.length; i++) {
   if (arr[i].exec != "SEPARATOR" && arr[i].exec != 1) {
    qStatus = top.editor.queryStatus(arr[i].exec);
	if (qStatus == 1 || qStatus == 0) {
      setMenuState(da[arr[i].el], da[arr[i].imgid], "unavailable"); 
    }
	else if (qStatus == 3  || qStatus == 11) {
       setMenuState(da[arr[i].el], da[arr[i].imgid], "off"); 
    } 
	else if (qStatus == 7) { 
       setMenuState(da[arr[i].el], da[arr[i].imgid], "on");
    }
   }
  }
}

function public_toggleDetTick() {
  if (top.editor.showDetails == true) {da['emshowDetailsImg'].src = "../images/tick.gif";}
  else {da['emshowDetailsImg'].src = "../images/spacer.gif";}
}
function public_toggleBdrTick() {
  if (top.editor.showBorders == true) {da['emshowBordersImg'].src = "../images/tick.gif";}
  else {da['emshowBordersImg'].src = "../images/spacer.gif";}
}
