var SmallMenuItems = 0;

function LoadSiteMap() {

        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>CLENGMenu</listName> \
                            <query> \
                               <Query> \
                                  <OrderBy> \
                                     <FieldRef Name='Orden' /> \
                                  </OrderBy> \
                               </Query> \
                            </query> \
                        <viewFields> \
                            <ViewFields> \
                               <FieldRef Name='Title' /> \
                               <FieldRef Name='Padre' /> \
                               <FieldRef Name='Url' /> \
                               <FieldRef Name='Orden' /> \
                               <FieldRef Name='ID' /> \
                           </ViewFields> \
                        </viewFields> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";


        jQuery.ajax({

            url: "/eng/_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResultSiteMap,
            contentType: "text/xml; charset=\"utf-8\""
        });

    }


function processResultSiteMap(xData, status) {

          var itemCount = 0;
          var divHtml = "";
		  //SmallMenuResultCount(xData, status);
			
          $(xData.responseXML).find("z\\:row").each(function() {
            var title = $(this).attr("ows_Title");
            var IdItem = $(this).attr("ows_ID");
            var urlSite = $(this).attr("ows_Url");
            //alert(urlSite);
            var padre = $(this).attr("ows_Padre");
            //alert("Menu " + title + " - padre:" + padre );
            if (padre == undefined)
               padre = "";
               
            if (padre == "0;#")
               padre = "";
   
            if (padre == ""){ 
               //alert("Menu: " + title + " - " + IdItem);
               itemCount++;
               divHtml = divHtml + "<div id='bloque'> <ul id='sitemap_detalle2'>";
               /*urlSite = urlSite.substring(urlSite.indexOf('<div>')+5,urlSite.indexOf('</div>'));*/
               divHtml = divHtml + " <li class='nombre_site'><a href='" + urlSite + "'>" + title + "</a></li>";
				 $(xData.responseXML).find("z\\:row").each(function() {

		            var padresub = $(this).attr("ows_Padre");
		            var urlSite2 = $(this).attr("ows_Url");
 		            var title2 = $(this).attr("ows_Title");				 
		            
		            if (padresub == undefined)
		               padresub = "";
		               
		            if (padresub != "")
		            padresub = padresub.substring(0,padresub.indexOf(';#'));
		            //alert(IdItem + " padre:" + padresub );
               			if(padresub==IdItem){
               			    /*urlSite2 = urlSite2.substring(urlSite2.indexOf('<div>')+5,urlSite2.indexOf('</div>'));*/
		       	     		divHtml = divHtml + " <li class='items' style='width:170px'><a href='" + urlSite2 + "'>" + title2 + "</a></li>";
		            	}
		
		        });

				 //alert(divHtml);            
	               
				divHtml = divHtml + "</ul></div>";
            }

        });  
            
        //alert(divHtml);   
        $("#sitemap").append(divHtml);

    }
    
    
function SiteMapResultCount(xData, status) {
          $(xData.responseXML).find("z\\:row").each(function() {
            var title = $(this).attr("ows_Title");
            
            if (title == undefined)
               title = "";
               
            if (title != "")
            	SmallMenuItems++;

        });
    }
    

