
/*-------------------------------------------------------------------------
 * RENDIFY - Custom jQuery Scripts
 * ------------------------------------------------------------------------

	1.	Plugins Init
	2.	Site Specific Functions
	3.	Shortcodes
	4.      Other Need Scripts (Plugins config, themes and etc)
	
-------------------------------------------------------------------------*/
"use strict";


jQuery(document).ready(function($){
	
	
/*------------------------------------------------------------------------*/
/*	1.	Plugins Init
/*------------------------------------------------------------------------*/
     
	
	
	
	/************** ToolTip *********************/


	function toolTipInit() {
		
			$(".main_menu li a").tooltip({
				placement: 'right'
			});
			
			
		
	}
	
	toolTipInit();



	/************** MixItUp Plugin *********************/
	$('#Grid').mixitup({
        effects: ['fade','grayscale'],
        easing: 'snap',
        transitionSpeed: 800
    });
	
	



	/************** Nice Scroll Plugin *********************/
	$("html").niceScroll({
		cursorcolor : '#E31E25',
		cursorborder : 0,
		zindex : 99999,
	});
	
	
	
    var loading = false;
	$(document).on("scroll",function(){ 	
		
		/*window.alert($(window).height());
		window.alert($(window).scrollTop());*/
		
		if(loading == false){		
			if(isOnScreen($(".gallery-item"))){
				loading = true;	
				$(".lazy").lazyload({effect : "fadeIn"});
            } 
			if(isOnScreen($(".portfolio-item"))){
				loading = true;	
				$(".lazy").lazyload({effect : "fadeIn"});
            }
        }
		
		function isOnScreen(element){
			var win = $(window);
			var screenTop = win.scrollTop();
			var screenBottom = screenTop + win.height();

			var elementTop = element.offset().top;
			var elementBottom = elementTop + element.height();
			
			return elementBottom > screenTop && elementTop < screenBottom;
		}
		
		
	});
	
	

	/************** LightBox *********************/
	$(function(){
		$('[data-rel="lightbox"]').lightbox();
	});


/*------------------------------------------------------------------------*/
/*	2.	Site Specific Functions
/*------------------------------------------------------------------------*/


	/************** Responsive Navigation *********************/

	$('.menu-toggle-btn').click(function(){
        $('.responsive_menu').stop(true,true).slideToggle();
    });

    $(".responsive_menu a").click(function(){
        	$('.responsive_menu').hide();
        });


    
	/************** Open Filters on gallery page *********************/
	$(".toggle-filter").click(function(){
		$(".filter-controls").slideToggle();
		return false;
	});



	/************** Open Different Pages *********************/
		
	$(".main_menu a").click(function(){
		var a =  $(this).attr('class');
		var b = a.split('-');
		var c = b[0];
		var d = b[1];
	    //window.alert(a);
		//window.alert(b);
		//window.alert(c);
		//window.alert(d);
   	    
		d = d.replace("homebutton", "");
		
		$("#menu-container .content").hide();
		$("#menu-container #menu-"+d).slideDown(400);
		$("#menu-container .homepage").hide();
	   
		document.title = $("#title"+d).val();
		window.history.pushState({url: "" + $(this).attr('href') + ""}, $(this).attr('title') , $(this).attr('href'));
		
			
		var imgfundo =  $("#imagemfundo"+d).val();
		var corfundo =  $("#corfundo"+d).val();
		$(".bg-image").fadeOut('slow', function(){
			$(this).css({
				'background-color' : corfundo,
				'background-image' : imgfundo,
				
			}).fadeIn('slow');
		});
		
		
		return false;
		
	    
	});

    /* atribuir um novo endereço de url para o link */ 
	$("a#comochegar").attr('href', 'http://maps.google.com/');
	$(".comochegar a").click(function(){
		/* definir para pedir localização somente quando clicar aqui...*/
	});
	
	
	/*$(".main_menu a")​​.click(function(e){
		e.preventDefault();
		window.history.pushState({url: "" + $(this).attr('href') + ""}, $(this).attr('title') , $(this).attr('href'));
	});​*/
 
	/*$(window).bind("popstate", function(e) {
		$("#menu-container").load(e.state.url);
		alert('teste');
	});*/
	
	/*window.addEventListener('popstate', function(event) {
		if (event.state) {
			alert('teste');
			$(".main-content").load(event.state.url);
		}
	}, false);*/
    
	$(".main_menu a.homebutton").click(function(){
		$("#menu-container .homepage").show();
		return false;
	});
	
	window.addEventListener('popstate', function(event) {
		if (event.state) {
			window.location.reload();
		}
	}, false);
	

	/************** Services Offer Effect *********************/
	$('.services .header .service-header').hover(function () {
	    var t = $(this);
	    t.find('h4').hide();
	    $(this).parent().find('.header-bg').stop(true, true).animate({
	        width: '100%'
	    }, 'fast', function () {
	        t.find('h4').addClass('active').fadeIn('fast')
	    })
	}, function () {
	    var t = $(this);
	    t.find('h4').hide();
	    t.parent().find('.header-bg').stop(true, true).animate({
	        width: 60
	    }, 'fast', function () {
	        t.find('h4').removeClass('active').fadeIn('fast')
	    });
	});

/*------------------------------------------------------------------------*/
/*	3.	Plugin lazy-loading  http://jquery.eisbehr.de/lazy/
/*------------------------------------------------------------------------*/
	
	$("#menu-container #menu-3").scroll(function() { 	
		$(".lazy").lazyload({effect : "fadeIn"});
	});
	
	
/*------------------------------------------------------------------------*/
/*	4.	Custom Select
/*------------------------------------------------------------------------*/

	var x, i, j, l, ll, selElmnt, a, b, c;
	/* Look for any elements with the class "custom-select": */
	x = document.getElementsByClassName("custom-select");
	l = x.length;
	for (i = 0; i < l; i++) {
	  selElmnt = x[i].getElementsByTagName("select")[0];
	  ll = selElmnt.length;
	  /* For each element, create a new DIV that will act as the selected item: */
	  a = document.createElement("DIV");
	  a.setAttribute("class", "select-selected");
	  a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
	  x[i].appendChild(a);
	  /* For each element, create a new DIV that will contain the option list: */
	  b = document.createElement("DIV");
	  b.setAttribute("class", "select-items select-hide");
	  for (j = 1; j < ll; j++) {
		/* For each option in the original select element,
		create a new DIV that will act as an option item: */
		c = document.createElement("DIV");
		c.innerHTML = selElmnt.options[j].innerHTML;
		c.addEventListener("click", function(e) {
			/* When an item is clicked, update the original select box,
			and the selected item: */
			var y, i, k, s, h, sl, yl;
			s = this.parentNode.parentNode.getElementsByTagName("select")[0];
			sl = s.length;
			h = this.parentNode.previousSibling;
			for (i = 0; i < sl; i++) {
			  if (s.options[i].innerHTML == this.innerHTML) {
				s.selectedIndex = i;
				h.innerHTML = this.innerHTML;
				y = this.parentNode.getElementsByClassName("same-as-selected");
				yl = y.length;
				for (k = 0; k < yl; k++) {
				  y[k].removeAttribute("class");
				}
				this.setAttribute("class", "same-as-selected");
				break;
			  }
			}
			h.click();
		});
		b.appendChild(c);
	  }
	  x[i].appendChild(b);
	  a.addEventListener("click", function(e) {
		/* When the select box is clicked, close any other select boxes,
		and open/close the current select box: */
		e.stopPropagation();
		closeAllSelect(this);
		this.nextSibling.classList.toggle("select-hide");
		this.classList.toggle("select-arrow-active");
	  });
	}

	function closeAllSelect(elmnt) {
	  /* A function that will close all select boxes in the document,
	  except the current select box: */
	  var x, y, i, xl, yl, arrNo = [];
	  x = document.getElementsByClassName("select-items");
	  y = document.getElementsByClassName("select-selected");
	  xl = x.length;
	  yl = y.length;
	  for (i = 0; i < yl; i++) {
		if (elmnt == y[i]) {
		  arrNo.push(i)
		} else {
		  y[i].classList.remove("select-arrow-active");
		}
	  }
	  for (i = 0; i < xl; i++) {
		if (arrNo.indexOf(i)) {
		  x[i].classList.add("select-hide");
		}
	  }
	}

	/* If the user clicks anywhere outside the select box,
	then close all select boxes: */
	document.addEventListener("click", closeAllSelect);
	
	
	/************** url amigaveis *****************/
	var url = window.location.href.toString();
	var x = url.split('&');
	var idtipopagina = x[1];
	var x = idtipopagina.split('=');
	var idtipopagina = x[0];
	var y = url.split('=');
	var idmenu = y[1];
	
	$("#menu-container .content").hide();
	$("#menu-container #menu-"+idmenu).slideDown(400);
	if (idtipopagina == 1) {
		$("#menu-container .homepage").show();
	}else{
		$("#menu-container .homepage").hide();
	}
	   
	document.title = $("#title"+idmenu).val();
	
	var imgfundo =  $("#imagemfundo"+idmenu).val();
		var corfundo =  $("#corfundo"+idmenu).val();
		$(".bg-image").fadeOut('slow', function(){
			$(this).css({
				'background-color' : corfundo,
				'background-image' : imgfundo,
				
			}).fadeIn('slow');
		});
	
	return false;
	
	/************** fim url amigaveis ***********/
	

});