﻿/*
* F5 Utils
*
*/

function SomenteNumero(selector) {
    $(selector || "input[isNaN=false]").bind("keyup blur focus", function (e) {
        e.preventDefault();
        var expre = /[A-Za-z\.\§\£\@\`\Ž\^\~\'\"\!\?\#\$\%\š\¬\_\+\=\.\,\:\;\<\>\|\°\ª\º\]\[\{\}\\ \)\(\*\&\-\/\\]/g;

        // REMOVE OS CARACTERES DA EXPRESSAO ACIMA
        if ($(this).val().match(expre))
            $(this).val($(this).val().replace(expre, ''));
    });
}


function set_maxLength() {
    $("textarea[_maxLength]").bind('keypress blur keyup', function (event) {
        var key = event.which;
        var _maxLength = $(this).attr('_maxLength');

        //todas as teclas incluindo enter
        if (key >= 33 || key == 13) {
            var maxLength = $(this).attr("_maxLength");
            var length = this.value.length;
            if (length >= maxLength) {
                $(this).val($(this).val().substring(0, _maxLength));
                event.preventDefault();
            }
        }
    });
}

function set_modal() {
    $('a[modal]').click(function () {
        var $modal = $($(this).attr('modal'));

        $('#wrapper').append($('<div></div>').addClass('bg-modal'));
        $('.bg-modal').fadeTo('fast', 50, function () { $modal.fadeIn(); });
        $('.bg-modal').css('opacity', 80)
                              .css('z-index', 98)
                              .css('height', $('#wrapper').innerHeight())
                              .css('width', $('#wrapper').innerWidth());



        if (!($.browser.msie && $.browser.version == '6.0')) {
            $modal.css({
                position: 'fixed',
                left: '50%',
                top: '50%',
                zIndex: 99
            }).css({
                marginLeft: '-' + ($modal.outerWidth() / 2) + 'px',
                marginTop: '-' + ($modal.outerHeight() / 2) + 'px'
            });
        }
        else {
            //hack ie 6
            $modal.css('_POSITION', 'absolute');
            $modal.css('_TOP', $(this).offset().top - 30 + 'px');
        }


        $('.btn-fechar', $modal).click(function () {
            $('.bg-modal').fadeTo('fast', 0, function () { $modal.fadeOut(); $(this).remove(); });
        });
    });
}

function set_sexyCombos() {
    $('select.sexy').not('[sexycombostarted]').each(function() {
        var item = $(this);
        var itemSelected = $('option[value=' + item.val() + ']', item).text() || $('option:first', item).text();

        item.sexyCombo({
            filterFn: function() { return true; },
            changeCallback: function(e) {
                var itemName = item.attr('name');
                $("option[value='" + this.getHiddenValue() + "']", item).attr('selected', 'selected');
                
                if (item.attr('onchange') != undefined && item.attr('onchange').toString().indexOf('__doPostBack') > -1) {
                    __doPostBack(itemName, '');
                }
            }
        });
        item.next().val(itemSelected);
    });

}

function set_prettyBox() {
    //Pretty-checkbox
    $('.pretty input[type=radio]:not(.hiddenBox),.pretty input[type=checkbox]:not(.hiddenBox)').prettyCheckboxes();
}

function set_listaPadrao() {
    $(document).ready(function () {

        $('.listapadrao').each(function () {
            var lista = $(this);

            $('.sf_listItemBody', lista).hide();

            //cor_sim / cor_nao
            $('ul > li:even', lista).addClass('corsim');

            //fecha todos para abrir novo
            $('.sf_listItemTitle', lista).click(function () {

                $('.sf_listItemBody', lista).slideUp();
                $('.sf_listItemTitleexp', lista)
					.addClass('sf_listItemTitle')
                    .removeClass('sf_listItemTitleexp');
            });

            //abre item para listas personalizadas
            $('.sf_listItemTitle a.expand', lista).click(function () {
                var $item = $(this).parent().parent();

                $('.sf_listItemBody:visible', lista).each(function () {
                    $(this).slideUp();
                    $('.sf_listItemTitleexp', $(this).parent()).removeClass('sf_listItemTitleexp');
                });

                $('.sf_listItemBody', $item).toggle();

                if ($('.sf_listItemBody', $item).is(':visible'))
                    $('h3', $item).addClass('sf_listItemTitleexp');
                else
                    $('h3', $item).removeClass('sf_listItemTitleexp');
                return false;
            });
        });
    });
}
function set_listaHistoria() {
    $(document).ready(function () {
        var lista = $('.listahistoria:first');
        $('.sf_listItemBody', lista).hide();
        $('.sf_listItemBody:first', lista).show();
        $('.sf_anchorList a:first', lista).addClass('ativo');
        $('.sf_anchorList a', lista).click(function () {
            $('.sf_listItemBody:visible', lista).hide();
            $('.sf_anchorList a', lista).removeClass('ativo');
            var anchor = $(this).addClass('ativo').attr('href');
            anchor = anchor.substr(anchor.search('#') + 1, 37);
            $('a[name=' + anchor + ']:first', lista).parent().next().show();
            return false;
        });

    });
}

function set_galeriaImagens() {
    $(document).ready(function () {
        $("#pikame").each(function () {
            $(this).PikaChoose({
                text: { play: "", stop: "", previous: "Anterior", next: "Próximo" },
                autoPlay: false,
                showCaption: false
            }).find(".clip .sombra").click(function () {
                $(this).parent().find("img").click();
            });


            $(this).jcarousel({
                scroll: 1,
                visible: 3,
                initCallback: function (carousel) {
                    $(carousel.list).find('img').click(function () {
                        var clicked = parseInt($(this).parents('.jcarousel-item').attr('jcarouselindex'));
                        var last = ($(this).parents('ul').find('li:last').index() == clicked - 1) ? true : false;
                        if (!last) {
                            clicked = (clicked - 2 <= 0) ? 0 : clicked - 2;
                        }
                        carousel.scroll(clicked);
                    });
                }
            });
        });
    });
}

function set_menu() {
    $(document).ready(function () {
        $('.RadMenu ul li .rmItem .rmSlide:visible').parent().addClass('rmExpandable');

        $('.RadMenu .rmSlide:visible .rmSlide:visible').each(function () {
            if (!$(this).prev().is('.rmFocused'))
                $(this).remove();
        });

    });
}
function set_pngFix() {
    if ($.browser.version == "6.0" && $.browser.msie) {

        $('img[src$=png]').each(function () {
            $(this).attr('src', $(this).attr('src').replace('.png', '.gif'))
        });
    }
}

function set_openCloseSexyCombo() {
	var lista = $('.filtro_OutrasUnidades .combo .list-wrapper');
	lista.hide();
	$('.filtro_OutrasUnidades .combo .icon').click(function() {
		lista.toggle();
	});

	$('.filtro_OutrasUnidades .combo .list-wrapper ul li a').click(function() {
		$('.filtro_OutrasUnidades .inputsexyfake').text($(this).text());
		$('.filtro_OutrasUnidades .combo .list-wrapper ul li').removeClass('active');
		$(this).parent().addClass('active');
		lista.hide();
	});
               
}


//INIT
$(function () {
    SomenteNumero();
    set_maxLength();
    set_modal();
    set_sexyCombos();
    set_listaPadrao();
    set_listaHistoria();
    set_galeriaImagens();
    set_prettyBox();
    set_pngFix();
    set_menu();
	set_openCloseSexyCombo();
})
