var SplidesArr = [];
var GoogleApiReady = false;

//AfterAjaxLoadBindEvents();

$(document).on('click', '.opbutton', function () {
    if ($(this).parent().hasClass("slowGrow")) {
        $(this).parent().removeClass("slowGrow");
		
    }
    else {
        $(this).parent().addClass("slowGrow");
		
    }
      
		
		// Start Close All  
			/*
			function CloseAll() {
			    divPanelOpen = document.getElementsByClassName("panel");
			    for (i = 0; i < divPanelOpen.length; i++) {
					
						 let DivPanel = $(divPanelOpen[i]).parent().find(".panel").attr("id");
						 document.getElementById(DivPanel).style.display = "none";
						 
						 let DivTitleClose = $(divPanelOpen[i]).parent().find(".opbutton").attr("div-title-close");
						 document.getElementById(DivTitleClose).innerHTML = "מידע על המלון";
						
						 stopSplideCarusel($(divPanelOpen[i]).parent().find(".splide").attr("id"));
					
						// $(divPanelOpen[i]).parent().removeClass("slowGrow");
						// $(divPanelOpen[i]).parent().find(".downarrow").html("&#11167");
						
						//  stopSplideCarusel($(divPanelOpen[i]).parent().find(".splide").attr("id"));
				
						//alert(i)
						//$(divPanelOpen[i]).parent().removeClass("slowGrow");
	            	 	//$(divPanelOpen[i]).parent().find(".downarrow").html("&#11167");
	           		 	//stopSplideCarusel($(divPanelOpen[i]).parent().find(".splide").attr("id"));
					 
					//if(divPanelOpen.style.display = "block") 
					//{		
						//divPanelOpen[i].style.display = "none";									
					//}
			    }
			}
			
			
				function CloseAll() {
				    $('.opbutton').each(function () {
				        if ($(this).parent().hasClass("slowGrow")) {
						
				            $(this).parent().removeClass("slowGrow");
				            $(this).parent().find(".downarrow").html("&#11167");
				            stopSplideCarusel($(this).parent().find(".splide").attr("id"));
							alert($(this).parent().find(".splide").attr("id"))
				        }
				    });
				}
			*/
			// End Close All  
		
		
        $(this).parent().find(".firstopen").click();
        let cpan = $(this).parent().find(".panel");
        cpan.toggleClass("activepanel");
        if (cpan.hasClass("activepanel")) {
		
		
		
		
            $(this).parent().find(".downarrow").html("&#11165");
            let DivImageName = $(this).parent().find(".splide__list").attr("id");
            let HotelId = $(this).parent().find(".splide__list").attr("data");
            let splideId = $(this).parent().find(".splide").attr("id")
			let DivFacilitieseName = $(this).parent().find(".splide__list").attr("divfacilitieseid");
			
			//alert(DivFacilitieseName)
			
            ShowDataImg(DivImageName, HotelId, DivFacilitieseName, splideId);
            //call this after you set the images -> startSplideCarusel(splideId);
            //startSplideCarusel($(this).parent().find(".splide").attr("id"));
			
			//$('#BtuOpenMoreInfo').css("display", "block");//EY
			
			let DivTitleClose = $(this).parent().find(".opbutton").attr("div-title-close");
			document.getElementById(DivTitleClose).innerHTML = "סגור מידע";
			
        }
        else {
			CloseAll()
			
            $(this).parent().find(".downarrow").html("&#11167");
            stopSplideCarusel($(this).parent().find(".splide").attr("id"));
			//$('#BtuOpenMoreInfo').css("display", "none");//EY
			
			let DivTitleClose = $(this).parent().find(".opbutton").attr("div-title-close");
			document.getElementById(DivTitleClose).innerHTML = "מידע על המלון";
			
        }
    });
    $(document).on('click', '.tablinks', function () {
        let allTabContents = $(this).parent().parent().find(".tabcontent");
        for (i = 0; i < allTabContents.length; i++) {
            $(allTabContents[i]).css("display", "none");
        }
        let allTabs = $(this).parent().parent().find(".tablinks");
        for (i = 0; i < allTabs.length; i++) {
            $(allTabs[i]).removeClass("active");
        }
        let ot = $(this).attr("ot");

        $(this).parent().parent().find("." + ot).css("display", (ot == "Tab2" ? "flex" : "block"));
        $(this).addClass("active");
        if (ot == "Tab2") {
            StartGmap($(this).parent().parent().find("." + ot));
        }
    });



function ChangeTab(evt, tabName) {
    // Declare all variables
    var i, tabcontent, tablinks;

    // Get all elements with class="tabcontent" and hide them
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }

    // Get all elements with class="tablinks" and remove the class "active"
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }

    // Show the current tab, and add an "active" class to the button that opened the tab
    document.getElementById(tabName).style.display = (tabName == "Tab2" ?"flex":"block");
    evt.currentTarget.className += " active";
    if (tabName == "Tab2") {
        StartGmap($("#" + tabName));
    }
}
function startSplideCarusel(sid) {
    
    let sp = new Splide('#' + sid, {
        type: 'loop',
        heightRatio: 0.3,
        autoWidth: true,
        direction: 'rtl',
        focus: 'center',
        gap: 10,
        lazyLoad:'nearby'
    }).mount();
    SplidesArr.push({ id: sid, sp: sp});
}
function stopSplideCarusel(sid) {
    for (var i = 0; i < SplidesArr.length; i++)
    {
        if (SplidesArr[i].id == sid) {
            SplidesArr[i].sp.destroy(true);
        }
    }
}
function StartGmap(elem) {
    if (GoogleApiReady) {
        let PlacesOFInterest = elem.find(".inlist");
        let longitude = elem.find(".map").attr("long");
        let latitude = elem.find(".map").attr("lat");
        var loc = new google.maps.LatLng(latitude, longitude);
        let map = new google.maps.Map(elem.find(".map")[0], {
            center: loc,
            zoom: 15,
            fullscreenControl:true
        });
		marker = new google.maps.Marker({
			 position: new google.maps.LatLng(latitude, longitude),/*updated*/
			 map: map
		 });
        service = new google.maps.places.PlacesService(map);
        service.nearbySearch({
            location: loc,
            radius: 20000,
            rankBy: google.maps.places.RankBy.PROMINENCE,
            "types": [
                "tourist_attraction","synagogue","airport"
            ]
        }, function (results, status) {
                if (status === google.maps.places.PlacesServiceStatus.OK) {
                    var ww = "";
                    PlacesOFInterest.html('');
                    for (var rr = 0; rr < 6 && rr < results.length; rr++) {
                        let dist = google.maps.geometry.spherical.computeDistanceBetween(loc, results[rr].geometry.location);
                        PlacesOFInterest.append('<li>' + '<span class="gleft">' + results[rr].name + '</span>' + '<span dir="rtl" lang="he"  class="gright">' + (dist/1000).toFixed(1)+ ' ק"מ ' +'</span>'+  '</li>');
                    }
                results.forEach(function (p) {
                    //var m = new google.maps.Marker({
                    //    position: p.geometry.location,
                    //    icon: {
                    //        url: p.icon,
                    //        scaledSize: new google.maps.Size(24, 24)
                    //    },
                    //    title: p.name,
                    //    map: map
                    //});
                });
                } else {
                    console.log(status);
            }
        });
    }
    else {

        setTimeout(StartGmap, 3000);
    }
    
}
function initMap() {
    GoogleApiReady = true;
}
function SetWidth() {
    $(".opbuttonwrapper").css("width", $(".answer_page3").width()+"px");
}
