(function($){
	
	$.fn.creMenu = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
		
		$(this).each(function() {
			var creMenu = $(this).data("creMenu");
			if (creMenu && isMethodCall) {
				if (creMenu[options] && $.isFunction(creMenu[options])) {
					creMenu[options].apply(creMenu, $.makeArray(args).slice(1));
				}
			} else if ( !isMethodCall && !creMenu ) {
				creMenu = new $.creMenu(this, options);
				$(this).data("creMenu", creMenu);
			}
		});
		
		return this;
	};
	
	$.creMenu = function(elem, options) {
		this.elem = $(elem);
		this.options = $.extend( {}, $.creMenu.defaults, options );
		this.init();
	};
	
	$.extend( $.creMenu.prototype, {
		init: function() {
			var self = this, menuOffset, menuWidth;
				
			this.currentMenu = undefined;
			this.hideTimeout = 0;
			
			$(">ul>li", this.elem).addClass("parent");
			this.menu = $(">ul>li>a", this.elem);
			
			this.menu.css({
				fontWeight: "bold"
			});
			
			this.hideTimeout = 0;
			
			menuOffset = this.elem.offset();
			menuWidth = this.elem.outerWidth();
			
			var l = -$("#header").offset().left;
			
			this.menu.each(function(){
				var el = $(this), p = el.parent(),
					o, w, h, w1, h1, d,
					dir = 0,
					submenu, submenuW, submenuH;

				w = el.innerWidth();
				o = el.offset();
				
				el.css({
					width: el.width(),
					fontWeight:""
				});
				
				submenu = p.find(".submenu");
				
				if ( submenu.length ) {
					w1 = submenu.width();
					
					submenu.css({
						display:"block",
						width: w1
					});
					
					w1 -= parseInt( $(">ul", submenu).css("paddingLeft"), 10) || 0;
					w1 -= parseInt( $(">ul", submenu).css("paddingRight"), 10) || 0;
					
					$(">ul", submenu).css("width", w1);
					
					$(">ul>li", submenu).each(function() {
						var l = $(this),
							m = 0;
							
						m += parseInt(l.css("paddingLeft"),10) || 0;
						m += parseInt(l.css("paddingRight"),10) || 0;
						
						l.css({
							width: w1 - m
						});
							
					});
					
					submenuW = Math.floor(submenu.outerWidth());
					submenuH = Math.floor(submenu.outerHeight());
					
					p.prepend(
							"<div class='hl'></div><div class='hr'></div><div class='hb' style='width:"+w+"px;'></div><div class='bl'></div><div class='br'></div><div class='bt'></div><div class='btl'></div><div class='btr'></div><div class='bb'></div><div class='bbl'></div><div class='bbr'></div>"
						);
						
					if ( o.left - menuOffset.left + submenuW > menuWidth ) {
						dir = 1;
					}
					
					d = 0;
					
					w1 = $(".hr", p).width();
					
					if ( dir == 0 ) {
						$(".bt", p).css({
							left: w + w1,
							width: submenuW - w - w1 - $(".btr", p).outerWidth(true)
						});
						
						$(".btr", p).css({
							left: submenuW - $(".btr", p).outerWidth(true)
						});

						$(".btl", p).remove();
						
						h1 = $(".btr", p).outerHeight(true);
						
					} else {
						p.addClass("toright");
						
						d = submenuW - w;
						
						$(".bt", p).css({
							left: 0 - d + $(".btl", p).outerWidth(true),
							width: submenuW - w - w1
						});
						
						$(".btl", p).css({
							left: 0 - d
						});

						$(".btr", p).remove();
						
						h1 = $(".btl", p).outerHeight(true);
						
						submenu.css({
							left:-d
						});
					}
						
					$(".bl", p).css({
						height: submenuH - ( (dir == 1) ? h1 : 0 ),
						left:0 - d
					});
					
					w1 = $(".br", p).outerWidth(true);
					$(".br", p).css({
						height: submenuH - ( (dir == 0) ? h1 : 0 ),
						left: submenuW - w1 - d
					});
					
					$(".bbl", p).css({
						left: 0 - d,
						top: submenuH
					});
					
					$(".bbr", p).css({
						left: submenuW - $(".bbr", p).outerWidth(true) - d,
						top: submenuH
					});
					
					w1 = $(".bbl", p).outerWidth(true);
					$(".bb", p).css({
						left: w1 - d,
						top: submenuH,
						width: submenuW - w1 - $(".bbr", p).outerWidth(true)
					});
					
					submenu.css("display","");
				}
			});
			
			this.menu.css({
				fontWeight: ""
			});
			
			
			
			$(".submenu", this.elem).bind("mouseenter", function(e) {
				self._onMouseOver.apply(self, [e]);
			});
			
			$(".submenu", this.elem).bind("mouseleave", function(e) {
				self._onMouseOut.apply(self, [e]);
			});
			
			this.menu.bind("mouseenter", function(e) {
				//self._onMouseOver.apply(self, [e]);
				self._onMouseOver.apply(self, [e]);
			});
			
			this.menu.bind("mouseleave", function(e) {
				self._onMouseOut.apply(self, [e]);
			});
			
			/*
			for(var i=0,n=this.menu.length; i<n; i++) {
				$(this.menu[i].trigger).bind("mouseover", function(event){
					self.onMouseOver.apply(self, [this, event]);
				});
				$(this.menu[i].trigger).bind("mouseout", function(event){
					self.onMouseOut.apply(self, [this, event]);
				});
				$($(this.menu[i].trigger).parent()).bind("mouseover", function(event){
					self.onMouseOver.apply(self, [$(this).find("a")[0], event]);
				});
				$($(this.menu[i].trigger).parent()).bind("mouseout", function(event){
					self.onMouseOut.apply(self, [$(this).find("a")[0], event]);
				});
				
				if (this.menu[i].hasSubmenu) {
					$(this.menu[i].submenu).bind("mouseover", function(event){
						self.onMouseOver.apply(self, [$(this).parents("li").find("a")[0], event]);
					});
					$(this.menu[i].submenu).bind("mouseout", function(event){
						self.onMouseOut.apply(self, [$(this).parents("li").find("a")[0], event]);
					});
				}
			}
			*/
		},
		
		hideMenu: function() {
			$(">ul>li", this.elem).removeClass("active");
		},
		
		_clearTimeout: function() {
			if ( this.hideTimeout ) {
				window.clearTimeout(this.hideTimeout);
				this.hideTimeout = null;
			}
		},
		
		_setTimeout: function() {
			var self = this;
			
			this._clearTimeout();
			this.hideTimeout = window.setTimeout(function(){
				self.hideMenu.call(self);
				self = null;
			}, 200);
		},
		
		_getParent: function(e) {
			p = e;
			
			while ( p.length && !p.hasClass("parent") ) {
				p = p.parent();
			}
			
			if ( p.length ) {
				return p;
			} else {
				return null;
			}
		},
		
		_onMouseOver: function(e) {
			var el = this._getParent($(e.target));
			
			this._clearTimeout();
			
			if ( !el.hasClass("active") ) {
				this.hideMenu();
				el.addClass("active");
			}
		},
		
		_onMouseOut: function(e) {
			this._setTimeout();
		}
	});
	
	$.extend( $.creMenu, {
		defaults: { }
	});
	
	
	$().ready(function(){
		$("#nav").creMenu();
		if (jQuery.jScrollPane && $(".news_list_holder").length) {
			$(".news_list_holder").jScrollPane({
				scrollbarWidth:11,
				arrowSize:11,
				showArrows:true
			});
		}
		
		if ($.browser.msie && $.browser.version < 7) {
			$("img[src$=png], #nav .submenu .c, #nav .submenu .r, #nav .submenu .l").pngfix({
				sizingMethod:"scale"
			});
		}
		
		if ($.browser.msie && $.browser.version < 8) {
			$("hr").each(function(){
				$(this).wrap("<div></div>");
				$(this).parent().addClass("hr");
			});
		}
		
	var $accordion = jQuery("#committees_accordion");
	
	$accordion.find('> li:odd').hide();
	$accordion.find('> li:even').click(function (e) {
		e.preventDefault();
		if(jQuery(this).next().css('display') == 'none') {
			jQuery(this).next().slideDown();
			} else {
			jQuery(this).next().slideUp();
			}
	});
		
		
	});
	
	
	
	
})(jQuery);
