var startList = function() {
  $('#nav li').bind('mouseover', function() { 
    this.className += ' over'; 
  });
  $('#nav li').bind('mouseout', function() { 
    this.className.replace(' over', ''); 
  });
}

var init_search_togglers = function() {
	var klass = 'search-select-options-toggler';
	var selectall = '<a href="javascript:;" class="'+klass+' select-all">Select&nbsp;All</a>';
	var selectnone = '<a href="javascript:;" class="'+klass+'">Select&nbsp;None</a><br />';
	var spacer = '&nbsp;|&nbsp;';
	if($('#ResourceCategoryResourceCategory').get(0)) {
		$('#ResourceCategoryResourceCategory').before(selectall);
		$('#ResourceCategoryResourceCategory').before(spacer);
		$('#ResourceCategoryResourceCategory').before(selectnone);
	}
	
	if($('#ResourceResourceType').get(0)) {
		$('#ResourceResourceType').before(selectall);
		$('#ResourceResourceType').before(spacer);
		$('#ResourceResourceType').before(selectnone);
	}
	
	if($('.'+klass).get(0)) {
		$('.'+klass).bind('click', function() {
			var toggle = $(this).hasClass('select-all');
			var sel = $(this).nextAll('select');
			$(sel).children('option').each(function() {
					this.selected = toggle;
			});
			$(sel).focus();
		});
	}
}

$(function() {
	init_search_togglers();
	startList();
});

// We need to do some browser sniffing to weed out IE 6 only
// because only IE6 needs this hover hack.
if (document.all && !window.opera && (navigator.appVersion.search("MSIE 6.0") != -1) && $.browser.msie) {
  function IEHoverPseudo() {
      $("#nav li").hover(function(){
          $(this).addClass("over").find("> ul").show().addShim();
        },function(){
          $(this).removeClass("over").find("> ul").removeShim().hide();
        }
      );
      // Add a hover class to all li for CSS styling. Silly naming is done
      // so we don't break CSS compatibility for .over class already in use
      // and due to the fact that IE6 doesn't understand multiple selectors.
      $("#nav li").hover(function(){
          $(this).addClass("ie-over");
        },function(){
          $(this).removeClass("ie-over");
        }
      );
    }

    // This is the jquery method of adding a function
    // to the BODY onload event.  (See jquery.com)
    $(document).ready(function(){ IEHoverPseudo() });
}

$.fn.addShim = function() {
  return this.each(function(){
	  if(document.all && $("select").size() > 0) {
	    var ifShim = document.createElement('iframe');
	    ifShim.src = "javascript:false";
			ifShim.style.width=$(this).width()+1+"px";
      ifShim.style.height=$(this).find("> li").size()*23+20+"px";
			ifShim.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
		  ifShim.style.zIndex="0";
    $(this).prepend(ifShim);
      $(this).css("zIndex","99");
		}
	});
};

$.fn.removeShim = function() {
  return this.each(function(){
	  if (document.all) $("iframe", this).remove();
	});
};
