
(function() {

	// need to set an empty user session
	window._appUser = window.appUser || {};
	window._appUser.sessionToken = "";
	
	// need to setup an app config
	window.AppConfig = window.AppConfig || {};
	window.AppConfig.ParseAppId = "zesqKJEzK7ncFXe57x4uWc4Moow3I2wGCq7zFcqI";
	window.AppConfig.ParseAPIUrl = "https://api.getlocalhop.com/1";
	window.AppConfig.GoogleAPIKeyForWidgets = "AIzaSyAzVBg1cy_IYpVKn43LsjdkmUhW1K0p_RM";
	
	// var the calendar configuration
	window.LHCalendarConfig = {
		selector : "#localhop-calendar-embed",
		path : "https://events.getlocalhop.com/",
		widgetPath : "https://events.getlocalhop.com//wp-content/themes/localhop/assets/widgets/calendar",
		calendarWidgetsPath: "https://events.getlocalhop.com//wp-content/themes/localhop/assets/widgets",
		facebookAppId : "785408764919780"
	};
	
	// set the version
	var version = "?v=571a8fdc365264446f2f83dc536c8a385ebcfd6e";
	var hasWSB = "";
    var wsb = hasWSB ? "&wsb=1" : "";

	// the files we need to load for the widget	
	// TODO: only load the frameworks file if our special javascript variable isn't loaded
	var javascriptFiles = [
		LHCalendarConfig.calendarWidgetsPath + "/frameworks.js" + version + wsb,
		LHCalendarConfig.widgetPath + "/js/widget.js" + version
	];
	var cssFiles = [
		"//maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css",
		"//fonts.googleapis.com/css?family=Open+Sans:400,400italic,600,600italic,700",
		LHCalendarConfig.widgetPath + "/css/widget.css" + version
	];

	document.write("<script src='"+javascriptFiles[0]+"'></script>"); // This line added to fix Ploud Calendar issues.
	//when the document is ready, we can load all our files and kickoff the calendar
	var readyInterval = setInterval(function() { 
		if(document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") {
			// clear the interval
			clearInterval(readyInterval);
			
			// document has at least been parsed
			var headElement = document.querySelector("head");
			var javascriptFileIndex = 0;
			var javascriptElements = [];
			var cssElements = [];
			var elementCount = 0;
			
			// create all the script elements
			for(var i = 0; i < javascriptFiles.length; i++) {
				// increment the number of elements we are loading
				elementCount++;
				// create this script element, and add it but don't load it yet.
				javascriptElements.push(document.createElement("script"));
				headElement.appendChild(javascriptElements[javascriptElements.length-1]);
				// add the load event listener
				javascriptElements[javascriptElements.length-1].addEventListener("load", function(evt) {
					// mark this as loaded
					elementCount--;
					// load the next file
					javascriptFileIndex++;
					if(javascriptFileIndex < javascriptElements.length) {
						javascriptElements[javascriptFileIndex].src = javascriptFiles[javascriptFileIndex];
					}
				});
			}
			
			// create all the syle link elements
			for(var i = 0; i < cssFiles.length; i++) {
				// increment the number of elements we are loading
				elementCount++;
				// create the link element, add it but don't load it yet.
				cssElements.push(document.createElement("link"));
				cssElements[cssElements.length-1].rel = "stylesheet";
				headElement.appendChild(cssElements[cssElements.length-1]);
				cssElements[cssElements.length-1].addEventListener("load", function(evt) {
					// mark this as loaded
					elementCount--;
				});
			}
			
			// now we set all the javascript src's
			javascriptElements[javascriptFileIndex].src = javascriptFiles[javascriptFileIndex];
			
			// now load all the css elements
			for(var i = 0; i < cssElements.length; i++) {
				cssElements[i].href = cssFiles[i];
			}
			
			// when everything has loaded we kickoff the calendar
			var kickoffInterval = setInterval(function() {
				if(elementCount <= 0) {
					clearInterval(kickoffInterval);
					// kickoff the widget
					simplr.command.execute("/embed-calendar-kickoff/");
				}
			}, 50);
		}
	}, 50);

})();