﻿function HomeExpedicao() {

    this.Initialize = function() {
        $().ready(function() {
            Home.ChangeFlickrPhotos();
            Home.ChangeBlogFeatured();
            Home.IntervalBlog();
            Home.LoadTrajeto();
            Home.Trajeto();
            Home.IntervalTwitter();
        });
    }

    this.ChangeFlickrPhotos = function() {
        var pathImage = "Content/Flickr/Photo/";
        $("#photosFlickr a").click(function() {
            var imagemThumb = $(this).children("img").attr("src");
            var nameImage = imagemThumb.substring(imagemThumb.lastIndexOf('/') + 1, imagemThumb.length);
            if (nameImage != "flickr-thumb.jpg")
                $("#featured .bg").css("background", "url(" + pathImage + nameImage + ")");
        });
    }
    
    var iBlogIndex = 1;
    this.ChangeBlogFeatured = function() {
        $("#blogHightlight li a").click(function() {
            var linkIndex = $(this).text();
            $("#blogHightlight .blogFeatured").hide();
            $("#blogFeatured-" + linkIndex).show();
            $("#blogHightlight .destaque a").css("background-position", "0 0");
            $("#blogHightlight .destaque0" + linkIndex + " a").css("background-position", "0 -31px");
            iBlogIndex = linkIndex;
        });
    }

    var iBlogIndex = 1;
    this.ChangeBlog = function() {
        iBlogIndex = (iBlogIndex > 4) ? 1 : iBlogIndex;
        $("#blogHightlight .blogFeatured").hide();
        $("#blogFeatured-" + iBlogIndex).show();
        $("#blogHightlight .destaque a").css("background-position", "0 0");
        $("#blogHightlight .destaque0" + iBlogIndex + " a").css("background-position", "0 -31px");

        iBlogIndex++;
    }
    this.IntervalBlog = function() {
        setInterval(Home.ChangeBlog, 5000);
    }

    
    
    //Mapa Trajetoria
    this.Trajeto = function() {
        $("#mapaTrajeto .boxLocais .anterior").click(function() {
            var iMarginTop = parseInt($('#mapaTrajeto .boxLocais .scroll ul').css("marginTop").substring(0, $('#mapaTrajeto .boxLocais .scroll ul').css("marginTop").length - 2));
            if (iMarginTop < 0)
                $('#mapaTrajeto .boxLocais .scroll ul').animate({ marginTop: "+=149px" }, 1000);
        })
        $("#mapaTrajeto .boxLocais .proximo").click(function() {
            var iMarginTop = parseInt($('#mapaTrajeto .boxLocais .scroll ul').css("marginTop").substring(0, $('#mapaTrajeto .boxLocais .scroll ul').css("marginTop").length - 2));
            if (iMarginTop > -890)
                $('#mapaTrajeto .boxLocais .scroll ul').animate({ marginTop: "-=149px" }, 1000);
        })
    }

    this.LoadTrajeto = function() {
        $$$.NewSystemRequest("Xml/Mapa-Trajeto.xml", null, Home.CreateTrajeto, null);
    }

    this.CreateTrajeto = function(pObj) {
        var listaHtml = new String();
        var index = new Number();

        $(pObj).find('trajeto').each(function(i) {
            index = i + 1;
            var cssAtual = new String();

            if ($(this).attr("tipo") == "atual")
                cssAtual = " icoAtual selected";
                
            else
                cssAtual = ($(this).attr("tipo") == "ok") ? " icoOk" : "";

            listaHtml += '<li class="trajeto' + index + cssAtual + '"><a href="javascript:void(0);" onclick="Home.LoadInfoValues(' + i + ')"><span>' + $(this).attr("tipo") + '</span>' + $(this).attr("cidade") + '</a></li>';

            if ($(this).attr("tipo") == "atual")
                Home.SetInfoValues(pObj, i);
        });

        $("#mapaTrajeto .listaTrajetos").html(listaHtml)
    }

    this.LoadInfoValues = function(pIndex) {
        $$$.NewSystemRequest("Xml/Mapa-Trajeto.xml", null, Home.SetInfoValues, pIndex);
    }

    this.SetInfoValues = function(pObj, pIndex) {
        //Selecionado
        $("#mapaTrajeto .listaTrajetos li").removeClass("selected");
        $("#mapaTrajeto .listaTrajetos .trajeto" + (pIndex + 1) + "").addClass("selected");

        // Mapa de Background
        if ($(pObj).find("trajeto").eq(pIndex).attr("mapa").trim() != "")
            $("#mapaTrajeto .mapa").css("background-image", "url(/Content/Mapas/" + $(pObj).find('trajeto').eq(pIndex).attr('mapa') + ")");

        //Fotos
        $("#mapaTrajeto .boxInformacoes .fotos span").text('(' + $(pObj).find("trajeto").eq(pIndex).children("fotos").attr("total") + ')');
        if ($(pObj).find("trajeto").eq(pIndex).children("fotos").text().trim() != "")
            $("#mapaTrajeto .boxInformacoes .fotos a").attr("href", $(pObj).find("trajeto").eq(pIndex).children("fotos").text());
        else
            $("#mapaTrajeto .boxInformacoes .fotos a").removeAttr("href");

        //Videos
        $("#mapaTrajeto .boxInformacoes .videos span").text('(' + $(pObj).find("trajeto").eq(pIndex).children("videos").attr("total") + ')');
        if ($(pObj).find("trajeto").eq(pIndex).children("videos").text().trim() != "")
            $("#mapaTrajeto .boxInformacoes .videos a").attr("href", $(pObj).find("trajeto").eq(pIndex).children("videos").text());
        else
            $("#mapaTrajeto .boxInformacoes .videos a").removeAttr("href");

        //Blog
        $("#mapaTrajeto .boxInformacoes .blog span").text('(' + $(pObj).find("trajeto").eq(pIndex).children("blog").attr("total") + ')');
        if ($(pObj).find("trajeto").eq(pIndex).children("blog").text().trim() != "")
            $("#mapaTrajeto .boxInformacoes .blog a").attr("href", $(pObj).find("trajeto").eq(pIndex).children("blog").text());
        else
            $("#mapaTrajeto .boxInformacoes .blog a").removeAttr("href");

        //Receitas
        $("#mapaTrajeto .boxInformacoes .receitas span").text('(' + $(pObj).find("trajeto").eq(pIndex).children("receitas").attr("total") + ')');
        if ($(pObj).find("trajeto").eq(pIndex).children("receitas").text().trim() != "")
            $("#mapaTrajeto .boxInformacoes .receitas a").attr("href", $(pObj).find("trajeto").eq(pIndex).children("receitas").text());
        else
            $("#mapaTrajeto .boxInformacoes .receitas a").removeAttr("href");

        //Musicas
        $("#mapaTrajeto .boxInformacoes .musicas span").text('(' + $(pObj).find("trajeto").eq(pIndex).children("musicas").attr("total") + ')');
        if ($(pObj).find("trajeto").eq(pIndex).children("musicas").text().trim() != "")
            $("#mapaTrajeto .boxInformacoes .musicas a").attr("href", $(pObj).find("trajeto").eq(pIndex).children("musicas").text());
        else
            $("#mapaTrajeto .boxInformacoes .musicas a").removeAttr("href");

        //Antropologica
        $("#mapaTrajeto .boxInformacoes .antropologica span").text('(' + $(pObj).find("trajeto").eq(pIndex).children("antropologica").attr("total") + ')');
        if ($(pObj).find("trajeto").eq(pIndex).children("antropologica").text().trim() != "")
            $("#mapaTrajeto .boxInformacoes .antropologica a").attr("href", $(pObj).find("trajeto").eq(pIndex).children("antropologica").text());
        else
            $("#mapaTrajeto .boxInformacoes .antropologica a").removeAttr("href");
    }

    //Altera Twitter
    var iTwitterIndex = 1;
    this.ChangeTwitter = function() {
        $("#boxTwitter p").hide();
        iTwitterIndex = (iTwitterIndex >= 14) ? 0 : iTwitterIndex;
        $("#boxTwitter p").eq(iTwitterIndex).fadeIn("slow");
        iTwitterIndex++
    }
    this.IntervalTwitter = function()
    {
        setInterval(Home.ChangeTwitter, 7000);
    }
    
    
}
