(function() {
    var window = this, undefined, widget = window.jsx.widget;

    jsx.widget.MenuBar = function(element) {
		var productInstance = this;
		element = $(element);
		this.element = element;
		element.find("li.menu").each(
			function(i) {
				var height, width, color, last;
				var container = $(this).find("div.menu-popup");
				var popup = $(this).find("div.menu-popup ul");
				var lis = $('> li', popup);
				var label = $(this).find("> .label").eq(0);
				$(this).click(
					function(event) {
						window.location.href = label.attr('href');
					}
				);
				
				if (popup.size()) {
					height = popup.height();
					popup.height(0);
					popup.width(popup.width());
					
					width = Math.max.apply(null, $.makeArray(lis.map(function() {
						return $(this).width();
					})));
					popup.width(width);
										
					$(this).hover(
						function(event) {
							$(this).addClass('hover');
							popup.stop();
							popup.animate({height:height}, 250);
						},
						function(event) {
							$(this).removeClass('hover');
							popup.stop();
							popup.animate({height:0}, 500);
						}
					);
				} else {
					$(this).hover(
						function(event) {
							$(this).addClass('hover');
						},
						function(event) {
							$(this).removeClass('hover');
						}
					);
				}
				
				$(this).find("li.menu-item").each(
					function(i) {
						var self = this, label, hover, bg;
						bg = $(this).find("div.background").eq(0);
						bg.height($(this).height()-8);
						bg.css('top', 4);
						bg.css('opacity',0);
						label = $(this).find("a.label").eq(0);
						
						$(this).hover(
							function(event) {
								label.addClass('hover');
								bg.stop();
								bg.animate({opacity:1}, 500);
							},
							function(event) {
								label.removeClass('hover');
								bg.stop();
								bg.animate({opacity:0}, 775);
							}
						);
					}
				);
			}
		);
		element.removeClass('preload');
    };
	jsx.widget.MenuBar.onInitialize = function(element, options) {
		$("#"+element).addClass('preload');
	};
})();
