﻿function HeaderSacolaCount() {
    var urlbase = GetUrlBaseParent() + "header/Header_sacola.ashx";
    $.ajax({
        type: "POST",
        url: urlbase,
        data : {},
        dataType: "text",
        success: function(msg, status) {
            
            if (parent.frSacolaRoda.document.getElementById(    "sp_rodape_itens_sacola_logado") == null)
                setTimeout('RodapeSacolaCount()', 1000);
            else
                parent.frSacolaRoda.document.getElementById("sp_rodape_itens_sacola_logado").innerHTML = msg;
            
            if (parent.frSacolaRodaNao.document.getElementById("sp_rodape_itens_sacola_Nao_logado") == null)
                setTimeout('RodapeSacolaCount()', 1000);
            else
                parent.frSacolaRodaNao.document.getElementById("sp_rodape_itens_sacola_Nao_logado").innerHTML = msg;
            
            if (parent.frSacola.document.getElementById("sp_header_itens_sacola") == null)
                setTimeout('RodapeSacolaCount()', 1000);
            else
                parent.frSacola.document.getElementById("sp_header_itens_sacola").innerHTML = msg;
            
        },
        error: function(xhr, msg, e) {
            if (parent.frSacolaRoda.document.getElementById("sp_rodape_itens_sacola_logado") == null)
                setTimeout('RodapeSacolaCount()', 1000);
            else
                parent.frSacolaRoda.document.getElementById("sp_rodape_itens_sacola_logado").innerHTML = "0";
            
            if (parent.frSacolaRodaNao.document.getElementById("sp_rodape_itens_sacola_Nao_logado") == null)
                setTimeout('RodapeSacolaCount()', 1000);
            else
                parent.frSacolaRodaNao.document.getElementById("sp_rodape_itens_sacola_Nao_logado").innerHTML = "0";
            
            if (parent.frSacola.document.getElementById("sp_header_itens_sacola") == null)
                setTimeout('RodapeSacolaCount()', 1000);
            else
                parent.frSacola.document.getElementById("sp_header_itens_sacola").innerHTML = "0";
        }
    });

}

function AtualizarBoxSacola(recursivo) 
{ 
        var urlbase =GetUrlBase() + "header/AtualizarBoxSacola.ashx";
    $.ajax({
        type: "POST",
        url: urlbase,
        data : {},
        dataType: "text",
        success: function(msg, status) {
       
            var data_msg = msg.split("(¨)");            
            
            for(var i = 0; i < data_msg.length; i++){                
            
                if(data_msg[i].indexOf("qtd_itens[¨]") > -1){
                    //alert("<strong>"+data_msg[i].replace("qtd_itens[¨]","")+"</strong> itens");
                    $("#ctl00_Header_hlkQtdItens").html("<strong>"+data_msg[i].replace("qtd_itens[¨]","")+"</strong> itens");
                }else if(data_msg[i].indexOf("nome_produto[¨]") > -1){                
                    $("#ctl00_Header_hlkUltimoProduto").html(data_msg[i].replace("nome_produto[¨]",""));
                    $("#ctl00_Header_hlkUltimoProduto").get(0).title = data_msg[i].replace("nome_produto[¨]","");
                    $("#ctl00_Header_imgUltimoProduto").get(0).title = data_msg[i].replace("nome_produto[¨]","");                    
                }
                else if(data_msg[i].indexOf("link_produto_desc[¨]") > -1){                    
                    $("#ctl00_Header_hlkImgUltimoProduto").get(0).href = data_msg[i].replace("link_produto_desc[¨]","");
                    $("#ctl00_Header_hlkImgUltimoProduto").click(function(){
                        document.location.href = $("#ctl00_Header_hlkImgUltimoProduto").get(0).href
                    });                   
                    
                    $("#ctl00_Header_hlkUltimoProduto").get(0).href = data_msg[i].replace("link_produto_desc[¨]","");
//                    $("#ctl00_Header_hlkUltimoProduto").click(function(){
//                        document.location.href = $("#ctl00_Header_hlkUltimoProduto").get(0).href;
//                    });
//                    
//                    $("#ctl00_Header_hlkFinalizarCompra2").click(function(){
//                        document.location.href = $("#ctl00_Header_hlkFinalizarCompra2").get(0).href;                        
//                    });
//                    
//                    $("#ctl00_Header_hlkQtdItens").click(function(){
//                        document.location.href = $("#ctl00_Header_hlkQtdItens").get(0).href;                        
//                    });                                 
                }                
                else if(data_msg[i].indexOf("url_imagem[¨]") > -1){                                
                    $("#ctl00_Header_imgUltimoProduto").get(0).src = data_msg[i].replace("url_imagem[¨]","");                                    
                    //alert(data_msg[i].replace("url_imagem[¨]","") + '<%"?v=" + (DateTime.Now.Second + DateTime.Now.Millisecond).toString();%>');
                }            
                else if(data_msg[i].indexOf("preco_por[¨]") > -1){
                    $("#ctl00_Header_ltrPreco").html("R$ " + data_msg[i].replace("preco_por[¨]",""));                
                }            
                else if(data_msg[i].indexOf("subtotal[¨]") > -1){                    
                    $("#ctl00_Header_ltrTotal").html("VALOR TOTAL: R$ " + data_msg[i].replace("subtotal[¨]",""));                                    
                }else{
                    //alert("nenhuma tag encontrada: " + data_msg[i]);
                } 
            } 
            
            if(recursivo != null && recursivo != undefined){
                if(recursivo > 0){
                    recursivo--;                    
                    AtualizarBoxSacola(recursivo);
                }
            }
            
        },
        error: function(xhr, msg, e) {
            //se tiver erro nao faz nada.
            /*            
            $("#sp_rodape_itens_sacola_logado").html("0");
            $("#sp_rodape_itens_sacola_Nao_logado").html("0");
            $("#sp_header_itens_sacola").html("0");
            */
        }
    });
}

//flag global para controle da abertura/fechamento do box de detalhes da sacola.
var boxAberto = false;

function fecharBoxSacola(){
    //alert(boxAberto);
    if(boxAberto){
        //$(".itens").css('display', 'none');
        $(".itens").click();
        boxAberto = false;
    }
}
function atualizaCarrinhoCompra() {
    if (navigator.appName == "Microsoft Internet Explorer")
        window.setTimeout("document.location.href = 'CarrinhoCompras.aspx';", 100);
    else
        window.setTimeout("document.location.href = 'Carrinho/CarrinhoCompras.aspx';", 100);
}
function abrirBoxSacola(closeWithDelay){    
    boxAberto = true;
    $(document).scrollTop(0);
    $(".itens").click();    
     if(closeWithDelay != undefined && closeWithDelay != null){    
        window.setTimeout("fecharBoxSacola();", closeWithDelay);        
    }
}


$("#ctl00_Header_hlkQtdItens").click(function(){
    document.location.href = $("#ctl00_Header_hlkQtdItens").get(0).href;
});

$("#ctl00_Header_hlkUltimoProduto").click(function(){
    document.location.href = $("#ctl00_Header_hlkUltimoProduto").get(0).href;
});

$("#ctl00_Header_hlkFinalizarCompra").click(function(){
    document.location.href = $("#ctl00_Header_hlkFinalizarCompra").get(0).href;                        
});

$("#ctl00_Header_hlkFinalizarCompra2").click(function(){
    document.location.href = $("#ctl00_Header_hlkFinalizarCompra2").get(0).href;                        
});

$("#ctl00_Header_hlkQtdItens").click(function(){
    document.location.href = $("#ctl00_Header_hlkQtdItens").get(0).href;                        
}); 
