//html/themes/incorporadora/js/theme
var validCaptcha = $('#validCaptcha').val();

$(window).on('load', function(){
    
    if(typeof grecaptcha !== "undefined") {
        widgetId1 = grecaptcha.render('gcaptcha', {
            'sitekey' : sitekey,
            'theme' : 'light'
		});
    }
    
	$(".loader-page").delay(100).fadeOut("slow");
	$(".show-page").toggle("clip");
	
	$(".container-alert .alert-effect").delay(1000).show("clip");
	
	var cond_click = true;
    $('body').on("click", ".box-navbar", function() {
        if(cond_click == true){
            $(".ico-menu").hide();
            $(".ico-clear").show();
            cond_click = false;
        }
        else if (cond_click == false){
            $(".ico-menu").show();
            $(".ico-clear").hide();
            cond_click = true;
        }
    })
    
    $('body').on("click", ".actIcoPesquisa", function() {
        $(".box-container-logo").hide();
        $(".box-icons-actions").hide();
        $(".box-pesquisa").show();
    })
    
    $('body').on("click", ".ico-voltar", function() {
        $(".box-container-logo").show();
        $(".box-icons-actions").show();
        $(".box-pesquisa").hide();
        $(".box-pesquisa").css('border-bottom','none');
        $(".autocomplete-responsive .box-pesquisa.ui-front .ui-widget.ui-widget-content").removeClass("d-block");
        $(".autocomplete-responsive .box-pesquisa.ui-front .ui-widget.ui-widget-content").hide();
    })
    
    $('body').on("click", ".actIcoPesquisaInactive", function() {
      return false;
    });
});

$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})

//center content div
var h = $(this).height();
var w = $(this).width();
var headerHome = 75;
var footerHome = 141;
var header = 75;
var footer = 60;
var header2 = 75;
var divs = header + footer;
var total = h - divs;

var totalBody = h - (headerHome + footerHome); 
var heightPage = $(".heightPage").height();



//scroll height autocomplete
$(document).on("click", ".act-busca-home", function() {
   scrollAutocomplete(".content-form.ui-front",$(this));
})

$(document).on("input", ".act-busca-home", function() {
   scrollAutocomplete(".content-form.ui-front",$(this));
})

$(document).on("click", ".inputPesquisa", function() {
    scrollAutocomplete(".navbar.ui-front",$(this));
})

$(document).on("input", ".inputPesquisa", function() {
    scrollAutocomplete(".navbar.ui-front",$(this));
})
 
function scrollAutocomplete(source_class,input){
    
    if(w > 991){
        var ui_menu_item = $(source_class+" li.ui-menu-item").outerHeight(true);
        var i = 0;
        
        $(source_class+" ul.ui-autocomplete li.ui-menu-item").each(function(){
             i++;
        });
        
        var total = i*ui_menu_item;
        var value = input.val();
        
        /*if(total >=200 || value.length === 0){*/
        if(total >=200){
            $(source_class+" ul.ui-autocomplete").addClass("ui-autocomplete-scroll");
        }else{
            $(source_class+" ul.ui-autocomplete").removeClass("ui-autocomplete-scroll");
        }
    }
}
/*end*/

$(".container-frame-page-home").css({"height": totalBody});

$(".container-frame-page").css({"height": total});

if(total < heightPage){
    $(".sidebar-left").css({"height": heightPage});
}else{
    $(".sidebar-left").css({"height": total});
}

var container_header_sidebar_left = $(".container-header-sidebar-left").height();
totalContainerPainel = (total - container_header_sidebar_left)-45;

$(".collapseHeight").on("shown.bs.collapse", function() {
  totalPanel = $(this).parents(".list-group.panel").height();
  addHeightPanel(totalPanel)
});
$(".collapseHeight").on("hidden.bs.collapse", function() {
  totalPanel = $(this).parents(".list-group.panel").height();
  removeHeightPanel(totalPanel)
});

function addHeightPanel(totalPanel){
    if(totalPanel >= totalContainerPainel){
        $(".list-group.panel").addClass("scroll-height").css("height",totalContainerPainel);
    }
}

function removeHeightPanel(totalPanel){
    
    totalPanel = $(".list-group.panel").css("height","auto").height();
    
    if(totalPanel < totalContainerPainel){
        $(".list-group.panel").removeClass("scroll-height").css("height","auto");
    }else{
        addHeightPanel(totalPanel)
    }
}

if(w <=767){
    var divs = header2 + footer;
    var total = h - divs;
    $(".container-frame-page").css({"height": total});
}

$(".collapse-nav").on("shown.bs.collapse", function() {
    $('.box-icons-actions .ico-pesquisa').removeClass('actIcoPesquisa');
    $('.box-icons-actions .ico-pesquisa').addClass('actIcoPesquisaInactive');
});
$(".collapse-nav").on("hidden.bs.collapse", function() {
    $('.box-icons-actions .ico-pesquisa').removeClass('actIcoPesquisaInactive');
    $('.box-icons-actions .ico-pesquisa').addClass('actIcoPesquisa');
});

$(".collapse-nav.collapse-nav-landing").on("shown.bs.collapse", function() {
    $('body').find("header.bg-theme").removeClass('fixed-top');
    $/*('html, body').animate({scrollTop: '0px'}, 0);*/
});
$(".collapse-nav.collapse-nav-landing").on("hidden.bs.collapse", function() {
    $('body').find("header.bg-theme").addClass('fixed-top');
});


$(".inputPesquisaMobile").click(function(){
   /* $(".box-caret.box-navbar-nav").show();*/
    $(".autocomplete-responsive .box-pesquisa.ui-front .ui-widget.ui-widget-content").addClass("d-block");
})

$(".inputPesquisaMobileHome").click(function(){
    /*$(".box-caret.box-navbar-nav").show();*/
    $(".box-pesquisa").css('border-bottom','1px solid #ccc');
    $(".autocomplete-responsive .box-pesquisa.ui-front .ui-widget.ui-widget-content").addClass("d-block");
})
//end


//action voltar o topo
$(document).scroll(function () {
	
	var posicaoScroll = $(document).scrollTop();
	
	if(w >= 768){
	    if(posicaoScroll > 1) 
        {
            $(".toTop").css({bottom: '120px'});
        } 
        else 
        {
        	$(".toTop").css({bottom: '-100px'});
        }
	}
});

//link ancora
$("a.linkAnchor").click(function(event){
    
    event.preventDefault();
    
	linkAncor = $(this).attr('href');
	
	event.preventDefault();
    $('html, body').animate(
    {
        scrollTop: $(linkAncor).offset().top - 60
    },1000);
   
});

//modal login site
$('body').on("click", ".openLogin", function() {
    $('.modal-login-site').modal('show');
});




$('#login-form-modal').submit(function(event){	

    event.preventDefault();
    
    
    loadButton($('.btn-login-modal'));
    
    if($('textarea[name=g-recaptcha-response]') != undefined)
	{
		var captcha = $('textarea[name=g-recaptcha-response]').val();
		if(captcha == '' && validCaptcha == true){
			$('#content-error').html('<div class="message login-warning alert alert-warning"><i class="fa fa-warning fa-lg mr-2"></i>Por favor preencher o captcha.</div>');
			loadButton(null,false);
			return false;
		}
	}
    
    //var email = $('#login-form-modal input[name=email]').val()
	//var password = $('#login-form-modal input[name=password]').val()	
	
    if($('#login-form-modal input[name=lembrar]').is(':checked')){
		var lembrar = 1;
	}else{
		var lembrar = 0;
	}
	
	var target = $(this).attr('action');
	
	/*var data = {
			email: email,
			password: password,
			lembrar: lembrar,
			modal: true
		}*/
		
	$data = getForm('#login-form-modal');
	data.push({"name": "lembrar", "value": lembrar});
	data.push({"name": "modal", "value": true});
	
	
	$.ajax({
		url: target,
		dataType: 'json',
		type: 'POST',
		data: data,
		success: function(retorno)
		{
			if (retorno.valid === true)
			{
			    if (retorno.redirect == null)
			        location.reload();
			    else
			        window.location = retorno.redirect;
			    
			}
			else
			{
			    //if(retorno.reset == true)
			    if(typeof grecaptcha !== "undefined") {
			        grecaptcha.reset(widgetId1);
			    }
			    
			    $.ajax({
            		url: base_url+"comdono/ajax/login_ajax/checkCaptcha",
            		dataType: 'json',
            		type: 'POST',
            		success: function(retorno)
            		{
            		    if(retorno.valid == true){
            		        $('.captcha').show();
            		        validCaptcha = true;
            		    }
                    }
    		    });
    		    
			    loadButton(null,false);
		        
                ico = '<img src="'+base_url+'/assets/img/comdono/icons/error.png">';
		        $('#login-form-modal #content-error').show();
		        $('#login-form-modal #content-error').html('<label class="error-login"><span>'+ico+'</span>'+retorno.msg+'</label>');
		        
			}
		},
		error: function(error)
		{
		    loadButton(null,false);
			/*$(":text,input[name=email]", $("#login-form-modal")).each(function () {
	            $(this).val("");			           
	        });
	        $(":text,input[name=password]", $("#login-form-modal")).each(function () {
	            $(this).val("");			           
	        });
			
			$(":checkbox", $("#login-form-modal")).each(function () {
	            $(this).prop({ checked: false });
	        });*/
			
			ico = '<img src="'+base_url+'/assets/img/comdono/icons/error.png">';
	        $('#login-form-modal #content-error').show();
	        $('#login-form-modal #content-error').html('<label class="error-login"><span>'+ico+'</span>Não foi possível realizar o acesso! Falha na Comunicação.</label>');
			
		}
	});
	return false;
});


function getForm($form)
{
    console.log($form);
    
    data = [];
    var nameRadio = [];
    
    //$($form).find('input:not([type=radio]):input:not([type=checkbox]):input:not([type=file])').each(function(key,value){
    $($form).find('input:not([type=radio]):input:not([type=checkbox]):input:not([type=file])').each(function(key,value){
        if(this.name != "")
            data.push({"name": this.name, "value": this.value});
    });
    
    $($form).find('input[type=file]').each(function(key,value){
        if(this.name != ""){
            data.push({"name": this.name, "value": $(this).data('value')});
        }
    });
    
    
    
    $($form).find('textarea').each(function(key,value)    {
        if(this.name != ""){
            
            if(typeof CKEDITOR !== 'undefined')
                data.push({"name": this.name, "value": CKEDITOR.instances[this.name].getData()});
            else
                data.push({"name": this.name, "value": this.value});
        }
    });
    
    $($form).find('input[type=radio]').each(function(key,value){
        if(nameRadio[this.name] == undefined)
            nameRadio[this.name] = '';
        if($(this).is(':checked'))
            nameRadio[this.name] = this.value;
    });
    
    for(var name in nameRadio){
        data.push({"name": name, "value": nameRadio[name]});
    };
    
    $($form).find('select option:selected').each(function(key,value)    {
        if(this.parentElement.localName == 'select')
            data.push({"name": this.parentElement.name, "value": this.value});
            
        if(this.parentElement.parentElement.localName == 'select')
            data.push({"name": this.parentElement.parentElement.name, "value": this.value});
    });
    
    $($form).find('input[type=checkbox]:checked').each(function(key,value)    {
        if(this.name != "")
            data.push({"name": this.name, "value": this.value});
    });
    
    $($form).find('input[type=checkbox]:not(:checked)').each(function(key,value)    {
        if(this.name != "")
            data.push({"name": this.name, "value": ""});
    });
    
    return data;
}

//excluir foto
/*$(".act-click").click(function(){
    var value = $(this).parent().find(".container-exluir.pergunta").show('clip');
    $(this).hide();
})*/
//class no input para redirecionar pagina
/*$("body").on("click",".act-busca", function() {
    window.location.href= base_url+"busca";
})*/

window.mobileAndTabletcheck = function() {
  var check = false;
  (function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
  return check;
};