/*!
 * Single Drop Down Menu 1.2
 * September 26, 2009
 * Corey Hart @ http://www.codenothing.com
 * modified : January 19, 2010
 * rumbero @ http://www.netcodev.de
 */
;(function($, undefined){
	// bgiframe is needed to fix z-index problem for IE6 users.
	$.fn.bgiframe = $.fn.bgiframe ? $.fn.bgiframe : $.fn.bgIframe ? $.fn.bgIframe : function(){
		// For applications that don't have bgiframe plugin installed, create a useless 
		// function that doesn't break the chain
		return this;
	};
 	// Drop Menu Plugin
	$.fn.singleDropMenu = function(options){
		return this.each(function(){
			// Default Settings
			var $obj = $(this), timer, menu,
				settings = $.extend({
					timer: 500,
					parentMO: undefined,
					childMO: undefined,
					show: 'show',
					hide: 'hide'
				}, options||{}, $.metadata ? $obj.metadata() : {});
	
			// Run Menu
			$obj.children('li').bind('mouseover.single-ddm', function(){
				// Clear any open menus
				if (menu && menu.data('single-ddm-i') != $(this).data('single-ddm-i'))
					closemenu();
				else
					menu =false;
				
				// Open nested list
				$(this).children('a').addClass(settings.parentMO).siblings('ul')[settings.show]();
				$(this).children('div.mn_activ').addClass(settings.parentMO).siblings('ul')[settings.show]();
				// Workaround change img src
				if($(this).children('a').children('img').attr('src') != null)
				$(this).children('a').children('img').attr('src',$(this).children('a').children('img').attr('src').replace(/\.gif/g,"_activ.gif"));
			}).bind('mouseout.single-ddm', function(){
				// Prevent auto close
				menu = $(this);
				timer = setTimeout(closemenu, settings.timer);
				// Workaround change img src
				if($(this).children('a').children('img').attr('src') != null)
				$(this).children('a').children('img').attr('src',$(this).children('a').children('img').attr('src').replace(/_activ\.gif/g,".gif"));
			}).bind('click.single-ddm', function(){
				// Prevent auto close
				menu = $(this);
				closemenu();
				// Workaround change img src
				if($(this).children('a').children('img').attr('src') != null)
				$(this).children('a').children('img').attr('src',$(this).children('a').children('img').attr('src').replace(/_activ\.gif/g,".gif"));
			}).each(function(i){
				// Attach indexs to each menu
				$(this).data('single-ddm-i', i);
			}).children('ul').bgiframe();
			// Dropped Menu Highlighting
			$('li > ul > li', $obj).bind('mouseover.single-ddm', function(){
				$('a', this).addClass(settings.childMO);
				if($(this).children('a').children('img').attr('src') != null)
				$(this).children('a').children('img').attr('src',$(this).children('a').children('img').attr('src').replace(/\.gif/g,"_activ.gif"));
			}).bind('mouseout.single-ddm', function(){
				$('a', this).removeClass(settings.childMO);
				if($(this).children('a').children('img').attr('src') != null)
				$(this).children('a').children('img').attr('src',$(this).children('a').children('img').attr('src').replace(/_activ\.gif/g,".gif"));
			}).bind('click.single-ddm', function(){
				closemenu();
			});
			// Closes any open menus when mouse click occurs anywhere else on the page
			$(document).click(closemenu);
			// Function to close set menu
			function closemenu(){
				if (menu && timer){
					menu.children('a').removeClass(settings.parentMO).siblings('ul')[settings.hide]();
					menu.children('div.mn_activ').removeClass(settings.parentMO).siblings('ul')[settings.hide]();
					clearTimeout(timer);
					menu = false;
				}
			}
		});
	};
})(jQuery);

