﻿/*

function ProcessImn() { }
function ProcessImnMarkers() { }

function criarMenu() {
$('#sharePointMenu>div>div>ul>li>ul>li>a').each(function (k, v) {
$('#menu').append("<li><a title='" + $(this).text() + "'  href='" + $(this).attr('href') + "'>" + $(this).find('span>span').text() + "</a></li>");
});
$('#menu>li:first').addClass('first');
$('#menu>li:last').addClass('last');
}

function criarBreadcrumb() {
$("#sharePointBreadcrumb>span>span>a").each(function (k, v) {
if (k != 1) $("#breadcrumb>div>ul:first").append("<li><a title='" + $(this).attr("title") + "' href='" + $(this).attr("href") + "'>" + $(this).text() + "</a></li>");
});
$("#breadcrumb>div>ul:first").append("<li class='last'><a href='#'>" + $("#sharePointBreadcrumb>span>span:last").text() + "</a></li>");
}

function criarBusca() {
var sharePointSearchContainer = $("#sharePointSearch>table>tbody>tr>td>div>div>div>table>tbody>tr>td").first();
var sharePointSearch = sharePointSearchContainer.find("input").first().removeAttr("style").removeAttr("class");
var tv1SearchContainer = $(".frmSearch>fieldset>p");
var tv1Search = tv1SearchContainer.find("input").hide();
tv1SearchContainer.prepend(sharePointSearch);
$(".frmSearch>fieldset>p>button").click(function () {
tv1Search.val(sharePointSearch.val());
sharePointSearchContainer.prepend(sharePointSearch);
tv1Search.show();
setTimeout("$('#sharePointSearch>table>tbody>tr>td>div>div>div>table>tbody>tr>td>a>img').click();", 100);
return false;
});
}

*/

(function ($) {

    $.fontSize = function () {
        function execute(increase) {
            $('.resize').find("*").each(function () {
                try {
                    var font = $(this).css('font-size');
                    if (!font) return;
                    var fontsize = (font.replace(/[^0-9]+/gi, "") * 1) + (increase ? 1 : -1);
                    var fontunit = font.replace(/[0-9]+/gi, "");
                    if (fontsize > 0) {
                        $(this).css({ 'font-size': fontsize + fontunit });
                    }
                } catch (e) {
                    alert(e);
                }
            });
        }
        $('.decrease').click(function () { execute(false) });

        $('.increase').click(function () { execute(true) });
    };

    $.fn.menu = function (obj) {
        var defaults = { 'menu': '#sharePointMenu>div>div>ul>li', 'nivel': 1, 'itemTemplate': function () { }, 'subItemTemplate': function () { } };
        var index = 0;
        $.extend(defaults, obj);
        function processar(item) {
            var temp = '';
            index++;
            $(item).children('ul').each(function (k1, v1) {
                $(this).children('li').each(function (k2, v2) {
                    var text = $(this).find('a>span>span:first').text();
                    var href = $(this).children('a:first').attr('href');
                    var title = $(this).children('a:first').text();
                    var current = (window.location.pathname == href || window.location.href == href || window.location.pathname.indexOf(href) > -1);
                    temp += defaults.nivel > index && $(this).children('ul').length > 0 ? defaults.itemTemplate({ 'index': index, 'content': processar($(this)), 'menu': defaults.subItemTemplate({ 'index': index, 'text': text, 'href': href, 'title': title, 'parent': true, 'current': current }) }) : defaults.subItemTemplate({ 'index': index, 'text': text, 'href': href, 'title': title, 'parent': false, 'current': current });
                });
            });
            index--;
            return temp;
        }
        return $(this).append(processar($(defaults.menu)));
    };

    $.breadcrumb = function () {
        var i = $("#sharePointBreadcrumb>span>span>a").size();
        var sBreadCrumb = '';
        var sTitle = '';
        var _first = true;
        $("#sharePointBreadcrumb>span>span>a").each(function (k, v) {
            var _breadcrumbLanguageValue = $(this).text().toLowerCase();
            var _isBreadcrumbLanguage = (_breadcrumbLanguageValue == 'pt-br' || _breadcrumbLanguageValue == 'en-us' || _breadcrumbLanguageValue == 'es-es' ? true : false);
            if (_breadcrumbLanguageValue == "portal redecard") {
                $(this).text("Redecard");
            }
            if (i != k + 1 && $(this).text() != '' && !_isBreadcrumbLanguage) {
                sBreadCrumb += "<li><a title='" + $(this).attr("title") + "' href='" + $(this).attr("href") + "'>" + $(this).text() + "</a></li>";
                if (_first) {
                    sTitle += $(this).text();
                    _first = false;
                }
                else
                    sTitle += " > " + $(this).text();
            }
        });
        // Setar título do documento
        document.title = sTitle + " > " + document.title;
        $("#breadcrumb>div>ul:first").prepend(sBreadCrumb);
        //$("#breadcrumb>div>ul:first").append("<li class='last'><strong>" + $("#sharePointBreadcrumb>span>span:last").text() + "</strong></li>");
    };

    $.searchSite = function () {
        var sharePointSearchContainer = $("#sharePointSearch>table>tbody>tr>td>div>div>div>table>tbody>tr>td").first();
        var sharePointSearch = sharePointSearchContainer.find("input").first().removeAttr("style").removeAttr("class");
        var tv1SearchContainer = $(".frmSearch>fieldset>p");
        var tv1Search = tv1SearchContainer.find("input").hide();
        tv1SearchContainer.prepend(sharePointSearch);
        $(".frmSearch>fieldset>p>button").click(function () {
            //tv1Search.val(sharePointSearch.val());
            //sharePointSearchContainer.prepend(sharePointSearch);
            //tv1Search.show();
            //setTimeout("$('#sharePointSearch>table>tbody>tr>td>div>div>div>table>tbody>tr>td>a>img').click();", 100);
            window.location = _languageFolder + "/pesquisa/" + _pagesListUrl + "/results.aspx?k=" + sharePointSearch.val();
            return false;
        });
    };

    $.menuSite = function () {
        function subMenuItemTemplate(json) {
            return '<li><a title="$title" href="$href" class="$class">$text</a></li>'.replace('$href', json.href).replace('$text', json.text).replace('$title', json.title).replace('$class', (!json.current ? '' : 'selected'));
        }
        $('#menu').menu({ 'nivel': 1, 'subItemTemplate': subMenuItemTemplate });
        $('#menu>li:first').addClass('first');
        $('#menu>li:last').addClass('last');
    };
    $.menuSubSite = function () {
        function menuItemTemplate(json) {
            return (json.menu ? json.menu : '') + '<div><ul>' + json.content + '</ul></div>';
        }
        function subMenuItemTemplate(json) {
            var temp = '<a title="$title" href="$href" class="png">$text</a>'.replace('$href', (json.href.indexOf('#') > -1 ? '#' : json.href)).replace('$text', json.text).replace('$title', json.title); //.replace('$class', (!json.current ? '' : 'active'));
            return json.parent ? '<h3 class="menuSharePoint header $activate">'.replace('$activate', (!json.current ? '' : 'ui-state-active')) + temp + '</h3>' : (json.index > 1 ? '<li>' + temp + '</li>' : '<h3 class="menuSharepoint $activate">'.replace('$activate', (!json.current ? '' : 'ui-state-active')) + temp + '</h3>');
        }
        $('#menuLateral>.accordion').menu({ 'menu': '#sharePointMenu1>div>div', 'nivel': 2, 'itemTemplate': menuItemTemplate, 'subItemTemplate': subMenuItemTemplate })
        $('#menuLateral>.accordion').find('h3:odd').addClass("bg");

        //$('#menuLateral>.accordion').find('.menuSharepoint:first').addClass('active');
    };

    $.menuAreaFechada = function () {
        function menuItemTemplate(json) {
            return (json.menu ? json.menu : '').replace('</li>', '<ul>' + json.content + '</ul></li>');
        }
        function subMenuItemTemplate(json) {
            return '<li><a title="" href="$href" class="$class">$text</a></li>'.replace('$href', _siteUrl + json.href).replace('$text', json.text).replace('$title', json.title).replace('$class', (json.current ? 'menuareafechadaactive' : ''));
        }
        $('#menu>ul:first').menu({ 'nivel': 2, 'itemTemplate': menuItemTemplate, 'subItemTemplate': subMenuItemTemplate });
        $('#menu>ul:first>li:first').addClass('first');
        $('#menu>ul:first>li:last').addClass('last');
        $('ul.sf-navbar li ul').each(function () {
            $(this).find('li:first').addClass('first');
            $(this).find('li:last').addClass('last');
        });
        if ($('.menuareafechadaactive').size() > 1) {
            $('.menuareafechadaactive:first').removeClass('menuareafechadaactive');
        }
    };

    $.loaderIndicadorFinanceiro = function (visible, error) {
        if (!error) {
            $('#indicadoresfinanceiro .loader img').show();
            if (!visible) {
                $('#indicadoresfinanceiro .loader').fadeOut('slow', function () { $(this).hide(); });
            } else {
                $('#indicadoresfinanceiro .loader').fadeIn('slow', function () { $(this).show(); });
            }
        } else {
            $('#indicadoresfinanceiro .loader img').hide();
            $('#indicadoresfinanceiro .loaderError').text(Cotacao.prototype.msgErro).show();
        }
    };

    $.loginLegado = function () {
        $("#youAre").change(function () {
            $("#youAre option:selected").each(function () {
                if ($(this).val() != "" && $(this).val() != "Opção 1") {
                    $("#youAre, #nCadastroField, #usuarioField, #senhaField").val("");
                    $("#senhaField").focus();
                    $("#usuarioField").focus();
                    $("#nCadastroField").focus();
                    $("#youAre").focus();
                    window.open("https://services.redecard.com.br/novoportal/DesktopDefault.aspx", "mywindow", "toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1");
                }
            });
        });
        $("#youAre, #nCadastroField, #usuarioField, #senhaField").keypress(function (e) {
            if (e.keyCode == 13) {
                return $.loginLegadoSend();
            }
        });
    };

    $.loginLegadoSend = function () {
        if ($("#youAre").val() == "") {
            alert('Favor prencheer o campo "Estabelecimentos" para efetuar o login!');
            $("#youAre").focus();
        } else if ($("#nCadastroField").val() == "" || $("#nCadastroField").val() == $("#nCadastroField").attr('title')) {
            alert('Favor prencheer o campo "Número de Cadastro" para efetuar o login!');
            $("#nCadastroField").focus();
        } else if ($("#usuarioField").val() == "" || $("#usuarioField").val() == $("#usuarioField").attr('title')) {
            alert('Favor prencheer o campo "Usuário" para efetuar o login!');
            $("#usuarioField").focus();
        } else if ($("#senhaField").val() == "" || $("#senhaField").val() == $("#senhaField").attr('title')) {
            alert('Favor prencheer o campo "Senha" para efetuar o login!');
            $("#senhaField").focus();
        } else {
            $("#_estabelecimento").val("1");
            $("#_nCadastro").val($("#nCadastroField").val());
            $("#_usuario").val($("#usuarioField").val());
            $("#_senha").val($("#senhaField").val());
            var s_FormDef = "<form id='frmPostLogin' action='https://portal.redecard.com.br/sites/fechado/_layouts/loginPageRedecard.aspx' method='POST'>{0}</form>";
            s_FormDef = s_FormDef.replace("{0}", $("#dfrmServicesPost").html()).replace("{1}", window.location.hostname);
            $("#dfrmSubmit").append(s_FormDef);
            $("#frmPostLogin").submit();
        }
        return false;
    };

})(jQuery);


$(document).ready(function () {
    $.fontSize();
    $('#redecardareafechadatipo1>#left>#bottomleft>div:nth-child(2)').css('margin-right', '0px');
});
