/*********************
//* jQuery Drop Line Menu- By Dynamic Drive: http://www.dynamicdrive.com/
//* Last updated: June 27th, 09'
//* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/

//* changed by: http://greenevillage.net/jquery/dropdown.aspx

*********************/
var last_element
var droplinemenu={

arrowimage: {classname: 'downarrowclass', src: '/images/dropline_down.gif', leftpadding: 5}, //customize down arrow image
animateduration: {over: 300, out: 200}, //duration of slide in/ out animation, in milliseconds

buildmenu:function(menuid){
	jQuery(document).ready(function($){
		var $mainmenu=$("#"+menuid+">ul")
// 		$mainmenu.append('<li class="top"><a href="Opportunities">Opportunities</a>\
// 	<ul style="visibility: visible; width: 960px; top: 130px; display: none;">\
// 		<li><a class="current_page" href="Current_Opportunities">Current Opportunities</a></li>\
// 		<li><a href="Teaching_English">Teaching English</a></li>\
// 		<li><a href="Medical_Work">Medical Work</a></li>\
// 	</ul>\
// 	</li>')
		var $headers=$mainmenu.find("ul").parent()
		var parents = $('.current_page').parentsUntil("#"+menuid+">ul").add('.current_page~ul')//.filter('ul')
		if (parents.length == 0) {
		   parents = $('#'+menuid+'>ul')
		}
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={h:$curobj.find('a:eq(0)').outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			if (!this.istopheader)
				$subul.css({left:0, top:this._dimensions.h})
			var $innerheader=$curobj.children('a').eq(0)
			$innerheader=($innerheader.children().eq(0).is('span'))? $innerheader.children().eq(0) : $innerheader //if header contains inner SPAN, use that
			$innerheader.append(
				'<img src="'+ droplinemenu.arrowimage.src
				+'" class="' + droplinemenu.arrowimage.classname
				+ '" style="border:0; padding-left: '+droplinemenu.arrowimage.leftpadding+'px" />'
			)
			$curobj.hoverIntent(
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					if ($targetul.queue().length<=1 && parents.index($curobj) < 0){ //if 1 or less queued animations and the current object is not part of the parents.
						if (this.istopheader)
							$targetul.css({ top: $mainmenu.offset().top+this._dimensions.h})
						if (document.all && !window.XMLHttpRequest) //detect IE6 or less, fix issue with overflow
							$mainmenu.find('ul').css({overflow: (this.istopheader)? 'hidden' : 'visible'})
						$targetul.slideDown(droplinemenu.animateduration.over)
						last_element = this;
						$('#'+menuid).stop().animate({
                    height: ($curobj.parents("ul").length)*$targetul.find('a:eq(0)').outerHeight()+$mainmenu.find('a:eq(0)').outerHeight()
                  }, 300)
                  // Hide static tiers for better readability.
                  if ($curobj.parents('ul').length < $('.current_page').parents("ul").length ) {
                     parents.filter('ul').not($curobj.parents('ul')).slideUp(droplinemenu.animateduration.over)
                     $('.current_page').parent().children('ul').not($curobj.parents('ul')).slideUp(droplinemenu.animateduration.over)
                  }
					}
				},
				function(e){
					var $targetul=$(this).children("ul:eq(0)")
					if ($targetul.queue().length<=1 && parents.index($curobj) < 0 ){ //if 1 or less queued animations
                  $targetul.slideUp(droplinemenu.animateduration.out)
                  if ((this == last_element || $(last_element).parents().index($(this)) >=0) && $('.current_page').parents("ul").length*$('.current_page').outerHeight() <= ($curobj.parents("ul").length)*this._dimensions.h && $curobj.parents('ul').length != 1){
                     if ($curobj.parentsUntil("#"+menuid+">ul").has('.current_page').length > 0) {
                        $('#'+menuid).animate({
                          height: ($curobj.parents("ul").length+1)*this._dimensions.h 
                        }, 100)
                     } else {
                        $('#'+menuid).animate({
                          height: ($curobj.parents("ul").length)*this._dimensions.h 
                        }, 100)
                     }
                  }
                  if ((this == last_element || $(last_element).parents().index($(this)) >=0) ) {
                     parents.filter('ul').slideDown(droplinemenu.animateduration.out)
//                      $('#'+menuid).animate({
//                        height: $('.current_page').parents("ul").length*$('.current_page').outerHeight()
//                      }, 100)
                     if (parents.length > 1) {
                        $('#'+menuid).animate({
                          height: (parents.filter("ul").length)*$('.current_page').outerHeight()+$mainmenu.find('a:eq(0)').outerHeight()
                        }, 100)
                     } else {
                        $('#'+menuid).animate({
                          height: $mainmenu.outerHeight()
                        }, 100)
                     }
                  }
               }
				}
			) //end hover
		}) //end $headers.each()
		parents.slideDown(0)
		var $menu_height = $mainmenu.find('a:eq(0)').outerHeight()
		$mainmenu.find("ul").not(parents).css({display:'none', visibility:'visible', width:$mainmenu.width()})
		parents.filter('ul').not($mainmenu).each(function(i){
		   var $curobj = $(this)
		   this.istopheader=$curobj.parents("ul").length==1? true : false
		   $curobj.css({visibility:'visible', width:$mainmenu.width() })
		   if (this.istopheader)
		      $curobj.css({ top:$mainmenu.offset().top+$curobj.parent().children().filter('a:eq(0)').outerHeight()})
		   else
		      $curobj.css('top', $curobj.parent().children().filter('a:eq(0)').outerHeight())
         $curobj.css('z-index', 90)
		})
		if (parents.length > 1) {
         $('#'+menuid).css({height: (parents.filter("ul").length)*$('.current_page').outerHeight()+$mainmenu.find('a:eq(0)').outerHeight()})
      } else {
         $('#'+menuid).css({height: $mainmenu.outerHeight()})
      }
	}) //end document.ready
}
}


