// Aon submenu
// Created by Juda van Mourik, 2008
// Only to be used in Aon Products


function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}

// options are not implemented in this release
jQuery.fn.aonsubmenu = function(options) {
		
	this.find("li").each(function() {
					
			// Get target
			var loc, id;
			loc = new String(trim($(this).html()));
			var locations = loc.split("/");
			var overruleId = $(this).attr("id");

			// Generate class
			var bodyid = $("body").attr("id");
			if(loc.substring(loc.length - 1) == "/") id = locations[locations.length - 2];
			else id = locations[locations.length - 1];

			if(overruleId != null && overruleId != "") id = overruleId;

			// IE Fix :) 
			id = trim(id);
			
			// If last part of bodyId starts with the link of this submenu, it must be made active.
			var offset = bodyid.length - id.length;
			if((offset >= 0) && (bodyid.lastIndexOf(id) === offset)) {; // endsWith implementation
				$(this).addClass("submenuitem_active");
			}
			
			// Generate html
			$(this).html("<a href='" + loc + "'>" + $(this).attr("title") + "</a>");
		});
}
