﻿var botaook = 0;
var handler = function () {
    if ($('td#conteudotemplate').is(':visible')) {
        $('td#conteudotemplate').fadeOut('fast');
    } else {
        $('td#conteudotemplate').fadeIn('slow');
    }
};

if ("onhashchange" in window) { // event supported?
    window.onhashchange = function () {
        hashChanged(window.location.hash);
    }
}
else { // event not supported:
    var storedHash = window.location.hash;
    window.setInterval(function () {
        if (window.location.hash != storedHash) {
            storedHash = window.location.hash;
            hashChanged(storedHash);
        }
    }, 100);
}

function hashChanged(newhash) {
    // codigo que pega o email informado na url e ja abre o status
    var url = newhash;
    var indiceinicio = url.indexOf('#');
    var emailjainformado = null;

    if (indiceinicio > -1) {
        emailjainformado = url.slice(indiceinicio + 1);
        $('#emailusuario').val(emailjainformado);

        if (botaook == 0)
            $('#btnok').click();

        botaook = 0;
    }
}

$(document).ready(function () {
    $('td#conteudotemplatefaq').load('/content/perguntas_frequentes.html');
    $('td#conteudotemplatefaq').hide();

});

$(document).ready(function () {
    $('#carregando').load('/content/carregando.html');
    $('#carregando').hide();

    //Inicia AJAX
    $('#carregando').ajaxStart(function () {
        $(this).show();
        $('#btrastreamento').unbind('click');
    });

    $('#carregando').ajaxStop(function () {
        $(this).hide();
    });

    // oculta a célula que mostra o status do pedido
    $('td#conteudotemplate').hide();

    $('#emailusuario').keypress(function (event) {
        if (event.which == '13') {
            $('#btnok').click();
        }
    });

    // define o que é executado quando clicar no botão de OK
    $('#btnok').click(function () {
        $('td#conteudotemplate').hide();
        var emailusuario = $('#emailusuario').val();

        botaook = 1;
        $.get("/home/acompanhe/" + emailusuario, {}, function (retorno) {
            $('td#conteudotemplate').html(retorno);
            $('td#conteudotemplate').fadeIn('slow');
            $('#emailusuario').focus();
            $('#emailusuario').select();

            var novaurl = document.URL;
            var indiceultimabarra = novaurl.lastIndexOf('/');
            novaurl = novaurl.slice(0, indiceultimabarra + 1);
            if ((!emailusuario == "Informe seu e-mail aqui") || (!emailusuario == "") || (!emailusuario == " ")) {
                window.location.href = novaurl + '#' + emailusuario;
            }

			//11:49 14/07/2011
			$('#tabelafeedback #trfeedback1').find('input#nope').bind('click', function() {
				$('.TxtAtendimento .textField').tipsy({gravity: 'e'});
				$('.TxtAtendimento .textField').fadeIn(1000);
				$('.TxtAtendimento #enviar').fadeIn(1000);
			});
			
			//11:49 14/07/2011
			$('#tabelafeedback > *').parent().fadeIn(1000);

            $('#btrastreamento').bind('click', handler);

        });
    });

    // codigo que pega o email informado na url e ja abre o status
    var url = document.URL;
    var indiceinicio = url.indexOf('#');
    var emailjainformado = null;

    if (indiceinicio > 0) {
        $('#emailusuario').focus();
        $('#emailusuario').select();
        emailjainformado = url.slice(indiceinicio + 1);
        $('#emailusuario').val(emailjainformado);
        $('#btnok').click();
    }
});

//08:08am 05/05/2011 - James
$(document).ready(function () {
    // Perguntas frequentes
    $('#btfaq').click(function () {
        if ($('td#conteudotemplatefaq').is(':visible')) {
            $('td#conteudotemplatefaq').fadeOut('fast', function () { $(this).hide(); });
        } else {
            //Esconde institucional
            if ($('td#conteudotemplateinstitucional').is(':visible')) {
                $('td#conteudotemplateinstitucional').fadeOut('fast', function () { $(this).hide(); });
            }

            //Esconde Trocas
            if ($('td#conteudotemplateptrocas').is(':visible')) {
                $('td#conteudotemplateptrocas').fadeOut('fast', function () { $(this).hide(); });
            }

            //Mostra perguntas frequentes
            if ($('td#conteudotemplatefaq').is(':hidden')) {
                $('td#conteudotemplatefaq').fadeIn('slow');
            }
        }
    });

    //Política de trocas
    $('#btptrocas').click(function () {
        if ($('td#conteudotemplateptrocas').is(':visible')) {
            $('td#conteudotemplateptrocas').fadeOut('fast', function () { $(this).hide(); });
        } else {
            //Esconde institucional
            if ($('td#conteudotemplateinstitucional').is(':visible')) {
                $('td#conteudotemplateinstitucional').fadeOut('fast', function () { $(this).hide(); });
            }

            //Esconde Perguntas frequentes
            if ($('td#conteudotemplatefaq').is(':visible')) {
                $('td#conteudotemplatefaq').fadeOut('fast', function () { $(this).hide(); });
            }

            //Mostra Política de trocas
            $('td#conteudotemplateptrocas').fadeIn('slow');
        }
    });

    //Institucional
    $('#btinstitucional').click(function () {
        if ($('td#conteudotemplateinstitucional').is(':visible')) {
            $('td#conteudotemplateinstitucional').fadeOut('fast', function () { $(this).hide(); });
        } else {
            //Esconde Política de trocas
            if ($('td#conteudotemplateptrocas').is(':visible')) {
                $('td#conteudotemplateptrocas').fadeOut('fast', function () { $(this).hide(); });
            }

            //Esconde Perguntas frequentes
            if ($('td#conteudotemplatefaq').is(':visible')) {
                $('td#conteudotemplatefaq').fadeOut('fast', function () { $(this).hide(); });
            }

            //Mostra Institucional
            $('td#conteudotemplateinstitucional').fadeIn('slow');
        }
    });

});
