/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
$.fn.droppy = function(options) {
    
  options = $.extend({speed: 250}, options || {});
  imgsrc = $(this).children("a").children("img").attr("src");
  
  this.each(function() {
    
    var root = this, zIndex = 1000;
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
    	var subnav = getSubnav(this);
    	/*
    	 * Moved from below setTimeout(function() {
    	 */
    	var imgsrc = $(this).children("a").children("img").attr("id");
		$(this).removeClass('hover');
		$('> a', this).removeClass('hover');
		
		/* this will skip current page image */
		var check_current = $(this).children("a").attr("class");
		if(check_current!=undefined)
		{
			matches = check_current.match(/current_active/);		
			if (!matches) 
			{
				$("#"+imgsrc).attr("src","http://mosap.s3.amazonaws.com/_templates/mosap/img/menu/"+imgsrc+".gif");			
			}
		}
		
    	/*
    	 * End moved chunk
    	 */
		
    	if (!subnav) return;
    	$.data(subnav, 'cancelHide', false);
    	setTimeout(function() {
    		if (!$.data(subnav, 'cancelHide')) 
    		{
    			//$(subnav).slideUp(options.speed);
    			$(subnav).hide();
    		}
    	},5);
    }
  
    function show() 
    {
    	var subnav = getSubnav(this);
    	/*
    	 * The swap was moved from below if (this.nodeName.toLowerCase() == 'ul') because we need it even if there is no subnav
    	 */
    	var imgsrc = $(this).children("a").children("img").attr("id");
		$(this).addClass('hover');
		$('> a', this).addClass('hover');
		$("#"+imgsrc).attr("src","http://mosap.s3.amazonaws.com/_templates/mosap/img/menu/"+imgsrc+"_active.gif");
    	/*
    	 * End moved chunk
    	 */
    	if (!subnav) return;
    	$.data(subnav, 'cancelHide', true);
      
    	//$(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
    	$(subnav).css({zIndex: zIndex++}).show();
      
    	if (this.nodeName.toLowerCase() == 'ul') 
    	{
    		var li = getActuator(this);
    		$(li).addClass('hover');
    		$('> a', li).addClass('hover');
    	}
	}
    
    function hideNot()
    {
    	
    }
    
    $('ul, li', this).hover(show, hideNot);
    $('li', this).hover(show, hide);
    			
    	//function(){
    	//	alert($(this).parent("li").children("a").children("img").attr("id"));
    	//	var imgsrc = $(this).children("a").children("img").attr("id");
		//	$(this).addClass('hover');
		//	$('> a', this).addClass('hover');
		//	$("#"+imgsrc).attr("src","/_templates/mosap/img/menu/"+imgsrc+"_active.gif");
		//},
		//function(){
		//	var imgsrc = $(this).children("a").children("img").attr("id");
		//	$(this).removeClass('hover');
		//	$('> a', this).removeClass('hover');
		//	$("#"+imgsrc).attr("src","/_templates/mosap/img/menu/"+imgsrc+".gif");
		//}
   // );
    
  });
  
};
