function load_ads(mode)
{
	if(mode == undefined)
		mode = 'fill';
		
	url = AdBlocks['url'];
	var urlConstructor = new Object;
	var newdiv= document.createElement('div');
	for(var j in AdBlocks['blocks'])
	{
		if(AdBlocks['blocks'][j]['type'] != undefined)
			var current_ad_type = AdBlocks['blocks'][j]['type'];
		else
			var current_ad_type = "default";

		

		
		if(mode == 'fill')
			AdBlocks['blocks'][j]['ad_zone_resized'] = false;
		
		if(AdBlocks['blocks'][j]['ads'] == 'auto')
		{
		
			if(AdBlocks['blocks'][j]['layout'] == 'vertical')
			{
				current_block_dimension = document.getElementById(j).offsetHeight;
				if(AdBlocks['dimensions'][current_ad_type]['height'] == '')
				{
					newdiv.className = (current_ad_type != 'default')?('boat_ad'+'_'+current_ad_type):'boat_ad';
					//newdiv.style.display = 'hidden';
					document.getElementById(j).appendChild(newdiv);
					AdBlocks['dimensions'][current_ad_type]['height'] = $(newdiv).outerHeight(true);
				}
				var ad_width = AdBlocks['dimensions'][current_ad_type]['height'];
			
			}
			else
			{
				current_block_dimension = document.getElementById(j).offsetWidth;
				if(AdBlocks['dimensions'][current_ad_type]['width'] == '')
				{
				
					newdiv.className = (current_ad_type != 'default')?('boat_ad'+'_'+current_ad_type):'boat_ad';
					//newdiv.style.display = 'hidden';
					document.getElementById(j).appendChild(newdiv);
					AdBlocks['dimensions'][current_ad_type]['width'] = $(newdiv).outerWidth(true);
					//AdBlocks['dimensions'][current_ad_type]['width'] = parseInt($(newdiv).css('width'))+parseInt($(newdiv).css('marginLeft'))+parseInt($(newdiv).css('marginRight'))+parseInt($(newdiv).css('paddingRight'))+parseInt($(newdiv).css('paddingLeft'));
				}
				var ad_width = AdBlocks['dimensions'][current_ad_type]['width'];
			}
		
			if(AdBlocks['blocks'][j]['block_size'] == undefined || current_block_dimension > AdBlocks['blocks'][j]['block_size'])
				AdBlocks['blocks'][j]['block_size'] = current_block_dimension;
			
			var ads_needed  = Math.floor(AdBlocks['blocks'][j]['block_size']/ad_width);
			AdBlocks['blocks'][j]['ads_needed'] = ads_needed;	
		}
		else
		{
			var ads_needed = AdBlocks['blocks'][j]['ads'];
			AdBlocks['blocks'][j]['ads_needed'] = ads_needed;
		}
		
		if(AdBlocks['blocks'][j]['ad_count'] == undefined)
		AdBlocks['blocks'][j]['ad_count'] = 0;
		
		if(urlConstructor[current_ad_type] == undefined)
			urlConstructor[current_ad_type] = 0;
			
		urlConstructor[current_ad_type] += ads_needed - AdBlocks['blocks'][j]['ad_count'];
	}
	
	make_ajax_call = 0;
	for(var k in urlConstructor)
	{
		if(urlConstructor[k] > 0)
		{
			if(urlConstructor[k] > make_ajax_call) //Set make_ajax_call to non zero if any ads are needed
				make_ajax_call = urlConstructor[k];
			url += '&ad_type['+k+']='+urlConstructor[k];
		}
	}
	
	if(AdBlocks['displayed_ad_ids'] != '')
		url += '&displayed_ad_ids='+AdBlocks['displayed_ad_ids'];
	//ONLY PERFORM AJAX CALL IF  MORE ADS ARE NEEDED!
	if(make_ajax_call == 0)
		return;
		
	var xmlhttp
	xmlhttp=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp!=null)
	{
	
	   timeoutId = window.setTimeout(function() 
        { 	xmlhttp.abort();
         //                               alert("Error"); 
		 }, 30000); 
	
				xmlhttp.open("GET",url,true)
		xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4)
		{
			// if "OK"
			if (xmlhttp.status==200)
			{
			    window.clearTimeout(timeoutId); 
				if( xmlhttp.responseText != "" )
				{
				 var boatAds = eval('(' + xmlhttp.responseText + ')');
				 handle_ads(boatAds, mode);
				}
			}
			else
			{
				alert("Problem retrieving data:" + xmlhttp.statusText)
			}
		}
 
		}   

		xmlhttp.send(null)
	}
	else
	{
		alert("Your browser does not support XMLHTTP.")
	}
}


function ad_window_resize(e) {
     window.clearTimeout(resizeTimeoutId);
     resizeTimeoutId = window.setTimeout('load_ads(\'append\');', 10);
}

function handle_ads(ad_data, mode)
{




var ad_pointers = new Object;


for(var j in AdBlocks['blocks'])
{
	if(AdBlocks['blocks'][j]['type'] != undefined)
		var current_ad_type = AdBlocks['blocks'][j]['type'];
	else
		var current_ad_type = "default";
		
	if(ad_pointers[current_ad_type] == undefined)
		ad_pointers[current_ad_type] = 0;

	
	var ad_pointer = ad_pointers[current_ad_type]
	
	var ad_div = document.getElementById(j);
	if(mode == 'fill')
		ad_div.innerHTML="";
	
	
	if(AdBlocks['blocks'][j]['ads_needed'] > AdBlocks['blocks'][j]['ad_count'] || mode == 'fill')
	{
	$(ad_div).outerHeight()
	
	for(looper = 0; looper < (AdBlocks['blocks'][j]['ads_needed']- AdBlocks['blocks'][j]['ad_count']); looper++)
	{
		
		if(ad_data[current_ad_type][ad_pointer] != undefined)
		{
		
			
		 	var newdiv= document.createElement('div');
			var current_ad = ad_data[current_ad_type][ad_pointer];
			
			AdBlocks['displayed_ad_ids'] += ((AdBlocks['displayed_ad_ids'] == '') ? '' : ',')+current_ad['ID'];
		//TEXT ONLY ADS
		//-------------------------------------------------------------------------------------------------------------------------
		if(current_ad_type == 'text')
		{	
			newdiv.className = 'boat_ad_text';
			var newlink = document.createElement('a');
			newlink.setAttribute('href', '/classifieds/'+ad_data[current_ad_type][ad_pointer]['ad_url']);
		
			if(current_ad['year'] != '' && current_ad['year'] != undefined)
				newlink.innerHTML += current_ad['year']+' ';
		
			newlink.innerHTML += current_ad['listing_title'];
			if(current_ad['make'] != '' && current_ad['make'] != undefined)
				newlink.innerHTML += ' '+current_ad['make'];
				//newlink.innerHTML += ' '+current_ad['num_photos']+' '+'photo(s)';
			newdiv.appendChild(newlink);
		}
		//PHOTO ONLY ADS
		//-------------------------------------------------------------------------------------------------------------------------
		else if(current_ad_type == 'photo')
		{
			newdiv.className = 'boat_ad_photo';
			var newlink = document.createElement('a');
			newlink.setAttribute('href', '/classifieds/'+current_ad['ad_url']);
			
			var newimage= document.createElement('img');
			
			if(current_ad['Main_photo'] == null)
				image_src = "/classifieds/templates/default/img/no_photo.gif";
			else
				image_src = '/classifieds/files/'+current_ad['Main_photo'];
				
			newimage.setAttribute('src' , image_src);
				
			newlink.appendChild(newimage);
			newdiv.appendChild(newlink);
			var newElem = document.createElement('br');
			newElem.setAttribute('style', 'clear:both;');
			newdiv.appendChild(newElem);
			
			//var newlink = document.createElement('a');
			//if(current_ad['num_photos'] != '' && current_ad['num_photos'] != undefined)
				//	 newlink.innerHTML = current_ad['num_photos']+' '+'photo(s)';
			
			
			var newElem = document.createElement('a');
			newElem.setAttribute('href', '/classifieds/'+current_ad['ad_url']);
				if(current_ad['year'] != '' && current_ad['year'] != undefined)
				newElem.innerHTML += current_ad['year']+' ';
		
			//newElem.innerHTML += current_ad['listing_title'];
			if(current_ad['make'] != '' && current_ad['make'] != undefined)
				newElem.innerHTML += ' '+current_ad['make'];
			if(current_ad['model'] != '' && current_ad['model'] != undefined)
				newElem.innerHTML += ' '+current_ad['model'];
			if(current_ad['price'] != '' && current_ad['price'] != undefined)
				newElem.innerHTML += ' - '+current_ad['price']+' - ';
			if(current_ad['city'] != '' && current_ad['city'] != undefined)
				newElem.innerHTML += ' '+current_ad['city'];	
			if(current_ad['b_states'] != '' && current_ad['b_states'] != undefined && current_ad['city'] != '' && current_ad['city'] != undefined)
				newElem.innerHTML += ',';
			if(current_ad['b_states'] != '' && current_ad['b_states'] != undefined)
				newElem.innerHTML += ' '+current_ad['b_states'];
			newdiv.appendChild(newElem);
			
			
			
			//year/make/model, price and location (city, state abbreviation).

			//newdiv.appendChild(newlink);
			
		}
		// DEFAULT (mixed photo & text) ADS
		//-------------------------------------------------------------------------------------------------------------------------
		else
		{
			newdiv.className = 'boat_ad';
			var newlink = document.createElement('a');
			newlink.setAttribute('href', '/classifieds/'+current_ad['ad_url']);
				
			var newimage= document.createElement('img');
			
			if(current_ad['Main_photo'] == null)
				image_src = "/classifieds/templates/default/img/no_photo.gif";
			else
				image_src = '/classifieds/files/'+current_ad['Main_photo'];
				
			newimage.setAttribute('src' , image_src);
				
			newlink.appendChild(newimage);
			newdiv.appendChild(newlink);
			
			var newElem = document.createElement('br');
			newElem.setAttribute('style', 'clear:both;');
			newdiv.appendChild(newElem);
			
			var newElem = document.createElement('span');
			
				if(current_ad['year'] != '' && current_ad['year'] != undefined)
				newElem.innerHTML += current_ad['year']+' ';
		
			newElem.innerHTML += current_ad['listing_title'];
			if(current_ad['make'] != '' && current_ad['make'] != undefined)
				newElem.innerHTML += ' '+current_ad['make'];
			newdiv.appendChild(newElem);

			var newElem = document.createElement('a');
			newElem.setAttribute('href', '/classifieds/'+current_ad['ad_url']);
			newElem.innerHTML = 'click for details';
			newdiv.appendChild(newElem);
		}			
			
			ad_div.appendChild(newdiv);
			
			//auto adjust ad container height to fit the ads
			if(AdBlocks['blocks'][j]['ad_zone_resized'] == false && AdBlocks['blocks'][j]['layout'] != 'vertical')
			{
				ad_div.style.height = $(newdiv).outerHeight(true)+'px';
				AdBlocks['blocks'][j]['ad_zone_resized'] = true;
			}
			ad_pointer++;
			ad_pointers[current_ad_type] = ad_pointer;
		}
	}
	AdBlocks['blocks'][j]['ad_count'] = AdBlocks['blocks'][j]['ads_needed'];
}
}

}
