﻿// Quantidade de empresas a serem exibidas por coluna
var empresasCount = 17 

// Variations
var variationLabel = location.href.split("/")[3];
var locationHref = location.href;
var variationPages = new Array();
variationPages["en"] = "Pages";
variationPages["pt"] = "Paginas";
variationPages["es"] = "Paginas";


$(document).ready(function() {
	
	/*$(".variationPagesLink").each(function() {
		$(this).attr("href", "/" + variationLabel + "/" + variationPages[variationLabel] + "/" + $(this).attr("href"));
	});
	
	$(".variationLink").each(function() {
		$(this).attr("href", "/" + variationLabel + "/" + $(this).attr("href"));
	});*/
	
	var query = "<Query> \
				   <OrderBy> \
				      <FieldRef Name='Title' Ascending='True' /> \
				   </OrderBy> \
				</Query>";
	
	var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>Empresas e links</listName> \
                        <viewFields> \
                            <ViewFields> \
                               <FieldRef Name='Title' /> \
                               <FieldRef Name='Url' /> \
                               <FieldRef Name='URL_x0020_EN' /> \
                           </ViewFields> \
                        </viewFields> \
                        <query>" + query + "</query> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";
    $.ajax({
        url: "/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
    
    // marca o link da página atual com a classe "current"
    $("#dm-menu a").each(function(index){
    	locationHref.indexOf($(this).attr("href").split("/")[$(this).attr("href").split("/").length-1]) > -1 ? $(this).addClass("current") : $(this).attr("class",$(this).attr("class"));
    });

	// evento acionado ao apertar o enter do teclado
    $("#searchkeyword").live("keydown", function(e){
		if(e.which == 13)
		{
			location.href = searchLink();
			return false;
		}
    });
    
    // evento acionado ao apertar clicar na lupa de pesquisa
    $("#searchbutton").click(function() { 
	    location.href = searchLink();
    });

});

// Gera Menu Superior
function menu(){
	
	$(".variationPagesLink").each(function() {
		$(this).attr("href", "/" + variationLabel + "/" + variationPages[variationLabel] + "/" + $(this).attr("href"));
	});
	
	$(".variationLink").each(function() {
		$(this).attr("href", "/" + variationLabel + "/" + $(this).attr("href"));
	});

}

// Funcionalidade de Pesquisa
function searchLink()
{
	return '/' + variationLabel + '/' + variationPages[variationLabel] + '/pesquisa.aspx?k=' + $('#searchkeyword').val() + '&s=' + variationLabel;
}

// Consulta de Empresas
function processResult(xData, status) {

	var xml = window.navigator.userAgent.indexOf("MSIE") > -1 ? xData.responseXML : xData.responseText;

	htmlResult = '<div class="dm-colunas"><ul>';
	
	var eCount = Math.ceil((Number($(xml).find("z\\:row").length) / 6));
	//console.log(eCount);
	//sCount = Number(String(eCount).split(".")[1].substring(0,1));
	
	empresasCount = 0; //sCount > 5 ? eCount : eCount + 1;

    $(xml).find("z\\:row").each(function(i) {

        if(i > 0 && i % eCount == 0)  //empresasCount.toFixed(0) == 0
        	htmlResult += '</ul></div><div class="dm-colunas"><ul>';
    	
    	var lang = location.href;
		if(lang.indexOf("/en/") > -1)
		{
    	htmlResult += $(this).attr("ows_URL_x0020_EN").split(",")[0].indexOf("http://#") > -1 ? "<li><a>" + $(this).attr("ows_Title") + "</a></li>" : "<li><a href='" + $(this).attr("ows_URL_x0020_EN").split(",")[0] + "' style='text-decoration:underline;'>" + $(this).attr("ows_Title") + "</a></li>";
    	}
    	else
    	{
    	htmlResult += $(this).attr("ows_Url").split(",")[0].indexOf("http://#") > -1 ? "<li><a>" + $(this).attr("ows_Title") + "</a></li>" : "<li><a href='" + $(this).attr("ows_Url").split(",")[0] + "' style='text-decoration:underline;'>" + $(this).attr("ows_Title") + "</a></li>";
    	}
 
    });
    
	htmlResult += '</ul></div>';
	
	$("#empresas").html(htmlResult);

}