﻿var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

function desenhaCaixa() {
    $(".caixa").each(function(i) {

        if ($(this).find(".b").length == 0) {

            $(this).css("background-image", "");
            var idTemp = "back" + i;
            $(this).prepend("<span class='b' id='" + idTemp + "' style='margin:0px;position:absolute;height:100%;width:100%;'></span>");
            var w = $(this).width();
            var h = $(this).height();
            var btn = Raphael(idTemp, w, h);
            var rect = btn.rect(0, 0, w, h, 10);
            rect.attr("fill", "#FFC425");
            rect.attr("opacity", "0.9");
            rect.attr("stroke-width", "0");

            if (sPage != "Contactos.aspx" && sPage != "Clientes.aspx") {
                $(this).draggable({ containment: "parent", cursor: "move", cancel: ".texto" });
                $(this).hover(function() {
                    $(this).css("cursor", "move");
                }, function() { $(this).css("cursor", "default"); });
            }
        }
    });
}

var jaDesenhoucaixas = ($(".caixa span") != undefined);


function pageLoad(sender, args) {
    desenhaCaixa();
    jaDesenhoucaixas = ($(".caixa span") != undefined);
}

$(document).ready(function() {



    jaDesenhoucaixas = false;
    desenhaCaixa();
    //AUTO SCROLLER
    $("#autoscroller").ready(function() {
        $("#autoscroller").css("cursor", "default");
        $("#autoscroller").cycle({
            fx: 'scrollLeft',
            pause: 1,
            timeout: 3000
        });
    });
    //END AUTO SCROLLER

    $("#fotoEntrada img").each(function() {
        $(this).addLoader("./img/zoomloader.gif");
        $(this).useLoader($(this).attr("src"), function() { });
    });


    $(".botao").each(function() {
        var rPages = $(this).find("#pages").val().split(",");
        var browser = navigator.appName;

        /*atribuir click event*/
        $(this).css("cursor", "pointer");
        $(this).click(function() {
            if (rPages[0] != sPage)
                window.location = rPages[0];
        });

        /*Desenhar o botão*/
        var h = $(this).height();
        var w = $(this).width();

        var btn = Raphael($(this).attr("id"), w, h);
        var s = btn.set();
        var dims = $(this).find("#dimensions").val().split(",");
        var rect = btn.rect(parseInt(dims[3]), parseInt(dims[2]), parseInt(dims[1]), parseInt(dims[0]));

        var text = btn.text(0, 0, $(this).attr("title"));
        var fsize = "12.5pt";
        if (browser == "Microsoft Internet Explorer") fsize = "9pt";
        text.attr("font-size", fsize);
        text.translate(parseInt(dims[3]) + 2 + rect.getBBox().width / 2, parseInt(dims[2]) + rect.getBBox().height - (text.getBBox().height + 10) * 2);
        text.rotate(-90);

        s.push(rect, text);


        /*alterar caso seja a página*/
        if (rPages[0].toLowerCase() == sPage.toLowerCase() || (rPages[1] != null && rPages[1].toLowerCase() == sPage.toLowerCase()) || (sPage == '' && rPages[0] == "default.aspx")) {
            rect.attr("fill", "#fff");
            text.attr("fill", "#4d4d4d");
            s.rotate($(this).find("#degrees").val());
        }
        else {
            rect.attr("fill", "#4d4d4d");
            text.attr("fill", "#fff");
        }
    });

    if (sPage.toLowerCase() == "servicos.aspx") {
        $(".links a").append("<label style='color: #FFF;'> +</label>");
    }


    // $("#logo").css("z-index", "999");
    // $("#logo").draggable({ containment: '#janela' });

    //    var logoCanvas = Raphael("logo", 160, 160);
    //    var logoImg = logoCanvas.image($("#logo img").attr("src"), 25, 55, 160, 50);
    //    logoImg.rotate(-80);
    //    $("#logo img").hide();
});