/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 *         vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.center = function(params) {

        var options = {

            vertical: true,
            horizontal: true

        }
        op = jQuery.extend(options, params);

   this.each(function(){

        //initializing variables
        var $self = jQuery(this);
        //get the dimensions using dimensions plugin
        var width = $self.width();
        var height = $self.height();
        //get the paddings
        var paddingTop = parseInt($self.css("padding-top"));
        var paddingBottom = parseInt($self.css("padding-bottom"));
        //get the borders
        var borderTop = parseInt($self.css("border-top-width"));
        var borderBottom = parseInt($self.css("border-bottom-width"));
        //get the media of padding and borders
        var mediaBorder = (borderTop+borderBottom)/2;
        var mediaPadding = (paddingTop+paddingBottom)/2;
        //get the type of positioning
        var positionType = $self.parent().css("position");
        // get the half minus of width and height
        var halfWidth = (width/2)*(-1);
        var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
        // initializing the css properties
        var cssProp = {
            position: 'absolute'
        };

        if(op.vertical) {
            cssProp.height = height;
            cssProp.top = '50%';
            cssProp.marginTop = halfHeight;
        }
        if(op.horizontal) {
            cssProp.width = width;
            cssProp.left = '50%';
            cssProp.marginLeft = halfWidth;
        }
        //check the current position
        if(positionType == 'static') {
            $self.parent().css("position","relative");
        }
        //aplying the css
        $self.css(cssProp);


   });

};

$(function() {
    
    
    
    //positionPopup();

    $('#FecharPopup').click(function() {
        $('#PopupHome').remove();
    });

    $('#MenuTopo ul li a').each(function() {

        var obj = $(this);
        var Href = obj.attr('href');
        Href = replaceHref(Href);
        Href = Href.replace('/', '');

        if(Href == pageHref || (Href == 'index' && pageHref == '')) {
            obj.parent().addClass('BackGroundMenu');
        }
    });

    $('#ValorFinanciamento').submit(function() {
        if($('#valor').val() == '') {
            alert('Informe o valor a ser Financiado');
            return false;
        }
    });

    $('.BoxDestaques:nth-child(5)').css('margin-right', '0');
    $('.BoxDestaques:nth-child(10)').css('margin-right', '0');

    $('.BoxResultado:nth-child(5)').css('margin-right', '0');
    $('.BoxResultado:nth-child(10)').css('margin-right', '0');
    $('.BoxResultado:nth-child(15)').css('margin-right', '0');

    $('.EmptyOnClick').emptyonclick();  
    
    $('.Numeric').numeric();
    
    $('.valor').numeric({allow:",."});
    
    $('#valor').maskMoney({symbol:"",decimal:",",thousands:".",precision:2});
    
    $('#ImgFotoGrande a').lightBox({
        txtImage : "Imagem", 
        txtOf : "de",
        setNavigation:false
    });  

    function loadPhoto (origin) {

        $('#ImgFotoGrande a img').attr('src', $(origin).attr('href'));
        $('#ImgFotoGrande a').attr('href', $(origin).attr('href'));
        $('#ImgFotoGrande a img').attr('class', $(origin).children().attr('class'));
        $('#ImgFotoGrande a').attr('title', $(origin).attr('title'));
        $('#ImgFotoGrande a img').attr('height', '224');

        if($(origin).children().attr('className') == 'horizontal')
            $('#ImgFotoGrande a img').attr('width', 300);
        else
            $('#ImgFotoGrande a img').removeAttr('width');
    }
    
    loadPhoto('.itemFoto');

    $('.itemFoto').click(function () {
        loadPhoto(this);
        return false;
    });

    // <![CDATA[
    if($("#CarrosselGaleria").width() > 0 && $("#CarrosselGaleria").height() > 0){
        $("#CarrosselGaleria").jcarousel({
            animation: 1500,
            visible: 5,
            scroll: 5
        })
    }
    // ]]>
    
    
    //form detalhes
    
    $('#FoneContato').change(function() {
        $(this).mask("(99)9999-9999");
    });

    $('#FrmMaisInfo').submit(function() {
        
        if($('#NomeContato').val() == 'Nome') {
            $('#NomeContato').val('');
        }

        if($('#EmailContato').val() == 'Email') {
            $('#EmailContato').val('');
        }

        if($('#FoneContato').val() == 'Telefone') {
            $('#FoneContato').val('');
        }
        
    });

    var divDetalhes = '<div class="ImgRequired"></div>';

    $("#FrmMaisInfo").validate({
        
        rules : {
            EmailContato:{
                email: true,
                required : true
            }
        },

        messages: {
            NomeContato: divDetalhes,
            EmailContato: divDetalhes,
            FoneContato: divDetalhes
        },

        submitHandler : function() {

            var NomeContato = $('#NomeContato').val();
            var EmailContato = $('#EmailContato').val();
            var FoneContato = $('#FoneContato').val();
            var TxtMsgMaisInfo = $('#TxtMsgMaisInfo').val();
            //var CodigoImovel = $('#DireitaForm + input:hidden').val();
            var CodigoImovel = $('input[name=CodigoImovel]').val();
            var OptContatoDetalhes = $('#OptContatoDetalhes').val();
            var lojaId = $('input[name=lojaId]').val();
            //alert(CodigoImovel); return false;
            $.ajax({
                url: '/default/contato/formdetalhes/',
                type: 'POST',
                data: ({
                    NomeContato : NomeContato,
                    EmailContato : EmailContato,
                    FoneContato : FoneContato,
                    TxtMsgMaisInfo : TxtMsgMaisInfo,
                    CodigoImovel : CodigoImovel,
                    OptContatoDetalhes : OptContatoDetalhes,
                    lojaId : lojaId
                }),
                success: function(data){
                    $('#conteudoMaisInformacoes').html(data);
                }
            });
            
        }
        
    });
    
    //form peca
    $('#FrmMaisInfoPeca').submit(function() {

        if($('#NomeContato').val() == 'Nome') {
            $('#NomeContato').val('');
        }

        if($('#EmailContato').val() == 'Email') {
            $('#EmailContato').val('');
        }

        if($('#FoneContato').val() == 'Telefone') {
            $('#FoneContato').val('');
        }

    });

    var divPeca = '<div class="ImgRequiredPeca"></div>';

    $("#FrmMaisInfoPeca").validate({
        
        rules : {
            EmailContato:{
                email: true,
                required : true
            }
        },

        messages: {
            NomeContato: divPeca,
            EmailContato: divPeca,
            FoneContato: divPeca
        }

    });

    //fale conosco
    $('#FrmMaisInfoPeca').submit(function() {

        if($('#NomeContato').val() == 'Nome') {
            $('#NomeContato').val('');
        }

        if($('#EmailContato').val() == 'Email') {
            $('#EmailContato').val('');
        }

        if($('#FoneContato').val() == 'Telefone') {
            $('#FoneContato').val('');
        }

    });

    $("#FrmMaisInfoPeca").validate({

        rules : {
            EmailContato:{
                email: true,
                required : true
            }
        },

        messages: {
            NomeContato: divPeca,
            EmailContato: divPeca,
            FoneContato: divPeca
        }

    });

    $('.openNyro').live('click', function(e) {
        e.preventDefault();

        var d = $(this).attr('rel').split('|');

        $.nyroModalManual({
            gallery: false,
            width: d[0],
            height: d[1],
            url: $(this).attr('href')
        });
    });

})

function popupradio(domain) {
    window.open(domain,"","width=540,height=340,scrollbars=0,resizable=0");
}

function popup(domain,width,height) {
    window.open(domain,"_blank","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=yes,width="+width+",height="+height);
}

function positionPopup() {

    if($('#PopupHome').length) {
        var widthPopup = $('#PopupHome').width();
        var width = $(document).width();
        var position = Math.round((width/2) - (widthPopup/2));

        $('#PopupHome').css('left', position+'px');   
        $('#PopupHome').css('visibility', 'visible');
        
    }

}
