// jquery.dropdown.js
// Lightweight jQuery Plugin for Simple Dropdown Menus
// (C) Stunf 2009

(function($) { 
	
	function Dropdown() {
		this.version 				 = 1;
		// pointer to anchor object which was clicked to open the dropdown		
		this.current_control = null;
		// pointer to div object which contains the dropdown menu
		this.current_menu    = null;
		
	}
		
	$.fn.dropdown = function(options) {
					
		if (!$.dropdown.initialized) {
			$(document).bind('click', function(event) { collapse(event); });
			$.dropdown.initialized = true;
		}
		
		var _this = this;
		
		return this.each(function() {
			var opts = $.extend({}, $.fn.dropdown.defaults, options);
					
			$this = $(this);			
			
			$a = null;
			
			if (opts.expandClass) {
				$a = $( "a."+opts.expandClass, $this );
			} else if (opts.expandId) {
				$a = $( opts.expandId );	
			} else {
				$a = $("a", $this);
			}
			
			var __this = $this;
			
			$a.bind('click', function() {
				$(this).blur();
				// Clicked on link.
				return expand(this, opts, __this);
			});
			
			//alert('New dropdown ' + $this.html());
			// do stuff with '$this'			
		});
	};

	// private functions
	
	function collapseCurrent() {
		if ( $.dropdown.current_menu != null ) {
			
			$.dropdown.current_menu.removeClass('dropdown_active');
			$.dropdown.current_control.removeClass('dropdown_active');
			
			$.dropdown.current_menu.css('visibility', 'hidden');
			$.dropdown.current_menu = null;
			$.dropdown.current_control = null;
		}
	}
	
	function collapse(e) {
		
		if ( $.dropdown.current_menu == null || $.dropdown.current_control == null ) {
			return true;
		}

		var targ  = null;
		var $targ = null;
		
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
			
		if ( !targ ) {
			return true;
		}
		
		$targ = $(targ);
		
		/*
		if ( $targ.attr('id') == $.dropdown.current_control.attr('id') ) {			
			return true;
		}
		*/
		
		if ( $targ.hasClass('submenu') || $targ.parents().hasClass('submenu') ) {
			if (!$targ.attr('collapse'))
			{
				// If clicked on the submenu itself, do not close.
				return true;				
			}			
		}
		
		collapseCurrent();
		
		return false;		
	}
	
	function expand(targ, opts, parentObj) {
				
		var $targ 	= $(targ);
		var item 		= $targ.parent();
		// Pointer to object containing the dropdown menu we should show.
		var submenu = (opts.submenu ? $(opts.submenu): $(".submenu", $(item)));
		
		if ( !opts.submenu ) {
			// Use default.
			submenu = $(".submenu", parentObj);
		}
		
		if ( submenu == null || item == null || $targ == null ) {
			return true;
		}
		
		if ( submenu.css('visibility') == 'visible' ) {
			// This menu is already active. Toggle. 
			collapseCurrent();
			return false;
		}
		
		collapseCurrent();

		$targ.addClass('dropdown_active');
		submenu.addClass('dropdown_active');
		
		$.dropdown.current_control = $targ;
		
		submenu.css('visibility', 'visible');
		$.dropdown.current_menu = submenu;
		
		if (opts.fitToId)
		{
			/* Huge hack to cover up an earlier hack. Turtles all the way down. */
			// $('#wrapviz').remove();
			// alert( $('#search_input').position().left );
			// $('body').append( "<div id='wrapviz' style='position: absolute; top: 10px; left: " + $('#search_input').position().left +  "px; width: " + 100 + "px; height: 3px; background-color: orange;'>&nbsp</div>" );
			if ($('#search_input').position().left > 0) {
				if ($.browser.msie) {
					submenu.css('left', $('#search_input').position().left + 10);
					submenu.css('right', '0px');
					submenu.css('top', $('#search_input').position().top + $('#search_input').height() + 24);
				} else {
					submenu.css('left', $('#search_input').position().left + 8);
					submenu.css('right', '0px');
					submenu.css('top', $('#search_input').position().top + $('#search_input').height() + 18);
				}
			} else {
				submenu.css('left', '');
				
				if ($.browser.msie) {
					submenu.css('right', '40px');
					submenu.css('top', $('#search_input').position().top + $('#search_input').height() + 10);
				} else {
					submenu.css('right', '36px');
					submenu.css('top', $('#search_input').position().top + $('#search_input').height() + 10);
				}
				
			}
			
			
		}
		
		return false;		
	};
	
	$.fn.dropdown.defaults = {
		autoShow: false,
		expandId: null,
		expandClass: null,
		submenu: null,
		fitToId: null
	};
	
	$.dropdown = new Dropdown(); // singleton instance
	$.dropdown.initialized = false;
	
})(jQuery);
