document.write("<link rel=\"stylesheet\" href=\"https://www.leafbuyer.com/cannawidget/css/wSelect.css?v=071018\" />");
document.write("<link rel=\"stylesheet\" href=\"https://www.leafbuyer.com/cannawidget/css/widget.css?v=091118\" />");
document.write("<link rel=\"stylesheet\" href=\"https://www.leafbuyer.com/cannawidget/grasscity/css/widget.css?t=1540192022565\" />");
document.write("<link rel=\"stylesheet\" href=\"https://www.leafbuyer.com/css/jquery-ui.css\" />");
document.write("<script>");
document.write("	if (typeof ga === 'undefined')");
document.write("	{");
document.write("		(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
document.write("			(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
document.write("				m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
document.write("			})(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
document.write("	}");
document.write("	ga('create','UA-45851918-8',{ 'name':'leafbuyer' });");
document.write("	ga('leafbuyer.send','event','grasscity','widget:embed','widget:-:widget:embed:' + document.location.hostname.replace(/^w+\./,'') + ':rXfu326j');");
document.write("	var	overlayTopOffset = 0;");
document.write("</script>");
document.write("<div class=\"lbmwidget\">");
document.write("	<div style=\"display: block; width: auto; text-align:center\">");
document.write("<div id=\"lbmfilters\" class=\"lbmfilters lbmclearfix\" style=\"\">");
document.write("			<input type=\"hidden\" name=\"lbmstype\" id=\"lbmstype\" value=\"business\">");
document.write("			<div class=\"lbmfilter\">");
document.write("				<select id=\"lbmcity\" name=\"city\">");
document.write("				</select>");
document.write("			</div>");
document.write("			<div class=\"lbmfilter\">");
document.write("				<select id=\"lbmvtype\" name=\"vtype\">");
document.write("				</select>");
document.write("			</div>");
document.write("			<div class=\"lbmclearfix\"></div>");
document.write("			<div class=\"lbmfilter\">");
document.write("				<input type=\"hidden\" name=\"lbmvendorID\" id=\"lbmvendorID\">");
document.write("				<input type=\"text\" name=\"lbmsearch\" id=\"lbmsearch\" placeholder=\"Shop Name\" style=\"border-radius:5px; width:200px; height:20px; font-size:12px; padding:1px 5px\" onfocus=\"jQuery(this).val(searchTerm); jQuery(this).autocomplete('search')\">");
document.write("			</div>");
document.write("		</div>");
document.write("<div class=\"lbmcanvas\" id=\"lbmcoupons-wrapper\">");
document.write("			<div id=\"lbmcoupons\"></div>");
document.write("		</div>");
document.write("		<div class=\"lbmnav\">");
document.write("<div style=\"float:left; width:40%\">");
document.write("				<img src=\"https://www.leafbuyer.com/cannawidget/grasscity/images/up_arrow.png\" id=\"coupon_prev\" alt=\"\" style=\"max-width:33px; max-height:33px; cursor:pointer\"/>");
document.write("				<img src=\"https://www.leafbuyer.com/cannawidget/grasscity/images/down_arrow.png\" id=\"coupon_next\" alt=\"\" style=\"max-width:33px; max-height:33px; cursor:pointer\"/>");
document.write("			</div>");
document.write("			<div style=\"float:right; width:60%\">");
document.write("				<a href=\"https://www.leafbuyer.com\" title=\"Leafbuyer.com\" target=\"_blank\"><img alt=\"\" src=\"https://www.leafbuyer.com/cannawidget/grasscity/images/poweredby.png\" style=\"margin-top:-2px\"></a>");
document.write("			</div>");
document.write("	</div>");
document.write("	</div>");
document.write("</div>");
document.write("<div id=\"lbmwoverlay\" class=\"lbmwidget-overlay\">");
document.write("  <div id=\"lbmwoverlay-contents\">");
document.write("	<div id=\"lbmcouponpanel\" style=\"display:none\">");
document.write("  	<div id=\"lbmdesktop\">");
document.write("	<div class=\"lbmcoupon-tools lbmwide lbmclearfix\">");
document.write("	  <div id=\"sharing\" class=\"lbmsharing\">");
document.write("		<a href=\"javascript:;\" onclick=\"lbmHideOverlay()\" class=\"lbmcoupon-close\"><img src=\"https://www.leafbuyer.com/images/modal-close.png\" width=\"22\" height=\"22\" border=\"0\"></a>");
document.write("        <a href=\"\" id=\"couponlinkurl\" target=\"_blank\" style=\"float:right; display:inline-block; margin:-5px 5px; 0 0; background-color:#2a76dc; color:#fff; height:30px; line-height:30px; padding:0 10px; border-radius:2px;\"></a>");
document.write("		<a href=\"javascript:;\" id=\"coupontextalert\" onclick=\"lbmPopupText()\" class=\"text-icon\" style=\"display:none; float:right; margin:-11px 10px 0 15px\"><img src=\"https://www.leafbuyer.com/img/icons/text-profile.png\" style=\"width:100px\"></a></li>");
document.write("        <a href=\"\" id=\"couponrecurl\" target=\"_blank\" style=\"float:right; display:inline-block; margin:-5px 5px; 0 0; background-color:#6FB2E1; color:#fff; height:30px; line-height:30px; padding:0 10px; border-radius:2px;\">REC PRE-ORDER</a>");
document.write("		<a href=\"\" id=\"couponmedurl\" target=\"_blank\" style=\"float:right; display:inline-block; margin:-5px 5px; 0 0; background-color:#6FB2E1; color:#fff; height:30px; line-height:30px; padding:0 10px; border-radius:2px;\">MED PRE-ORDER</a>");
document.write("		<a class=\"lbmprint\" href=\"javascript:lbmPopupPrint(false)\">print</a>");
document.write("	  </div>");
document.write("	  <div id=\"lbmpopupcoupon\" class=\"lbmcoupon lbmbig lbmclearfix\">");
document.write("		<div class=\"lbmcoupon-header\">");
document.write("		  <div><h3 id=\"couponheading\" class=\"lbmresizable\"></h3></div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmpricebar\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div id=\"couponrange\" class=\"lbmcell lbmdiscount\"></div>");
document.write("			<div id=\"couponpercentage\" class=\"lbmcell lbmdiscount\"></div>");
document.write("			<div id=\"couponpricecell\" class=\"lbmcell\">");
document.write("			  <div class=\"lbmtitle lbmoriginal\" id=\"couponoriginal\"></div>");
document.write("			  <div class=\"lbmtitle lbmsale\" id=\"couponsale\"></div>");
document.write("			</div>");
document.write("			<div id=\"couponquantitycell\" class=\"lbmcell\">");
document.write("			  <div class=\"lbmtitle\">Quantity</div>");
document.write("			  <div id=\"couponweight\" class=\"lbmvalue\"></div>");
document.write("			</div>");
document.write("			<div id=\"couponthccbdcell\" class=\"lbmcell\">");
document.write("			  <div id=\"couponthccbdtitle\" class=\"lbmtitle\"></div>");
document.write("			  <div id=\"couponthccbd\" class=\"lbmvalue\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmclearfix lbmtable lbmdescription\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell lbmcol50\">");
document.write("			  <div id=\"coupontext\" class=\"lbmtext\"></div>");
document.write("			  <div id=\"coupondisclaimer\" class=\"lbmdisclaimer\"></div>");
document.write("			</div>");
document.write("			<div class=\"lbmcell lbmlogo lbmcol50\">");
document.write("			  <div class=\"lbmtextcenter\"><img id=\"couponlogo\" class=\"lbmcouponlogo\" src=\"\"></div>");
document.write("			  <div id=\"couponvendor\" class=\"lbmtextcenter\"></div>");
document.write("			    <div id=\"couponwebsite\" class=\"lbmtextcenter\" style=\"margin:10px 0\">");
document.write("			      <a href=\"https://eaze.co/2hwAGRe\" target=\"_blank\" style=\"background-color:#2a76dc; color:#fff; padding:5px 15px; border-radius:5px; text-align:center\">visit website</a>");
document.write("			    </div>");
document.write("			  <div id=\"couponlink\" class=\"lbmtextcenter\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmfooter lbmclearfix\">");
document.write("		  <div class=\"lbmcol20 lbmleft\">");
document.write("			<div id=\"couponexpires\"></div>");
document.write("		  </div>");
document.write("		  <div class=\"lbmcol80 lbmright\">");
document.write("			<span id=\"couponaddr\" class=\"lbmtextright\"></span> | <span id=\"couponphone\" class=\"lbmtextright\"></span>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div id=\"couponmedrecdel\" class=\"lbmmedrecdel lbmtextcenter\"></div>");
document.write("		<div id=\"couponbarcode\" class=\"lbmtextcenter\"></div>");
document.write("	  </div>");
document.write("	  <div class=\"lbmterms\">");
document.write("		<div class=\"lbmdisclaimer\" style=\"font-size:10px\">This advertisement is for customer reference only and has no value and is not intended to be exchanged for any product. Leafbuyer (Leafbuyer Technologies, Inc) is not responsible for the content of any advertisement. All advertisements are the sole responsibility of the vendor publishing the advertisement. If an advertisement is not valid, please contact the vendor directly as errors may have been made in the creation of the advertisement. This advertisement is only intended to be viewed by those of legal age and in the state/local area where the product is legally open for sale. Additionally, all state and local laws and restrictions apply to all advertisements. Void where prohibited.</div>");
document.write("	  </div>");
document.write("	</div>");
document.write("	</div>");
document.write("  	<div id=\"lbmmobile\" class=\"lbmmobile\">");
document.write("	<div class=\"lbmcoupon-tools lbmnarrow lbmclearfix\">");
document.write("	  <div id=\"sharing\" class=\"lbmsharing\">");
document.write("		<a href=\"javascript:;\" onclick=\"lbmHideOverlay()\" class=\"lbmcoupon-close\"><img src=\"https://www.leafbuyer.com/images/modal-close.png\" width=\"22\" height=\"22\" border=\"0\"></a>");
document.write("        <a href=\"\" id=\"couponlinkurl\" target=\"_blank\" style=\"float:right; display:inline-block; margin:-5px 5px; 0 0; background-color:#6FB2E1; color:#fff; height:30px; line-height:30px; padding:0 10px; border-radius:2px;\"></a>");
document.write("		<a href=\"javascript:;\" id=\"coupontextalert\" onclick=\"lbmPopupText()\" class=\"text-icon\" style=\"display:none; float:right; margin:-16px 10px 0 15px\"><img src=\"https://www.leafbuyer.com/img/icons/text-profile.png\" style=\"width:100px\"></a></li>");
document.write("        <a href=\"\" id=\"couponrecurl\" target=\"_blank\" style=\"float:right; display:inline-block; margin:-5px 5px; 0 0; background-color:#6FB2E1; color:#fff; height:30px; line-height:30px; padding:0 10px; border-radius:2px;\">REC PRE-ORDER</a>");
document.write("		<a href=\"\" id=\"couponmedurl\" target=\"_blank\" style=\"float:right; display:inline-block; margin:-5px 5px; 0 0; background-color:#6FB2E1; color:#fff; height:30px; line-height:30px; padding:0 10px; border-radius:2px;\">MED PRE-ORDER</a>");
document.write("	  <a class=\"lbmprint\" href=\"javascript:lbmPopupPrint(true)\">print</a>");
document.write("	  </div>");
document.write("<img id=\"lbmcoupon-image\" style=\"display:none\" />");
document.write("	  <div id=\"lbmcoupon-contents\" class=\"lbmcoupon lbmsmall lbmclearfix\">");
document.write("		<div class=\"lbmcoupon-header\">");
document.write("		  <div><h3 id=\"couponheading\" class=\"lbmcouponheading lbmresizable\"></h3></div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmpricebar\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div id=\"couponpercentage\" class=\"lbmcell lbmdiscount\"></div>");
document.write("			<div id=\"couponpricecell\" class=\"lbmcell\">");
document.write("			  <div class=\"lbmtitle lbmoriginal\" id=\"couponoriginal\"></div>");
document.write("			  <div class=\"lbmtitle lbmsale\" id=\"couponsale\"></div>");
document.write("			</div>");
document.write("			<div id=\"couponquantitycell\" class=\"lbmcell\">");
document.write("			  <div class=\"lbmtitle\">Quantity</div>");
document.write("			  <div id=\"couponweight\" class=\"lbmvalue\"></div>");
document.write("			</div>");
document.write("<!--");
document.write("			<div id=\"couponthccbdcell\" class=\"lbmcell\">");
document.write("			  <div id=\"couponthccbdtitle\" class=\"lbmtitle\"></div>");
document.write("			  <div id=\"couponthccbd\" class=\"lbmvalue\"></div>");
document.write("			</div>");
document.write("-->");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmclearfix lbmdescription\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell\">");
document.write("			  <div id=\"coupontext\" class=\"lbmtext\" style=\"height:initial; max-height:initial\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmclearfix\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell\">");
document.write("			  <div id=\"coupondisclaimer\" class=\"lbmdisclaimer\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmclearfix\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell\">");
document.write("				<div id=\"couponmedrecdel\" class=\"lbmtextcenter\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmclearfix\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell lbmlogo\">");
document.write("			  <div class=\"lbmtextcenter\"><img id=\"couponlogo\" class=\"lbmcouponlogo\" src=\"\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmclearfix\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell lbmvendor\">");
document.write("			  <div id=\"couponvendor\" class=\"lbmtextcenter\"></div>");
document.write("			  <div class=\"lbmtextcenter\"><span id=\"couponaddr\"></span><br><span id=\"couponphone\" class=\"lbmtextright\"></span></div>");
document.write("			  <div id=\"couponlink\" class=\"lbmtextcenter\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmclearfix\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell\">");
document.write("			  <div id=\"couponexpires\" class=\"lbmexpires lbmtextcenter\"></div>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("		<div class=\"lbmtable lbmclearfix\">");
document.write("		  <div class=\"lbmrow\">");
document.write("			<div class=\"lbmcell lbmtextcenter\">");
document.write("			  <a href=\"http://www.leafbuyer.com\" target=\"blank\"><img src=\"https://www.leafbuyer.com/images/widget-footer.png\" border=\"0\" alt=\"Visit LeafBuyer.com\"></a>");
document.write("			</div>");
document.write("		  </div>");
document.write("		</div>");
document.write("	  </div>");
document.write("	  <div class=\"lbmterms\">");
document.write("		<div class=\"lbmcol lbmdisclaimer\"><a href=\"javascript:null(void)\" onclick=\"jQuery('#lbmfineprint').toggle()\">legal notice</a><p id=\"lbmfineprint\">This advertisement is for customer reference only and has no value and is not intended to be exchanged for any product. Leafbuyer (Leafbuyer Technologies, Inc) is not responsible for the content of any advertisement. All advertisements are the sole responsibility of the vendor publishing the advertisement. If an advertisement is not valid, please contact the vendor directly as errors may have been made in the creation of the advertisement. This advertisement is only intended to be viewed by those of legal age and in the state/local area where the product is legally open for sale. Additionally, all state and local laws and restrictions apply to all advertisements. Void where prohibited.</p></div>");
document.write("	  </div>");
document.write("	</div>");
document.write("	</div>");
document.write("	</div>");
document.write("<style>");
document.write("#lbmwoverlay .lbmsignupform {");
document.write("	width:100%;");
document.write("}");
document.write("#lbmwoverlay .lbmpanel * {");
document.write("	font-size:14px;");
document.write("}");
document.write("#lbmwoverlay .lbmpanel {");
document.write(" 	display:none;");
document.write("	padding:10px;");
document.write("	max-width:400px;");
document.write(" }");
document.write("#lbmwoverlay .lbmpanel input,");
document.write("#lbmwoverlay .lbmpanel select {");
document.write("	border:solid #d0d0d0 2px;");
document.write("	border-radius:3px;");
document.write("	font-size:14px;");
document.write("	max-width:80%;");
document.write("	height:25px;");
document.write("}");
document.write("#lbmwoverlay .lbmpanel input[type=\"button\"] {");
document.write("	width:100px;");
document.write("	background-color:rgb(129,35,23);");
document.write("	color:#fff;");
document.write("	border-radius:5px;");
document.write("	text-align:center;");
document.write("}");
document.write("</style>");
document.write("	<div id=\"lbmemailpanel\" class=\"lbmpanel\">");
document.write("		<div style=\"margin-bottom:10px\" class=\"lbmpanelheader\">");
document.write("			<a href=\"javascript:;\" onclick=\"lbmHideOverlay()\" class=\"lbmcoupon-close\"><img src=\"https://www.leafbuyer.com/images/modal-close.png\" width=\"22\" height=\"22\" border=\"0\"></a>");
document.write("			<h2>Sign Up for Leafbuyer Deal Alerts</h2>");
document.write("		</div>");
document.write("		<div class=\"lbmcontent lbmsignup\">");
document.write("			<div class=\"lbmsignup-header\" style=\"text-align:center; margin-bottom:10px; font-size:16px\">");
document.write("				Get exclusive updates on the hottest cannabis deals and industry events in your area!");
document.write("			</div>");
document.write("			<form>");
document.write("			<div class=\"lbmtable lbmsignupform lbmclearfix\">");
document.write("				<div style=\"margin-bottom:10px\">");
document.write("					Email Address<br>");
document.write("					<input type=\"text\" name=\"email\" id=\"emailEmail\" style=\"width:250px\">");
document.write("				</div>");
document.write("				<div style=\"margin-bottom:10px\">");
document.write("					Mobile Phone #<br>");
document.write("					<input type=\"text\" name=\"phone\" id=\"emailPhone\" style=\"width:100px\" onblur=\"this.value=this.value.replace(/[^0-9]/g,''); this.value=this.value.substring(0,Math.min(10,this.value.length))\">");
document.write("				</div>");
document.write("				<div style=\"margin-bottom:10px\">");
document.write("					ZIP/Post Code<br>");
document.write("					<input type=\"text\" name=\"zip\" id=\"emailZip\" style=\"width:60px\">");
document.write("				</div>");
document.write("<div class=\"lbmrow\">");
document.write("					<div class=\"lbmcell\">");
document.write("						<input type=\"button\" value=\"submit\" class=\"lbmbutton\" onclick=\"lbmDoEmailSignup(); return false\">");
document.write("					</div>");
document.write("				</div>");
document.write("			</div>");
document.write("			</form>");
document.write("		</div>");
document.write("	</div>");
document.write("	<div id=\"lbmtextpanel\" class=\"lbmpanel\">");
document.write("		<div style=\"margin-bottom:10px\" class=\"lbmpanelheader\">");
document.write("			<a href=\"javascript:;\" onclick=\"lbmHideOverlay()\" class=\"lbmcoupon-close\"><img src=\"https://www.leafbuyer.com/images/modal-close.png\" width=\"22\" height=\"22\" border=\"0\"></a>");
document.write("			<h2>Sign Up for Text Alerts</h2>");
document.write("		</div>");
document.write("		<div class=\"lbmcontent lbmsignup\">");
document.write("			<div class=\"lbmsignup-header\" style=\"text-align:center; margin-bottom:10px; font-size:16px\">");
document.write("				Enter your mobile phone number to receive Deal Alerts via text message from this dispensary.");
document.write("			</div>");
document.write("			<form>");
document.write("			<div class=\"lbmtable lbmsignupform lbmclearfix\">");
document.write("				<div style=\"margin-bottom:10px\">");
document.write("					Mobile Phone #<br>");
document.write("					<input type=\"text\" name=\"phone\" id=\"textphone\" style=\"width:100px\" onblur=\"this.value=this.value.replace(/[^0-9]/g,''); this.value=this.value.substring(0,Math.min(10,this.value.length))\">");
document.write("				</div>");
document.write("<div style=\"margin-bottom:10px\">");
document.write("					First Name<br>");
document.write("					<input type=\"text\" name=\"firstname\" id=\"textfirstname\" style=\"width:150px\">");
document.write("				</div>");
document.write("				<div style=\"margin-bottom:10px\">");
document.write("					Last Name<br>");
document.write("					<input type=\"text\" name=\"lastname\" id=\"textlastname\" style=\"width:150px\">");
document.write("				</div>");
document.write("				<div class=\"lbmrow\">");
document.write("					<div class=\"lbmcell\">");
document.write("						<input type=\"button\" value=\"submit\" class=\"lbmbutton\" onclick=\"lbmDoTextSignup(); return false\">");
document.write("					</div>");
document.write("				</div>");
document.write("			</div>");
document.write("			</form>");
document.write("		</div>");
document.write("	</div>");
document.write("  </div>");
document.write("</div>");
document.write("<!-- start omit --><!-- end omit -->");
document.write("<span id=\"lbmruler\" style=\"visibility:hidden; text-wrap:none\"></span>");
document.write("");


var	_debug = (document.location.href.match(/.*\/leafbuyer/) != null);

var	lbmCouponID = -1;

function lbmPopupCoupon(id,v)
{
	lbmCouponID = id;

	lbmUpdateCoupon(id);

	jQuery('#lbmemailpanel').hide();
	jQuery('#lbmtextpanel').hide();
	jQuery('#lbmcouponpanel').show();

	lbmShowOverlay();
	lbmResize(false);

	jQuery('.lbmresizable').each(function(index,value) {
		if (jQuery(this).is(':visible'))
			while (pixels(jQuery(this)) > jQuery(this).width())
				jQuery(this).css('font-size',
					(jQuery(this).css('font-size').replace(/[^0-9]/g,'') - 1) + 'px');
	});

	jQuery('#lbmcoupon-image').html('').hide();
	jQuery('#lbmcoupon-contents').show();

	_lbvendor = v;
	_lbcoupon = _coupons[id].attributes.couponID;

	ga('leafbuyer.send','event','grasscity','widget:lightbox','widget:-:widget:lightbox:'+_vendor+':'+_lbvendor);
	ga('leafbuyer.send','event','grasscity','widget:lightbox-coupon','widget:-:widget:lightbox:'+_vendor+':'+_lbvendor+':'+_lbcoupon);
	ga('leafbuyer.send','event','grasscity','tracker','widget:-:widget:tracker:'+_vendor+':'+_lbvendor+':'+_lbcoupon);
	lb('click','coupon:lightbox:'+_lbvendor+':'+_lbcoupon+':'+lat+':'+lng);
}

function lbmPopupEmail()
{
	jQuery('#lbmemailpanel').show();
	jQuery('#lbmtextpanel').hide();
	jQuery('#lbmcouponpanel').hide();

	lbmShowOverlay();
	lbmResize();
}

function lbmDoEmailSignup()
{
	jQuery.ajax({
//		url: "https://www.leafbuyer.com/php/signup.php?firstname=" + jQuery('#emailFirstName').val() + "&lastname=" + jQuery('#emailLastName').val() + "&company=" + jQuery('#emailCompany').val() + "&email=" + jQuery('#emailEmail').val() + "&city=" + jQuery('#emailCity option:selected').text() + "&source=grasscity",
		url: "https://www.leafbuyer.com/php/signup.php?email=" + jQuery('#emailEmail').val() + "&phone=" + jQuery('#emailPhone option:selected').text() + "&zip=" + jQuery('#emailZip').val() + "&source=grasscity",
		type: 'GET',
		success: function(response) {
			alert(response);
			lbmHideOverlay();

//			ga('leafbuyer.send','event','newsletter','widget::newsletter:signedup');
			ga('leafbuyer.send','event','newsletter','signedup','widget:');
		}
	});
}

function lbmPopupText()
{
	jQuery('#lbmemailpanel').hide();
	jQuery('#lbmtextpanel').show();
	jQuery('#lbmcouponpanel').hide();

	lbmShowOverlay();
	lbmResize();
}

var	textAlertData = { type:'create_member', merchant_token:'', phone:'', email:'', firstname:'', lastname:'' };

function lbmDoTextSignup()
{
	if (jQuery('#textphone').val().length < 10)
	{
		alert("Phone number does not appear to be valid (expecting 10 digits).");
		jQuery('#textphone').focus();
		return;
	}

	var	keys = Object.keys(textAlertData);

	for (var i = 0; i < keys.length; i++)
	{
		if (textAlertData[keys[i]] != '')
			continue;

		textAlertData[keys[i]] = jQuery('#text' + keys[i]).val();
	}

	textAlertData['merchant_token'] = _coupons[lbmCouponID].vendor.key;

	jQuery.post("https://www.leafbuyer.com/php/createLoyaltyMember.php?t=" + (new Date()).getTime(),
		textAlertData).done( function(response) {
//			alert(response);
			lbmHideOverlay();

			ga('leafbuyer.send','event','textalert','complete','widget::complete:'+_lbvendor);
	});
}

var _coupons = [], _types = [], _subtypes = [];
var _vendor = document.location.host + ':rXfu326j',
	_lbvendor = '',
	initialize = false;
var	lat = null, lng = null;

function lb(e, d)
{
	jQuery.ajax({
		url: "https://www.leafbuyer.com/proxy/tracker.lbn?e=" + e + "&d=" + d,
		type: 'GET',
		complete: function(response) {
		},
		success: function(response) {
		}
	});
}

function lbmUpdate(init)
{
	initialize = init;

	jQuery('#lbmloading').show();
	jQuery('#lbmnonefound').hide();
	jQuery('.caroufredsel_wrapper').css('visibility','hidden');

	var	type = jQuery('#lbmvtype').val(),
		mode = jQuery('#lbmstype').val(),
		vtype = null,
		ptype = null,
		url = "https://www.leafbuyer.com/proxy/widgetsearch.lbn?vid=rXfu326j&init=" + init;

	if (lat != null && lng != null)
		url += "&lat=" + lat + "&lng=" + lng;

	if (mode == 'business')
	{
		if (type != null)
		{
			vtype = type.split(":")[0];
			ptype = type.split(":")[1];

			url += (jQuery('#lbmcity').val() != null ? "&city=" + jQuery('#lbmcity').val() : '') + (vtype != null ? "&vtype=" + vtype : "") + (ptype != null ? "&ptype=" + ptype : "");
		}
	}
	else if (mode == 'name')
		url += (jQuery('#lbmvendorID').val() != null ? "&vendorID=" + jQuery('#lbmvendorID').val() : '');

	url += "&t=" + (new Date()).getTime(),

	jQuery.ajax({
		url: url,

		success: function(data) {
			lbmUpdateCallback(data);
		}
	});
}

var	types = new Array();

function lbmUpdateCallback(xml)
{
	jQuery('#lbmcoupons').html('');
//console.log("parsing");
try
{
	var xmla = xml2array(xml);
//console.log("done");
	var	root = xmla.rsp,
		filters = root.filters,
		coupons = root.coupons;	

	if (typeof(filters) != 'undefined')
	{
//console.log("processing filters");
		var	list = filters.cities;

		for (var i = 0; i < list.attributes.count; i++)
		{
			var	city = (list.attributes.count == 1 ? list.city : list.city[i]);

			if (city.attributes.typeIDs == '')
				continue;

			types[city.value] = city.attributes.typeIDs.split(/,/);

			jQuery('#lbmcity').append(jQuery('<option/>', { 
				value: city.value,
				text : city.value.replace(/,.*/,'') 
			}));
		}

		if (list.attributes.closest != null)
			jQuery('#lbmcity option[value="' + list.attributes.closest + '"]').prop('selected',true);

		if (jQuery('#moredeals').length != 0)
		{
			if (list.attributes.city != null && list.attributes.state != null)
				jQuery('#moredeals').attr('href','/deals/dispensaries/' + list.attributes.state + '/' + list.attributes.city.replace(/ /,'+'));
			else
				jQuery('#moredeals').attr('href','/deals-near-me');;
		}

		if (lat == null && lng == null)
		try
		{
			lat = list.attributes.lat;
			lng = list.attributes.lng;
		}
		catch (e)
		{}

		list = filters.types;

		for (var i = 0; i < list.attributes.count; i++)
		{
			var	item = (list.attributes.count == 1 ? list.type : list.type[i]),
				type = item.attributes.name;

			_types[i] = type;
			_subtypes[i] = new Array();

			for (var j = 0; j < item.attributes.count; j++)
			{
				var	subtype = item.subtype[j];
				_subtypes[i][j] = subtype;
			}
		}

		jQuery('#lbmcity').trigger('change', [ false ]);
	}

	destroyCarousel();

	var	html = '';
//console.log("processing coupons");
	for (var i = 0; i < coupons.attributes.count; i++)
	{
		var	coupon = (coupons.attributes.count == 1 ? coupons.coupon : coupons.coupon[i]);
//console.log(coupon.product + ' > ' + coupon.vendor.name);
		if (i > 0)
		{
			// this logic make sure that no two vendor locations' coupons
			// appear next to each other regardless of how the locations have
			// been ordered

			if (coupon.vendor.name.toLowerCase().substring(0,Math.min(5,coupon.vendor.name.length)) == coupons.coupon[i - 1].vendor.name.toLowerCase().substring(0,Math.min(5,coupons.coupon[i - 1].vendor.name.length)))
			{
				for (var j = i + 1; j < coupons.attributes.count; j++)
				{
					if (coupon.vendor.name.toLowerCase().substring(0,Math.min(5,coupon.vendor.name.length)) != coupons.coupon[j].vendor.name.toLowerCase().substring(0,Math.min(5,coupons.coupon[j].vendor.name.length)))
					{
						var	c1 = coupon, c2 = coupons.coupon[j];

						coupons.coupon[i] = c2;
						coupons.coupon[j] = c1;

						coupon = coupons.coupon[i];
						break;
					}
				}
			}

			if (i % 8 == 0)
			{
				html += '<div class="lbmwidget-coupon" style="text-align:center" onclick="lbmPopupEmail()">';
				html += '	<div class="lbmcoupon-signup">Get Exclusive Deals<br>in your Inbox!</div>';
				html += '	<div class="lbmcoupon-signup-btn">SIGN UP NOW</div>';
				html += '</div>';
			}

		}

		_coupons[i] = coupon;

		var	maxlen = 70,
			name = coupon.vendor.name.replace(/&apos/g,'\\&apos');

		html += '<div id="coupon_' + coupon.attributes.couponID + '" class="lbmwidget-coupon" onclick="lbmPopupCoupon(' + i + ',\'' + name + ':' + coupon.vendor.vendorID + '\')">';

		html += '	<div class="lbmcoupon-logo"><img src="https://www.leafbuyer.com/vendors/' + coupon.vendor.vendorID + '/thumblogo.png"></div>';
		html += '	<div class="lbmcoupon-body"><span>';
		html += '		<div class="lbmcoupon-heading">';
		html += '			<div class="lbmcoupon-type">';

		if (coupon.medical == 'true' && coupon.retail == 'false')
			html += 'Med Only';
		else if (coupon.medical == 'false' && coupon.retail == 'true')
			html += 'Rec Only';
		else if (coupon.medical == 'true' && coupon.retail == 'true')
			html += 'Med/Rec';

		html += '			</div>';
		html += '			<div class="lbmcoupon-title">' + coupon.product + '</div>';
		html += '		</div>';

		var	text = coupon.text.replace(/(<br>)+/gi,' ').replace(/<a.*?>/i,'').replace(/<\/a>/i,'');

		html += '		<div>';
		html += '			<div class="lbmcoupon-type">' + (typeof(coupon.vendor.key) !== 'undefined' ? '<img src="https://www.leafbuyer.com/cannawidget/images/text-signup.png" alt="Sign up for text alerts">' : '') + '</div>';
		html += '			<div class="lbmcoupon-text">' + (text.length > maxlen ? text.substring(0, maxlen).replace(/ [^ ]+$/,'') +  '...' : text) + '</div>';
		html += '		</div>';
		html += '	</span></div>';
	
		html += '</div>';
	}

	jQuery('#lbmcoupons').html(html);
}
catch (e)
{
	jQuery.ajax({
		url: 'https://www.leafbuyer.com/cw-error?m=' + e.message.replace(/ /g,'+')
	});
}

	try
	{
		buildCarousel();
	}
	catch (e)
	{
		console.log(e.message);
	}

	if (coupons.attributes.count == 0)
	{
		jQuery('#lbmloading').hide();
		jQuery('#lbmnonefound').show();
	}
	
	jQuery('.caroufredsel_wrapper').css('visibility','visible');
	
	jQuery('#lbmloading').hide();

//	startTimer();
}

function pixels(el)
{
	var ruler = jQuery('#lbmruler').css('font', el.css('font')).css('white-space','nowrap').html(el.html());
	return ruler.width();
}

function viewProfile()
{
	ga('leafbuyer.send','event','grasscity','widget:profile','widget:-:widget:profile:'+_vendor);
	ga('leafbuyer.send','event','grasscity','widget:profile','widget:-:widget:profile:'+_lbvendor);
}

function lbmUpdateCoupon(id)
{
	id = lbmCouponID;

	var	active = jQuery('#lbmdesktop'),
		inactive = jQuery('#lbmmobile');

	if (jQuery('#lbmmobile').css('display') != 'none')
	{
		active = jQuery('#lbmmobile');
		inactive = jQuery('#lbmdesktop');
	}

	jQuery(inactive).find("*").each(function() {
		if (typeof(jQuery(this).attr("id")) != 'undefined')
			jQuery(this).attr('id',
				'_' + jQuery(this).attr('id').replace(/^_/,''));
	});

	jQuery(active).find("*").each(function() {
		if (typeof(jQuery(this).attr("id")) != 'undefined')
			jQuery(this).attr('id',
				jQuery(this).attr('id').replace(/^_/,''));
	});

	var	cxml = _coupons[id],
		fontsize = 28,
		tgt = jQuery(active).attr('id');

	if (typeof(cxml.vendor.recurl) != 'undefined')
		jQuery('#' + tgt + ' #couponrecurl').attr('href',cxml.vendor.recurl);
	if (typeof(cxml.vendor.medurl) != 'undefined')
		jQuery('#' + tgt + ' #couponmedurl').attr('href',cxml.vendor.medurl);
	if (typeof(cxml.vendor.linkurl) != 'undefined')
	{
		jQuery('#' + tgt + ' #couponlinkurl').attr('href',cxml.vendor.linkurl);
		jQuery('#' + tgt + ' #couponlinkurl').text(cxml.vendor.linktext);
	}

	jQuery('#' + tgt + ' #couponheading').css('font-size',fontsize+'px').html(cxml.product);
/*
	var	i = 0;
	while (pixels(jQuery('#' + tgt + ' #couponheading')) > 340)
	{
		jQuery('#' + tgt + ' #couponheading').css('font-size',
			(jQuery('#' + tgt + ' #couponheading').css('font-size').replace(/[^0-9]/g,'') - 1) + 'px');

		if (i++ > fontsize)
			break;
	}
*/
	jQuery('#' + tgt + ' #couponheading').html(cxml.product);

	if (cxml.display == 'range')
	{
		jQuery('#' + tgt + ' #couponrange').html(cxml.lowPrice.replace(/\.00/,'') + " - " + cxml.highPrice.replace(/\.00/,''));
	}
//	else if (cxml.display == 'discount')
	{
	if (typeof(cxml.percentage) != 'undefined')
		jQuery('#' + tgt + ' #couponpercentage').html(Math.floor(cxml.percentage) + "% Off");
	if (typeof(cxml.originalPrice) != 'undefined')
		jQuery('#' + tgt + ' #couponoriginal').html((jQuery('#lbmdesktop').length != 0 ? "Original " : "") + cxml.originalPrice.replace(/\.00/,''));
	if (typeof(cxml.salePrice) != 'undefined')
		jQuery('#' + tgt + ' #couponsale').html((jQuery('#lbmdesktop').length != 0 ? "Sale " : "") + cxml.salePrice.replace(/\.00/,''));
	if (typeof(cxml.quantity) != 'undefined')
		jQuery('#' + tgt + ' #couponweight').html(cxml.quantity);
	}

	jQuery('#' + tgt + ' #coupontext').html(cxml.text);
	jQuery('#' + tgt + ' #coupondisclaimer').html(cxml.disclaimer);
	jQuery('#' + tgt + ' #couponexpires').html(cxml.expires != '' ? "Expires " + cxml.expires : "No expiration");
	jQuery('#' + tgt + ' #couponvendor').html(cxml.vendor.name);
	jQuery('#' + tgt + ' #couponlink').html('<a href="https://www.leafbuyer.com/' + cxml.vendor.typename + '/' + cxml.vendor.state + '/' + cxml.vendor.city + '/' + cxml.vendor.pathname + '" target="_blank" rel="nofollow" onclick="viewProfile()">View all our deals on Leafbuyer.com</a>');
	jQuery('#' + tgt + ' #couponlogo').attr('src','https://www.leafbuyer.com/vendors/' + cxml.vendor.vendorID + '/logo.png?t=' + (new Date()).getTime());
	jQuery('#' + tgt + ' #couponaddr').html(cxml.vendor.addr1 + ', ' + (cxml.vendor.addr2.length != 0 ? cxml.vendor.addr2 + ', ' : '') + cxml.vendor.city + ', ' + cxml.vendor.state);
	jQuery('#' + tgt + ' #couponphone').html(cxml.vendor.phone);

	jQuery('#' + tgt + ' #couponmed').hide();
	jQuery('#' + tgt + ' #couponrec').hide();
	jQuery('#' + tgt + ' #coupondel').hide();
	jQuery('#' + tgt + ' #couponmedrecdel').html('');

	if (cxml.vendor.medical == 'true')
		jQuery('#' + tgt + ' #couponmed').show();
	if (cxml.medical == 'true')
		jQuery('#' + tgt + ' #couponmedrecdel').html('Medical Only');

	if (cxml.vendor.retail == 'true')
		jQuery('#' + tgt + ' #couponrec').show();
	if (cxml.retail == 'true')
		jQuery('#' + tgt + ' #couponmedrecdel').html('Recreational Only');

	if (cxml.medical == 'true' && cxml.retail == 'true')
		jQuery('#' + tgt + ' #couponmedrecdel').html('Medical or Recreational');

	if (cxml.vendor.delivery == 'true')
		jQuery('#' + tgt + ' #coupondel').show();

	jQuery('#' + tgt + ' #couponwebsite').hide();

	if (cxml.vendor.vendorID == 6968)
		jQuery('#' + tgt + ' #couponwebsite').show();

	jQuery('#' + tgt + ' #couponbarcode').hide();
	jQuery('#' + tgt + ' #couponbarcode').html('');

	if (cxml.barcode != null)
	{
		jQuery('#' + tgt + ' #couponbarcode').show();
		jQuery('#' + tgt + ' #couponbarcode').html('<img src="https://www.leafbuyer.com/vendors/' +
			cxml.vendor.vendorID + '/barcodes/' + cxml.barcode + '.png" class="lbmbarcode" onerror="jQuery(\'#' + tgt + ' #couponbarcode\').css(\'height\',\'0px\'); jQuery(this).hide()">');
	}

	var	title = null, value = null;
	var	thc = (typeof(cxml.thc) != 'undefined' ? cxml.thc : null),
			cbd = (typeof(cxml.cbd) != 'undefined' ? cxml.cbd : null);

	jQuery('#' + tgt + ' #couponthccbdcell').hide();

	if (thc != null || cbd != null)
	{
		if (cxml.type == 1)
		{
			if (thc != null && cbd != null)
			{
				title = "THC / CBD Levels";
				value = thc + " / " + cbd;
			}
			else if (thc != null)
			{
				title = "THC Level";
				value = thc;
			}
			else if (cbd != null)
			{
				title = "CBD Level";
				value = cbd;
			}

			if (title != null && value != null)
			{
				jQuery('#' + tgt + ' #couponthccbdtitle').html(title);
				jQuery('#' + tgt + ' #couponthccbd').html(value);

				jQuery('#' + tgt + ' #couponthccbdcell').show();
			}
		}
	}

	jQuery('#' + tgt + ' #couponrange').hide();
	jQuery('#' + tgt + ' #couponpercentage').hide();
	jQuery('#' + tgt + ' #couponpricecell').hide();
	jQuery('#' + tgt + ' #couponoriginal').hide();
	jQuery('#' + tgt + ' #couponsale').hide();
	jQuery('#' + tgt + ' #couponquantitycell').hide();
	jQuery('#' + tgt + ' #couponrecurl').hide();
	jQuery('#' + tgt + ' #couponmedurl').hide();
	jQuery('#' + tgt + ' #couponlinkurl').hide();
	jQuery('#' + tgt + ' #coupontextalert').hide();

	if (typeof(cxml.vendor.recurl) != 'undefined')
		jQuery('#' + tgt + ' #couponrecurl').show();
	if (typeof(cxml.vendor.medurl) != 'undefined')
		jQuery('#' + tgt + ' #couponmedurl').show();
	if (typeof(cxml.vendor.linkurl) != 'undefined')
		jQuery('#' + tgt + ' #couponlinkurl').show();
	if (typeof(cxml.vendor.key) != 'undefined')
		jQuery('#' + tgt + ' #coupontextalert').show();

	if (cxml.display == 'range')
	{
		jQuery('#' + tgt + ' #couponrange').show();
	}
//	else if (cxml.display == 'discount')
	{
	if (typeof(cxml.percentage) != 'undefined')
		jQuery('#' + tgt + ' #couponpercentage').show();

	if (typeof(cxml.originalPrice) != 'undefined')
	{
		jQuery('#' + tgt + ' #couponoriginal').show();
		jQuery('#' + tgt + ' #couponpricecell').show();
	}

	if (typeof(cxml.salePrice) != 'undefined')
	{
		jQuery('#' + tgt + ' #couponsale').show();
		jQuery('#' + tgt + ' #couponpricecell').show();
	}

	if (typeof(cxml.quantity) != 'undefined')
		jQuery('#' + tgt + ' #couponquantitycell').show();
	}

	if (cxml.vendor.city.match(/(boulder|garden city)/i))
		jQuery('#' + tgt + ' #lbmpopupcoupon').addClass('lbmnodashes');
	else
		jQuery('#' + tgt + ' #lbmpopupcoupon').removeClass('lbmnodashes');
}

function lbmResize(redraw)
{
	if (! jQuery('#lbmwoverlay').is(':visible'))
		return;

	if (typeof(redraw) !== "undefined" && redraw)
		lbmUpdateCoupon();

	jQuery('#lbmwoverlay-contents').css('height','initial');
	jQuery('#lbmwoverlay-contents').css('overflow-y','visible');
	jQuery('#lbmwoverlay-contents').css('width','initial');
	jQuery('#lbmwoverlay-contents').css('overflow-x','visible');

//	if (jQuery('#lbmwoverlay-contents').height() > getWindowHeight() - 44)
	if (jQuery('#lbmwoverlay-contents').height() > jQuery(window).height() - 44)
	{
		jQuery('#lbmwoverlay-contents').height(jQuery(window).height() - 44);
		jQuery('#lbmwoverlay-contents').css('top','10px');
		jQuery('#lbmwoverlay-contents').css('overflow-y','scroll');
	}
//	else
//		jQuery('#lbmwoverlay-contents').css('overflow-x','visible');

//	if (jQuery('#lbmwoverlay-contents').width() > getWindowWidth() - 44)
	if (jQuery('#lbmwoverlay-contents').width() > jQuery(window).width() - 44)
	{
		jQuery('#lbmwoverlay-contents').width(jQuery(window).width() - 44);
		jQuery('#lbmwoverlay-contents').css('left','10px');
		jQuery('#lbmwoverlay-contents').css('overflow-x','scroll');
	}
//	else
//		jQuery('#lbmwoverlay-contents').css('overflow-x','visible');
}

var cpopup = null;

function lbmPopupPrint(mobile)
{
	ga('leafbuyer.send','event','grasscity','widget:print','widget:-:widget:print:'+_vendor+':'+_lbvendor);
	ga('leafbuyer.send','event','grasscity','widget:print-coupon','widget:-:widget:print:'+_vendor+':'+_lbvendor+':'+_lbcoupon);
	lb('click','coupon:print:'+_lbvendor+':'+_lbcoupon+':'+lat+':'+lng);

	if (mobile)
	{

		return;
	}

	if (cpopup == null || cpopup.closed)
		cpopup = window.open();

	var doc = cpopup.document;

	var html = '<html><head>';

	html += '<link rel="stylesheet" href="https://www.leafbuyer.com/cannawidget/css/widget.css" />';
	html += '<link rel="stylesheet" href="https://www.leafbuyer.com/cannawidget/grasscity/css/widget.css" />';

//	html += '<link rel="stylesheet" href="https://www.leafbuyer.com/css/grasscity.css" />';
	html += '<link href="//fonts.googleapis.com/css?family=Lato:400,700,700italic" rel="stylesheet" type="text/css">';

	html += '<script type="text/javascript" src="https://www.leafbuyer.com/js/vendor/jquery.js"><\/script>';
	html += '<script>';
	html += '	jQuery(window).on("load", function(e) { jQuery("#lbmwoverlay").css("display","block").css("background-image","none"); setTimeout( function() { window.print(); window.close(); }, 100); } );';
	html += '<\/script>';
	html += '<\/head>';
	html += '<body>';
	html += '<div id="lbmwoverlay">' + jQuery('#lbmwoverlay-contents').html().replace(/"lbmsharing"/g,'"lbmsharing" style="visibility:hidden"').replace(/"lbmmobile">/,'"lbmmobile" style="visibility:hidden">') + '<\/div>';

	html += '<\/body>';
	html += '<\/html>';

	doc.write(html);
	doc.close();

	cpopup.focus();
}

var	resizeTimer = null;

function destroyCarousel()
{
	jQuery("#lbmcoupons").trigger("destroy");
}

function buildCarousel()
{
	if (! jQuery.fn.carouFredSel)
	{
		setTimeout(buildCarousel, 100);
		return;
	}

	var	num = jQuery('#lbmmobile').css('display') != 'none' ? 3 : 5;

	jQuery("#lbmcoupons-wrapper").outerHeight(jQuery(".lbmwidget-coupon").outerHeight() * num + 2);
	
	jQuery('.lbmcoupon-body').each( function() {
		jQuery(this).outerWidth(jQuery(this).closest('.lbmwidget-coupon').width() -
			jQuery(this).prev().outerWidth() - 5);
	});
//	jQuery('.lbmcoupon-text, .lbmcoupon-heading').each( function() {
	jQuery('.lbmcoupon-heading').each( function() {
		jQuery(this).outerWidth(jQuery(this).closest('.lbmcoupon-body').outerWidth());
	});

	jQuery("#lbmcoupons").carouFredSel({

		direction:"up",
		items: { visible:num },
	
		scroll:{ items:1, pauseOnHover:'immediate-resume' },
		auto:{ play:true,timeoutDuration:4000 },
		prev:{ button:'#coupon_prev', key:'up' },
		next:{ button:'#coupon_next', key:'down' },
		debug: true
	});
}

var	searchTerm = "";

function onLoad()
{
	jQuery("#lbmsearch").autocomplete({
		open: function() {
			searchItem = null;
		},
		source: function( request, response ) {
			searchTerm = request.term;

			var	l1,l2,
				reStartsWith = new RegExp( "^" + jQuery.ui.autocomplete.escapeRegex(request.term).replace(/ /g,' *'), "i" ),
				reContainsNotStarts = new RegExp( ".+" + jQuery.ui.autocomplete.escapeRegex(request.term).replace(/ /g,' *') + ".*", "i" ),
				reContains = new RegExp( ".*" + jQuery.ui.autocomplete.escapeRegex(request.term).replace(/ /g,' *') + ".*", "i" );

			l1 = jQuery.map( clients, function( item ) {
				if (reStartsWith.test( item.label ) ||
					reStartsWith.test( item.label.replace(/[^a-z 0-9]/gi,'') ))
				{
					return { label: item.label, vid: item.value }
				}
			});

			l2 = jQuery.map( clients, function( item ) {
				if (reContainsNotStarts.test( item.label ) ||
					reContainsNotStarts.test( item.label.replace(/[^a-z 0-9]/gi,'') ))
				{
					return { label: item.label, vid: item.value }
				}
			});

			response(l1.concat(l2));
		},
		select: function( e, ui ) {
			jQuery('#lbmvendorID').val(ui.item.vid);
			jQuery('#search').val(ui.item.label);
			searchItem = ui.item;
		},

		close: function( e, ui ) {
			if (searchItem != null)	// item selected from list
			{
				jQuery('#lbmstype').val('name');
				jQuery('#lbmvendorID').val(searchItem.vid);

				lbmUpdate(false);

				jQuery('#lbmsearch').blur();
			}
			else
				searchTerm = '';
		},
		minLength: 3,
		autoFocus: true
	});

	jQuery('select').on('keydown', function(e){
		if(e.keyCode === 38 || e.keyCode === 40) { //up or down
			e.preventDefault();
			return false;
		}
	});

	var	_timer = setInterval(function() {
		if (jQuery('#lbmwoverlay #lbmwoverlay-contents').length != 0)
		{
			jQuery(document.body).append(jQuery('#lbmwoverlay').detach());
			clearInterval(_timer);
		}
	}, 500);

	jQuery('#lbmwoverlay').on("show", function () {
		jQuery('#lbmcoupons').trigger('stop', true);
//console.log('show popup');
	});
	jQuery('#lbmwoverlay').on("hide", function () {
		jQuery('#lbmcoupons').trigger('play', true);
//console.log('show popup');
	});

	jQuery('#lbmcity').on('change', function(event, update) {
		if (typeof update === "undefined")
			update = true;

		jQuery('#lbmvtype').find('option').remove();

		for (var i = 0; i < _types.length; i++)
		{
			if (_subtypes[i].length == 0)
				continue;

			jQuery('#lbmvtype').append(jQuery('<option/>', { 
				value: _types[i].split(':')[1] + ':0',
				text : _types[i].split(':')[0]
			}));

			var	added = false, found = false;

			for (var j = 0; j < _subtypes[i].length; j++)
			{
				var	ptype = _subtypes[i][j],
					value = ptype.replace(/.*:/,''),
					text = ptype.replace(/:.*/,'');

				if (jQuery.inArray(value, types[jQuery('#lbmcity option:selected').val()]) == -1)
					continue;

				found = true;

//				if (_types[i].match(/.*:[23]/))		// no headshop or growstore subtypes
//					continue;

				added = true;

				jQuery('#lbmvtype').append(jQuery('<option/>', { 
					value: _types[i].replace(/.*:/,'') + ':' + value,
					text : '-' + text
				}));
			}

			if (! added && ! found)
				jQuery('#lbmvtype option:last').remove();
		}

		if (jQuery('#lbmcity option:selected').val().match(/.*[,:]ca/))
//			jQuery('#lbmvtype option:contains("dispensaries")').text("collectives").wSelect('change');
			jQuery('#lbmvtype option:contains("shops")').text("collectives").wSelect('change');
		else
//			jQuery('#lbmvtype option:contains("collectives")').text("dispensaries").wSelect('change');
			jQuery('#lbmvtype option:contains("collectives")').text("shops").wSelect('change');

//		jQuery('#lbmvtype option:eq(1)').prop('selected', true);	// flower
		jQuery('#lbmvtype option:eq(0)').prop('selected', true);	// shops

		jQuery('#lbmvtype').wSelect('reset').wSelect();
//		jQuery('.wSelect').css('width','120px');

		jQuery('#lbmstype').val('business');

		if (update)
			lbmUpdate(false);

		jQuery('#lbmcity').wSelect();
		jQuery('.wSelect').css('width','120px');
		jQuery('.wSelect').css('margin-bottom','5px');
	});

	jQuery('#lbmvtype').change(function() {
		jQuery('#lbmstype').val('business');
		lbmUpdate(false);
	});

	jQuery('#lbmwidget').css('visibility','visible');

	if (jQuery('#lbmmobile').css('display') != 'none' && navigator.geolocation)
	{
		navigator.geolocation.getCurrentPosition(function(position) {
			lat = position.coords.latitude;
			lng = position.coords.longitude;

			lbmUpdate(true);
		},
		function(error) {
			lbmUpdate(true);
		});
	}
	else

		lbmUpdate(true);

	jQuery(window).resize(function() {
		clearTimeout(resizeTimer)
		resizeTimer = setTimeout(lbmResize,500);
	});
}

var	onLoadCalled = false,
	checkAttempts = 0;

function checkjQuery()
{
	if (typeof jQuery === "undefined")
		setTimeout(checkjQuery, 100);
	else if (! onLoadCalled)
	{
		onLoadCalled = true;

		jQuery(window).on('load', function(e) {
			jQuery('.lbmwidget span.jcf-select').remove();
		});

		// load jQuery-dependent plugins

		var e = document.createElement("script");

		e = document.createElement("script");
		e.type="text/javascript";
		e.src = "https://www.leafbuyer.com/js/jquery-ui-min.js";
		document.getElementsByTagName("head")[0].appendChild(e);

		e = document.createElement("script");
		e.type="text/javascript";

		e.src = "https://www.leafbuyer.com/js/widget.js";
		document.getElementsByTagName("head")[0].appendChild(e);

		checkjQueryUI();
	}
}

function checkjQueryUI()
{
	if (checkAttempts > 10)
	{
		jQuery('.lbmwidget').parent().hide();
		return;
	}

	if (typeof jQuery.ui === "undefined" ||
		typeof jQuery.fn.wSelect === "undefined")
	{
		setTimeout(checkjQueryUI, 500);
		checkAttempts++;
	}
	else
		onLoad();
}

var e = document.createElement("script");

e.type="text/javascript";
e.src = "https://www.leafbuyer.com/js/widgetdropdowns.js?d=20181022";
document.getElementsByTagName("head")[0].appendChild(e);

checkjQuery();

function nagmon() {}	/* DO NOT REMOVE - NAGIOS CHECKS THIS */

