/*
Written by: Adam Crownoble (adam@bryan.edu)
Date: April 3 2006
License: LGPL (http://www.gnu.org/copyleft/lesser.html)
*/

var DropDownMenu = Class.create();
DropDownMenu.prototype = {

 initialize: function(menuElement) {

  this.menu = menuElement;
  this.itemsPerRow = 6;// number of items per row for this website
  this.id = menuElement.id;
  this.timeout = null;
  this.offsetBorderWidth=1;
  this.tStr='';
  this.cSubmenu;
  this.eSubmenu;
  this.eMButton;
  this.openCounter=0;
  
  /*// Build Top Level Menu Array
  this.buttons = $A(this.menu.getElementsByTagName('a')).findAll(
   function(a) {
   	
	   	// Make the Event Trigger the Link instead of the LI
	    if (a.parentNode.parentNode == menuElement){
	    	// look to see if the link has a submenu
	    	// only attach an event if there is a submenu
	    	var hasChildren  = $A(a.parentNode.getElementsByTagName('ul')).findAll(
   				function(ul) {return (ul)});
   				
	    	if(hasChildren.length>0){
	    		return a;
	    	}
	    	
	    }
   }
  );
  */
  
  // Build Top Level Menu Array
  this.buttons = $A(this.menu.getElementsByTagName('li')).findAll(
   function(li) {
   	
	   	// Make the Event Trigger the Li instead of the Link
	    if (li.parentNode == menuElement){
	    	// look to see if the link has a submenu
	    	// only attach an event if there is a submenu
	    	var hasChildren  = $A(li.getElementsByTagName('ul')).findAll(
   				function(ul) {return (ul)});
   				
	    	if(hasChildren.length>0){
	    		//alert(li.id);
	    		return li;
	    	}
	    	
	    }
   }
  );

  // Set the width of the top nav for Opera
  
  this.bWidths = $A(this.menu.getElementsByTagName('li')).findAll(
   function(li) {
	   
	    if (li.parentNode == menuElement){
	    	liWidth=Element.getWidth(li.firstChild);
	    	li.style.width = (liWidth+10)+'px';
	    	
	    }
   }
  );
  
  /** Use this when needed 
  this.links = $A(this.menu.getElementsByTagName('a')).findAll(
   function(a) {
	   	// Make the Event Trigger the Link instead of the LI
	    if (a.parentNode.parentNode != menuElement){
	    	return a;
	    }
   }
  );
  */
  
  this.li = $A(this.menu.getElementsByTagName('li')).findAll(
   function(li) {
	   	// Make the Event Trigger the Link instead of the LI
	    if (li.parentNode != menuElement){
	    	return li;
	    }
   }
  );
  
  // build Submenu array
  this.submenus = $A(this.menu.getElementsByTagName('ul'));
  
  // attach events to links in top level menu  
  this.buttons.each(
   function(button) {
	 	Event.observe(button, 'mouseover',this.tExpand.bindAsEventListener(this));
		Event.observe(button, 'mouseout',this.tCollapse.bindAsEventListener(this));
   }.bind(this)
  );
 
 // attach events to the li elements 
 this.li.each(
 	function(link){
 		Event.observe(link, 'mouseover',this.tChildOver.bindAsEventListener(this));
		Event.observe(link, 'mouseout',this.tChildOut.bindAsEventListener(this));
 	}.bind(this) 
 );
 
 }, // initialize done
 
 tChildOut: function(event){
 	if(this.eSubmenu){
 			clearTimeout(this.timeout);
 			this.timeout = setTimeout(this.collapse.bind(this),700);
 	}
 },
 tChildOver: function(event){ 	
 	if(Element.visible(this.eSubmenu)){
 		clearTimeout(this.timeout);
 		Element.show(this.eSubmenu);
 	}
 },
 tCollapse: function(event){
 	this.cSubmenu = this.findSubmenu(Event.element(event));
 	if(this.cSubmenu){
 			clearTimeout(this.timeout);
 			this.timeout = setTimeout(this.collapse.bind(this),700);
 	}
 },
 tExpand: function(event){
 	this.eSubmenu = this.findSubmenu(Event.element(event));
 	//links
 	this.eMButton = this.findButton(Event.element(event));
 	
 	//expand the submenu if it's hidden, othewise continue showing submenu
 	if(this.eSubmenu && !(Element.visible(this.eSubmenu))){
 			clearTimeout(this.timeout);
	 		this.timeout = setTimeout(this.expand.bind(this),0);
 	}else{
 		clearTimeout(this.timeout);
 		Element.show(this.eSubmenu);
 	}
 },
 expand: function() {
  var submenu = this.eSubmenu;
  // this is so we can place the submenu absolutely
  // using links
  //var mButton = this.eMButton.parentNode;
  // using li
  var mButton = this.eMButton;
 
  if (submenu) {
    this.submenus.each(
     	function(sub1,submenu) {
    		if(Element.visible(sub1)){
    			 Element.hide(sub1);//alert('d');
    		}
  	  	}
	);
	//this.fixRowSizes(submenu);
	// keep the Practice areas in horizontal format
	//if(this.eMButton.id=='a_2'){
	if(this.eMButton.id=='li_2'){
	    this.placeSubMenu(submenu,mButton);
	    this.fixRowSizes(submenu);
	    this.makeAllDividers(submenu);
	}
    //Effect.toggle(submenu,'appear');
    //Effect.Appear(submenu, { duration: 2.0,from: 0.0001, to: 0.9999} );
    Element.show(submenu);
  }
 },
 collapse: function(event) {
  var submenu = this.cSubmenu;
  if (submenu) {
  		//Effect.toggle(submenu,'appear');
        Element.hide(submenu);
        // this is an ie6 hack
        //var placeHolder = $('subMenu');
        //if(placeHolder){
	    //    Element.hide(placeHolder);
        //}
  }
 },
 collapseAndShow: function() {
  var submenu = this.cSubmenu;
  if (submenu) {
        Element.hide(submenu);
        Element.show(submenu);
  }
 },
 placeSubMenu: function (submenu,mButton){
 	// Need to figure out the height of the submenus
    // We'll do this by finding how many in the list and counting rows.
    
    this.links = $A(submenu.getElementsByTagName('li')).findAll(
	   function(li) {
	    return (li.parentNode == submenu);
	   }
	  );
    var rows = Math.ceil(this.links.length / this.itemsPerRow);
    //alert(rows);
    var rowHeight = 24;
    var subMenuHeight= Math.round(rows*rowHeight);
    
  	var placeHolder = $('subMenu');
  	Element.show(placeHolder);
  	// Figure out exactly where to put the submenu based on a div.
  	yPlaceHolder = this.findPosY(placeHolder);
  	xPlaceHolder = this.findPosX(placeHolder);
  	yLI = this.findPosY(mButton);
  	xLI = this.findPosX(mButton);
  	// if the main menu has a border then we need to compensate for it
  	// by adding the width of the border
    // if a menu item doesn't have a border we have to compensate for that too.
    
  	xNew=xLI - xPlaceHolder;//+this.offsetBorderWidth;
  	yNew=yPlaceHolder-yLI;
  	//alert("x-"+xNew+" y-"+yNew);
  	submenu.style.top = yNew+'px';
  	submenu.style.left = -xNew+'px';
  	this.makeAllDividers(submenu);
  	
  	// set the submenu container to the correct height
  	//placeHolder.style.height = subMenuHeight + 'px';
    Element.hide(placeHolder);
 },
 fixRowSizes: function(submenu){
 	this.links = $A(submenu.getElementsByTagName('li')).findAll(
	   function(li) {
	    return (li.parentNode == submenu);
	   }
	  );
	var menuWidth = Element.getWidth(submenu);
	
	var numberOfLinks = this.links.length;
	//alert(numberOfLinks);
	// space out the links
	
	if(numberOfLinks < this.itemsPerRow){
		var i=0;
		this.links.each(function(li){
			    
				switch(numberOfLinks){
					case 1:
					li.style.width= menuWidth +'px';
					break;
					case 2:
					li.style.width= (menuWidth/2)-1 +'px';
					break;
					case 3:
					li.style.width= (menuWidth/3)-1 +'px';
					break;
					case 4:
					li.style.width= (menuWidth/4)-1 +'px';
					break;
					case 5:
					if(i<=1){
						li.style.width= (menuWidth/5)-1 +'px';
					}else{
						li.style.width= (menuWidth/5)-2 +'px';
					}
					i++;
					break;
				}
			
		});
	}else if(numberOfLinks % this.itemsPerRow != 0){
		var i=0;
		var remainder = numberOfLinks % this.itemsPerRow;
		this.links.each(function(li){
	 		//console.log(i,numberOfLinks-(6-remainder),remainder);
	 		if(i==(numberOfLinks-(6-remainder))){
				//console.log(remainder);
				switch(remainder){
					case 1:
					li.style.width= menuWidth +'px';
					break;
					case 2:
					li.style.width= ((menuWidth/6)*5)-1 +'px';
					break;
					case 3:
					li.style.width= ((menuWidth/6)*4)-1 +'px';
					break;
					case 4:
					li.style.width= ((menuWidth/6)*3)-1 +'px';
					break;
					case 5:
					if(i<=1){
						li.style.width= ((menuWidth/6))-1 +'px';
					}else{
						li.style.width= ((menuWidth/6)*2)-2 +'px';
					}
					
					break;
				}
	 		}
			i++;
		});
	}
	
 },
 makeAllDividers: function(submenu){

    var currList=$A(submenu.getElementsByTagName('li')).findAll(function(li) {
				return (li.parentNode == submenu);
		});

		var x=1;
		var pix=2;
		var remainder = currList.length % this.itemsPerRow;
		if(currList.length >= 5){
	 		currList.each(function(li) {
	 			/*
	 		  if(x==0){li.style.borderRight= pix + 'px solid black';}
			  if(x==1){li.style.borderRight= pix + 'px solid black';}
			  if(x==2){li.style.borderRight= pix + 'px solid black';}
			  if(x==4){li.style.borderRight= pix + 'px solid black';}
			  if(x==5){li.style.borderRight= pix + 'px solid black';}
			  if(x==6){li.style.borderRight= pix + 'px solid black';}
			  if(x==8){li.style.borderRight= pix + 'px solid black';}
			  if(x==9){li.style.borderRight= pix + 'px solid black';}
			  if(x==10){li.style.borderRight= pix + 'px solid black';}
			  if(x==12){li.style.borderRight= pix + 'px solid black';}
			  if(x==13){li.style.borderRight= pix + 'px solid black';}
			  if(x==14){li.style.borderRight= pix + 'px solid black';}
			  */
			  if(x%6==0 && x>0 || x==(currList.length)){li.style.borderRight= 0 + 'px solid black';}
			  else{
			  	{li.style.borderRight= pix + 'px solid black';}
			  }
			  
			  if(x==(currList.length-remainder)+1){li.style.borderRight= pix + 'px solid black';}
			  //if(x==(currList.length-remainder)-1){li.style.borderRight= pix + 'px solid black';}
			  //if(x==(currList.length-remainder)-4){li.style.borderRight= pix + 'px solid black';}
			  		  
			  x++;
			  
			});
			
		}else if(currList.length == 3){
			currList.each(function(li) {
				if(x==1){li.style.borderRight= pix + 'px solid black';}
				if(x==2){li.style.borderRight= pix + 'px solid black';}
				if(x==3){li.style.borderRight= 0 + 'px solid black';}
				x++;
			});
		}else if(currList.length == 2){
			currList.each(function(li) {
				if(x==1){li.style.borderRight= pix + 'px solid black';}
				if(x==2){li.style.borderRight= 0 + 'px solid black';}
			x++;
			});
		}else if(currList.length == 1){
			currList.each(function(li) {
				if(x==1){li.style.borderRight= 0 + 'px solid black';}
				
			x++;
			});
		}
},
 
 findButton: function(element) {
	  var button = false;
	  
	  while(element.parentNode) {
		   if(this.buttons.include(element)) { button = element;}
		   element = element.parentNode;
	  }
	  return button;
 },

 findSubmenu: function(element) {
  var button = this.findButton(element);
  //alert(button);
  // if links
  //var submenu = button.parentNode.getElementsByTagName('ul')[0];
  // if li
  var submenu = button.getElementsByTagName('ul')[0];
  //alert(submenu.id)
  return submenu;
 },
 findPosY: function (obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  },
 findPosX: function (obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  },
  makeDividers: function (element){
	var tListCont = element; 
    var currList=$A(tListCont.getElementsByTagName('li')).findAll(function(li) {
				return (li);
		});
		
		var x=0;
		var pix=2;
 		currList.each(function(li) {
 		  /*
 		  if(x==0){li.style.borderRight= pix + 'px solid black';}
		  if(x==1){li.style.borderRight= pix + 'px solid black';}
		  if(x==2){li.style.borderRight= pix + 'px solid black';}
		  if(x==4){li.style.borderRight= pix + 'px solid black';}
		  if(x==5){li.style.borderRight= pix + 'px solid black';}
		  if(x==6){li.style.borderRight= pix + 'px solid black';}
		  if(x==8){li.style.borderRight= pix + 'px solid black';}
		  if(x==9){li.style.borderRight= pix + 'px solid black';}
		  if(x==10){li.style.borderRight= pix + 'px solid black';}
		  if(x==12){li.style.borderRight= pix + 'px solid black';}
		  if(x==13){li.style.borderRight= pix + 'px solid black';}
		  if(x==14){li.style.borderRight= pix + 'px solid black';}
		  */
		  		  
		  x++;
		  
		});
},
 xGetComputedStyle: function (oEle, sProp, bInt){
  var s, p = 'undefined';
  var dv = document.defaultView;
  if(dv && dv.getComputedStyle){
    s = dv.getComputedStyle(oEle,'');
    if (s) p = s.getPropertyValue(sProp);
  }
  else if(oEle.currentStyle) {
    // convert css property name to object property name for IE
    var i, c, a = sProp.split('-');
    sProp = a[0];
    for (i=1; i<a.length; ++i) {
      c = a[i].charAt(0);
      sProp += a[i].replace(c, c.toUpperCase());
    }
    p = oEle.currentStyle[sProp];
  }
  else return null;
  return bInt ? (parseInt(p) || 0) : p;

 },
 
 xDef: function () {
	  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
	  return true;
 },
 
 xStr: function(s) {
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
  return true;
 },
 xNum: function () {
  for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
  return true;
 }

};