jQuery(document).ready(function($){

	var iTheWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
	var iTheHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

	$(window).scroll(function() {


		if ($(".loadmore").hasClass("ready"))
		{

    		if (isInViewport($(".loadmore.ready")) ) {
    			// run ajax and load page 2 posts

    			var iThePage = $(".holder .row.inner.masonry.ready:last-of-type").attr("data-page")*1 + 1;

    			//var iTheMax = $(".holder .row.inner.masonry.ready:last-of-type").attr("data-max-page")*1;

    			//console.log(iTheMax);
    			console.log(iThePage);

    			var sTheCat = $(".loadmore").data("category");

    			var $content = $(".holder");

    			$(".loadmore").removeClass("ready");

    			$.ajax({
    				type: "post",
    				url: "/wp-admin/admin-ajax.php",
    				data:
    					{
    						action: "acg_loadmore",
    						page: iThePage,
    						cat: sTheCat

    					},
    				beforeSend: function () {

    					//nah


    				},
    				success: function (response) {
    					$content.append(response);

    					if (response){

    					//if (iThePage < iTheMax)

    				    	$(".loadmore").addClass("ready");

    				    	$(".holder .row.inner.masonry.ready:last-of-type").attr("data-page", iThePage);

    				    	console.log(iThePage);

                        }

    				},
    				error: function (error) {
    					//nah
    				}
    			});
    			return false;
    		}

		}
	});

    function isInViewport($this) {

        var elementTop = $this.offset().top;
        var elementBottom = elementTop + $this.outerHeight();
        var viewportTop = $(window).scrollTop();
        var viewportBottom = viewportTop + $(window).height();
        return elementBottom > viewportTop && elementTop < viewportBottom;
    };


	$(document).on("click", "[data-href]", function(e)
    {
		window.location.href = $(this).attr('data-href');
	});

	$('.img-holder').imageScroll({
		speed: 0.1
	});


	$(document).on("click", ".burger", function(e)
	{

   		if ($("nav.mobile").hasClass("hidden"))
   		{
   			$("nav.mobile").removeClass("hidden");
   		}
   		else
   		{
			$("nav.mobile").addClass("hidden");

   		}
  	});

  	$.extend($.scrollTo.defaults, {
	  axis: 'y',
	  duration: 800
	});

	$(".arrow.down").click( function(event) {

		if ( $(this).closest(".panel").next().length > 0 ) {
			$oTheElement = $(this).closest(".panel").next();
		} else {
			$oTheElement = $("footer");
		}
		$.scrollTo($oTheElement.offset().top, { });
	});




	$(".arrow.up").click( function(event) {
		$.scrollTo(0, { });
	});

	doResize();

	$( "body" ).animate({
	    opacity: 1
	  }, 1000, function() {
	  	doResize();
		$("body").addClass("loaded");
	});

	$(window).on("resize", function()
	{
		doResize();
	});

  	function doResize()
  	{

  		$(".panel").removeAttr("style");
  		iTheWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
		iTheHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

		var iTheDiff = 0;
		var iTheOuterHeight = 0;

		var instances = $('.img-holder');
		var instance = $(instances.get(0));


        if (iTheWidth > 767)
        {

            var heights = [];

            if ($(".container.offices").length > 0 )
            {

                $(".container.offices .row").find(".office").each ( function (index) {

                    heights.push($(this).find(".overlay").outerHeight());

                });

                $(".container.offices .row").find(".office").each ( function (index) {


                    $(this).find(".overlay").removeAttr("style");

                    if (index == 0 && $(this).find(".overlay").outerHeight() < heights[1])
                    {
                        $(this).find(".overlay").css({ "height" : heights[1] + "px"});
                    }

                    if (index == 1 && $(this).find(".overlay").outerHeight() < heights[0])
                    {
                        $(this).find(".overlay").css({ "height" : heights[1] + "px"});
                    }

                    if (index == 2 && $(this).find(".overlay").outerHeight() < heights[3])
                    {
                        $(this).find(".overlay").css({ "height" : heights[3] + "px"});
                    }

                    if (index == 3 && $(this).find(".overlay").outerHeight() < heights[2])
                    {
                        $(this).find(".overlay").css({ "height" : heights[2] + "px"});
                    }

                });


            }

        }
        else
        {
            if ($(".container.offices").length > 0 )
            {

                $(".container.offices .row").find(".office").each ( function (index) {


                    $(this).find(".overlay").removeAttr("style");
                });

            }

        }




		if ($(".flexible_content_facts").length > 0 && $(".flexible_content_facts").find(".columns.col-xs-12").length > 1)
		{

			if (iTheWidth > 769)
			{
				var iTheH = 0;
				$(".flexible_content_facts").find(".columns.col-xs-12").each( function (index) {

					console.log($(this).height());
					if ($(this).height() >= iTheH)
					{
						console.log($(this).height());
						iTheH = $(this).height();
					}
				});

				$(".flexible_content_facts").find(".columns.col-xs-12").css({"height" : iTheH + "px"});

			}
			else
			{


			}

		}



		if ($(".panel .columns").length > 0)
		{

			if (iTheWidth > 769)
			{
				var iTheH = 0;
				$(".panel .columns").each( function (index) {

					console.log($(this).height());
					if ($(this).height() >= iTheH)
					{
						console.log($(this).height());
						iTheH = $(this).height();
					}
				});

				$(".panel .columns").css({"height" : iTheH + "px"});

			}
			else
			{


			}

		}




		$(".panel").each( function(index)
		{

			iTheOuterHeight = $(this).find(".outer").outerHeight();

			//console.log( $(this).attr("class") );
			//console.log( iTheOuterHeight );
			//console.log( iTheHeight );

			if ( iTheOuterHeight > iTheHeight   )
			{
				if (iTheWidth > 769)
				{
					h = iTheOuterHeight + 200;
				}
				else
				{
					h = iTheOuterHeight + 80;
				}
			}
			else
			{
				h = iTheHeight;
			}


				$(this).css({"height" : h + "px"});
				$(this).css({"width" : iTheWidth + "px"});
				$(this).attr("data-width", iTheWidth).attr("data-height", iTheOuterHeight).attr("data-window-height", iTheHeight);

		});

		$(".subsidiary").each( function (index)
		{
			if (iTheWidth > 769)
			{
				$(this).find(".flex").css({"height" : ($(this).width()*0.5) + "px"/*, "padding-top" : iTheDiff + "px", "padding-bottom" : iTheDiff + "px"*/});
			}
		});

		if (instance)
		{
			instance.imageScroll('enable');

			if ( $(".flexible_content_callout").find(".over"))
			{
				$(".flexible_content_callout").find(".over").appendTo($('.imageHolder'));

				if (iTheWidth <= 768)
				{
					$(".container.over").css({"width" : (iTheWidth * 0.9) + "px"});
					//alert("fu");

				}
			}
		}

		if (iTheWidth > 769)
		{





			if ($(".digit").length > 0)
			{
				var elW = $(".digit").width();
				$('.digit').closest(".inner").find(".absolute").css({"margin-left" : (elW * 1.2) + "px"});
			}

		}
		else
		{






			if ($(".digit").length > 0)
			{
				$(".digit").closest(".inner").find(".absolute").removeAttr("style");
			}
		}

		if( $(".flexible_content_video").length > 0)
		{
			var w = $(".flexible_content_video").width();

			var ow = $(".flexible_content_video iframe").attr("width");

			var oh = $(".flexible_content_video iframe").attr("height");

			var diff = w/ow;

			$(".flexible_content_video iframe").attr("width", $(".flexible_content_video").width() );

			$(".flexible_content_video iframe").attr("height", oh*diff);

		}

  	}
});




