if( typeof jQuery == "undefined" )
{
	var script = document.createElement('script')
	script.setAttribute("type","text/javascript")
	script.setAttribute("src", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js")
	if( typeof script != "undefined" )
	{
		document.getElementsByTagName("head")[0].appendChild(script)
	}
}

function doCoupons()
{
	if( typeof jQuery == "undefined" )
	{
		setTimeout(doCoupons,10);
	}
	else
	{
		(function($)
		{
			$(document).ready(function()
			{
				$.ajax({
					type: 'GET',
					url: 'https://www.webmarketingconsole.com/api/?rt=coupon_feed&format=json&lid='+lid,
					dataType: 'jsonp',
					crossDomain: true,
					error: function(jqXHR, textStatus, errorThrown)
					{
						
					},
					complete: function(jqXHR, textStatus)
					{
						
					},
					success: function(reply)
					{
						var data = reply.data;
						for( var i in data.couponFeed )
						{
							var div = $("<div class='coupon'></div>");
							
							div.append("<h1 class='c_head'>" + data.couponFeed[i].head + "</h1>");
							
							if( data.couponFeed[i].subhead != "" )
							{
								div.append("<h2 class='c_subhead'>" + data.couponFeed[i].subhead + "</h2>");
							}
							
							if( data.couponFeed[i].thumb_ext != null )
							{
								div.append("<img class='c_thumb' src='http://www.searchmagnetlocal.com/coupon/" + data.couponFeed[i].id + "." + data.couponFeed[i].thumb_ext + "' />");
							}
							
							div.append("<div class='c_desc'>" + data.couponFeed[i].description + "</div>");
							div.append("<img class='c_qr' src='http://www.searchmagnetlocal.com/qr_code/php/qr_img.php?d=http://www.searchmagnetlocal.mobi/coupon.php?lid=" + data.couponFeed[i].lid + "&e=H&s=2&t=J' />");
							
							var date_start = new Date(data.couponFeed[i].t_start * 1000);
							var date_expire = new Date(data.couponFeed[i].t_expire * 1000);
							
							div.append("<div class='c_date'>This coupon is valid: " + date_start.toDateString() + " through " + date_expire.toDateString() + "</div>");
							
							div.append("<div class='c_terms'>" + data.couponFeed[i].c_cond + "</div>");
							
							$("#couponfeed").append(div);
						}
					}
				});
			});
		})(jQuery);
	}
}

setTimeout(doCoupons,10);
