//Contador de caracteress
jQuery.fn.apTextCounter=function(config){
    function __checkCount(el,e,__c){
        var count=__c.maxCharacters-$(el).val().length;
        var countDisplay=(__c.direction!=="down")?(__c.maxCharacters-count):count;
        if(count<=0){
            var k=e.which;
            if((k>47&&k<91)||(k>95&&k<112)||(k>184&&k<250)||k==13||k==32){
                e.preventDefault();
                e.stopPropagation();
                if(__c.onMaxCharactersReached!==null){
                    if(__c.scope!==null){
                        __c.onMaxCharactersReached.call(__c.scope,{
                            count:countDisplay,
                            config:__c
                        });
                    }else

                    {
                        __c.onMaxCharactersReached({
                            count:countDisplay,
                            config:__c
                        });
                    }
                }
                return countDisplay;
            }
        }
        if(__c.onCharacterCountChecked!==null){
            if(__c.scope!==null){
                __c.onCharacterCountChecked.call(__c.scope,{
                    count:countDisplay,
                    config:__c
                });
            }else

            {
                __c.onCharacterCountChecked({
                    count:countDisplay,
                    config:__c
                });
            }
        }
        return countDisplay;
    }
    function __updateTracker(el,count,__c){
        var msg=__c.trackerTemplate.replace(/%s/i,count);
        $(__c.tracker).html(msg);
        if(__c.onTrackerUpdated!==null){
            if(__c.scope!==null){
                __c.onTrackerUpdated.call(__c.scope,{
                    count:count,
                    config:__c
                });
            }else

            {
                __c.onTrackerUpdated({
                    count:count,
                    config:__c
                });
            }
        }
    }
    return this.each(function(){
        var __this=this;
        this.__c=jQuery.extend({
            maxCharacters:100,
            direction:"down",
            tracker:"#tracker",
            trackerTemplate:"%s",
            scope:null,
            onCharacterCountChecked:null,
            onMaxCharactersReached:null,
            onTrackerUpdated:null
        },config);
        __checkCount(this,null,this.__c);
        __updateTracker(this,(this.__c.direction==="down")?this.__c.maxCharacters:0,this.__c);
        $(this).keyup(function(e){
            var count=__checkCount(__this,e,__this.__c);
            __updateTracker(__this,count,__this.__c);
        });
        $(this).keydown(function(e){
            var count=__checkCount(__this,e,__this.__c);
            __updateTracker(__this,count,__this.__c);
        });
    });
};

window.onload = function () {

    //função para limpar forms
    $.fn.clearForm = function() {
        return this.each(function() {
            var type = this.type, tag = this.tagName.toLowerCase();
            if (tag == 'form')
                return $(':input',this).clearForm();
            if (type == 'text' || type == 'password' || tag == 'textarea')
                this.value = '';
            //else if (type == 'checkbox' || type == 'radio')
            //this.checked = false;
            else if (tag == 'select')
                this.selectedIndex = -1;
        });
    };

    $('.colorbox').colorbox();
    $("a[rel='galeria']").colorbox();
    $("a[rel='galeria_salvador']").colorbox();
    $("a[rel='galeria_brasilia']").colorbox();
    $("a[rel='catalogo']").colorbox();
    $("a[rel='outdoor']").colorbox();
    $("a[rel='galeria_open']").colorbox({
        open    : true,
        onClosed: function(){
            history.go(-1)
        }
    });
    $(".colorbox_colecao_anterior").colorbox({
        iframe  : true,
        width   : 800,
        height  : 530
    });

     $(".data").mask("99/99/9999");
     $(".cep").mask("99.999-999");
     $(".telefone").mask("(99) 9999-9999");
     $(".cpf").mask("999.999.999-99");
     $(".cnpj").mask("99.999.999/9999-99");

     $('#LinkPresentation, #LinkPresentationInterno').click(function(){
        $.fn.colorbox({
            width   :"90%",
            height  :"90%",
            iframe  :true,
            href:BASE_URL+"presentation/index.php"
        });
        return false;
     });

     $('#MensagemFormulario').hide(0, function(){
         $('form').clearForm()
         alert($(this).html())
     });


     $('.link_estado').click(function(){

         $('.estado_selecionado').removeClass('estado_selecionado');
         $(this).addClass('estado_selecionado')

         var linkestado = $(this);
         $('.representantes_estado_oculto').fadeOut()
         $(linkestado).parent('li').children('.representantes_estado_oculto').fadeIn()
         
         
         return false;
     })

    

    var ConteudobackgroundImage = $('#Conteudo').css('backgroundImage');
    $('#FecharColecao').click(function(){
        $('#VisualizacaoColecao').fadeOut(500, function(){
            $('#Colecao').fadeIn();
            $('#Conteudo').css('backgroundImage', ConteudobackgroundImage);
        })
    })

    $('.LinkCatalogo').click(function(){
        $('#Colecao').fadeOut(500, function(){
            $('#Conteudo').css('backgroundImage', 'none');

            $('#VisualizacaoColecao').css("top", ($(window).height()-100 - $('#VisualizacaoColecao').height())/2  + 'px');

            $('#VisualizacaoColecao').fadeIn(500, function(){
                $('.zoom').jqzoom({
                    zoomWidth: 300,
                    zoomHeight: 305
                });
                $('.anythingSlider').anythingSlider({
                    easing: "easeInOutExpo",
                    autoPlay: false,
                    delay: 10000,
                    startStopped: true,
                    animationTime: 600,
                    hashTags: true,
                    buildNavigation: true,
                    pauseOnHover: true,
                    startText: "Próxima",
                    stopText: "Anterior"
                });
                $('.link_miniatura').click(function(){
                    var url = $(this).attr('href');
                    var title = $(this).attr('title');
                    $('#LinkCatalogoGrande').attr('href', url)
                    $('#ImagemCatalogo').fadeOut(300,function(){
                        $(this).attr('src', url).fadeIn(500)
                        $('#Legenda').html( title.replace(/\/{1}/gi,"<br />") );
                    })
                    return false;
                });
                

                 $('.colorbox_galeria').colorbox({
                    innerWidth		: 900,
                    innerHeight		: 530,
                    scrolling		: false
                });
                
            });
        })
        return false;
    });
	
    //Menu
    $('#SiteMenu').hover(
        //Abre o menu
        function(){
            $('#AbreMenu').toggleClass('aberto');
            //$('#Conteudo').css('opacity', 0.1)
            $('#SiteFundo').animate({
                opacity: 0.3
            },500)
            $('#Conteudo').animate({
                opacity: 0.1
            },500)
            $('object').hide()
            $('#ContainerMenu').stop( true, false );
            $('#ContainerMenu').animate({
                height	: 365
            }, 1000, 'easeOutBounce')
        },
        //Fecha o menu
        function(){
            $('#AbreMenu').toggleClass('aberto');
            //$('#Conteudo').css('opacity', 1)
            $('#SiteFundo').animate({
                opacity: 0.65
            },500)
            $('#Conteudo').animate({
                opacity: 1
            },500)
			
            $('#ContainerMenu').stop( true, false );
            $('#ContainerMenu').animate({
                height	: 0
            }, 1000, 'easeOutBounce',function(){
                $('object').show();
            })
			
        }
        );

	
   


}


/**
 * alert
 *
 * sobrescreve a função alert pela aparência do colorbox
 *
 * @access public
 * @return void
 **/
function alert(mensagem){
    $.fn.colorbox({
        open	: true,
        width   : 300,
        height  : 100,
        close	: "Fechar",
        html	:'<p class="palert">'+mensagem+'</p>'
    });
    return false;
}
