/*
	This is the manager that will process the ads across the site
	Ads slow the loading of the page, and because of the JS/Iframe combination, they tend to block the load of other elements on the page
	Please use this mechanism to make sure you don't affect the load time of the page
	
	USAGE:
		
		adsManager.add("#myAdsContainer", '<a href="http://ad-server.com/"><img src="http://link-to-ad.com/ad.png"></a>');
		
*/

var adsManager = new function() {
	var ads = new Array();
	
	/*
		@target has to containe the element where the content is inserted
		@target has to contain the element jQuery style, with a "#" or ".", or as a jQuery object directly
		@content is the HTML that will be inserted into @target
	 */
	this.add = function(target, content) {
		ads.push({
			t: $(target),
			c: content
		});
	}
	
	/*
		Load the ads, but make sure they are doing so only AFTER the page is completed, 
		with a small delay to give the browser time to finish the display
	 */
	$(window).load(function(){
	//$(document).ready(function(){
	//this.loadAds = function(){
		setTimeout(function(){
			$(ads).each(function(){
				$(this.t).html(this.c);

				/*
					Lazy loader handler, it will avoid ads from loading if they ain't showing
					at screen.
				*/
				$(this.t).find("img").each(function(){
					if (typeof $(this).lazyload == 'function') // make sure lazyload is loaded, avoid script errors
						$(this).lazyload({
							placehoolder: "", // // will avoid https issues
							effect: "fadeIn",
							threshold: 100
						});
				});

			});
		}, 500);
	});
}
