(function() {
	
	var random_ = function(max) {
		return Math.floor(Math.random() * max);
	};
	
	var embed_ = function(ad) {
		var output = '';
		if(ad.type == 'image') {
			output = '<div class="random-image-ad"><a href="'+ ad.href + '"><img alt="' + ad.alt + '" height="' + ad.height + '" src="' + ad.src + '" width="' + ad.width + '" /></a></div><div class="phone">1-866-256-2531</div>';
		} else if(ad.type == 'flash') {
			var id = random_(99999999);
			output = '<div class="random-flash-ad"><object data="' + ad.src + '" height="' + ad.height + '" id="random-flash-ad-' + id + '" type="application/x-shockwave-flash" width="' + ad.width + '"><param name="movie" value="' + ad.src + '" /></object></div>';
		}
		return output;
	};
	
	var RandomAds = {
		
		init: function(ads) {
			var rand = null;
			$.each(ads, function(index, position) {
				if(rand == null) {
					rand = random_(position.ads.length);
				}
				$(position.selector).html(embed_(position.ads[rand]));
			});
		}
	
	};
	
	window.RandomAds = RandomAds;
	
})();

$(document).ready(function() {
	var positions = [
	     {
	    	 selector: '#edr-top-banner-ad',
	    	 ads: [
	    	       { src: '/ads/orange/edr-ad-669x86.jpg', width: 669, height: 86, type: 'image', alt: 'Find a Drug Rehab Near You', href: '/help-is-here' }
	    	 ]
	     },
	     {
	    	 selector: '#edr-right-column-ad',
	    	 ads: [
	    	       { src: '/ads/orange/edr-ad-302x252.jpg', width: 302, height: 252, type: 'image', alt: 'Find a Drug Rehab Near You', href: '/help-is-here' }
	    	 ]
	     },
	     {
	    	 selector: '#edr-bottom-banner-ad',
	    	 ads: [
	    	       { src: '/ads/orange/edr-ad-669x86.jpg', width: 669, height: 86, type: 'image', alt: 'Find a Drug Rehab Near You', href: '/help-is-here' }
	    	 ]
	     }
	];
	
	RandomAds.init(positions);
});
