/* DynaMan version .7 March,2007
copyright (c) 2007 Henrik Bechmann, Bechmann Software Services, Toronto Canada, all rights reserved.
www.bechmann.ca. General Public Licence.
*/

//menuman default styling
menuman$menuitemdefaultstyles = function(theComponent,theMetadata,theObject){
 var theOwner=theObject.owner, theContainer=theObject.container;
 var theDefaults;
 try {
  theDefaults = theOwner.menustyles.startup.styles;
  if (!theComponent.styles) theComponent.styles={};
  for (var theSlot in theDefaults) {
   theComponent.styles[theSlot]=theDefaults[theSlot];
  }
 } catch (e) {}
 
 try {
  theDefaults = theContainer.menustyles.startup.styles;
  if (!theComponent.styles) theComponent.styles={};
  for (var theSlot in theDefaults) {
   theComponent.styles[theSlot]=theDefaults[theSlot];
  }
 } catch (e) {}
}

//menuman classes
adlml.classes.menublock = function(){
  this.menustyles = {
   startup:{},
   normal:{styles:{borderColor:"#ddd"}},
   rollover:{styles:{borderColor:"#eee #bbb #bbb #eee"}},
   selected:{styles:{borderColor:"#bbb #eee #eee #bbb"}},
   open:{styles:{backgroundImage:"url(arrowdown.gif)"}},
   closed:{styles:{backgroundImage:"url(arrowright.gif)"}}
  }
 }
adlml.classes.menustrip = function () {
  this.menustyles = {}
  this.iscontainer=true;
  this.styles={
   display:"none"
  }
 }
adlml.classes.menucolumn=function(){
  this.iscontainer=true;
  this.styles={
   display:"none"
  }
 }
adlml.classes.menuitem=function(){
  this.styles={
   width:"124px"
  }
 }
adlml.classes.menudivider=function(){}
adlml.classes.menunode=function(){}

 //menuman prototypes
adlml.prototypes.menublock={
  manager:'MenuMan',
  onsetup:function(theObject) {
   theObject.outerelement.onmouseover=MenuM$onmouseover;
   theObject.outerelement.onmouseout=MenuM$onmouseout;
   theObject.outerelement.onclick=MenuM$onclick;
  },
  metadata:{
   className:'menublock',
   attributes:['style'],
   conversions:{style:adlml$parsestyle},
   properties:{
    menustyles:{
     properties:{
      startup:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      normal:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      rollover:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      selected:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      open:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      closed:{attributes:['style'],conversions:{style:adlml$parsestyle}}
     }
    }
   },
   html:'<div class="menublock-platform"></div> '
  }
 }
adlml.prototypes.menustrip={
  onsetup:function(theObject) {
   if (theObject.container && (theObject.container.metadata.className!="menublock")) {
    theObject.outerelement.className+=" dropdown";
   }
   if ((theObject.parent.metadata.className=="menunode") && (theObject.parent.children[0].metadata.className=="menuitem")) {
    theObject.menuitem=theObject.parent.children[0];
	theObject.menuitem.submenu=theObject; 
    theObject.menuitem.middleelement.className+=" arrow-right";
   }
  },
  metadata:{
   className:'menustrip',
   attributes:['style'],
   conversions:{style:adlml$parsestyle},
   properties:{
    menustyles:{
     properties:{
      startup:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      normal:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      rollover:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      selected:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      open:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      closed:{attributes:['style'],conversions:{style:adlml$parsestyle}}
     }
    }
   },
   html:'<div class="menustrip-frame"><div class="menustrip-box"><div class="menustrip-liner"></div></div></div> '
  }
 }
adlml.prototypes.menucolumn={
  onsetup:function(theObject) {
   if (theObject.container.metadata.className=="menustrip") {
    theObject.outerelement.className+=" dropdown";
   } else {
    theObject.outerelement.className+=" flyout";
   }
   if ((theObject.parent.metadata.className=="menunode") && (theObject.parent.children[0].metadata.className=="menuitem")) {
    theObject.menuitem=theObject.parent.children[0]; 
	theObject.menuitem.submenu=theObject; 
    theObject.menuitem.middleelement.className+=" arrow-right";
   }
  },
  metadata:{
   className:'menucolumn',
   attributes:['style'],
   conversions:{style:adlml$parsestyle},
   properties:{
    menustyles:{
     properties:{
      startup:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      normal:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      rollover:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      selected:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      open:{attributes:['style'],conversions:{style:adlml$parsestyle}},
      closed:{attributes:['style'],conversions:{style:adlml$parsestyle}}
     }
    }
   },
   html:'<div class="menucolumn-frame"><div class="menucolumn-box"><div class="menucolumn-liner"></div></div></div> '
  }
 }
adlml.prototypes.menuitem={
  onapplystyles:function(theObject){
   if (theObject.parent.metadata.className!='menunode') {
    if (theObject.parent.metadata.className=="menustrip") {
     if (document.all) {//IE
	  theObject.outerelement.style.styleFloat="left";
	 } else {
	  theObject.outerelement.style.cssFloat="left";
	 }
    }
   }
  },
  metadata:{
   className:'menuitem',
   attributes:['style'],
   conversions:{style:adlml$parsestyle},
   setdefaults:menuman$menuitemdefaultstyles,
   html:'<div class="menuitem-frame"><div class="menuitem-box"><div class="menuitem-liner"></div></div></div> '
  }
 }
adlml.prototypes.menunode={
  onapplystyles:function(theObject){
   if (theObject.parent.metadata.className=="menustrip") {
    if (document.all) {//IE
     theObject.outerelement.style.styleFloat="left";
    } else {
	 theObject.outerelement.style.cssFloat="left";
    }
   }
  },
  metadata:{
   className:'menunode',
   html:'<div class="menunode-frame"></div> '
  }
 }
adlml.prototypes.menudivider={
  metadata:{
   className:'menudivider',
   attributes:['style'],
   conversions:{style:adlml$parsestyle},
   html:'<div class="menudivider"></div>'
  }
 }

adlml.registerengine('MenuMan',MenuMan);

// ---------[ Menu Manager ]-----------
//var MenuM$popupblock=null, MenuM$popuptarget=null,MenuM$popupblocks=[]; 
var MenuM$timeoutindex=null, MenuM$openbranchlist=[],MenuM$closebranchlist=[];

var MenuMan = {
 initialize:MenuM$initialize,
 timeoutvalue:500,
 istimeout:false,
 metadata:{className:'MenuMan'},
 oninitialize:null
}

function MenuM$initialize() {
 if (MenuMan.oninitialize) MenuMan.oninitialize(MenuMan);
}

function MenuM$setTimeout() {
 if (!MenuMan.istimeout) {
  MenuM$timeoutindex=setTimeout("MenuM$clearopenlist()",MenuMan.timeoutvalue);
  MenuMan.istimeout=true;
 }
}

function MenuM$clearTimeout() {
 clearTimeout(MenuM$timeoutindex);
 MenuMan.istimeout=false;
}

function MenuM$applymenustyles(theContext,theObject) {
 var theStyleEvent={subject:theObject,context:theContext,styles:{}};
 var theSourceObject;
 theSourceObject=theStyleEvent.subject.owner;
 MenuM$collectmenustyles(theSourceObject,theStyleEvent);
 theSourceObject=theStyleEvent.subject.container;
 MenuM$collectmenustyles(theSourceObject,theStyleEvent);
 theSourceObject=theStyleEvent.subject;
 MenuM$collectmenustyles(theSourceObject,theStyleEvent);
 adlml.applystylelist(theObject,theStyleEvent.styles);
}
function MenuM$collectmenustyles(theSourceObject,theStyleEvent) {
 var theStyleSource,theSlot,theContext=theStyleEvent.context;
 try {
  theStyleSource=theSourceObject.menustyles[theContext].styles;
 } catch(e) {};
 if (theStyleSource) {
  for (theSlot in theStyleSource) {
   theStyleEvent.styles[theSlot]=theStyleSource[theSlot];
  }
 };
}

//runtime
function MenuM$getmousetarget(evt) {
 var theEvent = (evt?evt:(event?event : null));
 if (!theEvent) return;
 var theTarget=theEvent.target?theEvent.target:(theEvent.srcElement?theEvent.srcElement :null);
 if (!theTarget) return;
 var theSessionID=theTarget.getAttribute("dlsessionid");
 while (theTarget && (!theSessionID)) {
  theTarget=theTarget.parentNode;
  theSessionID=theTarget.getAttribute("dlsessionid");
 };
 if (!theSessionID) {
  return null;
 }
 theTarget=adlml.heap[theSessionID];
 return theTarget;
}

var iteration=0;
function MenuM$onmouseover(evt) {
// return;
 var theObject,theTestSubject,theSubject,theCloseSubject,sharedpointer,theObjectClass,
 theList=["menuitem","menucolumn","menustrip"],theReferenceObject;
 MenuM$clearTimeout();
 //find target
 theTarget=MenuM$getmousetarget(evt);
 if (!theTarget) return;//IE
 theObject=theTarget;
 theObjectClass=theObject.metadata.className;
 iteration++
 if ((theObjectClass=="menunode") && (!theObject.children)) {
  if (theObject.container.parent.metadata.className=="menublock") {
   MenuM$setTimeout();
   return;
  }
 }
 if (theObjectClass=="menustrip") {
  if (theObject.parent.metadata.className=="menublock") { 
   MenuM$setTimeout();
   return;
  }
 }
 if (!theList.in_array(theObjectClass)) return;
 var theLength=MenuM$openbranchlist.length;
 //find any associated menuframe...
 if (theObjectClass=="menuitem") theSubject=theObject.submenu;
 if (!theSubject) {
  if (theObjectClass=="menuitem") {
   theSubject=theObject.container;
  } else {//menustrip or menucolumn
   theSubject=MenuM$openbranchlist[theLength-1];
   if (!theSubject) theSubject=theObject;
  }
 }
 MenuM$closebranchlist=MenuM$openbranchlist.clone();
 MenuM$openbranchlist=new Array();
 MenuM$openbranchlist[0]=theSubject; //menustrip or menucolumn
 while (theSubject.container) {
  theSubject=theSubject.container;
  MenuM$openbranchlist[MenuM$openbranchlist.length]=theSubject;
 }
 MenuM$openbranchlist=MenuM$openbranchlist.reverse();//top down
 var theLength;
 sharedpointer=-1;
 theLength=Math.min(MenuM$openbranchlist.length,MenuM$closebranchlist.length);
 for (var i=0;i<theLength;i++) {
  if (MenuM$openbranchlist[i]==MenuM$closebranchlist[i]) {
   sharedpointer=i;
  } else {break;}
 }
 for (var i=MenuM$closebranchlist.length-1;i>sharedpointer;i--) {
  theCloseSubject=MenuM$closebranchlist[i];
  if ((theCloseSubject.outerelement.style.display) && (theCloseSubject.outerelement.style.display=="block")) {
   theCloseSubject.outerelement.style.display="none";
   if (theCloseSubject.menuitem) MenuM$applymenustyles("closed",theCloseSubject.menuitem);
  }
 }
 MenuM$closebranchlist=new Array();
 if (MenuM$openbranchlist.length>0) {
  theSubject=MenuM$openbranchlist[MenuM$openbranchlist.length-1];
  if (!theSubject.outerelement.style.display) theSubject.outerelement.style.display="none";//IE rendering
  if (theSubject.outerelement.style.display=="none") {
   theSubject.outerelement.style.display="block";
   if (theSubject.menuitem) MenuM$applymenustyles("open",theSubject.menuitem);
  }
 }
 
 if (theObjectClass=="menuitem") MenuM$applymenustyles("rollover",theObject);
}

function MenuM$onmouseout(evt) {
 var theTarget;
 theTarget=MenuM$getmousetarget(evt);
 if (theTarget && (theTarget.metadata.className=="menuitem")) {
  MenuM$applymenustyles("normal",theTarget);
 }
 MenuM$setTimeout();
}

function MenuM$onclick(evt) {
// return;
 var theEvent = (evt?evt:(event?event:null));
 if (theEvent) {
  var theTarget=theEvent.target?theEvent.target:(theEvent.srcElement?theEvent.srcElement:null);
  if (theTarget) {
   if ((theTarget.tagName=="A")||(theTarget.parentNode.tagName=="A")) {
    MenuM$clearopenlist();
   }
  }
 }
}
function MenuM$clearopenlist() {
 var theCloseSubject;
 for (var i=MenuM$openbranchlist.length-1;i>=0;i--) {
  theCloseSubject=MenuM$openbranchlist[i];
  if ((theCloseSubject.metadata.className!="menublock") && 
   (theCloseSubject.container && (theCloseSubject.container.metadata.className!="menublock"))) {
   if ((theCloseSubject.outerelement.style.display) && 
    (theCloseSubject.outerelement.style.display=="block")) {
    theCloseSubject.outerelement.style.display="none";
    if (theCloseSubject.menuitem) MenuM$applymenustyles("closed",theCloseSubject.menuitem);
   }
  }
 }
 MenuM$openbranchlist=[];
}
// ---------[ Dynamic Layout Manager ]-----------
var DynaMan = {
 initialize:DynaMan$initialize
}
function DynaMan$initialize() {
 adlml.initialize();
 adlml.setdatamodel(document);
 MenuMan.initialize();
}

