var closeMenu = 0;
var isOpenMenu = false;
var globalThisMenu = "";
var globalUnfoldSpeed = "";
var globalMenuHeight = 0;
		
function canCloseMenu() {
	//alert(globalCon);
	//alert(globalThis);
	clearTimeout(closeMenu);
	globalThisMenu.children('.menuCarousel').stop().animate({ opacity : 0 }, globalUnfoldSpeed, 'swing', function(){
			$(this).css('display', 'none');
		isOpenMenu = false;
		});
	}

$.fn.aDropdownMenu = function(options){
	var settings = $.extend({
		top : 0,
		left : 0,
		margin : 0,
		padding : 0,
		visibleItems : 10,
		hideArrows : false,
		slideSpeed : 400,
		unfoldSpeed : 300,
		separator : 'images/menu_dropdown_divider.png',
		topImage : { src : 'images/menu_dropdown_up.png', eventType : 'click', event : function(){ alert('clicked top'); } },
		bottomImage : { src : 'images/menu_dropdown_down.png', eventType : 'click', event : function(){ alert('clicked bottom'); } }
		}, options);
	
	if(settings.items.length < settings.visibleItems){
		settings.visibleItems = settings.items.length;
		settings.hideArrows = true;
		}

	$this = $(this)
	.mouseover(function(e){
		//$(this).css('textDecoration', 'underline');
		clearTimeout(closeMenu);
		if (isOpenMenu == false) {
			isOpenMenu = true;
			if($(this).children('.menuCarousel').css('display') == 'none'){
				$(this).children('.menuCarousel').css({ display : 'block' });
				}
			$(this).children('.menuCarousel').stop().animate({ opacity : 1 }, settings.unfoldSpeed, 'swing', function(){
				if ($.browser.msie) {
					this.style.removeAttribute('filter');
					}
				});
			/* dCon.css('display', 'block').stop().animate({ opacity: 1, height: 266 }, settings.slideSpeed, function(){
				if($.browser.msie) {
					this.style.removeAttribute('filter');
					}
				}); */
			}
		})
	.mouseout(function(e){
		//$(this).css('textDecoration', 'none');
		/*$(this).children('.menuCarousel').stop().animate({ opacity : 0 }, settings.unfoldSpeed, 'swing', function(){
			$(this).css('display', 'none');
			}); */
			clearTimeout(closeMenu);
			globalThisMenu = $(this);
			globalUnfoldSpeed = settings.unfoldSpeed;
			if($.browser.msie) {
				this.style.removeAttribute('filter');
				}
			closeMenu = setTimeout('canCloseMenu()', 500);
		});
	
	var submenu = $('<ul><\/ul>')
	.addClass('submenu')
	.css({
		height : settings.visibleItems * 26
		});
	//.css({ top : settings.top, left : settings.left });
		
	$('<div></div>').addClass('menuCarousel').css({
		width : settings.width > 0 ? settings.width : $(this).width(),
		top : settings.top,
		left : settings.left
		}).html(submenu).appendTo($this);
	
	for(var i in settings.items){
		var item = settings.items[i];
		if(item.type == 'link'){
			var newitem = $('<li><\/li>')
			.html($('<a>' + item.title + '</a>').attr({ href : item.url }))
			.appendTo(submenu);
			}
		else if(item.type == 'menu'){
			var newitem = $('<li><\/li>')
			.attr('class', 'dropdown')
			.html($('<a>' + item.title + '</a>'))
			.appendTo(submenu)
			.aDropdownMenu(item.menu);
			}
		if(typeof(newitem) !== 'undefined'){
			if(i >= settings.visibleItems){
				newitem.css({ opacity : 0, display : 'none' });
				}
			newitem
			.css({
				position : 'absolute',
				top : settings.hideArrows == true ? i * 26 : i * 26 + 25,
				zIndex : 1
				});
			if(typeof(settings.items[i + 1]) !== 'undefined'){
				//newitem.append($('<img>').attr('src', settings.separator));
				}
			}
		}
	if(settings.hideArrows == false){
		$('<img>') // up button -> slide items down
		.attr('src', settings.topImage.src)
		.addClass('menuprev')
		.mouseover(function(){ $(this).css({ cursor : 'pointer' }); })
		.mouseout(function(){ $(this).css({ cursor : 'default' }); })
		.click(function(){
			$(this).siblings('ul').children('li').css({ display : 'block' }).each(function(){
				if($(this).parent().children(':first').position().top == 25){
					$(this).parent().children('li').each(function(){
						$(this).css('display', $(this).css('opacity') == 0 ? 'none' : 'block');
						});
					return false;
					}
				$(this)
				.animate({
					top : $(this).position().top + 26,
					opacity : $(this).position().top >= (settings.visibleItems - 1) * 26 + 25 || $(this).position().top < -1 ? 0 : 1
					}, settings.slideSpeed, function(){
						$(this).css('display', $(this).css('opacity') == 0 ? 'none' : 'block');
						});
				});
			})
		.prependTo(submenu.parent());
		
		$('<img>') // down button -> slide items up
		.attr('src', settings.bottomImage.src)
		.addClass('clear').addClass('menunext')
		.mouseover(function(){ $(this).css({ cursor : 'pointer' }); })
		.mouseout(function(){ $(this).css({ cursor : 'default' }); })
		.click(function(){
			$(this).siblings('ul').children('li').css({ display : 'block' }).each(function(){
				if($(this).parent().children(':last').position().top == ((settings.visibleItems - 1) * 26 + 25)){
					$(this).parent().children('li').each(function(){
						$(this).css('display', $(this).css('opacity') == 0 ? 'none' : 'block');
						});
					return false;
					}
				$(this).animate({
					top : $(this).position().top - 26,
					opacity : $(this).position().top <= 25 || $(this).position().top >= ((settings.visibleItems + 1) * 26 + 25) ? 0 : 1
					}, settings.slideSpeed, function(){
						$(this).css('display', $(this).css('opacity') == 0 ? 'none' : 'block');
						});
				});
			})
		.appendTo(submenu.parent());
		}
	
	};

