$(document).ready(function() {
	var domainServerURL = $("#domainServerURL").val();
	var websiteOrganizationId = $("#websiteOrganizationId").val();
	var _csrf = $("#_csrf").val();
	
	if(window.location.href.indexOf("/org/") > -1) {
		var sponsorUrl = '/public/'+websiteOrganizationId+'/widget/sponsors-list?_csrf='+_csrf;
	}else{
		var sponsorUrl = domainServerURL+'/widget/sponsors-list?_csrf='+_csrf;
	}
	$.ajax({
		type : "POST",
		url: sponsorUrl,
		success: function(response) {
			var result = JSON.parse(response["result"]);
			if(result) {
				var sponsorsSlideshowList = '';
				var composedSponsorsSlideshowList = JSON.parse(response["json"]);
				var isSponsorsAvailable = 0;
				if(composedSponsorsSlideshowList.length > 0){
					for ( var sponsorsIndex = 0; sponsorsIndex < composedSponsorsSlideshowList.length; sponsorsIndex++){
						if(composedSponsorsSlideshowList[sponsorsIndex][1] != null && composedSponsorsSlideshowList[sponsorsIndex][1] != '' && composedSponsorsSlideshowList[sponsorsIndex][3] == 1){
							//sponsorsSlideshowList += '<li>';
							if(composedSponsorsSlideshowList[sponsorsIndex][0] != null && composedSponsorsSlideshowList[sponsorsIndex][0] != ''){
								sponsorsSlideshowList += '<a href="'+composedSponsorsSlideshowList[sponsorsIndex][0]+'" class="sponsorUrl" target="_blank" rel="nofollow">';
							}
							if(composedSponsorsSlideshowList[sponsorsIndex][1] != null && composedSponsorsSlideshowList[sponsorsIndex][1] != ''){
								sponsorsSlideshowList += '<img src="'+composedSponsorsSlideshowList[sponsorsIndex][1]+'" class="sponsorImg" title="'+composedSponsorsSlideshowList[sponsorsIndex][4]+'" />';
							}
							if(composedSponsorsSlideshowList[sponsorsIndex][0] != null && composedSponsorsSlideshowList[sponsorsIndex][0] != ''){
								sponsorsSlideshowList += '</a>';
							}
							//sponsorsSlideshowList += '</li>';
							isSponsorsAvailable = 1;
						}
					} 
					if(isSponsorsAvailable == 0){
						$(".sponsor-multi-sliders").attr("style","width:100% !important");
						sponsorsSlideshowList = '<div style="font-size:13px; margin-top:95px; ">There are no sponsors to display.</div>';
					}
				}else{
					$(".sponsor-multi-sliders").attr("style","width:100% !important");
					sponsorsSlideshowList = '<div style="font-size:13px; margin-top:75px; margin-bottom:80px; ">There are no sponsors to display.</div>';
				}
				if(isSponsorsAvailable == 0){
                    $(".sponsor-multi-sliders").attr("style","display:none");
                    $(".sponsor-multi-sliders-text").html(sponsorsSlideshowList);
                    $('logo-slider').css("display","block");
                }else{
                    for(var i= 0; i < 4; i++){
                        $(".sponsor-multi-sliders").append(sponsorsSlideshowList);
                    }
                    startSponsorsAnimation(); // Start the animation for continuous scroll
			    }
			}else{
				$(".sponsor-multi-sliders").html("<span class='red strong align-center'>"+response.errorMsg+"</span>");
			}
		},
		error: function() {
			$(".sponsor-multi-sliders").html("<span class='red strong align-center'>There is a problem while displaying the sponsors list.</span>");
        }
	});
	if(($(".leftWidgets").length == 0 || $(".leftWidgets").length == undefined) && ($(".rightWidgets").length == 0 || $(".rightWidgets").length == undefined)){
		$(".scroll-img").attr("style","width: 75% !important;");
	}
});

function startSponsorsAnimation() {
	 var sponsorsDiv = document.querySelector('.sponsor-multi-sliders');
	 if (sponsorsDiv) {
	   var images = sponsorsDiv.querySelectorAll('img');
	   var imageCount = images.length;
	   var animationDuration = imageCount *3; // Adjust the speed as desired
	   $(".sponsor-multi-sliders").css("animation-duration", animationDuration + "s");
	 }
}