(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 label = $(this).find("> .label").eq(0);
				var minMenuWidth = 1;
				
				$(this).click(
					function(event) {
						window.location.href = label.attr('href');
					}
				);
				
				$(this).find("li.menu-item img").each(function() {
					minMenuWidth = Math.max(minMenuWidth, $(this).width());
				});
				
				if (popup.length) {
					width = container.width() - parseInt(popup.css("padding-left")) - parseInt(popup.css("padding-right")) + 4;
					if (width < minMenuWidth) {
						width = minMenuWidth;
					}
					popup.width(width);
					height = popup.height();
					popup.height(0);
					
					$(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');
	};
})();