﻿var genericErrorMessage = new Array();

function init() {
    try {
        util();
        initLoader();
        cufon();
        tabs();
        siteMap();
        dropList();        
    } catch (e) {
        alert(
			'Arquivo   : ' + e.fileName + ' \n ' +
			'Linha     : ' + e.lineNumber + ' \n ' +
			'Nome      : ' + e.name + ' \n ' +
			'Descrição : ' + e.message + ' \n '
		);
    };
}

var ajaxExecuting = false;

//Ajax Setup

$.ajaxSetup({
    //timeout: (1000 * 60),
    //ifModified: true,
    cache: false,
    //    async: false,
    error: function (x, e, error) {
        LogFailure(x.status, $.ajaxSettings.url, 'Ocorreu um erro.Estamos trabalhando para corrigir.');
        return true;
        if (x.status == 0) {
            LogFailure(x.status, $.ajaxSettings.url, 'You are offline!!\n Please Check Your Network.');
        }
        else if (x.status == 404) {
            LogFailure(x.status, $.ajaxSettings.url, 'URL not found.');
        }
        else if (x.status == 400) {
            LogFailure(x.status, $.ajaxSettings.url, 'Bad Request');
        }
        else if (x.status == 500) {
            LogFailure(x.status, $.ajaxSettings.url, 'Internel Server Error.');
        }
        else if (e == 'parsererror') {
            LogFailure(x.status, $.ajaxSettings.url, 'Error.\nParsing JSON Request failed.');
        }
        else if (e == 'timeout') {
            LogFailure(x.status, $.ajaxSettings.url, 'Request Time out.');
        }
        else {
            LogFailure(x.status, $.ajaxSettings.url, 'Unknow Error.\n' + x.responseText);
        }
    }
});

function LogFailure(pStatus, pUrlCorrent, pErrorMessage) {
    var dados = {
        "Status": pStatus,
        "urlCorrent": pUrlCorrent,
        "ErrorMessage": pErrorMessage
    };

    genericErrorMessage.push(pErrorMessage);

    callServerMethodByPost('/Service/Failure/FailureService.svc/Failure/', dados, function () {
        ModalBox.Show({ Title: getErroMessage(), Error: true, Width: 300 });
    });
}

function getErroMessage() {
    var error = "";
    $.each(genericErrorMessage, function (i, e) {
        error += e + "<br /> ";
    });

    return error;
}

function activateLoader() {
    if (ajaxExecuting) {
        $('#ajax_loading').show();
    }
}

function initLoader() {
    $(document).ready(function () {
        $('body').append('<div id="ajax_loading"><p><img src="/Images/ico/ico_loading.gif"></p></div>');

        $(document).mousemove(function (e) {
            if ($("#ajax_loading:visible").length > 0) {
                $('#ajax_loading').css({ top: (e.pageY + 18), left: (e.pageX + 18) });
            }
        });
    });

    $(document).ajaxStart(function () {
        ajaxExecuting = true;
        setTimeout(activateLoader, 200);
    }).ajaxStop(function () {
        cufon();
        ajaxExecuting = false;
        $('#ajax_loading').hide();
    });

    //    window.onbeforeunload = function () {
    //        $('#ajax_loading').show();
    //    };
}

function util() {
    /* target blank */
    var a = $("a[rel='external-link']");
    if (a.length > 0) {
        for (var x = 0; x < a.length; x++) { a.eq(x).attr('target', '_blank') };
    };

    /*help - show/hide */
    var box_help = $('.box_help');
    var box_bthelp = $('.box_bthelp');

    $('a.ico_help ').click(function () {
        box_help.show();
        box_bthelp.hide();
    });
    $('a.ico_close_help ').click(function () {
        box_help.hide();
        box_bthelp.show();
    });

    /*hover-focus input*/
    $("input").focus(function () {
        $(this).addClass("hover_input");
    });
    $("input").blur(function () {
        $(this).removeClass("hover_input");
    });

    $(".txtSearchClass").keypress(function (event) {
        var k = event.which;
        /* 
        0:      non-input keys      
        48-57:  0-9      
        64-90:  @ A-Z
        97-122: a-z      
        45:     - 
        

        */
        if (k != 0 && k == 8 && k != 45 && (k < 48 || k > 57) && (k < 97 || k > 122) && (k < 64 || k > 90) && k != 46)
            return false;
    });

    $(".txtSearchClass").keyup(function () {
        var inp = $(this).val();
        if (inp.match(/[^a-zA-Z0-9-\s@.]/g))
            $(this).val(inp.replace(/[^a-zA-Z0-9-\s@.]/g, ''));
    });


    
    /* $("input").hover(function () {
    $(this).addClass("hover_input");
        
    }, function () {
    $(this).addClass("hover_out");
        
    });*/
}

function cufon() {
    Cufon.replace('.gillSans16', { fontWeight: 'normal', fontSize: '16px' });
    Cufon.replace('.gillSans11Light', { fontWeight: 'normal', fontSize: '11px', color: '#666' });
    Cufon.replace('.gillSans11', { fontWeight: 'normal', fontSize: '11px', color: '#333333' });
    Cufon.replace('.gillSans11Hover', { fontWeight: 'normal', fontSize: '11px', color: '#999999', hover: { fontWeight: 'bold', color: '#333333'} });
    Cufon.replace('.gillSans11Red', { fontWeight: 'normal', fontSize: '11px', color: '#E21F1C' });
    Cufon.replace('.gillSans22', { fontWeight: 'normal', fontSize: '22px' });
    Cufon.replace('.gillSans16Light', { fontWeight: 'light', fontSize: '16px', color: '#666', letterSpacing: '1px' });
    Cufon.replace('.gillSans20Light', { fontWeight: 'light', fontSize: '20px', color: '#666', letterSpacing: '1px' });
    Cufon.replace('.ui-datepicker-title span', { fontWeight: 'light', fontSize: '16px', color: '#808080' });

}

function tabs() {
    $('.tabs').each(function () {
        var tab = $(this),
			nav = $(this).find('.tabs_nav'),
			nav_li = nav.find('li'),
			content = $(this).find('.tabs_content');

        nav_li.each(function (x) {
            $(this).find('a.linktab').live('click', function () {
                if ($(this).attr('rel') == 'open-others') {
                    tab.find('.drop_list').show();
                    dropList();
                } else {
                    tab.find('.drop_list').hide();
                };
                nav.find('a.linktab').removeClass('selected');
                nav.find('a.linktab').parent().removeClass('border_bottom_none').removeClass('nodash');
                $(this).addClass('selected');
                $(this).parent().addClass('border_bottom_none');

                if (!$(this).parent().is(".tabs .tabs_nav li:first-child"))
                    $(this).parent().prev().addClass("nodash");
                content.removeClass('show').eq(x).addClass('show');
            });
        });
    });
}

function siteMap() {
    $("a.bt_sitemap").toggle(function () {
        $("div.content_sitemap").hide()
				.parent()
				.find('a.bt_sitemap')
				.addClass('mais');

    }, function () {
        $("div.content_sitemap").show()
			.parent()
			.find('a.bt_sitemap')
			.removeClass('mais');

        var b = $('body')[0];
        $(b).scrollTo('100%', 200, { queue: true })

    });
}

function dropList() {

    $(".drop_list ol>li").live('mouseover', function () {
        $(".drop_sub", this).show()
    });

    $(".drop_list ol>li").live('mouseout', function () {
        $(".drop_sub", this).hide()
    });

}


$(function () {
    init();
    var UserName = $('span[id$=lblName]').html();
    if (UserName) {
        $('#UserName').html('&nbsp;' + UserName);
        $('#FirstName').html('&nbsp;' + UserName.substring(0, UserName.indexOf(' ')));
    }
});


















