﻿var $recent = $('.recent h2');
var $win = $(window);


function filterWorkHashtags() {
    if (location.href.indexOf('work') > -1) {

     
        if (location.hash != '') {
            var hash = location.hash.replace("#", "");
            var getFilter = $('.work__filters a').filter('[data-tag="' + hash + '"]');

            if (getFilter.length > 0) {
                $('.work__filters a').filter('[data-tag="' + hash + '"]')[0].click();
            }
        }
    }
}





$(document).ready(function () {




    // Check for the 'odg_visitor' cookie
        odgVisitor = Cookies.get('odg_visitor');

        if (!odgVisitor) {
            // If 'odg_visitor' cookie is null, undefined or empty, create a new one
            var randomNumber = Math.floor(Math.random() * 1000000) + 1; // Generate a random number between 1 and 1,000,000
            Cookies.set('odg_visitor', randomNumber, { expires: 365 }); // Set 'odg_visitor' cookie with the random number as value, and expire in 7 days
     
        } 

        odgVisitor = Cookies.get('odg_visitor');
        if (!odgVisitor) {
            odgVisitor="none"; 
        }
 


    setTimeout(function () {

       

    
        //if useragent doesnt contain the word "bot"
        if (navigator.userAgent.toLowerCase().indexOf('bot') == -1) {
            $.get("/meetODG/track?msg=" + location.href  + " - " + odgVisitor, function (data) {

            });
}
    }, 2000);



        //if hashtag equals perdue, click the perdue video
if (location.hash == "#perdue") {
    $.get("/meetODG/track?msg=Perdue Video Sig " + odgVisitor, function (data) {

    });
    $("#perduelauncher").click();
} 



        //if hashtag equals perdue, click the perdue video
        if (location.hash == "#bcreaaccess") {
            $.get("/meetODG/track?msg=BCREA Access Hash " + odgVisitor, function (data) {
        
            });
            openFeedPost('BCREAAccess');
        } 
        
        

            //if hashtag equals perdue, click the perdue video
            if (location.hash == "#techtopaws") {
                $.get("/meetODG/track?msg=Techtop Hash" + odgVisitor, function (data) {
            
                });
                openFeedPost('Techtop');
            } 
            

                //if hashtag equals perdue, click the perdue video
        if (location.hash == "#pharmacy") {
            $.get("/meetODG/track?msg=Pharmacy Hash " + odgVisitor, function (data) {
        
            });
            openFeedPost('Pharmacy');
        } 
        



    $(".videoIntro").click(function(){

        $(".overviewVideoLauncher").click();


        gtag('event', 'ODG Intro Video', {
            'event_category': 'Videos',
            'event_label': 'ODG'
        });
        $.get("/meetODG/track?msg=ODG Intro Video", function (data) {

        });
 

    });

    $(".videoIntro__close").click(function(){
        event.stopPropagation();
        $(".videoIntro").hide();

        document.cookie = "hideVideo=true;max-age=300;path=/";

        $.get("/meetODG/track?msg=CLOSE ODG Intro Video", function (data) {

        });

    });

 

       // Function to check if the user has scrolled down 800 pixels
       function checkScrollPosition() {
        const scrollPosition = $(window).scrollTop(); // Get the current scroll position
   
        var displayVideoPosition=1100;
        var isHome=false;
        var isAbout=false;

        if (location.href == "https://www.opacitydesigngroup.com" || location.href == "https://www.opacitydesigngroup.com/")
        {
            isHome=true;
            
        }

        if (location.href == "https://opacitydesigngroup.com" || location.href == "https://opacitydesigngroup.com/")
        {
            isHome=true;
            
        }
        if (location.href == "https://www.opacitydesigngroup.com/about" || location.href == "https://www.opacitydesigngroup.com/about/")
        {
            isAbout=true;
             displayVideoPosition=0;

             if ($(window).width() < 768) {
                displayVideoPosition = 1800;
            }
        }
        
    
        if ((scrollPosition >= displayVideoPosition) && (isHome || isAbout)) {
          // User has scrolled down 800 pixels or more

        

          var checkHideCookie = Cookies.get('hideVideo');

         
          if (typeof checkHideCookie === 'undefined') {
            $(".videoIntro").show();
          }
   
        }
        else{
            $(".videoIntro").hide();
        
        }
      }
  
      // Attach the scroll event listener
      $(window).on("scroll", checkScrollPosition);


      //display on about page without scrolling, unless video previousy closed.
      if (location.href == "https://www.opacitydesigngroup.com/about" || location.href == "https://www.opacitydesigngroup.com/about/")
      {
        var checkHideCookie = Cookies.get('hideVideo');

        if (typeof checkHideCookie === 'undefined') {

            if ($(window).width() > 768) {
                $(".videoIntro").show();
            }
    
        }
      }
      

   
    filterWorkHashtags();

    $(".work__client").click(function (e) {

        //prevent anything else from being clicked

        odgVisitor = Cookies.get('odg_visitor');
        if (!odgVisitor) {
            odgVisitor="none"; 
        }
 


        if ($(this).hasClass("hdvideo")) {
            $.get("/meetODG/track?msg=HD Video " + odgVisitor, function (data) {

            });
            $("#hdlauncher").click();
        }
        else if ($(this).hasClass("perduevideo")) {

            
            $.get("/meetODG/track?msg=Perdue Video " + odgVisitor, function (data) {

            });

            $("#perduelauncher").click();
        }
        else {
            location.href = $(this).data("href");
        }
        
        e.stopPropagation();
        
    });

    $(".work__client a").click(function (e) {

        //prevent anything else from being clicked
        e.stopPropagation();

    });


    $(".recent__client").click(function (e) {

        //prevent anything else from being clicked
      
        odgVisitor = Cookies.get('odg_visitor');
        if (!odgVisitor) {
            odgVisitor="none"; 
        }
 


        if ($(this).hasClass("hdvideo")) {
            $.get("/meetODG/track?msg=HD Video " + odgVisitor, function (data) {

            });
            $("#hdlauncher").click();
        }
        else if ($(this).hasClass("perduevideo")) {

            
            $.get("/meetODG/track?msg=Perdue Video " + odgVisitor, function (data) {

            });

            $("#perduelauncher").click();
        }
        else {
            location.href = $(this).data("href");
        }
        e.stopPropagation();
        
    });

    $(".recent__client a").click(function (e) {

        //prevent anything else from being clicked
        e.stopPropagation();

    });
});


$(window).on('hashchange', function () {
    filterWorkHashtags();

    //scroll to top of page without jquery
    if (location.pathname=='/work')
        document.body.scrollTop = document.documentElement.scrollTop = 0;


        
});



$(".work__filters a").click(function () {

    $(".work__filters a").removeClass("selected");

    $(this).addClass("selected");

    var desiredTag = $(this).data("tag");


    $(".work__inner .work__client").each(function (index,item) {

        $(this).hide().appendTo('#tempContainer');
    })




        $("#tempContainer .work__client").each(function (index, item) {

            
            if (desiredTag == "all" || $(this).data("tags").indexOf(desiredTag) > -1) {
                
                $(this).appendTo('.work__inner').show();
            }
        })


    $('.work__inner .work__client').sort(function (a, b) {
        // Get the values of the data-sort attributes
        var aSort = $(a).data('sort');
        var bSort = $(b).data('sort');

        // Compare the values and return a sorting order
        if (aSort < bSort) {
            return -1;
        }
        if (aSort > bSort) {
            return 1;
        }

        // If the values are equal, return 0
        return 0;
    }).appendTo('.work__inner');



});



function elementScrolled(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top;
    return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
}


function elementScrolledToTop(elem) {
    if (($(elem).position().top - $(window).scrollTop()) < 5) {
        return true;
    }
    return false;
}



function elementThirdScrolled(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top;
    var elemHeight = $(elem).height() / 3;
    return (((elemTop + elemHeight) <= docViewBottom) && (elemTop >= docViewTop));
}





$win.on('scroll', function () {


    if ($('.recent__title').length > 0) {
        var top = $win.scrollTop();

        var translate = "translateX(-" + top / 50 + "%)";
        $('.recent__title').css("transform", translate);

        //var windowHeight = $(window).height();
        //var docViewTop = $(window).scrollTop();
        //var docViewBottom = docViewTop + $(window).height();
        //var distanceFromBottom = docViewBottom - $(".recent").position().top;
        //console.log(distanceFromBottom);

        //var percentClientInMiddle = ((distanceFromBottom - 250) / windowHeight) * 100

        //console.log(percentClientInMiddle);



        //var translateClientsText = "translateX(-" + (percentClientInMiddle / 4) + "%)";
        //$('.clients > h5').css("transform", translateClientsText);

        //if (elementThirdScrolled('.recent__client-tiletown')) {
        //    if (!$(".recent__client-tiletown").hasClass("animateFadeUp")) {
        //        $(".recent__client-tiletown").addClass("animateFadeUp");
        //    }
        //}

        ////   if(elementThirdScrolled('.client.enwave')) { 
        ////     if (!$(".client.enwave").hasClass("animateFadeUp"))
        ////     {
        ////       $(".client.enwave").addClass("animateFadeUp");
        ////     }
        //// }

        //if (elementThirdScrolled('.recent__client-pello')) {
        //    if (!$(".recent__client-pello").hasClass("animateFadeUp")) {
        //        $(".recent__client-pello").addClass("animateFadeUp");
        //    }
        //}
    }

   


});



var browserLevel = 0;
var browserZoomLevel = Math.round(window.devicePixelRatio * 100);



$(window).resize(function () {

    var newLevel = Math.round(window.devicePixelRatio * 100);
    console.log("current zoomn level is" + browserZoomLevel);

    console.log("new zoom level is " + newLevel);
    if (newLevel < browserZoomLevel) {
        $("html").removeClass("level" + browserLevel);
        browserLevel = browserLevel - 1;
    }
    else if (newLevel > browserZoomLevel) {
        $("html").removeClass("level" + browserLevel);
        browserLevel = browserLevel + 1;
    }
    console.log("setting is: " + browserLevel);
    browserZoomLevel = newLevel;
    // $("html").addClass("level" + browserLevel);



    document.documentElement.style.setProperty('--text-multiplier', '1');


    if (browserLevel != 0) {
        amountToScale = 1 + (browserLevel * 0.05);
        document.documentElement.style.setProperty('--text-multiplier', amountToScale);
    }




});



document.addEventListener("DOMContentLoaded", function () {
    var lazyVideos = [].slice.call(document.querySelectorAll("video.lazy"));


    // Store the original src in data-original-src
    lazyVideos.forEach(function (lazyVideo) {
  
        lazyVideo.setAttribute("data-original-src", lazyVideo.getAttribute("data-src"));
    });

    if ("IntersectionObserver" in window) {
        var lazyVideoObserver = new IntersectionObserver(function (entries, observer) {
            entries.forEach(function (video) {
                if (video.isIntersecting) {
                    for (var source in video.target.children) {
                        var videoSource = video.target.children[source];
                        if (typeof videoSource.tagName === "string" && videoSource.tagName === "SOURCE") {
                            videoSource.src = videoSource.dataset.src;
                        }
                    }

                    video.target.load();
                    video.target.classList.remove("lazy");
                    lazyVideoObserver.unobserve(video.target);
                }
            });
        });

        lazyVideos.forEach(function (lazyVideo) {
            lazyVideoObserver.observe(lazyVideo);
        });
    }
});


document.addEventListener("DOMContentLoaded", function () {
    var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));

    // Store the original src in data-original-src
    lazyImages.forEach(function (lazyImage) {
        lazyImage.setAttribute("data-original-src", lazyImage.getAttribute("data-src"));
    });

    if ("IntersectionObserver" in window) {
        let lazyImageObserver = new IntersectionObserver(function (entries, observer) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting) {
                    let lazyImage = entry.target;
                    lazyImage.src = lazyImage.dataset.src;
                    lazyImage.classList.remove("lazy");
                    lazyImageObserver.unobserve(lazyImage);
                }
            });
        });

        lazyImages.forEach(function (lazyImage) {
            lazyImageObserver.observe(lazyImage);
        });
    } else {
        // Possibly fall back to event handlers here
    }
});


