/********************************************************************************************************
Projeto                     =   SiteV10
Arquivo                     =   anuncie.js
Data de criacao             =   08/08/2009
********************************************************************************************************/
function Anuncie()
{
	this.UlNavegation = new String("#NavProdutos li img");
	this.keyAuth = new String();
	this.FranquiaId = new Number();
	this.DepartamentoId = new Number();
	this.NumRows = new Number();
	
	this.Initialize = function()
	{
		$().ready(function(){

			$("#NavProdutos li img").click(function(){
				Anuncie.SelectNavIten(this);
			}).mouseover(function(){
				Anuncie.OverNavIten(this, "over");
			}).mouseout(function(){
				Anuncie.OverNavIten(this, "out");
			});
		});
	}
	
	// Pega Nome da imagem
	this.GetNavName = function(pSrc)
	{
		return new String(pSrc.substring((pSrc.lastIndexOf("/")+1), pSrc.length -4));
	}
	
	// Pega Caminho da imagem
	this.GetNavPath = function(pSrc)
	{
		return new String(pSrc.substring(0, (pSrc.lastIndexOf("/")+1)));
	}
	
	// Serealiza nome da imagem
	this.SerealizeNavName = function(pName)
	{
		return (pName.lastIndexOf("-ove")!= -1 || pName.lastIndexOf("-act")!= -1)? pName.substring((pName.lastIndexOf("/")+1), pName.length -4) : pName;
	}
	
	// Altera imagem no mouseover
	this.OverNavIten = function(pObj, pEvent)
	{
		try
		{
			var sName = Anuncie.GetNavName(pObj.src);
			var sPath = Anuncie.GetNavPath(pObj.src);
			if(sName.lastIndexOf("-act") == -1){
				sName = Anuncie.SerealizeNavName(sName);
				if(pEvent == "over")sName = sName + "-ove";
				pObj.src = sPath + sName + ".gif";
			}
		}
		catch(e) 
		{
	    	//throw (e);
	    	return false;
		}
	}
	
	// Altera imagem selecionada
	this.SelectNavIten = function(pObj)
	{
		try
		{
			var sName = Anuncie.GetNavName(pObj.src);
			var sPath = Anuncie.GetNavPath(pObj.src);
			sName = Anuncie.SerealizeNavName(sName);
			$("#NavProdutos li img").each(function(){
				var sOldName = Anuncie.GetNavName(this.src);
				if (sOldName.lastIndexOf("-act") != -1){
					var newName = Anuncie.SerealizeNavName(sOldName);
					this.src = sPath + newName + ".gif"
					Anuncie.ShowNavInfo(this.alt);
				}
			});
			pObj.src = sPath + sName + "-act.gif";
			Anuncie.ShowNavInfo(pObj.alt);
		}
		catch(e) 
		{
	    	//throw (e);
	    	return false;
		}
	}
	
	// Esconde e Exibe as informacoes do produto
	this.ShowNavInfo = function(pInfoName)
	{
		try
		{
			var oInfo = $("#produto" + pInfoName);
			($(oInfo).css("display") == "block") ? $(oInfo).css("display", "none") : $(oInfo).css("display", "block");
		}
		catch(e)
		{
	    	//throw (e);
	    	return false;
		}
	}
	
	// Configura Navegacao
	this.CreatePaginacao = function(pkeyAuth, pNumRows)
	{
		this.keyAuth = pkeyAuth;
		this.NumRows = pNumRows;
		var iPageIndex = $("#resultadoPageIndex").text();
		var pageTotal = Number(Math.ceil($("#resultadoTotal").text() /  this.NumRows));
		
		if($("#resultadoTotal").text() <= 6)
		{
			$("#navAvancar").hide();
			$("#navAvancar").hide();
		}
		else
		{
			if(iPageIndex == pageTotal) $("#navAvancar").hide();
			else $("#navAvancar").show();
				
			if(iPageIndex == 1)	$("#navVoltar").hide();
		}
		$("#resultadoPaginacao").text(iPageIndex + " de " + pageTotal);
		$("#resultadoNavegacaoID").hide();

	}
	
	this.PopularResultados = function(pIdSegmento, pIdFranquia, pPageIndex, pNumRows, pFiltro,  pUrlLink)
	{
		try 
		{
            // Variavel que Recebe o LinkDefautl
            var linkDefault = new String();
            var caminhoWS = new String();
            var lData = new String();

		
            // Monta o LinkDefault com o Caminho que o Link do DHT deve Ter
            linkDefault = window.location.protocol + "//" + window.location.host + "/";
            // Seta o Caminho do WS
            caminhoWS = linkDefault + "ws/anuncio.asmx/GetHTMLResultadosAtivos";
            // Monta a String lData
            lData = "authenticationKey=" + this.keyAuth + "&pIdSegmento=" + pIdSegmento+ "&pIdFranquia=" + pIdFranquia + "&pPageIndex=" + pPageIndex + "&pNumRows=" + pNumRows + "&pFiltro=&pUrlLink=" + pUrlLink;
            // Monta o Ajax
            $.ajax({
                type: "POST",
                url: caminhoWS,
                data: lData,
                dataType: "xml",
                beforeSend: function(XMLHttpRequest) {
                    Util.showLoading();
                },
                complete: function() {
                    Util.hideLoading();
                },
                error: function(errorRequest) {
                    alert('Erro' + errorRequest);
                    Util.hideLoading();
                },
                success: function(loadedContentHTML) {
                    $(loadedContentHTML).find('ofertas').each(function() {
                        var html = $(this).text();
                        $("#ctl00_content_containerResultadoID").html(html);
                    });
                }
            });
            
            $("#resultadoNavegacaoInicioID").hide();
			$("#resultadoNavegacaoID").show();
        }
        catch (e) {
            throw (e);
            return false;
        }
	}
	
}
