  $(document).ready(function(){
  	setupMenu();
	});
 
 function isIE()

      {
 
      	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);

      }

function setupMenu(){
	
	var docurl = document.URL;
	
	
	
	
	var xml = "" + '<menuContent>'
						+ '<menuItem name="What We Offer">'
							+	'<page name="Products &amp; Services" url="http://ppp-inc.com/productsandservices.php" />'
							+	'<page name="Production Machining" url="http://ppp-inc.com/productionmachining.php" />'
							+	'<page name="Assemblies" url="http://ppp-inc.com/assemblies.php" />'
							+	'<page name="Prototype Machining" url="http://ppp-inc.com/prototypemachining.php" />'
							+	'<page name="Quality Assurance" url="http://ppp-inc.com/qualityassurance.php" />'
						+	'</menuItem>'
						+ '<menuItem name="Facilities &amp; Equipment">'
							+	'<page name="CNC Lathes" url="http://ppp-inc.com/facilitiesandequipment_cnclathes.php" />'
							+	'<page name="CNC Vertical Machining Centers" url="http://ppp-inc.com/facilitiesandequipment_cncverticalmachiningcenters.php" />'
							+	'<page name="Thread Rolling" url="http://ppp-inc.com/facilitiesandequipment_threadrolling.php" />'
							+	'<page name="CNC Multi-Axis Mill/Turn Centers" url="http://ppp-inc.com/facilitiesandequipment_cncmultiaxismillturncenters.php" />'
							+	'<page name="CNC Swiss Turning Centers" url="http://ppp-inc.com/facilitiesandequipment_swissturningcenters.php" />'
							+	'<page name="Precision Grinding" url="http://ppp-inc.com/facilitiesandequipment_precisiongrinding.php" />'
							+	'<page name="EDM Machines" url="http://www.ppp-inc.com/facilitiesandequipment_edmMachines.php" />'
							+	'<page name="Other Equipment &amp; Processes" url="http://ppp-inc.com/facilitiesandequipment_otherequipmentandprocesses.php" />'
						+	'</menuItem>'
						+ '<menuItem name="Industries Served">'
							+	'<page name="Biomedical" url="http://ppp-inc.com/biomedical.php" />'
							+	'<page name="Aerospace" url="http://ppp-inc.com/aerospace.php" />'
							+	'<page name="Commercial" url="http://ppp-inc.com/commercial.php" />'
						+	'</menuItem>'
						+ '<menuItem name="About Us">'
							+	'<page name="Our Staff" url="http://ppp-inc.com/ourstaff.php" />'
							+	'<page name="Company History" url="http://ppp-inc.com/companyhistory.php" />'
						+	'</menuItem>'
						+ '<menuItem name="Certifications" url="http://ppp-inc.com/certifications.php">'
						+	'</menuItem>'
						+ '<menuItem name="Submit an RFQ" url="http://ppp-inc.com/submitanrfq.php">'
						+	'</menuItem>'
						+ '<menuItem name="Contact Us" url="http://ppp-inc.com/contactus.php">'
						+	'</menuItem>'
						+ '<menuItem name="Home" url="http://ppp-inc.com/index.php">'
						+	'</menuItem>'
					+	'</menuContent>';
					//alert(123);
	
	

		var $dom = $.xmlDOM(xml, function(error){
		// alert(error);
		});


	
	
	var td = $("td[background='menubg.jpg'], #menuTD");
	//alert(td.html());
	//td.html('');
	td.find('script').remove();
	td.find('object').remove();
	td.find('noscript').remove();
	//alert(td.html());
	var tables = td.html();
	//var eventTable = td.find('table:eq(0)');
	//var newsTable = td.find('table:eq(2)');
	td.css('background-image','url(images/menuBGSlice.jpg)').css('background-repeat','repeat-y').html('');
	
	$('<img src="images/menuTop.jpg" />').appendTo(td);
	//build the menu
	
	
		$dom.find('menuItem').each(function(){
		//alert(1);
		var sub = $('<div class="sublinks"></div>').hide();
		var subLinks = $('<ul></ul>').appendTo(sub);
		var main = $('<div class="menuItem"></div>');
		if ($(this).attr("url")) {
			
			if (docurl == $(this).attr("url")) {
				var mainLink = $('<a class="topLink selected" href="' + $(this).attr("url") + '">' + $(this).attr("name") + '</a>').appendTo(main);
			}
			else {
				var mainLink = $('<a class="topLink" href="' + $(this).attr("url") + '">' + $(this).attr("name") + '</a>').appendTo(main);
			}
			
		}
		else {
			
			var mainLink = $('<a class="topLink downCursor">' + $(this).attr("name") + '</a>').toggle(function(){
				sub.show();
				mainLink.css('cursor', 'n-resize');
			}, function(){
				sub.hide();
				mainLink.css('cursor', 's-resize');
			}).appendTo(main);
			
			$(this).find('page').each(function(){
			
				if (docurl == $(this).attr("url")) {
					$('<li class="menuPage"><a class="selected" href="' + $(this).attr("url") + '"><span>' + $(this).attr("name") + '</span></a></li>').appendTo(subLinks);
					sub.show();
				}
				else {
					$('<li class="menuPage"><a href="' + $(this).attr("url") + '"><span>' + $(this).attr("name") + '</span></a></li>').appendTo(subLinks);
				}
			});
			sub.appendTo(main);
		}
		
		
		main.appendTo(td);
	});
	$("<br /><br />").appendTo(td);
	$(tables).appendTo(td);

	
	//eventTable.appendTo(tables);
	//newsTable.appendTo(tables);
	
	
	var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-881749-22']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


setTimeout(checkMenuLoaded,1000);
}

function checkMenuLoaded(){
	
	if(!$('.menuItem')){
		setupMenu();
	}
}
