//serves the mini_ads

function serve_ads(call_url,show_url,click_url,div_class_name){

	jQuery(window).ready(function($){
	
		var ad_divs = $('.'+div_class_name);
	
		// get the ads from the server
		$.get(call_url,function(data){
		
			// we fill up all the divs that we can
			// for each advert we send a view track back to the server
			// if more ads exits than we have divs, the over spill is not
			// rendered ( this is acceptable since the registering that an ad has been viewed,
			// will only occur if that ad is rendered ). 
			for(idx=0;idx<data.length;idx++){
				ad = data[idx];
				
				try{
					next_div = $(ad_divs[idx]);
				} catch (error){
					break;
				}
				
				id = ad['id']
				
				//first link
				link_dv = next_div.find('.ad-block-link');
				link_dv.html(ad['title']);
				link_dv.attr('href',click_url+'?advert_id='+id);
				
				//next link
				link_dv = next_div.find('.ad-block-url');
				link_dv.html(ad['url']);
				link_dv.attr('href',click_url+'?advert_id='+id);
				
				//description
				next_div.find('.ad-block-body').html(ad['description']);
				
				//ad served - so tell the server
				$.get(show_url+'?advert_id='+id);
			}
			
		},'json');
	
	});

}
