jQuery.noConflict()

var fddm={
	arrowpath: 'http://www.rosarito.gob.mx/rto/images/menu/arrow.gif',
	animspeed: 200, 
	showhidedelay: [150, 150], 
	startzindex:1000,
	builtMenuIds: [],

	positionul:function($, $ul, e, $anchor){
		var istoplevel=$ul.hasClass('jqMenu')
		var docrightedge=$(document).scrollLeft()+$(window).width()-40
		var docbottomedge=$(document).scrollTop()+$(window).height()-40
		if (istoplevel){ 
			var offsets=$anchor.offset()
			var anchorsetting=$anchor.data('setting')
			var x=offsets.left+anchorsetting.useroffsets[0]+(anchorsetting.dir=="h"? $anchor.outerWidth() : 0)
			var y=offsets.top+anchorsetting.useroffsets[1]+(anchorsetting.dir=="h"? 0 : $anchor.outerHeight())
			x=(x+$ul.data('dimensions').w > docrightedge)? x-(anchorsetting.useroffsets[0]*2)-$ul.data('dimensions').w+$anchor.outerWidth()+(anchorsetting.dir=="h"? -($anchor.outerWidth()*2) : 0) : x
			y=(y+$ul.data('dimensions').h > docbottomedge)? y-(anchorsetting.useroffsets[1]*2)-$ul.data('dimensions').h-$anchor.outerHeight()+(anchorsetting.dir=="h"? ($anchor.outerHeight()*2) : 0) : y
		}
		else{
			var $parentli=$ul.data('$parentliref')
			var parentlioffset=$parentli.offset()
			var x=$ul.data('dimensions').parentliw
			var y=0
			x=(parentlioffset.left+x+$ul.data('dimensions').w > docrightedge)? x-$ul.data('dimensions').parentliw-$ul.data('dimensions').w : x
			y=(parentlioffset.top+$ul.data('dimensions').h > docbottomedge)? y-$ul.data('dimensions').h+$ul.data('dimensions').parentlih : y
		}
		$ul.css({left:x, top:y})
	},
	
	showbox:function($, $menu, e){
		clearTimeout($menu.data('timers').hidetimer)
		$menu.data('timers').showtimer=setTimeout(function(){$menu.show(fddm.animspeed)}, this.showhidedelay[0])
	},

	hidebox:function($, $menu){
		clearTimeout($menu.data('timers').showtimer)
		$menu.data('timers').hidetimer=setTimeout(function(){$menu.hide(100)}, this.showhidedelay[1])
	},


	buildMenu:function($, $menu, $target){
		$menu.css({display:'block', visibility:'hidden', zIndex:this.startzindex}).addClass('jqMenu').appendTo(document.body)
		$menu.bind('mouseenter', function(){
			clearTimeout($menu.data('timers').hidetimer)
		})		
		$menu.bind('mouseleave', function(){fddm.hidebox($, $menu)})
		$menu.data('dimensions', {w:$menu.outerWidth(), h:$menu.outerHeight()})
		$menu.data('timers', {})
		var $lis=$menu.find("ul").parent()
		$lis.each(function(i){
			var $li=$(this).css({zIndex: 1000+i})
			var $subul=$li.find('ul:eq(0)').css({display:'block'})
			$subul.data('dimensions', {w:$subul.outerWidth(), h:$subul.outerHeight(), parentliw:this.offsetWidth, parentlih:this.offsetHeight})
			$subul.data('$parentliref', $li)
			$subul.data('timers', {})
			$li.data('$subulref', $subul)
			$li.children("a:eq(0)").append('<img src="'+fddm.arrowpath+'" class="rightarrowclass" style="border:0;" />')
			$li.bind('mouseenter', function(e){
				var $targetul=$(this).css('zIndex', ++fddm.startzindex).addClass("selected").data('$subulref')
				if ($targetul.queue().length<=1){
					clearTimeout($targetul.data('timers').hidetimer)
					$targetul.data('timers').showtimer=setTimeout(function(){
						fddm.positionul($, $targetul, e)
						$targetul.show(fddm.animspeed)
					}, fddm.showhidedelay[0])
				}
			})
			$li.bind('mouseleave', function(e){
				var $targetul=$(this).data('$subulref')
				clearTimeout($targetul.data('timers').showtimer)
				$targetul.data('timers').hidetimer=setTimeout(function(){$targetul.hide(100).data('$parentliref').removeClass('selected')}, fddm.showhidedelay[1])
			})
		})
		$menu.find('ul').andSelf().css({display:'none', visibility:'visible'})
		this.builtMenuIds.push($menu.get(0).id)
	},

	

	init:function($, $target, $menu)
	{
		if (this.builtMenuIds.length==0)
		{ 
			$(document).bind("click", function(e){if (e.button==0){ $('.jqMenu').find('ul').andSelf().hide()}})
		}
		
		if (jQuery.inArray($menu.get(0).id, this.builtMenuIds)==-1)
			this.buildMenu($, $menu, $target)
		
		if ($target.parents().filter('ul.jqMenu').length>0) 
			return
		var useroffsets=$target.attr('data-offsets')? $target.attr('data-offsets').split(',') : [0,0]
		useroffsets=[parseInt(useroffsets[0]), parseInt(useroffsets[1])]
		$target.data('setting', {dir: $target.attr('data-dir'), useroffsets: useroffsets})
		$target.bind("mouseenter", function(e){
			$menu.css('zIndex', ++fddm.startzindex)
			fddm.positionul($, $menu, e, $target)
			fddm.showbox($, $menu, e)
		})
		$target.bind("mouseleave", function(e){	fddm.hidebox($, $menu) })
	}
}

jQuery.fn.addMenu=function(menuId, options){
	var $=jQuery
	return this.each(function(){
		var $target=$(this)
		if (typeof options=="object"){ 
			if (options.dir)
				$target.attr('data-dir', options.dir)
			if (options.offsets)
				$target.attr('data-offsets', options.offsets)
		}
		if ($('#'+menuId).length==1)
			fddm.init($, $target, $('#'+menuId))
	})
};

jQuery(document).ready(function($){
	var $anchors=$('*[data-menu]')
	$anchors.each(function(){
		$(this).addMenu(this.getAttribute('data-menu'))
	})
})

function ddlistmenu(id, className){
	var menu=document.createElement('ul')
	if (id)
		menu.id=id
	if (className)
		menu.className=className
	this.menu=menu
}

ddlistmenu.prototype={
	addItem:function(url, text, target){
		var li=document.createElement('li')
		li.innerHTML='<a href="'+url+'" target="'+target+'">'+text+'</a>'
		this.menu.appendChild(li)
		this.li=li
		return this
	},
	addSubMenu:function(){
		var s=new ddlistmenu(null, null)
		this.li.appendChild(s.menu)
		return s

	}
}
