/*local js*/

var getStoreHours = {

	fetchStoreHours:function(){
		console.log('get hours...');
		var self = this;		
		$.ajax({
			url: '/api/async.php?t=widgetFeed&m=lists&a=get_locations',
			type: 'GET',
			dataType: 'json',
			success: self.onData,
			error: function(err) {
				console.log(err);
			}
		});
	},

	onData: function(data) {
		$.each(data.list,function(index,item){			
            console.log(item);
            //
            var address = '';
		    var hours = '';
		    var directionsBtn = '';
            //
            var locationName = item.locationName;
			var locationID = item.id;
			var address1 = item.address1;
			var address2 = item.address2;
			var city = item.city;
			var zipcode = item.zipcode;
			var phone = item.phone;
            var tollFreePhone = item.tollFreePhone;
			var state = item.state;
			var days1 = item.days1;
			var days2 = item.days2;
			var days3 = item.days3;
			var days4 = item.days4;
			var days5 = item.days5;
			var days6 = item.days6;
			var days7 = item.days7;
			var hours1 = item.hours1;
			var hours2 = item.hours2;
			var hours3 = item.hours3;
			var hours4 = item.hours4;
			var hours5 = item.hours5;
			var hours6 = item.hours6;
			var hours7 = item.hours7;
			
			// Address
			address += "<address>";			
			if( locationName ){
				//address += locationName;
				//address += "<br>";
			} 
			if( address1 ){
				address += address1;
				address += "<br>";
			}
			if( address2 ){
				address += address2;
				address += "<br>";
			}
			address += city + ', ' + state + ' ' + zipcode;
			address += "</address>";				
			
            phoneHtml = "<address>";
            if( tollFreePhone ){
                tollFreePhoneText = tollFreePhone;
                tollFreePhoneText = tollFreePhoneText.replace(/[^0-9]/g,'');
                tollFreePhoneText = tollFreePhoneText.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
                phoneHtml += "<a href='tel:" + tollFreePhone + "'>" + tollFreePhoneText + " (toll free)</a>";
                phoneHtml += "<br>";
            }
            if( phone ){
                if( tollFreePhone ){
                    //phoneHtml += "<br>";
                }
				phone = phone.replace(/[^0-9]/g,'');
				phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
				phoneHtml += "<a href='tel:" + phone + "'>" + phone + "</a>";
				phoneHtml += "<br>";
			}					
			phoneHtml += "</address>";	

			// Directions
			var directions = "https://www.google.com/maps/dir/?api=1&destination=" + address1 + "+" + address2 + "+" + city + "+" + state + "+" + zipcode + "";
            directionsBtn += "<a href='" + directions + "' target='_blank' class='d-block mb-3 btn-get-directions text-underline text-dark'>Get Directions</a>";

			// Hours
			hours += "<address>";
			if( days1 || hours1 ){
				if( days1 && hours1 ){
					days1 += ":";
				}
				hours += "<strong>" + days1 + "</strong>";
                hours += "<br>";
				hours += "<span>" + hours1 + "</span>";
				hours += "<br>";
			}
			if( days2 || hours2 ){
				if( days2 && hours2 ){
					days2 += ":";
				}
				hours += "<strong>" + days2 + "</strong>";
                hours += "<br>";
				hours += "<span>" + hours2 + "</span>";
				hours += "<br>";
			}
			if( days3 || hours3 ){
				if( days3 && hours3 ){
					days3 += ":";
				}
				hours += "<strong>" + days3 + "</strong>";
                hours += "<br>";
				hours += "<span>" + hours3 + "</span>";
				hours += "<br>";
			}
			if( days4 || hours4 ){
				if( days4 && hours4 ){
					days4 += ":";
				}
				hours += "<strong>" + days4 + "</strong>";
                hours += "<br>";
				hours += "<span>" + hours4 + "</span>";
				hours += "<br>";
			}
			if( days5 || hours5 ){
				if( days5 && hours5 ){
					days5 += ":";
				}
				hours += "<strong>" + days5 + "</strong>";
                hours += "<br>";
				hours += "<span>" + hours5 + "</span>";
				hours += "<br>";
			}
			if( days6 || hours6 ){
				if( days6 && hours6 ){
					days6 += ":";
				}
				hours += "<strong>" + days6 + "</strong>";
                hours += "<br>";
				hours += "<span>" + hours6 + "</span>";
				hours += "<br>";
			}
			if( days7 || hours7 ){
				if( days7 && hours7 ){
					days7 += ":";
				}
				hours += "<strong>" + days7 + "</strong>";
                hours += "<br>";
				hours += "<span>" + hours7 + "</span>";				
			}			
			hours += "</address>";			

            // ----------------------------------------------------
            // Update DOM
            //$('.btn-get-directions').attr('onclick', "window.open('" + directions + "', '_blank')");		
            $('[data-location="'+locationID+'"].store-address').html(address + '' + directionsBtn );		
            $('[data-location="'+locationID+'"].store-hours').html(hours);
            $('[data-location="'+locationID+'"].store-phone').html(phoneHtml);

		});



	
    }
};

self.getStoreHours.fetchStoreHours();

(function() {	
	/**/        
}());
