  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-137043477-4');//Hide Video button on VDP
setTimeout(function(){
    $( '#media1-app-root .media-content .m-4' ).attr("style", "margin:16px 16px 50px 16px !important;");
    $( '#media1-app-root .media-content .media-gallery-toolbar div .p-3 .d-inline-block' ).each(function( index ) {
        if (index == 1) {
            $( this ).attr("style", "display: none !important;");
        }
    });
},500);
    
var CarVideos = null;

if (CarVideos != null) {

var VINMap = {};
var i = null;
for (i = 0; CarVideos.length > i; i += 1) {
    VINMap[CarVideos[i].VIN] = CarVideos[i].video;
    
}
 
var hasVIN = function(VIN) {
    return VINMap[VIN];
};

function youtube_parser(url){
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    var match = url.match(regExp);
    return (match&&match[7].length==11)? match[7] : false;
}

var VideoVIN = document.getElementsByName("vin")[0].value;
var CarVid = hasVIN(VideoVIN);

if (CarVid != null) {

function loadScript( url, callback ) {
  var script = document.createElement( "script" )
  script.type = "text/javascript";
  if(script.readyState) {  // only required for IE 9
    script.onreadystatechange = function() {
      if ( script.readyState === "loaded" || script.readyState === "complete" ) {
        script.onreadystatechange = null;
        callback();
      }
    };
  } else {  //Others
    script.onload = function() {
      callback();
    };
  }

  script.src = url;
  document.getElementsByTagName( "head" )[0].appendChild( script );
}

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

        // Moves all slides over 1 to make room for 360 video
        var OLDslideWidths = Array();
        var NEWslideWidths = Array();
        var PermWidth = null;

        $( '.photo-carousel .slider .slider-frame .slider-list .slider-slide' ).each(function( index ) {
        OLDslideWidths[index] = Math.round( $( this ).position().left * 10 ) / 10;
          //console.log( index + ": " + OLDslideWidths[index] );
        });

        $.each( OLDslideWidths, function (index, value ) {
            PermWidth = OLDslideWidths[1]; //Get the width from the 2nd element and then nudge all of them over 1 slide
        //console.log(PermWidth);
                if (index == 0 ) {
                    var NewWidth = PermWidth; //Move it over so the video doesn't cover it.
                } else {
                    var NewWidth = Math.round( (PermWidth+value) * 10 ) / 10;
                }
            NEWslideWidths[index] = NewWidth;
            //console.log(index + ": Orignial: " + value + " - New: "+NewWidth);
        });
        //console.log(NEWslideWidths);
        if (NEWslideWidths != null) {
            $( '.photo-carousel .slider .slider-frame .slider-list .slider-slide' ).each(function( index ) {
                $(this).css( "left", NEWslideWidths[index]+"px" );
            });
            //End slider mod

            var PhotosDIV = document.querySelector('.photo-carousel .slider .slider-frame .slider-list');

            var PhotosDIVHeight = $('.photo-carousel .slider .slider-frame .slider-list').css( "height" );
            console.log(PhotosDIVHeight);

            var YTEmbed = "<li class='slider-slide' style='position: absolute; left: 0px; top: 0px; display: inline-block; list-style-type: none; vertical-align: top; width: "+PermWidth+"px; height: "+PhotosDIVHeight+"; box-sizing: border-box; margin: auto 0px;min-height:200px;'><style>.lti-videos-container{position:relative; height:"+PhotosDIVHeight+"; width:100%;} .lti-videos{width:100%; height:"+PhotosDIVHeight+"}.lti-videos iframe{position:absolute;top:0;left:0;width:100%;height:"+PhotosDIVHeight+";}.lti-titles{width:100%; background:rgb(235, 10, 30);color:#fff;font-size:125%;text-align:center;position:absolute; z-index:100; padding:3%; top:0;}</style><div class='lti-videos-container'><div class='lti-videos'><iframe src='https://www.youtube.com/embed/"+CarVid+"?rel=0&showinfo=0&loop=10&autoplay=1&mute=1' frameborder='0' allowfullscreen='allowfullscreen' mozallowfullscreen='mozallowfullscreen' msallowfullscreen='msallowfullscreen' oallowfullscreen='oallowfullscreen' webkitallowfullscreen='webkitallowfullscreen'></iframe></div><div class='lti-titles'>Check out the 360&deg; Walkaround Video</div></div></li>";

            PhotosDIV.insertAdjacentHTML('afterbegin', YTEmbed);
        } else {
            console.log("No sliders yet");
        }

    },2000);
});


//console.log(VideoVIN + " - Video "+CarVid);
    
    $.ajax({url: "https://tracking.carvid360.com/lithiatoyotamissoula.com//tracking.php?VIN="+VideoVIN,context: document.body}); // Record VIN & video if available

} else {
    $.ajax({url: "https://tracking.carvid360.com/lithiatoyotamissoula.com//tracking.php?VIN="+VideoVIN,context: document.body}); // Record VIN & video if available
} // End VIN in list check

} // End Video list check

