/***********************************************
* AnyLink Drop Down Menu- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code

* Versifier Communications Adapatation- separation of concerns
* menu data, CSS template and code all reside in separeate files
* for maximum capability. Vertical menu response and regulard drop down functionality are included

- v_dropdownmenu() is the vertical behavior (bu designer should choose it
- dropdownmenu() is standard
*

	ANYLINK MENU SYSTEM 
	based on AnyLink Dropdown and AnyLink Vertical Menu 
	- css based menus
	- includes option for graphic bullets, complete css control
	- link a menu to any <a> (anchor) element for images or text links
	via adding the folowing triggers to mouse events:
	
	onMouseOver= dropdownmenu(this, event, menuArrayInDataFile, '165px') 
	onMouseOut=delayhidemenu()
	- include menu data in separate file, declared before the behavior file
	- data format is:
	menuArray10 = new Array()
	menuArraySearch[0] = '<a href="google.com">Google</a>'
	menuArraySearch[0] = '<a href="yahoo.com">Yahoo</a>'
	
	workaround for javascript requires that the javascipt form of 
	a link -> javascript:myfunction() must be put into a variable to 
	avoid quotes/literals nesting conflicts, like this:
	
	mylink = "javascript:myfunction('myvar')"
	
	Notes
	
	stylesheet must be declared first before behavior files 
	
	
	*****************************************************************************/

/********************************************************
 Settings and Defaults
 Can be modified by file which includes this one
 ********************************************************/

var disappeardelay=250 //menu disappear speed onMouseout (in miliseconds)
var hidemenu_onclick="yes" //hide menu when user clicks within menu?
var horizontaloffset=2 // [vert only] ------horizontal offset of menu from default location. (0-5 is a good value)

var menuwidth='165px' //default menu width, can we remove??? (or) , it must have a value
// var menubgcolor='lightyellow'  

/* menu bgcolor is a problem: 
we cannot remove it if the item gets written into the document 
in a particular way 

*/

/********************************************************
 Data Samples
 Create arrays as below and call the array in 
 the dropdownmenu() function. Externalize in separate
 js file for easy manu management. Comment out the
 samples below as required.
 ********************************************************/

/*
var menu1=new Array()
menu1[0]='<a href="http://www.javascriptkit.com">JavaScript Kit</a>'
menu1[1]='<a href="http://www.freewarejava.com">Freewarejava.com</a>'
menu1[2]='<a href="http://codingforums.com">Coding Forums</a>'
menu1[3]='<a href="http://www.cssdrive.com">CSS Drive</a>'

//Contents for menu 2, and so on
var menu2=new Array()
menu2[0]='<a href="http://cnn.com">CNN</a>'
menu2[1]='<a href="http://msnbc.com">MSNBC</a>'
menu2[2]='<a href="http://news.bbc.co.uk">BBC News</a>'
		
*/

/********************************************************
 Core 
functions are used by attaching to event triggers.
They are submenus, and are attached to each portion
of the main menu. A partclar format of CSS file is
ussed for all visual elements (i.e. a specific set of 
elements must be present)

Works with text links and links attached to images.

 Call dropwdownmenu() or v_dropdownmenu() with the
 following args:
 [tbd]
 
 Call delayhidemenu() to make the menu disappear. This
 function is usually attached to onMouseOut()

 Core functionality for set up and display of menus. Two
 forms of display are: dropdownmenu, where the menu
 drops down from the bottom of an item
 
 It's smart enough to know if the browser or 
 document has moved to far in any direction, and
 will adjust its position accordingly
 
 to doL
 inspecition and updating of browser detection-related
 functions, FF is being misread 
 
 
 ********************************************************/


/* browser detection - [note:not accurately detecting FF */
var ie4=document.all
var ns6=document.getElementById&&!document.all



/* browser specific action [note:not accurately detecting FF */
/*

/* DEBUG - confirm specific behavior - color in div overdides the css
vertical version works for both */

if (ie4||ns6)
//- vertical behavior version ****
document.write('<div id="dropmenudiv" style="visibility:hidden;width: 160px" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>')
// dropdown-version - fails when menubgcolor not defined
// document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+';background-color:'+menubgcolor+'" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>')




/* core functions */

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function showhide(obj, e, visible, hidden, menuwidth){
if (ie4||ns6)   // [vert ] uses no quotes
dropmenuobj.style.left=dropmenuobj.style.top="-500px"

if (menuwidth!=""){   // [ vertical behavior did not check this and made the assignments anyway, isse relates to the bahavior when menuwidth is assigned/not assigned ]
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=menuwidth
}
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode &&
document.compatMode!="BackCompat")? document.documentElement :
document.body
}

/* 
provides positioning calculations to optimally
place a dropdown submenu on a horizontal menu bar 
 */

function clearbrowseredge(obj, whichedge){
var edgeoffset=0
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera? 
iecompattest().scrollLeft+iecompattest().clientWidth-15 : 
window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth

if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : 
window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
// [ vert just assigns edge offest to dropmenuobj.y; no adjustments
if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
}
}
return edgeoffset
}


/* vertical menu variation:
 provides positioning calculations to optimally
 place a popup submenu on a vertical menu */
function v_clearbrowseredge(obj, whichedge){
var edgeoffset=0
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera?
iecompattest().scrollLeft+iecompattest().clientWidth-15 :
window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
// [ dropdown does not subtract obj.offsetWidth
if (windowedge-dropmenuobj.x-obj.offsetWidth < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth
}
else{
var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 :
window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move menu up?
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
// [ drop down calculates ege offest as follows -> edgeoffset = dropmenuobj.y+obj.offsetHeight-topedge
if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either? (position at top of viewable window then)
edgeoffset=dropmenuobj.y
}
}
return edgeoffset
}

function populatemenu(what){
if (ie4||ns6)
dropmenuobj.innerHTML=what.join("")
}

function dropdownmenu(obj, e, menucontents, menuwidth){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidemenu()
dropmenuobj=document.getElementById? 
document.getElementById("dropmenudiv") : dropmenudiv
populatemenu(menucontents)

if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
// [ vert calculates top and left differently - a key point ]
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
return clickreturnvalue()
}

function v_dropdownmenu(obj, e, menucontents, menuwidth){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidemenu()
dropmenuobj=document.getElementById?
document.getElementById("dropmenudiv") : dropmenudiv
populatemenu(menucontents)

if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
// [ drop down calculates top and left differently - a key point ]
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+horizontaloffset+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
}
return clickreturnvalue()
}


function clickreturnvalue(){
if (ie4||ns6) return false
else return true
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function dynamichide(e){
if (ie4&&!dropmenuobj.contains(e.toElement))
delayhidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& 
!contains_ns6(e.currentTarget, e.relatedTarget))
delayhidemenu()
}

function hidemenu(e){
if (typeof dropmenuobj!="undefined"){
if (ie4||ns6)
dropmenuobj.style.visibility="hidden"
}
}

function delayhidemenu(){
if (ie4||ns6)
delayhide=setTimeout("hidemenu()",disappeardelay)
}

function clearhidemenu(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}

if (hidemenu_onclick=="yes")
document.onclick=hidemenu

