

$(document).ready(function () {

    /************************************************
    /												/
    /	Funções Padrões -> Com a biblioteca JQuery	/
    /	Por Diego Marques							/
    /	Agencia DMK - 2007							/
    /												/
    ************************************************/

    /* FUNÇÃO PARA DEBUG
    $.log('minha mensagem');
    OU
    $.log(objeto)
    */
    $.log = function (coisa) { if (window.console) { console.log(coisa); } else { a(coisa); } }

    /*
    Mostra um Alert
    */
    function a(x) { alert(x); }

    /*
    "abrirRel"
    Acressenta a classe "abrirRel" no link e no
    campo rel o elemento alvo.
		
    Seletores: '#' -> por id
    '.' -> por classe
		
    Ex.: <a href="javascript:void(0);" title="Saiba Mais" class="abrirRel" rel="#solucoes">Saiba mais</a>
    */
    $(".abrirRel").click(function () {
        $($(this).attr("rel")).slideToggle("fast");
    });

    /*
    "treeMenu"
    Menu de abertura para o próximo elemento com a classe .hide
		
    Ex.: <a href="javascript:void(0);" title="Sobre o Estúdio" class="destaque treeMenu">Sobre o Estúdio</a>
    <ul class="hide"> (...)
    */
    $(".treeMenu").toggle(function () {
        $(this).addClass("mouse_over").next(".hide").slideDown("fast");
    }, function () {
        $(this).removeClass("mouse_over").next(".hide").slideUp("fast");
    });

    ///////////////////////////////////////* Funções Específicas *////////////////////////////////////////

    /********************************** Div Flutuante do Twitter **********************************/
    $(document).ready(function () {
        //Hide (Collapse) the toggle containers on load
        $("#slide-twitter").show();

        //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
        $("#abre-twit").click(function () {
            $(this).toggleClass("fecha-twit").next().animate({ width: 'toggle' });

            return false; //Prevent the browser jump to the link anchor
        });
    });


    /********************************** Máscaras **********************************/
    $("#news_niver").maskedinput("99/99/9999");

    /********************************** Nossa Equipe **********************************/
    $.abrir_perfil = function (este, aberto) {
        $(este).addClass("desabilitado");
        abrir = $(este).parent().siblings(".perfil");
        $(abrir).addClass("perfil_visual").addClass(aberto);
        $(este).parent().siblings(".perfil").children("div").hide().siblings("." + esq_dir).show();
        if (esq_dir == "esq") { $(abrir).prev().addClass("suporte_dir").prev().addClass("ativo_esq"); } else { $(abrir).prev().addClass("ativo_dir").prev().addClass("suporte_esq"); }
        $(abrir).slideDown("fast");
    }
    $.fechar_perfil = function (este, abrir_novo) {
        $(este).parent().slideUp("fast", function () {
            $(este).parent().parent().find(".desabilitado").removeClass("desabilitado");
            $(este).parent().attr("class", "").addClass("perfil").prev().removeClass("ativo_dir").removeClass("suporte_dir").prev().removeClass("suporte_esq").removeClass("ativo_esq");
            if (abrir_novo) { $.abrir_perfil(abrir_novo, aberto); }
        });
    }
    $(".perfil_bt").click(function () {
        aberto = $(this).attr("rel");
        esq_dir = aberto.split("_");
        esq_dir = esq_dir[1];
        if ($(this).parent().siblings(".perfil").attr("class").indexOf("perfil_visual") == -1) {
            $.abrir_perfil(this, aberto);
        } else {
            perfil_aberto = $(this).parent().siblings(".perfil").attr("class");
            if (perfil_aberto.indexOf(aberto) == -1) {
                fechar = $(this).parent().siblings(".perfil").children(".fechar");
                $.fechar_perfil(fechar, this);
            }
        }
    });
    $(".perfil").children(".fechar").click(function () { $.fechar_perfil(this); });

    // Desabilitar botão direito //
    function desabilitar() { return false; }
    //	document.oncontextmenu=desabilitar ;

    /********************************* Abrir Janela **********************************/

    $("#bloqueio").css({ opacity: 0.8 }); // Define a opacidade da div de bloqueio

    fechaPopup = function () {
        $("#popup_conteudo_interno").css("opacity", 1);
        $("#popup").fadeOut("slow", function () {
            $("#bloqueio").fadeOut("slow");
        });
    } // Função para fechar popup

    $("#popup").children(".fechar").click(function () { fechaPopup(); }); // Adiciona evento ao botão de fechar a popup

    abreJanela = function (titulo, execFuncao) {
        $("#popup_conteudo").children("h2").html(titulo);
        $("#popup_conteudo_interno").css("opacity", 1);
        scroll(0, 0);
        $("#bloqueio").height($(document).height() + 70).fadeIn("slow", function () {
            $("#carregando").fadeIn("fast", function () {
                execFuncao();
            });
        });
    }
    fimAbreJanela = function () {
        $("#popup_conteudo_interno").css("opacity", 1);
        $("#carregando").fadeOut("fast", function () {
            $("#popup").fadeIn("slow");
        });
    }

    mudaConteudoJanela = function (execFuncao) {
        $("#popup_conteudo_interno").fadeTo("slow", 0.1, function () {
            $("#popup_conteudo_interno").slideUp("slow").children("div").addClass("invisivel").parent().css({ opacity: 0.999 });
            $("#carregando").addClass("carregando_preto").fadeIn(1000, function () {
                execFuncao();
            });
        });
    }
    fimMudaConteudoJanela = function () {
        $("#carregando").fadeOut("slow", function () {
            $(this).removeClass("carregando_preto");
            $("#popup_conteudo_interno").slideDown("slow", function () {
                $("#popup_conteudo_interno").children("div").css("overflow", "visible");
                $(this).children("div").removeClass("invisivel").fadeIn("slow"); //.children("img").css({opacity:0.5,visibility:"visible"})
                $("#popup_conteudo_interno").css("opacity", 1);
            }).children("div").addClass("invisivel");
        });
    }

    /********************************* Abrir Popup da Equipe da Index **********************************/

    $(".abrePopupEquipeRel").hover(function () {
        alvo = $(this).attr("rel");
        $("#nossa_equipe_links_janela").children("." + alvo).fadeIn("fast"); //.show();
    }, function () {
        alvo = $(this).attr("rel");
        $("#nossa_equipe_links_janela").children("." + alvo).fadeOut("fast"); //.hide();
    });

    /********************************* Abrir Foto / Album **********************************/

    /*$(".abrirFoto").click( function(){
    var titulo = this.title || this.name || null;
    var grupo = this.rel || false;
    montaPopupAbrirFoto(titulo,this.href,grupo);
    return false;
    });
	
    function montaPopupAbrirFoto(titulo,caminhoHref,grupo){
		
    var i, totalImagens, imagemAberta;
    var imagensAlvo = new Array();
		
    $.log(titulo+","+caminhoHref+","+grupo);
		
    imagensAlvo = $("a[@rel="+grupo+"]").get();
    totalImagens = imagensAlvo.length;
		
    for(i=0; i<totalImagens; i++){
    if(imagensAlvo[i].href == caminhoHref){
    imagemAberta = i;
    }
    }
		
    $.log((imagemAberta+1)+"/"+totalImagens);
		
    abreJanela(grupo, function(){
    ajax('abrirFoto','popup_conteudo_interno',true,false,'','', function(){
    configuraPopupAbrirFoto(totalImagens, imagemAberta, imagensAlvo);
    fimAbreJanela();
    });
    });
    }
	
    function configuraPopupAbrirFoto(totalImagens, imagemAberta, imagensAlvo){
    $.log("totalImagens:"+totalImagens+", imagemAberta:"+imagemAberta+", imagensAlvo:"+imagensAlvo[2]);
		
    }
    */

    $(".abrirFoto").click(function () {
        hrefImg = $(this).attr("href");
        nomeJanela = $("#migalha_de_pao").next().html();
        abreJanela(nomeJanela, function () { ajax('abrirFotoIndividual', 'popup_conteudo_interno', true, false, '', 'hrefImg=' + hrefImg, function () { fimAbreJanela(); }); });
        return false;
    });

    // Botão Fale Conosco //
    $(".bt_fale_conosco").click(function () {
        abreJanela('Fale Conosco', function () { ajax('faleConosco', 'popup_conteudo_interno', true, false, '', '', function () { fimAbreJanela(); }); });
    });

    // Depoimentos //
    $(".lernaintegra").click(function () {
        objeto = $(this).parent().parent().next().children(".resumo");
        classe = objeto.attr("class");
        if (classe.indexOf("aberto") == -1) {
            objeto.slideUp("fast", function () {
                $(this).addClass("aberto").next().slideDown("fast", function () {
                    objeto.parent().next().children(".resumo").slideUp("fast", function () {
                        $(this).addClass("aberto").next().slideDown("fast");
                    });
                });
            });
        }
    });
    $(".pergunta").children(".completo").children(".fechar").click(function () {
        objeto = $(this).parent();
        classe = objeto.attr("class");
        objeto.slideUp("fast", function () {
            $(this).prev().removeClass("aberto").slideDown("fast");
        });
    });
    $(".resposta").children(".completo").children(".fechar").click(function () {
        objeto = $(this).parent().parent().prev().children(".resumo");
        classe = objeto.attr("class");
        if (classe.indexOf("aberto") != -1) {
            objeto.next().parent().next().children(".resumo").next().slideUp("fast", function () {
                $(this).prev().removeClass("aberto").slideDown("fast", function () {
                    objeto.next().removeClass("aberto").slideUp("fast", function () {
                        $(this).prev().slideDown("fast").removeClass("aberto");
                    });
                });
            });
        }
    });

    // zeraValor //
    $(".zeraValor").addClass("ZVvazio");

    $(".zeraValor").focus(function () {
        classes = $(this).attr("class");
        if (classes.indexOf("ZVvazio") != -1) {
            $(this).val("").removeClass("ZVvazio");
        }
    });

    $(".zeraValor").blur(function () {
        valor = $(this).attr("title");
        if ($(this).val() == "") {
            $(this).val(valor).addClass("ZVvazio");
        }
    });

    limpaForm = function (formularioAlvo, callback) {
        $("#" + formularioAlvo + " .ZVvazio").each(function (i) {
            this.value = "";
        });
        callback();
        $("#" + formularioAlvo + " .ZVvazio").each(function (i) {
            valorTitle = $(this).attr("title");
            $(this).val(valorTitle);
        });
    }

    // Abrir SWF //
    $(".abrirSWF").click(function () {
        dados_abre_swf = $(this).attr("rel");
        dados_abre_swf = dados_abre_swf.split("|");
        nomeJanela = $(this).html();
        abreJanela('Onde Estamos', function () { ajax('abrirSWF', 'popup_conteudo_interno', true, false, '', '', function () { fimAbreJanela(); removecontornoflash('swfAberto', '', 'swfAberto', dados_abre_swf[1], dados_abre_swf[2], './', dados_abre_swf[0], dados_abre_swf[3]); }); });
        return false;
    });

    // Abre vale presente //
    $(".abreImgValePresente").click(function () {
        $("#bloqueio").height($(document).height() + 70).fadeIn("slow", function () {
            $('body').append('<img src=\'imagens/vale-presente-bg.jpg\' class=\'fechaImgValePresente\' alt=\'Vale Presente\' border=\'0\' style=\'position:absolute; left:50%; top:100px; margin-left:-410px; z-index:999; display:none; cursor: pointer;\' />');
            $(".fechaImgValePresente").fadeIn("slow").click(function () {
                $(this).fadeOut("slow", function () {
                    $(this).remove();
                    $("#bloqueio").fadeOut("slow");
                });
            });
        });
    });


    /*///////////////////
    ImageBox	 ///
    /////////////////*/


    i = 1
    $(".album_aberto li a").each(function () {
        $(this).attr("ordem2", i);
        i += 1;
    });


    $(".album_aberto li a").click(function () {

        obj = $(this).parents("ul.album_aberto");
        pos = parseInt($(this).attr("ordem2"));
        var src = $(this).attr("rel");
        var img = new Image();
        var altura = 0;
        var largura = 0;
        var tituloAlbum = $(this).parents("ul:eq(0)").attr("title");

        $("#imageBox .imagem a").css("visibility", "visible")

        if (pos == 1) {
            $("#imageBox .anterior").css("visibility", "hidden");
        }
        if (pos == 35) {
            $("#imageBox .proximo").css("visibility", "hidden");
        }

        $(".carregando").show();

        img.onload = function () {
            altura = img.height;
            largura = img.width;
            larguraJ = $(window).width();
            novaLargura = parseInt((larguraJ - largura) / 2);
            topS = $(window).scrollTop() + 30;

            $("#imageBox").css({
                width: largura + 4 + "px",
                left: novaLargura + "px",
                minHeight: altura + 40 + "px",
                top: topS + "px"
            });

            $("img.ampliada").attr("src", img.src);

            Top = parseInt((altura - 37) / 2);
            $("#imageBox .imagem a").css("top", Top);
            $(".carregando").css({ height: altura + "px", width: largura + "px" }).hide();
            img = null;
        };

        img.oneerror = function () { alert("erro"); };

        img.src = src;

        conta = $(this).attr("ordem") + " de " + $(this).parents("ul:eq(0)").attr("id");

        tituloImg = typeof ($(this).attr("title")) == "undefined" ? "" : $(this).attr("title");
        linkname = typeof ($(this).attr("name")) == "undefined" ? "" : $(this).attr("name");
        metaTITULO = typeof ($(this).attr("metaTITULO")) == "undefined" ? "" : $(this).attr("metaTITULO");
        metaDESCRICAO = typeof ($(this).attr("metaDESCRICAO")) == "undefined" ? "" : $(this).attr("metaDESCRICAO");
        //	alert(enderecoLink);
        /*
        enderecoLink='<div style="float:right;"> <div class="addthis_toolbox addthis_default_style" style="text-align:right;" addthis:url="'+linkname+'" addthis:title="'+metaTITULO+'" addthis:description="'+metaDESCRICAO+'"> <a href="https://www.addthis.com/bookmark.php?v=250&amp;username=xa-4c7534b5722fe636" class="addthis_button_compact" addthis:url="'+linkname+'" addthis:title=""'+metaTITULO+'" addthis:description="'+metaDESCRICAO+'">Compartilhe esta foto</a> <span class="addthis_separator">|</span> <a class="addthis_button_facebook"></a> <a class="addthis_button_google"></a> <a class="addthis_button_twitter"></a> <g:plusone size="medium" count="false"></g:plusone> </div> <script type="text/javascript" src="https://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c7534b5722fe636"> </script> </div>'
	
        */

        if (linkname == ""){
            $(".enderecoLink").html('');
        }
        else {
            $(".enderecoLink").html('<iframe frameborder="0" scrolling="no" style="border:none; background-color:#FFF9ED !important; height:32px; position:relative; float:right; widht:300px;" width="300px" height="32px" src=http://www.tattooyes.com.br/teste2.asp?url="' + linkname + '"></iframe>');
        }

        $(".tituloImg").html(tituloImg);
        $(".tituloAlbum").html(tituloAlbum);
        $(".contImg").html(conta);
        /*
        $(".metaURL").html(metaURL);
        $(".metaTITULO").html(metaTITULO);
        $(".metaDESCRICAO").html(metaDESCRICAO);
        */
        //	$(".enderecoLink").html(enderecoLink);
        //alert(enderecoLink)
        $("#imageBox .anterior").attr("rel", parseInt(pos) - 1);
        $("#imageBox .proximo").attr("rel", parseInt(pos) + 1);

        $("#bloqueio").height($(document).height() + 70).fadeIn("slow", function () {
            $("#imageBox").fadeIn("fast");
        });

    });

    $("#imageBox .mudarImg").click(function () {

        $(".album_aberto li a[@ordem2='" + $(this).attr("rel") + "']").click();

    });


    $("#imageBox a.fechar").click(function () {

        $("#imageBox").fadeOut(function () {
            $("#bloqueio").fadeOut("fast");
        });


    });

    $(window).scroll(function () {
        topS = $(window).scrollTop() + 30;
        $("#imageBox").css({ top: topS + "px" });
    });


    //otimizacao
    $('a, form').attr('target', '_parent');

    $("#ricthmedia").appendTo("body");
});




