$(document).ready(function () {


    /***************************************************    
    METODOS
    ***************************************************/

    $.fn.AnimarFoto = function (toPosition, duracao, after) {
        this.stop(true).animate(toPosition, { duration: duracao, complete: after });
        return this;
    }

    $.fn.HabilitarHover = function () {
        this.hover(
	        function () { $(this).AnimarFoto($(this).data("hover"), 150, null); },
	        function () { $(this).AnimarFoto($(this).data("normal"), 150, null); }
	    );
        return this;
    }

    $.fn.HabilitarClick = function () {
        this.toggle(
	        function () { $(this).unbind("mouseenter mouseleave").css('z-index', '5').AnimarFoto($(this).data("click"), 500, null); },
	        function () { $(this).AnimarFoto($(this).data("normal"), 500, function () { $(this).css('z-index', '0').HabilitarHover(); }) }
	    );
        return this;
    }

    function BuscarArtigos(artigos, pageToken) {

        if (pageToken != undefined)
            pageToken = '&pageToken=' + pageToken;
        else
            pageToken = '';

        $.ajax({
            url: "https://www.googleapis.com/blogger/v2/blogs/5935396602628454838/posts?key=AIzaSyBrj_QSReFtpKBnGUPHPWFs9mX2yEvNWC8" + pageToken,
            cache: false,
            type: "GET",
            dataType: "jsonp",
            crossDomain: true,
            success: function (objJson) {
                if (objJson.items != undefined)
                    $.merge(artigos, objJson.items);
                if (objJson.nextPageToken != undefined)
                    BuscarArtigos(artigos, objJson.nextPageToken)
                else
                    MontarArtigos(artigos);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.responseText);
            }
        });
    }

    function MontarArtigos(artigos) {

        $("#divMenuArtigos img").hide();
		
		if (artigos.length == 0)
			$("#divMenuArtigos").append("<p>&nbsp;</p><p>Nenhum artigo foi encontrado</p>");
		else
	        for (var cont in artigos) {
	
	            var artigo = artigos[cont];
	     	       var htmlMenu = $("<li id='atigo" + artigo.id + "'>" + artigo.title + "</li>").data("artigo", artigo);
	            $("#divMenuArtigos ul").append(htmlMenu);
	
	        }

        $("#divMenuArtigos ul li").click(function () {
            var artigo = $(this).data("artigo");
            var divArtigo = $("<div id='divArtigo'></div>");
            divArtigo.html("<h2>" + artigo.title + "</h2>" + artigo.content);
            $("#divConteudo").html(divArtigo);
        });

    }

    function AplicarConteudo(html, pagina, param) {

        $("#divConteudo").html(html);

        if (pagina == 'midia') {

            if (param == 'artigos') {
				
				$("#divMenuArtigos p").remove();	
                $("#divMenuArtigos ul").html("");
                $("#divMenuArtigos img").show();

                $("#divFotos").css('display', 'none');
                $("#divStringball").css('display', 'none');
                $("#divMenuArtigos").css('display', 'block');
				$("#divConteudo").css('overflow','auto');
	
                BuscarArtigos([]);

            }
            else {
                $("#divFotos").css('display', 'none');
                $("#divStringball").css('display', 'block');
                $("#divMenuArtigos").css('display', 'none');
				$("#divConteudo").css('overflow','hidden');

                if ((param == 'videos') || (param == 'eventos') || (param == 'feiras'))
                    $("#showcase").awShowcase(
                    {
                        width: 468,
                        height: 293,
                        auto: false,
                        interval: 3000,
                        continuous: false,
                        loading: true,
                        tooltip_width: 200,
                        tooltip_icon_width: 32,
                        tooltip_icon_height: 32,
                        tooltip_offsetx: 18,
                        tooltip_offsety: 0,
                        arrows: false,
                        buttons: false,
                        btn_numbers: false,
                        keybord_keys: true,
                        mousetrace: false,
                        pauseonover: true,
                        transition: 'vslide', /* vslide/hslide/fade */
                        transition_speed: 500,
                        show_caption: 'show', /* onload/onhover/show */
                        thumbnails: true,
                        thumbnails_position: 'outside-last', /* outside-last/outside-first/inside-last/inside-first */
                        thumbnails_direction: 'horizontal', /* vertical/horizontal */
                        thumbnails_slidex: 1 /* 0 = auto / 1 = slide one thumbnail / 2 = slide two thumbnails / etc. */
                    });
            }

        }
        else {
            $("#divFotos").css('display', 'block');
            $("#divStringball").css('display', 'none');
            $("#divMenuArtigos").css('display', 'none');
        }

        if (pagina == 'revenda') {
            $("#divFotos #foto1").attr("src", "./images/revenda/folder2.jpg");
            $("#divFotos #foto2").attr("src", "./images/revenda/prounha.jpg");
            $("#divFotos #foto3").attr("src", "./images/revenda/oleo.jpg");
            $("#divFotos #foto4").attr("src", "./images/revenda/folder.jpg");
            $("#divFotos #foto5").attr("src", "./images/revenda/logo.jpg");

            $("#divRevenda span.link").click(function () {
                var conteudo = $(this).attr("id");
                if (conteudo) {
                    BuscarConteudo(conteudo);
                }
            });
        }
        else {
            $("#divFotos #foto1").attr("src", "./images/fotos/prounha2.jpg");
            $("#divFotos #foto2").attr("src", "./images/fotos/prounha3.jpg");
            $("#divFotos #foto3").attr("src", "./images/fotos/oleo.jpg");
            $("#divFotos #foto4").attr("src", "./images/fotos/folder.jpg");
            $("#divFotos #foto5").attr("src", "./images/fotos/prounha1.jpg");
        }

        if (pagina == 'produtos') {

            $("#divProdutos a").fancybox({
                'titleShow': false,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'autoScale': false
            });

            $("#aCastanha").tooltip({ tip: "#ttCastanha", relative: true, position: 'bottom center', offset: [-25, 0] });
            $("#aCopaiba").tooltip({ tip: "#ttCopaiba", relative: true, position: 'bottom center', offset: [-50, 200] });

        }


        if ((pagina == 'revenda') || (pagina == 'contato')) {

            $("#btnEnviar > a").click(function () {
                var dados = $("form").serialize();
                $.ajax({
                    url: "email.asp",
                    cache: false,
                    data: dados,
                    type: "POST",
                    dataType: "script",
                    success: function (html) {
                        //alert(html);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        //alert(xhr.responseText.substr(3700,500));
                        $("#divAlerta").html("<p class='erro'>Erro ao enviar E-mail</p>");
                        jQuery("#divRevenda input,#divContato input,#divContato textarea").removeClass("erro");
                    }
                });

            });

            $("textarea").keypress(function () {
                return (this.value.length <= 300);
            });

            $("#txtDDD").inputmask("99", { "placeholder": "" });
            $("#txtTelefone").inputmask("9999-9999");
            $("#txtCPF").inputmask("999.999.999-99");
            $("#txtCEP").inputmask("99999-999");
            $("#txtCNPJ").inputmask("99.999.999/9999-99");
            $("#txtUF").inputmask("aa", { "placeholder": "" });

        }

    }

    function BuscarConteudo(conteudo) {

        var pagina = "";
        var param = "";

        if (conteudo.match("-")) {
            pagina = conteudo.substr(0, conteudo.indexOf("-"));
            param = conteudo.substr(conteudo.indexOf("-") + 1);
        }
        else
            pagina = conteudo;

        if (pagina == 'compre')
            window.open("http://prounha.webstorelw.com.br/");
        else {
            $("#divMenu ul li").removeClass("sel");
            $("#divMenu ul #" + pagina).addClass("sel");

            var dados = "pagina=" + pagina + "&param=" + param;

            $.ajax({
                url: "conteudo.asp",
                cache: false,
                data: dados,
                type: "POST",
                dataType: "html",
                success: function (html) {
                    AplicarConteudo(html, pagina, param)
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    //alert(xhr.responseText.substr(3500,500));
                    $("#divConteudo").html("Erro ao carregar conteúdo");
                }
            });
        }
    }


    /***************************************************    
    FOTOS
    ***************************************************/

    $("#divFotos #foto1").data("normal", { rotate: '13deg', scale: 0.55, translateX: '-114px', translateY: '48px' });
    $("#divFotos #foto1").data("hover", { scale: 0.6, translateX: '-105px', translateY: '44px' });
    $("#divFotos #foto1").data("click", { rotate: '0deg', scale: 1, translateX: '0px', translateY: '40px' });


    $("#divFotos #foto2").data("normal", { rotate: '-3deg', scale: 0.55, translateX: '132px', translateY: '44px' });
    $("#divFotos #foto2").data("hover", { scale: 0.6, translateX: '120px', translateY: '40px' });
    $("#divFotos #foto2").data("click", { rotate: '0deg', scale: 1, translateX: '0px', translateY: '40px' });


    $("#divFotos #foto3").data("normal", { rotate: '-3deg', scale: 0.55, translateX: '-134px', translateY: '200px' });
    $("#divFotos #foto3").data("hover", { scale: 0.6, translateX: '-122px', translateY: '182px' });
    $("#divFotos #foto3").data("click", { rotate: '0deg', scale: 1, translateX: '0px', translateY: '40px' });


    $("#divFotos #foto4").data("normal", { rotate: '0deg', scale: 0.55, translateX: '152px', translateY: '228px' });
    $("#divFotos #foto4").data("hover", { scale: 0.6, translateX: '138px', translateY: '207px' });
    $("#divFotos #foto4").data("click", { rotate: '0deg', scale: 1, translateX: '0px', translateY: '40px' });


    $("#divFotos #foto5").data("normal", { rotate: '-11deg', scale: 0.55, translateX: '-75px', translateY: '290px' });
    $("#divFotos #foto5").data("hover", { scale: 0.6, translateX: '-68px', translateY: '264px' });
    $("#divFotos #foto5").data("click", { rotate: '0deg', scale: 1, translateX: '0px', translateY: '40px' });



    $("#divFotos img").each(function () {
        $(this).transform($(this).data("normal")).HabilitarHover().HabilitarClick();
    });

    /***************************************************    
    MENU ROTATIVO MIDIA
    ***************************************************/

    $("#ulStringball").stringball({
        camd: 690,
        radi: 140,
        speed: 20,
        framerate: 30
    });

    $("#divStringball").css('display', 'none');

    $("#ulStringball li a").click(function (e) {
        var conteudo = $(this).attr("name");
        if (conteudo) {
            BuscarConteudo(conteudo);
        }
    });


    /***************************************************    
    MENU PRINCIPAL
    ***************************************************/

    $("#divMenu ul li").click(function () {
        var conteudo = $(this).attr("id");
        if (conteudo) {
            BuscarConteudo(conteudo);
        }
    });

    $("#home").click();


    /***************************************************    
    PRINCIPAL
    ***************************************************/
    $("#divPrincipal").css("visibility", "visible");

});

