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

  gtag('config', 'UA-137043477-4');var CarVideos = null;
$('.media .video').css( "display", "none" );

    // Create data attributes from vehicle info on SRP so we can grab some more info
$('.last dd').each(function () {
    var CarVidLabel = $(this).prev().text().replace(/(^,)|(,$)|(:$)/g, "").replace(/\s+/g, '-').replace(/\#+/g, 'no').toLowerCase();
    var CarVidData = $(this).text().replace(/(^,)|(,$)|(:$)/g, "");
    $(this).attr("data-"+CarVidLabel, CarVidData);
    //console.log(CarVidLabel+": "+CarVidData);
});

if (CarVideos != null) {

//window.onload = function() {
//if(!window.jQuery) {
//    var script = document.createElement('script');
//    script.type = 'text/javascript';
//    script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js';    

//    document.getElementsByTagName('head')[0].appendChild(script);
//    console.log("jQuery wasn't detected, so we loaded it.");
//  }
//}

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;
}

$('.hproduct[data-vin]').each(function(){
	var CurrentCar = $(this).data('vin');
    var CurrentCarMeta = {
            stock_no: $(this).find(".last dd").data('stock-no'),
            stock_status: $(this).find(".last dd").data('vehicle-status'),
            msrp: atob($(this).data('msrp')),
            internet_price: atob($(this).data('internetprice')),
            year: $(this).data('year'),
            make: $(this).data('make'),
            model: $(this).data('model'),
            trim: $(this).data('trim'),
            body: $(this).data('bodystyle'),
            // transmission: $(this).find(".last dd").data('transmission'),
            color: $(this).data('exteriorcolor'),
            // interior_color: $(this).find(".last dd").data('interior-color'),
            type: $(this).data('type')
        };
    var CurrentCarMetaEncoded = encodeURI(JSON.stringify(CurrentCarMeta));
if (CarVideos != null) {
	var CarVid = hasVIN(CurrentCar);
} else {
   var CarVid = null;
}
    
    

	if (CarVid != null) {
		$('.hproduct[data-vin="'+CurrentCar+'"]').css( "border", "2px solid rgb(235, 10, 30)" ).css( "border-radius", "5px 5px 0 0" ).css( "padding-bottom", "5px" ).after("<style>.lti-title {width:100%; background:rgb(235, 10, 30);color:#fff;font-size:15pt;text-align:center;border-radius: 0 0 5px 5px;}.lti-title a, .lti-title a :hover{text-decoration:none; color:#fff;}</style><div class='lti-title'><a href='/catcher.esl?vin="+CurrentCar+"&utm_source=internal-referral&utm_medium=lti-360video&utm_campaign=360-video-SRP-link#lti-360video'>Check out the 360&deg; Walkaround Video</a></div>");
        $.ajax({url: "https://tracking.carvid360.com/lithiatoyotamissoula.com//tracking.php?VIN="+CurrentCar+"&meta="+CurrentCarMetaEncoded,context: document.body}); // Record VIN & video if available
    } else {
        $.ajax({url: "https://tracking.carvid360.com/lithiatoyotamissoula.com//tracking.php?VIN="+CurrentCar+"&meta="+CurrentCarMetaEncoded,context: document.body}); // Record VIN even if no video available
    }
});


        