(function($) {
    
  /**
   * @author Adrian Nowicki <adrian-nowicki.com>
   * move subnav into proper parents of nav
   * overcome the difficulty of PHP template system
   */
	var
	 $parentLinks = $('#menu-top a'),
	 $childLinks = $('#menu-sub a'),
	 hrefAttr;
	 
	
	 
	$childLinks.each(function(i, childLink) {
		
		var 
		  hrefParams = [];
		hrefAttr = $(this).attr('href');
		hrefParams = hrefAttr.split('?').pop().split('&');
		$.each(hrefParams, function() {
			
			if (this.indexOf('h=') == 0) {		
				branchId = this;
				return true;
			}
		});
		
		$parentLinks.each(function() {
			
			if ($(this).attr('href').indexOf(branchId) != -1) {
				
				if ($(this).next('ul').length == 0) {
					$(this).after('<ul></ul>');
				}
				$(this).next('ul').append($(childLink).parent());
			}
		});
	}); 
  
	$('#menu-sub').remove();
		
  $('#menu-top').ptMenu();
  
})(jQuery);
