

var CarAds = function(options) {
	this._opts = options;
}

CarAds.prototype = {

	_opts: null,

	printAds: function(carsString) {
		var template = this._opts.template.html();
		var carAdsBlock = this._opts.printTo;
		var carAdsContainer = this._opts.container;
		var otherAdsLink = this._opts.otherAdsLink;
        var th = this;

    jQuery.ajax({
			url: this._opts.serviceUrl,
			data: {
				cars: carsString
			},
			success: function(info) {

				$.each(info.ads, function() {
					var adHtml = $.template(template, this);
					carAdsBlock.append(adHtml);
				});
				carAdsContainer.show();

				if (info.link) {
					otherAdsLink.attr('href', info.link)
					otherAdsLink.show();
				}

			},
            complete: function (XMLHttpRequest, textStatus) {

                if(textStatus !== "success" || XMLHttpRequest.status !== 200)
                    setTimeout(function(){th.printAds(carsString); console.log('+')}, 200);
  
            },
			dataType: 'jsonp'
		})
	}

};


